Lecture 10: A Walkthrough of an XNA 2D game Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology2D Games • Using “Sprites” – All textures – Simple to make or obtain • Early games before 3D revolution – Space Invaders, Lode Runner, Donkey Kong, Pac-Man • Do not require high performance accelerators • Simple enough for your grandparents to enjoy • Easy to do using XNA framework • No “effect” (.fx) usedMy Game: GoogleShower • Shoot the bad dawgs! • Consist of four main objects – The shooter (ship.cs) – The bad dawgs (meteros.cs) – The missile (missile.cs) – Music (AudioComponent.cs) • The moving objects are all made of sprites Adapted from Chapter 3 of XNA 2.0 Game ProgrammingScreen ShotDemo GoogleShower Game ExampleDrawing Sprite Using XNA • Use “texture” • Store with XNA’s texture2D class • Include new texture images into the Content Pipeline • Use “Content.Load” to associate texture variablesAdding Game Component (C# source file) • Adding new DrawableGameComponent class of objects in the game • Project Add ComponentsFull Screen ModeGame Services • Game services maintain loose coupling between objects that need to interact with each other • Register a “global” SpriteBatch for drawing all sprites • Draw() method will look for an active SpriteBatch in GameServices • All GameComponents will use this SpriteBatch Registering a Game Service In LoadContent() Use GetService to acquire serviceDrawing Background in One Pass Draw other spritesDrawString (Scoreboard) • Draw using SpriteBatch • Create a font sprite • Based on available Fonts in the system • Add font in LoadContent()Manage Components • Components: member of GameComponentCollection (i.e., Microsoft.XNA.Framework.Game) • Use Components.Add() to add a new component to the list • Components.RemoveAt(j) removes jth componentGame Logic • Embedded inside Update() function • Logistics check • Check collision • Check if a missile is fired • Check if should advance levelsPause the Game • Poll the keyboard state • Simply do not perform any update inside the Update()Built-in Test for Collision Detection • Test bounding boxes of given rectangular sprites • Return a boolean result • BoundingSphere also provides similar methodXNA Game Audio Component • Use “Content pipeline” again • Use Microsoft Cross-Platform Audio Creation Tool (or XACT) in XNA Game Studio 2.0XNA Game Audio Component Cont’d • Create wave banks and sound banks • Compile them into XAP file used by the content manager “checked” for looping background musicXNA Game Audio Component Cont’d • Create a new GameComponent for audio • Initialize WaveBank and SoundBank in the C# code This “Content” directory is located in <Proj>/bin/x86/<Sln Config>/Content AudioEngine object is the program reference to the audio services in your machine provided by XNABackground Looping Music • Add in Initialize code of the Game • PlayCue is a method of SoundBankPlay Sound On Event • Shoot.wav in SoundBank was not set to “infinite”, thus will only be played onceGame Over • Remove all components • Replace background canvas • Pause the musicOrganized Game Structure SoundBank Play Again?Class GameScene : DrawableGameComponent PlayScene Play
View Full Document