Windows 8 Game Development 101, Part 2
This is the second post in a series that goes over some basics of game development. While this series is targeted at beginners, you’ll find yourself going through these motions for every game you create. I hope you find something useful and are able to finish with a better understanding of game development. Don’t miss Part 0 and Part 1 of this series!
In this post I’m going to talk about the Game Loop. When you start programming your game the first thing you need to create is your game loop. The game loop is what makes your game run! All games, from the simple to complex, have a game loop that runs continuously. The game loop has a core set of methods that allow it to do all sort of useful and necessary things for your games. Here’s what they are and what they do.
In this method, you will start any required services and non-asset resources. For example, if you plan on using random numbers in your game, that initialization statement would go here. If you need to connect to a web service or a network, that should be done here as well. Initialize is called before any other method in the game loop is called, so the time is takes to execute this code will be perceived as a delay by the player. This method is called once.
You’ll load all the things you need for your game in the Load method. If you want your game to have a background, you’ll point to your background asset here. If your game to have character sprites, you’ll point to those asset files here. If you want sound, you’ll tell your game where to find the sound files in the Load method. You get the idea. Everything that you want to appear in your game (textures, sprites, fonts, 3D models, sound) you will load in this part of the game loop. This method is called once.
This is where lots of fun magic happens! All of the player and game interaction happens in Update. If a character is moving, it happens in Update. If you need collision detection, it happens in Update. If there is an explosion, guess where it happens? Update! All of your player input is also dealt with in Update. Just because all this stuff happens in Update doesn’t mean the code has to long and messy. Use those object-oriented programming skills and create methods that you can call from Update. For example, you might have a method called UpdateEnemies that is called from Update to move the enemies across the screen, check for collisions or even play a sound effect. Update is called multiple times per second, the exact number depends on the frame rate of the display device.
Just like Update, lots of magic stuff happens in Draw! Draw uses the textures and sprites that you defined in Load and the object information from Update to display all the visual parts of your game on the screen. Draw and Update word closely together to make the changes to game (movement, explosions, etc.) look seamless. Draw is called multiple times per second, the exact number depends on the frame rate of the display device.
While this is a non-digital example, it helps to illustrate that all games go through the game loop. This loop hold true for your favorite digital game as well whether it’s popping balloons or shooting zombies.
We’re getting our non-asset resources ready. In this case, we’re going to start our multi-player network. For Monopoly, this means we’re going to gather the family around the kitchen table.
This is where we get all of our game assets ready. Is the game board here? Check. Is the money here? Check. Are the game pieces here, including the thimble? Check. And so on until we know the location of all of the things we’ll need to play Monopoly.
Update and Draw
Just like the game you’ll develop, Update and Draw happen multiple times per turn in Monopoly. In any given turn a player may roll dice (Update) and move their game piece (Draw), buy a property (Update) and put a house on it (Draw) or draw a Chance card (Update) and go to jail (Draw) along with a number of other actions a player can take.
Hopefully you’ve been able to learn a little something about game development. Want to know more? Let me know what you want to see next in the comments! Don’t forget, you can get started on building games and apps for Windows 8 with 30 to Launch. Sign up today!
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)