Game Programming Lecture One - Fullcoll.edu

Transcription

Game Programming – Lecture OneGame programming involves (in most cases) using a Game Engine. Game Engines are applications used to create games.Most game engines include three major components, tools to create and edit game components, built-in componentsthat can be included in the game, and a game runtime. The reason that there are game engines is to simplify gameprogramming and provide an environment for handling complex graphic programming elements.Before creating a game and using a game engine like GameMaker you need to understand some of the high levelconcepts behind game programming. These concepts include: Game LoopGame ObjectsGame and Object StateEvent/CallbacksGame LoopThe Game Loop are the steps a game goes through when it runs. The basic web loop steps are:1.2.3.4.Get input from player or otherUpdate the game stateRedraw the screenGo to step 1The first step is getting input from the player or from other sources. These inputs could be from moving the mouse,pressing keys, or even having objects in the game collide with each other. The second stage is information inside thegame is updated as a result of the inputs. During this update step the game programmer can instruct the game tochange elements of the game based on the input. This is where the game programmer creates the things he or shewants the game to do before the next step. The final step in the game loop is redrawing the screen and reflecting theupdated game state.As an example, consider how an object moves in a game when the player uses the mouse or keyboard or a controller.1.2.3.4.Read the status of the keyboard/controller and get the move commandUpdate the draw location of the game object based on information gathered during the input step.Redraw the screen with the game object at the new location.Go back to step one.Since the screen refreshes during each game loop, the speed of the game is normally referred to as the Frames PerSecond (FPS) since that is how many times the game will refresh the screen. The apparent movement of objects on thescreen happens because each time through the game loop objects move small amounts. At 30 or 60 times a second theobject appears to move smoothly across the screen.This is a very simplified view of the Game Loop. Real games likely have multiple game loops and processes that run inparallel with the game loop performing tasks. Object movement is not a fixed value based on the speed of the gameloop but is a multiple of the absolute time it took for the game loop to process. For example, suppose you wanted acharacter on the screen to move 10 pixels a second on the screen. To keep the movement smooth and not be tied to thespeed of the game loop, the game would:A. Measure the number of milliseconds it took the game loop to complete.B. Multiply the number of pixels to move per second by the loop speed divided by 1 second (10 * .005/1)C. Move the object a fractional number of pixels for each game loop.Using this millisecond measurement method does not rely on how fast the game is running.

[Note: A Pixel is an imaginary dot on the screen representing a color and position used to draw images. As you set yourscreen resolution up or down (the number of pixels that will fit on the screen) the pixels get “larger” or “smaller”.]Game ObjectsBecause game programming is very complex, various strategies have been used to simplify game development. One ofthese is having the Game Engine represent game elements using an Object Model. The Object Model is an abstract wayto represent game elements that makes it easy to deal with large number of game elements.A Game Object is normally what the player can interact with in the game. Things like treasure chests, opponents, food,health, doors, and anything that the player can interact with. Objects will have identity, properties, and will be able todo things (called Methods). Thinking of game elements as Objects helps keep the object related behaviors andproperties in one identifiable area. Game Objects can also be hooked into the Event/Callback system so the game enginecan more easily manage games with a lot of game objects.As an example, suppose we have a monster game object. This game object has the following Object information:ID: MonsterX (or some game generated random ID value)Properties: Health, Strength, Stamina, TreasureMethods: Get and Set Health and Stamina, Get Strength and TreasureWhen the game is played, the main game character engages in combat with the Monster. Each time the player hits theMonster the amount of health for the monster is reduced. The Monster method to reduce health will be called and thecurrent amount of monster health displayed on the screen. When the Monster hits the player’s character the Monsterstrength will be used to calculate how much health the player is reduced. When the monster is killed the game calls theMonster’s Get Treasure method to transfer the treasure to the player’s character.Thinking of game objects in this way helps simplify creating the game elements. This also makes it easy for the gameengine to hook into our game objects using the Event/Callback method.Game and Object StateAnother important concept in game programming is the Game and Object state. A State is just the collection of data thatdescribe the object and the game. Object states might include position, rotation, health, name, and so forth. Everythingthat the object contains would be the object state. That is, if we wanted to re-create the Game Object everything weneed to perform this task would be the Object State.Game State is like the state of an Object but it includes a lot more. The Game State would include the number andlocation of all objects, the progress of the player, and basically everything we would need to recreate the state of thegame.Event/CallbackOne of the central ideas behind managing complex game programming is the Event/Callback structure. This includes twoof the game loop elements, getting inputs and updating game state. Most game engines will, as the game runs, createan Event Queue. As the game retrieves input it converts these into Event objects and puts them on the Event Queue.Each event object has a name and possibly a value. The Event Queue is used by game objects to update data andperform actions.For example, if the game pulls a collision event between two objects (perhaps a sword collides with a monster) the gameengine creates and Event object and puts it on the Event Queue. The Monster object in the game may continually checkthe event queue for Events it is interested in, such as getting hit by a sword. When the Monster object sees the event itcalls the method for reducing its own health.

