Skip to main content
main-content
Top

About this book

Developing computer games is a perfect way to learn how to program in modern programming languages. This book teaches how to program in C# through the creation of computer games – and without requiring any previous programming experience.

Contrary to most programming books, Egges, Fokker and Overmars do not organize the presentation according to programming language constructs, but instead use the structure and elements of computer games as a framework. For instance, there are chapters on dealing with player input, game objects, game worlds, game states, levels, animation, physics, and intelligence. The reader will be guided through the development of four games showing the various aspects of game development. Starting with a simple shooting game, the authors move on to puzzle games consisting of multiple levels, and conclude the book by developing a full-fledged platform game with animation, game physics, and intelligent enemies. They show a number of commonly used techniques in games, such as drawing layers of sprites, rotating, scaling and animating sprites, showing a heads-up display, dealing with physics, handling interaction between game objects, and creating pleasing visual effects such as snow or glitter. At the same time, they provide a thorough introduction to C# and object-oriented programming, introducing step by step important aspects of programming in general, including many programming constructs and idioms, syntax diagrams, collections, and exception handling.

The book is also designed to be used as a basis for a game-oriented programming course. For each part, there are concluding exercises and challenges, which are generally more complex programming endeavors. Lots of supplementary materials for organizing such a course are available on the accompanying web site http://www.csharpprogramminggames.com, including installation instructions, solutions to the exercises, software installation instructions, game sprites and sounds.

Table of Contents

Getting Started

Frontmatter

Chapter 1. Building Your First Game Application

Abstract
This chapter provides an introduction to this book. In this chapter, we will explain which tools you are going to need, and how to set up a basic game development environment for programming a game in C#. We will also explain how to run the example projects.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 2. Programming

Abstract
In this chapter, we give an overview of what programming is exactly, how it has evolved, and how it has been used to develop games. We will start with the premise that a computer consists of a processing unit and memory. The first (very low-level) programming languages were called Assembler languages. These languages were difficult to use; each processor had its own Assembler language. Newer, machine-independent programming languages followed different paradigms. We make a distinction between declarative programming languages and functional programming languages. We also cover object-oriented programming languages such as C++, Java, and C#, which are part of the declarative paradigm. We then give an overview of different ways to translate code written in these languages into a program that can be executed by a computer. After that, we introduce game engines and how they are used in programs. We conclude the chapter with a brief overview of how code is designed, specified, and implemented.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 3. Game Programming Basics

Abstract
This chapter covers the basic aspects of programming games. We will introduce the game loop, which consists of two main parts: updating the world, and drawing the world. We will show how this concept is defined by game engines, and how it is used to create a simple game application. The game application is used to introduce a number of basic C# programming concepts, such as instructions, methods, classes, namespaces, and compilation units. We show that instructions are grouped in methods, and methods, in turn, are grouped in classes. We will show that you can include libraries in your program that contain code written by others. We also discuss the basic rules of program layout, and how to use comments to clarify your code.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 4. Creating a Game World

Abstract
In this chapter, we will show how to create a game world by storing information in the memory. We will introduce basic (numerical) types and variables, and how they can be used to store or change information. We will discuss the difference between instructions and expressions, and we will introduce a number of operators that can be used to combine expressions. We will show you how to store more complicated information such as sprites and sounds, and how you can use them to draw a nice-looking game world with sounds. We will present a few examples that illustrate how the information stored in the memory is updated and drawn in the game loop.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Creating Colorful Games

Frontmatter

Chapter 5. Knowing What the Player Is Doing

Abstract
This chapter discusses dealing with player input in games. We will show how to retrieve what the player is doing and how the game world changes depending on that information. We show how to read the current mouse position and how to change the game world using that position. We discuss the basic idea that a class is a blueprint for an object (also called an instance of that class), and can therefore be used as a type. Constructor methods are introduced as the methods responsible for creating an instance of the class that they belong to.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 6. Reacting to Player Input

Abstract
In this chapter, we will show you how your game program can react to mouse clicks and button presses. In order to do this, we need an instruction called if that executes an instruction (or a group of instructions) if a condition is met. We will show you how to formulate conditions for these instructions using boolean values. We will also introduce enumerated types as another kind of primitive type. We will use the if-instruction to deal with keyboard and gamepad input in the Painter game.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 7. Basic Game Objects

Abstract
This chapter deals with organizing instructions in different methods and classes. You will learn how to write your own methods, properties, and classes, and how you can use these programming concepts to design different game object classes. We will discuss the difference between values and references and what the null keyword means in that context. Finally, we will show the difference between a class and a struct. We further develop the Painter game in this chapter to incorporate classes that are used to create the instances of the various game objects in the game.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 8. Adding Interaction

