Learning C# by Programming Games

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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.

Author(s): Arjan Egges; Jeroen D Fokker; Mark H. Overmars
Publisher: Springer
Year: 2013

Language: English
Pages: 443

POE
Cover
Learning C# by Programming Games
Preface
Introduction
Required Materials and Tools
Using this Book as a Basis for a Programming Course
Acknowledgements
Contents
Part I: Getting Started
Chapter 1: Building Your First Game Application
1.1 Structure of This Book
1.2 Getting and Installing the Tools
1.3 Creating Your First Game Application
1.4 Running the Newly Created Project
1.5 Projects and Solutions
1.6 Running the Examples in This Book
1.7 What You Have Learned
Chapter 2: Programming
2.1 Introduction
2.2 Computers and Programs
2.2.1 Processor and Memory
2.2.2 Programs
2.3 Programming Languages
2.3.1 The Early Days: Imperative Programming
2.3.2 Procedural Programming: Imperative + Procedures
2.3.3 Object-Oriented Programming: Procedural + Objects
2.3.4 Java
2.3.5 C#
2.4 Translating a Program
2.4.1 Assembler
2.4.2 Compiler
2.4.3 Interpreter
2.4.4 Compiler + Interpreter
2.4.5 Compiler + Compiler
2.5 Programming Games
2.5.1 Games Are Special Programs
2.5.2 Game Engines
2.5.3 The XNA Game Engine
2.6 Developing Games
2.6.1 Small Scale: Edit-Compile-Run
2.6.2 Large Scale: Design-Specify-Implement
2.7 What You Have Learned
Chapter 3: Game Programming Basics
3.1 Introduction
3.2 Building Blocks of a Game
3.2.1 The Game World
3.2.2 The Game Loop: Updating and Drawing
3.2.3 The Game Loop in XNA
3.3 The Structure of a Program
3.3.1 The Basic Game Application in More Detail
3.3.2 Other Types of Applications
3.3.3 A Method: A Group of Instructions with a Name
3.3.4 A Class: A Group of Methods with a Name
3.3.5 Syntax Diagrams
3.3.6 Namespaces
3.3.7 A Compilation Unit: A Group of Classes in a Text File
3.3.8 Relying on Other Classes
3.3.9 Subclasses: A Special Version of Another Class
3.3.10 Calling a Method
3.3.11 Update and Draw
3.3.12 Different Kinds of Methods
3.3.13 The Graphics Device
3.4 Program Layout
3.4.1 Comments
3.4.2 Layout
3.4.3 Whitespace and Indentation
3.5 What You Have Learned
Chapter 4: Creating a Game World
4.1 Introduction
4.2 Basic Types and Variables
4.2.1 Types: Different Kinds of Structured Information
4.2.2 Declaration and Assignment of Variables
4.2.3 Instructions and Expressions
4.2.4 Arithmetic Operators
4.2.5 Priority of Operators
4.2.6 Other Numerical Types
4.2.7 Declaration and Initialization of const Variables
4.2.8 DiscoWorld: A Changing Background Color
4.2.9 Scope of Variables
4.2.10 Setting the Background Color
4.3 Managing Game Assets
4.3.1 Introduction
4.3.2 Locating Sprites
4.3.3 Loading Sprites
4.3.4 Drawing Sprites
4.3.5 Adding a Background Sprite
4.3.6 Music and Sounds
4.4 What You Have Learned
Part II: Creating Colorful Games
Chapter 5: Knowing What the Player Is Doing
5.1 Introduction
5.2 A Sprite Following the Mouse Pointer
5.2.1 Retrieving the Mouse Position
5.2.2 Methods and Properties
5.2.3 Changing the Origin of a Sprite
5.3 Classes, Types, and Objects
5.3.1 Methods and Properties Can Manipulate an Object
5.3.2 A Class Is a Blueprint for an Object
5.3.3 A Constructor Describes How an Object Is Created
5.3.4 this: The Object We Are Currently Manipulating
5.3.5 Properties: Retrieving or Changing Parts of an Object
5.3.6 Member Variables and Local Variables
5.4 Using the Mouse Position to Rotate the Cannon Barrel
5.5 What You Have Learned
Chapter 6: Reacting to Player Input
6.1 Introduction
6.2 Reacting to a Mouse Click
6.2.1 ButtonState: An Enumerated Type
6.2.2 The if-Instruction: Executing an Instruction Depending on a Condition
6.3 Boolean Values
6.3.1 Comparison Operators
6.3.2 Logic Operators
6.3.3 The Boolean Type
6.4 More on if-Instructions
6.4.1 An if-Instruction with an Alternative
6.4.2 A Number of Different Alternatives
6.4.3 Toggling the Cannon Barrel Behavior
6.5 Handling Keyboard and Gamepad Input
6.5.1 Basics of Handling Keyboard Input
6.5.2 A Multicolored Cannon
6.5.3 Handling the Keyboard Input
6.6 What You Have Learned
Chapter 7: Basic Game Objects
7.1 Introduction
7.2 Structuring the Game World
7.2.1 The Basics
7.2.2 The HandleInput Method: Organizing Instructions in Methods
7.2.3 Different Kinds of Methods: With or Without Parameters, with or Without a Result
7.2.4 Declarations Versus Parameters
7.2.5 Why Declarations Are Useful
7.3 Classes and Objects
7.3.1 An Object Is a Group of Variables with Methods and Properties
7.3.2 Objects in the Painter Game
7.3.3 Adding Classes to Your Project
7.3.4 Classes Can Be Used as a Type
7.4 The Cannon Class
7.4.1 Data in the Cannon Class
7.4.2 Adding Methods to the Cannon Class
7.4.3 Accessing the Data in the Cannon Class
7.4.4 Retrieving the Color of the Cannon Object
7.4.5 Adding More Properties
7.4.6 Properties Do Not Always Directly Correspond to Data
7.5 Reorganizing the Class Structure
7.5.1 A Class for Handling Input
7.5.2 The GameWorld Class
7.5.3 Managing the Game Objects
7.6 Objects and Types
7.6.1 Copy by Value or by Reference
7.6.2 The null Keyword
7.6.3 Different Kinds of Object Types
7.7 What You Have Learned
Chapter 8: Adding Interaction
8.1 Introduction
8.2 The Ball Class
8.2.1 Structure of the Class
8.2.2 Initializing the Ball
8.2.3 Shooting the Ball
8.2.4 Updating the Ball
8.2.5 Drawing the Ball on the Screen
8.3 The PaintCan Class
8.3.1 A Class with Multiple Instances
8.3.2 Randomness in Games
8.3.3 Calculating a Random Velocity and Color
8.3.4 Updating the Paint Can
8.3.5 Drawing the Cans on the Screen
8.4 Handling Collisions Between the Ball and the Cans
8.5 What You Have Learned
Chapter 9: A Limited Number of Lives
9.1 Introduction
9.2 The Number of Lives
9.2.1 Maintaining the Number of Lives
9.2.2 Indicating the Number of Lives to the Player
9.3 The while-Instruction: Executing Instructions Multiple Times
9.3.1 A Shorter Notation for Incrementing Counters
9.4 The for-Instruction: A Compact Version of while
9.5 A Few Special Cases
9.5.1 No Repeat at All
9.5.2 Infinite Repeat
9.5.3 Nested Repeats
9.6 Restarting the Game After the Game Is Over
9.7 What You Have Learned
Chapter 10: Organizing Game Objects
10.1 Introduction
10.2 Similarities Between Game Objects
10.3 Inheritance
10.3.1 Game Objects and Inheritance
10.3.2 The ThreeColorGameObject Class
10.4 Creating Special Versions of Game Objects
10.4.1 The Cannon Class
10.4.2 Overriding Methods from the Base Class
10.4.3 Accessing Member Variables from the Base Class
10.4.4 Access Modifiers and Methods/Properties
10.4.5 The Final Cannon Class
10.5 The Ball Class
10.5.1 Outline of the Class
10.5.2 The Reset Method
10.5.3 base Versus this
10.5.4 Polymorphism
10.5.5 Sealed Methods
10.6 Hierarchies of Classes
10.6.1 A Subclass `Is a Kind of' Base Class
10.6.2 Designing Class Hierarchies
10.7 What You Have Learned
Chapter 11: Finishing the Game
11.1 Introduction
11.2 Adding Motion Effects
11.3 Adding Sounds and Music
11.4 Maintaining a Score
11.5 Drawing Text on the Screen
11.6 Characters, Strings and Type Conversion
11.6.1 The char Type
11.6.1.1 History of char
11.6.1.2 Using Single and Double Quotes
11.6.1.3 Special char Values
11.6.1.4 Calculating Things with char
11.6.2 String Operations
11.7 What You Have Learned
Part III: Structures and Patterns
Chapter 12: Collections of Game Objects
12.1 Introduction
12.2 Many Game Objects
12.3 Snowflakes
12.4 Collections of Objects
12.5 The IList Interface
12.6 The Collection Interface
12.7 The ISet Interface
12.8 The IDictionary Interface
12.9 The Array: A Simpler Version of the List
12.9.1 Strings: A Special Kind of Array
12.10 foreach: Do Something with All the Items in a List or an Array
12.11 Iterating with an Iterator
12.12 What You Have Learned
Chapter 13: Fullscreen Games
13.1 Introduction
13.2 Setting the Resolution
13.3 Updating the Graphics Device
13.4 Correcting the Mouse Position
13.5 Scaling the Sprites
13.6 Making Sure We Can Still Quit the Game
13.7 What You Have Learned
Chapter 14: Game Objects in a Structure
14.1 Introduction
14.2 Game Objects in a Grid
14.2.1 Grids in Games
14.2.2 Using a Grid to Draw Sprites
14.2.3 Grid Operations
14.2.4 More Possibilities with Grids
14.3 Hierarchy of Game Objects
14.3.1 Anatomy of a Game Object
14.3.2 Relations Between Game Objects
14.3.3 Local Versus Global Positions
14.3.4 Layers of Game Objects
14.4 Different Kinds of Game Objects
14.4.1 A Sprite Game Object
14.4.2 A List of Game Objects
14.4.3 A Grid of Game Objects
14.4.4 A Grid of Jewels
14.4.5 Moving Smoothly on the Grid
14.4.6 Selecting Rows in the Grid
14.5 Creating the Game Objects
14.6 What You Have Learned
Chapter 15: Redesigning the Game World
15.1 Introduction
15.2 Better Asset Management
15.2.1 Moving Sprites Around
15.2.2 The Asset Manager
15.2.3 Loading and Retrieving Assets
15.3 Accessing the Game World
15.4 Game Objects Using the Asset Manager
15.4.1 The SpriteGameObject Class
15.4.2 The RowSelectGameObject Class
15.4.3 The Jewel Class
15.5 What You Have Learned
Chapter 16: Gameplay Programming
16.1 Introduction
16.2 Interaction Between Game Objects
16.2.1 Finding Other Game Objects
16.2.2 Assigning Identifiers to Game Objects
16.2.3 Finding Game Objects
16.2.4 Recursion: A Method Calling Itself
16.2.5 Finding the Game World
16.2.6 Updating the RowSelectGameObject Class
16.3 Introducing a Couple of New Game Object Types
16.3.1 The ScoreGameObject Class: Maintaining the Current Score
16.3.2 A Moving Jewel Cart
16.4 Finding Combinations of Jewels
16.4.1 Handling the Combinations
16.4.2 Finding Valid Combinations of Jewels
16.4.3 Removing Jewels from the Grid
16.4.4 Updating Other Game Objects
16.5 What You Have Learned
Chapter 17: Game States
17.1 Introduction
17.2 Adding a Title Screen
17.3 Adding a Button for Showing the Help Frame
17.4 Overlays
17.5 What You Have Learned
Chapter 18: Finishing the Game
18.1 Introduction
18.2 Extra Points for Multiple Combinations
18.3 Time in Games
18.3.1 Other Useful Classes for Managing Time
18.3.2 A Timer for Controlling Visibility of a Game Object
18.4 A Field of Glitters
18.4.1 The Constructor
18.4.2 Adding Glitters
18.4.3 Accessing Pixel Color Data
18.4.4 Updating the Glitter Field
18.4.5 Drawing the Glitter Field
18.4.6 Adding Glitters to Game Objects
18.5 Adding Music and Sound Effects
18.6 What You Have Learned
Part IV: Making Your Games Appealing
Chapter 19: Sprite Sheets
19.1 Introduction
19.2 Overview of the Example Project
19.3 Loading a Sprite Sheet
19.4 Managing the Sprite Sheet
19.5 Finalizing the Example
19.6 What You Have Learned
Chapter 20: Menus and Settings
20.1 Introduction
20.2 Setting up the Menu
20.3 Adding an on/off Button
20.4 Adding a Slider Button
20.5 Reading and Storing Game Settings
20.6 What You Have Learned
Chapter 21: Game State Management
21.1 Introduction
21.2 Basics of Managing Game States
21.2.1 A Generic Game Loop Object Interface
21.2.2 The GameStateManager Class
21.3 Adding States and Switching Between Them
21.4 The Level Menu State
21.5 What You Have Learned
Chapter 22: Loading Levels from Files
22.1 Introduction
22.2 Structure of a Level
22.3 The Tile Class
22.4 Other Level Information
22.5 File Reading in C#
22.5.1 Reading a Text File
22.5.2 Streams Versus Text Readers and Writers
22.5.3 Reader
22.5.4 Text Reader
22.5.5 Stream Reader
22.5.6 Abstract Classes
22.6 Reading the Levels from the Text File
22.6.1 The loadLevels Method
22.6.2 Creating the Level Object
22.6.3 The switch-Instruction: Handling Alternatives
22.6.4 The break-Instruction
22.6.5 Loading Different Kinds of Tiles
22.7 Completing the Level Class
22.8 Maintaining the Player's Progress
22.9 What You Have Learned
Chapter 23: Pairing the Penguins
23.1 Introduction
23.2 Penguin Selection
23.2.1 The Arrow Class
23.2.2 The Animal Selector
23.3 Input Handling Order
23.4 Updating Animals
23.5 Dealing with Meeting Other Game Objects
23.6 Maintaining the Number of Pairs
23.7 What You Have Learned
Chapter 24: Finishing the Game
24.1 Introduction
24.2 Separating Code into Different Libraries
24.2.1 Creating and Filling a Library
24.2.2 Public or Private Classes
24.2.3 A Convenient GameEnvironment Class
24.3 Finishing the User Interface
24.3.1 Showing Hints
24.3.2 Resetting the Level
24.3.3 Moving to the Next Level
24.3.4 Music and Sounds
24.4 Some Final Notes
24.5 What You Have Learned
Part V: Animation and Complexity
Chapter 25: Creating the Main Game Structure
25.1 Introduction
25.2 Overview of the Game Structure
25.3 The Structure of a Level
25.4 Water Drops
25.5 The Tile Class
25.6 Setting up the Level Class
25.7 Creating the Level Object
25.8 Exceptions
25.8.1 Dealing with Errors
25.8.2 The try-catch Instruction
25.8.3 Throwing Your Own Exceptions
25.9 What You Have Learned
Chapter 26: Animation
26.1 Introduction
26.2 What Is Animation?
26.3 The Animation Class
26.4 Playing an Animation
26.4.1 Updating the Animation that Is Currently Playing
26.4.2 Mirroring Sprites
26.5 An Animated Game Object
26.6 The Player Class
26.7 What You Have Learned
Chapter 27: Game Physics
27.1 Introduction
27.2 Locking the Character in the Game World
27.3 Setting the Player at the Right Position
27.4 Jumping …
27.5 … and Falling
27.6 Collision Detection
27.7 Retrieving Bounding Boxes
27.8 Per-pixel Collision Detection
27.9 Handling Collisions Between the Character and the Tiles
27.10 Dealing with the Collision
27.11 What You Have Learned
Chapter 28: Intelligent Enemies
28.1 Introduction
28.2 The Rocket
28.2.1 Creating and Resetting the Rocket
28.2.2 Programming the Rocket Behavior
28.3 A Patrolling Enemy
28.3.1 The Basic PatrollingEnemy Class
28.3.2 Different Types of Enemies
28.4 Other Types of Enemies
28.5 Loading the Different Types of Enemies
28.6 What You Have Learned
Chapter 29: Adding Player Interaction
29.1 Introduction
29.2 Collecting Water Drops
29.3 Ice Blocks
29.4 Enemies Colliding with the Player
29.5 What You Have Learned
Chapter 30: Finishing the Game
30.1 Introduction
30.2 Adding a Timer
30.2.1 Making the Timer Go Faster or Slower
30.2.2 When the Timer Reaches Zero…
30.3 Drawing Mountains and Clouds
30.4 Finalizing the Level Progression
30.5 What You Have Learned
Appendix A: Exercises and Challenges
A.1 Exercises and Challenges for Part I
Exercises
Challenges
A.2 Exercises and Challenges for Part II
Exercises
Challenges
A.3 Exercises and Challenges for Part III
Exercises
Challenges
A.4 Exercises and Challenges for Part IV
Exercises
Challenges
A.5 Exercises and Challenges for Part V
Exercises
Challenges
Appendix B: Syntax Diagrams
B.1 Introduction
B.2 Compilation Unit
B.3 Top-Level Declaration
B.4 Type Declaration
B.5 Parameters
B.6 Member
B.7 Block
B.8 Declaration
B.9 Initialization
B.10 Type
B.11 Instruction
B.11.1 If-Instruction
B.11.2 While-Instruction
B.11.3 For-Instruction
B.11.4 Foreach-Instruction
B.11.5 Switch-Instruction
B.11.6 Try-Catch-Instruction
B.12 Expression
B.13 Constant
B.14 Symbol
B.15 Number
Further Reading
Glossary
Index