GameMaker StructureThe structure of gamemaker implements many of these general game objects. Gamemaker creates an Event queue andtreats game elements as objects. The objects will have properties (usually sprites) and you can attach an event to theobject. When the event shows up in the GameMaker event queue the game engine directs it to the appropriate object.When you start a new Project and add an object (discussed below) the various resources for you game appear in theResources menu:To add new resources you right click the item category and choose how you want to add or create the item:

If you have a game object and click the object you see:GameMaker has created an object. The object has areas where a Sprite can be added to the object along with Events.

Clicking the Events brings up the Event menu. These are the events that have been attached to the object. Notice thatthe list of events is based on keyboard input.When the game runs and one of the keys in the event list is pressed, the game object will be notified.Double click the Event to see the Action:

GameMaker references actions by a set of icons. You will drag the icon representing the action into the Event area andset the options on the action. In this case you are setting the speed of the object (pumpkinobj) and the direction whenthe left key is pressed. However, pressing a key is one event and releasing the key is a separate event. The Key Up – Anyevent will set the speed to 0 and stop the object.Another object in this project is the Star object.

In this project we will use a key pressed event to perform the following: Create a Star object next to the PumpkinSet the star object direction and speed.1. Double click the Pumpkinobj object to open the object editor windows:

2. Click the Add Event button in the middle window and choose Key Pressed / Space:

3. In the Toolbox area of the third window type Instance until you see a red lightbulb icon4. Drag this icon into the Key Press – Space area. You see the Create Instance dialog box.5. Click the icon next to the Instance name to choose an Object.6. Click on the Star object. This is the object that will be created.Below the object name are the X and Y values indicating where the star object will be created. These values referto the screen coordinates with X 0 and Y 0 being the upper left corner of the screen. By default this is set tozero. In order to create the Star object next to the pumpkin object you need to click both Relative check boxes.This will use the position of the pumpkin object and make the position of the star object match the pumpkinobject’s position.

7. Click both Relative check boxes.We now want to set the star object in motion and have it move at a specified speed in a particular direction. However, ifwe drag the speed icon and the direction icon into this window it will be applied to the pumpkin object. We want itapplied to the Star object.This is done in GameMaker by adding an Apply To icon so that speed and direction commands are applied to the starobject.8. In the toolbox window type the text Apply To to search for this command. You should see a small gray icon:

9. Drag this icon below the Create Instance box. Notice the word Empty next to the Apply To command. We mustfirst choose which object we want to apply the speed and direction commands to.10. Click the word Empty to select this command and then click the Down Arrow icon.11. Choose the Star object from the popup menu. You are now ready to apply a direction and speed command tothis instance. This is done by attaching these commands to the right side of the icon and not the bottom.12. In the toolbox area type the text Direction to locate the direction icon.

13. Drag the Direction icon and attach it to the right side of the Apply To command:14.15.16.17.Click the right arrow to set the direction.Type the text speed in the Toolbox search area.Drag the speed icon and attach it to the right side of the Apply To command.Set the speed to 5.

The command should look like:18. Save your project and run it. When you press the Spacebar you should see a Star object move from the pumpkinto the right side of the screen.

Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications used to create games. Most game engines include three major components, tools to create and edit game components, built-in components that can be included in the