Abstract
In this chapter, we will define more game object types (the ball and the paint cans). For each different kind of game object, we will add a class to the program. We will look into how game objects should interact with each other. As an example of this interaction, we will show how to handle basic collisions between game objects. Furthermore, we will discuss randomness in games as a useful way to increase replayability of your games.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 9. A Limited Number of Lives

Abstract
In this chapter, we are going to make the game more interesting by giving the player a limited number of lives. If the player misses too many paint cans, she/he dies. In this chapter, we will discuss how to deal with that, and how to display the current number of lives to the player. In order to do the latter, we introduce a few programming concepts that allow us to repeat a group of instructions several times, notably the for and while instructions. We will also show how to restart/reset a game after the game is over.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 10. Organizing Game Objects

Abstract
In this chapter, we are going to look at the similarities between the different types of game object, and how we can express these similarities in C#. The main technique used for this is inheritance. Inheritance is a technique that is used to structure related classes in a hierarchy. We will show how to override methods in a subclass to provide specific behavior for that class. We will deal with access modifiers for class members. We will also introduce the concept of polymorphism, which allows calling the right version of a method automatically.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 11. Finishing the Game

Abstract
In this chapter, we will finish the Painter game by adding a few extra features, such as motion effects, sounds and music, and maintaining and displaying a score. In order to do the latter, we introduce the string and char types.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Structures and Patterns

Frontmatter

Chapter 12. Collections of Game Objects

Abstract
In this chapter, we will introduce collections and arrays, and show that they are a very powerful tool for making game code scalable and more flexible. We also introduce interfaces in this chapter, and we show that they define the way the collection classes are related to each other. We also discuss the foreach instruction and how it is used to traverse all the elements in an array or a collection. Finally, we discuss the relationship between arrays and strings.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 13. Fullscreen Games

Abstract
In this chapter, we are going to deal with viewing games in full screen mode. We will start developing the Jewel Jam game by adding some methods for switching between full screen and windowed mode. As a result, we also need to know how to correctly scale the sprites that are being displayed. Furthermore, we need to correct the mouse position so that switching between the various viewing modes is seamless.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 14. Game Objects in a Structure

Abstract
Games often require that game objects are adhering to some kind of structured game world. Many board or puzzle games have this requirement. These games impose a set of rules, which binds the playing pieces to certain positions or configurations on the playing board. Furthermore, game objects can be part of a hierarchy. For example, a tire object is part of a wheel object, which, in turn, could be a part of a car object. In this chapter we show how to create these kinds of structure using the collection classes. We introduce the scene graph as a representation of this structure. We also talk about local and global (world) positions of game objects.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 15. Redesigning the Game World

Abstract
Many things change over the course of running a game: levels are completed, the player’s character may change, new quests and enemies are introduced, and so on. In terms of programming games, this translates into more classes and changing relations between instances of these classes. There are, however, also a couple of things that will not change at all while a game is running. For example, the process for loading sprites and sounds will probably stay the same throughout the game, so the class that deals with this will not really change. In this chapter, we will investigate how to set up a class for dealing with various assets in a game, an instance of which is accessible to all the objects that need assets such as sprites and sounds.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 16. Gameplay Programming

Abstract
In this chapter we are going to look into the gameplay programming of the Jewel Jam game. First, we will talk about interaction between game objects. In order to have interaction, game objects need to be able to find each other. We achieve this by adding identifiers to game objects, so that we can search for them in a list or in a hierarchy. We also introduce a few new game objects that are used in the game, such as the jewel cart. Finally, we will tackle the main gameplay element of this game: finding combinations of jewels and removing those jewels from the board once the player has found them.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 17. Game States

Abstract
Most games have a variety of different states. A player can be in a menu, can be playing, or can be viewing the current high scores. In this chapter, we will add a few different states to the Jewel Jam game. We also develop a couple of basic menu items such as a button or a help frame.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 18. Finishing the Game

Abstract
In this chapter, we will finish the game Jewel Jam. As a first step, we are going to give the player extra points when he/she makes two or three valid combinations of three jewels at the same time. Secondly, we are going to add a nice visual effect by showing glitters on the jewels in the game. Finally, we will add sound and music to the game.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Making Your Games Appealing

Frontmatter

Chapter 19. Sprite Sheets

Abstract
In this chapter, we will start building the first elements of the Penguin Pairs game. We introduce the concepts of sprite strips and sprite sheets, meaning that several sprites are stored in the same image. This can be very useful, as we will show in this chapter. We will show how to incorporate these sprite strips and sheets in your game, and we design a SpriteSheet class that you can use to do this.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 20. Menus and Settings

