Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One...

13
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 game, and a game runtime. The reason that there are game engines is to simplify game programming 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 level concepts behind game programming. These concepts include: Game Loop Game Objects Game and Object State Event/Callbacks Game Loop The Game Loop are the steps a game goes through when it runs. The basic web loop steps are: 1. Get input from player or other 2. Update the game state 3. Redraw the screen 4. Go to step 1 The 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 the game is updated as a result of the inputs. During this update step the game programmer can instruct the game to change elements of the game based on the input. This is where the game programmer creates the things he or she wants the game to do before the next step. The final step in the game loop is redrawing the screen and reflecting the updated 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. Read the status of the keyboard/controller and get the move command 2. Update the draw location of the game object based on information gathered during the input step. 3. Redraw the screen with the game object at the new location. 4. 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 Per Second (FPS) since that is how many times the game will refresh the screen. The apparent movement of objects on the screen happens because each time through the game loop objects move small amounts. At 30 or 60 times a second the object 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 in parallel with the game loop performing tasks. Object movement is not a fixed value based on the speed of the game loop but is a multiple of the absolute time it took for the game loop to process. For example, suppose you wanted a character on the screen to move 10 pixels a second on the screen. To keep the movement smooth and not be tied to the speed 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.

Transcript of Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One...

Page 1: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

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 game, and a game runtime. The reason that there are game engines is to simplify game

programming 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 level

concepts behind game programming. These concepts include:

Game Loop

Game Objects

Game and Object State

Event/Callbacks

Game Loop

The Game Loop are the steps a game goes through when it runs. The basic web loop steps are:

1. Get input from player or other

2. Update the game state

3. Redraw the screen

4. Go to step 1

The 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 the

game is updated as a result of the inputs. During this update step the game programmer can instruct the game to

change elements of the game based on the input. This is where the game programmer creates the things he or she

wants the game to do before the next step. The final step in the game loop is redrawing the screen and reflecting the

updated 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. Read the status of the keyboard/controller and get the move command

2. Update the draw location of the game object based on information gathered during the input step.

3. Redraw the screen with the game object at the new location.

4. 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 Per

Second (FPS) since that is how many times the game will refresh the screen. The apparent movement of objects on the

screen happens because each time through the game loop objects move small amounts. At 30 or 60 times a second the

object 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 in

parallel with the game loop performing tasks. Object movement is not a fixed value based on the speed of the game

loop but is a multiple of the absolute time it took for the game loop to process. For example, suppose you wanted a

character on the screen to move 10 pixels a second on the screen. To keep the movement smooth and not be tied to the

speed 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.

Page 2: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

[Note: A Pixel is an imaginary dot on the screen representing a color and position used to draw images. As you set your

screen resolution up or down (the number of pixels that will fit on the screen) the pixels get “larger” or “smaller”.]

Game Objects

Because game programming is very complex, various strategies have been used to simplify game development. One of

these is having the Game Engine represent game elements using an Object Model. The Object Model is an abstract way

to 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 to

do things (called Methods). Thinking of game elements as Objects helps keep the object related behaviors and

properties in one identifiable area. Game Objects can also be hooked into the Event/Callback system so the game engine

can 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, Treasure

Methods: Get and Set Health and Stamina, Get Strength and Treasure

When the game is played, the main game character engages in combat with the Monster. Each time the player hits the

Monster the amount of health for the monster is reduced. The Monster method to reduce health will be called and the

current amount of monster health displayed on the screen. When the Monster hits the player’s character the Monster

strength will be used to calculate how much health the player is reduced. When the monster is killed the game calls the

Monster’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 game

engine to hook into our game objects using the Event/Callback method.

Game and Object State

Another important concept in game programming is the Game and Object state. A State is just the collection of data that

describe the object and the game. Object states might include position, rotation, health, name, and so forth. Everything

that the object contains would be the object state. That is, if we wanted to re-create the Game Object everything we

need 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 and

location of all objects, the progress of the player, and basically everything we would need to recreate the state of the

game.

Event/Callback

One of the central ideas behind managing complex game programming is the Event/Callback structure. This includes two

of the game loop elements, getting inputs and updating game state. Most game engines will, as the game runs, create

an 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 and

perform actions.

For example, if the game pulls a collision event between two objects (perhaps a sword collides with a monster) the game

engine creates and Event object and puts it on the Event Queue. The Monster object in the game may continually check

the event queue for Events it is interested in, such as getting hit by a sword. When the Monster object sees the event it

calls the method for reducing its own health.

Page 3: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

GameMaker Structure

The structure of gamemaker implements many of these general game objects. Gamemaker creates an Event queue and

treats game elements as objects. The objects will have properties (usually sprites) and you can attach an event to the

object. 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 the

Resources menu:

To add new resources you right click the item category and choose how you want to add or create the item:

Page 4: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

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.

Page 5: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

Clicking the Events brings up the Event menu. These are the events that have been attached to the object. Notice that

the 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:

Page 6: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

GameMaker references actions by a set of icons. You will drag the icon representing the action into the Event area and

set the options on the action. In this case you are setting the speed of the object (pumpkinobj) and the direction when

the left key is pressed. However, pressing a key is one event and releasing the key is a separate event. The Key Up – Any

event will set the speed to 0 and stop the object.

Another object in this project is the Star object.

Page 7: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

In this project we will use a key pressed event to perform the following:

Create a Star object next to the Pumpkin

Set the star object direction and speed.

1. Double click the Pumpkinobj object to open the object editor windows:

Page 8: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

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

Page 9: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

3. In the Toolbox area of the third window type Instance until you see a red lightbulb icon

4. 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 refer

to the screen coordinates with X=0 and Y=0 being the upper left corner of the screen. By default this is set to

zero. 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 pumpkin

object’s position.

Page 10: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

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, if

we drag the speed icon and the direction icon into this window it will be applied to the pumpkin object. We want it

applied 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 star

object.

8. In the toolbox window type the text Apply To to search for this command. You should see a small gray icon:

Page 11: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

9. Drag this icon below the Create Instance box. Notice the word Empty next to the Apply To command. We must

first 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 to

this 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.

Page 12: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

13. Drag the Direction icon and attach it to the right side of the Apply To command:

14. Click the right arrow to set the direction.

15. Type the text speed in the Toolbox search area.

16. Drag the speed icon and attach it to the right side of the Apply To command.

17. Set the speed to 5.

Page 13: Game Programming Lecture One - Fullerton Collegestaff One.pdf · Game Programming – Lecture One Game programming involves (in most cases) using a Game Engine. Game Engines are applications

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 pumpkin

to the right side of the screen.