Abstract
In this chapter, we are going to design a variety of useful GUI elements for menus, such as an on/off button and a slider button. Secondly, we will show how you can read and store game settings, such as music volume or whether hints are allowed or not using a game settings manager.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 21. Game State Management

Abstract
This chapter shows how to design a game state manager. Often, games will have a title screen, menus for options, menus for selecting different levels, screens to display the high score after finishing a level, a menu to select different characters and attributes, and so on. A game state manager can help switching between these states seamlessly. We will show how different states can be a part of the game loop, and how we can switch between them. For that, we design a generic game loop interface that defines the structure of all objects that are part of the game loop in some way.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 22. Loading Levels from Files

Abstract
Many games consist of different levels. Especially in casual games such as puzzles and maze games, the game may have several hundreds of levels. With the programming tools that we have seen thus far, we could add different levels to our game by, for example, writing a generic Level class, and then defining (hundreds of) subclasses, where each class defines what the particular level looks like. A much better approach, however, would be to store the different levels in a text file, and then read the information from this text file to create the levels. Then, if we want to add another level to our game, we do not have to change the source code but we can simply edit the text file. In this chapter, we will see how we can load levels from a text file using the C# file I/O classes. As an example, we will show how to create a tile-based game world based on information saved in a text file.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 23. Pairing the Penguins

Abstract
In this chapter, we will program the main gameplay for the Penguin Pairs game. We will discuss how to move penguins around on the board, and what to do when a penguin collides with another game object such as a shark or another penguin. We will also develop a selector object for a penguin, as well as an indicator for the number of pairs made.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 24. Finishing the Game

Abstract
In this chapter, we will finalize the game. As a first step, we will reorganize our code a bit so that parts of it are more easily usable by other programs. We do this by placing part of the code in a library. Then, we will finish the game by extending the user interface and adding sound effects and music.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Animation and Complexity

Frontmatter

Chapter 25. Creating the Main Game Structure

Abstract
In this chapter, we will lay out the framework that we will use for the Tick Tick game. We will build the game upon the library that we have developed and used in the Penguin Pairs game. This means that we already have a basic design for handling game states and settings, a hierarchy of game objects and more. We also show in this chapter how to distribute the code of a complex class with many methods over several files (using partial classes). Furthermore, we discuss exceptions in this chapter and we show how they can be used to make your game more robust.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 26. Animation

Abstract
In this chapter, we are going to show you how to add animations to your game. In the games we have developed until now, game objects could move around on the screen, but adding something like a running character to your game is slightly more challenging. In this chapter, we will write a program that contains a character walking on the bottom of the screen. The character can walk from the left to the right and vice versa, based on which arrow key (left or right) the player is pressing.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 27. Game Physics

Abstract
In this chapter, we go into depth on how the character interacts with the game world. We can make a character move from left to right, but if we simply place the character in the level, he/she will only be able to walk on the bottom of the screen. This is not enough. We want the character to be able to jump on top of wall tiles, we want the character to fall down if he/she moves off a wall tile, and we do not want the character to fall off the edge of the screen. For these things, we need to implement a basic physics system. There are two aspects of physics: one is giving the character the ability to jump or fall, the other is handling collisions between the character and other game objects, and responding to these collisions. Both aspects will be dealt with in this chapter.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 28. Intelligent Enemies

Abstract
As a next step in developing our game, let us introduce some peril to the player by adding dangerous enemies. The enemies are generally not controlled by the player (that would make it too easy). Therefore, we need to define some kind of smart (or stupid) behavior. In this chapter, we build different types of enemy that exhibit different types of behavior. As a result, the player will have different gameplay options, and has to develop different strategies to complete each level. We use inheritance to create variety in the behavior of enemies.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 29. Adding Player Interaction

Abstract
In this chapter, we will add more interaction between the player and the objects in the level. Currently, the player can walk around, and there is a basic physics system that allows the player to jump, collide with wall tiles, or fall out of the screen. First, we will look at a very simple kind of interaction: collecting the water drops. Then, we will show how to create the behavior that allows the player to slide over ice. Finally, we will deal with the part of the program that deals with the various player-enemy interactions in the game.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars

Chapter 30. Finishing the Game

Abstract
In this chapter, we will finish the Tick Tick game. First, we will add a timer such that the player has a limited amount of time to complete each level. Then, we will add a few mountains and clouds to the background to make the game visually more interesting. Finally, we will discuss progressing through the levels by adding two extra game states: the ‘game over’ state, and the ‘level finished’ state.
Arjan Egges, Jeroen D. Fokker, Mark H. Overmars
Additional information