22
CRE Programming Club Class 8 Robert Eckstein and Robert Heard

CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Embed Size (px)

DESCRIPTION

The Loop All games start with a simple loop. The loop will continue until the program is exited. There is often a second loop on the inside that is a “control loop,” and handles things like pausing or quitting the current game (but not the program itself.) We’ll see an example of this shortly.

Citation preview

Page 1: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

CRE Programming Club

Class 8Robert Eckstein and Robert Heard

Page 2: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Introduction to Game Design

Games have to be fun.Games have be challenging.The best games are easy to learn and difficult to master.

Page 3: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

The LoopAll games start with a simple loop.The loop will continue until the program is exited.There is often a second loop on the inside that is a “control loop,” and handles things like pausing or quitting the current game (but not the program itself.)We’ll see an example of this shortly.

Page 4: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

What State Are You In?

Laying DownSittingWalkingJumpingRunningBikingHow do you go from one state to another?

Page 5: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

The State MachineGames often make use a (finite) state machine. A state machine is a very common tool in programming.A state machine is a fancy name for a program that is always in one--and exactly one--of several states.Common game states: main menu, starting level, playing level, finishing level, paused, cutscene, etc.

Page 6: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Transitions and EventsState machines have to have the ability to move from one state to another: these are called transitions. Transitions are usually started with events.An event might be the user pressing a key, clicking a mouse, or something in the program’s logic that says it’s time for a transition to occur. Something triggers them.

Page 7: CRE Programming Club Class 8 Robert Eckstein and Robert Heard
Page 8: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

playGame = “True”

While (playGame = "True")

‘ Do something inside the loop...

‘ And something else...

‘ And something else...

‘ What happens if playGame is set to “False”?

EndWhile

A Simple Game Loop

Page 9: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

We Immediately Have a Problem...

Computers come with different CPUs that run at different speeds!A faster CPU will race through the game loop more rapidly than a slower CPU.That means your game may run too slowly on older CPUs, or too quickly on newer CPUs!We need the game to run at a consistent speed on all platforms.

Page 10: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Creating DelaysRemember that one of the Clock object operations

could be used like a stopwatch? What if we start measuring the time at the beginning of the loop...

startClock = Clock.ElapsedMilliseconds

Elapsed milliseconds since what? The epoch, which in typically exactly midnight on January 1st, 1970.

Page 11: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

How Long Did It Take to Run?

We then ask the computer for the current elapsed milliseconds at the end of the loop and subtract what we got at the start of the loop...

timeTaken = Clock.ElapsedMilliseconds - startClock

Page 12: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

At the End of the Loop...

We then write some delay code to ensure that the program runs at the same speed no matter how fast the computer is...

delay = 50 - timeTaken

If (delay > 0) Then

Program.Delay(delay)

EndIf

Page 13: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Frames Per Second (fps)

Where did we get 50 from? Nowhere. But we can instead specify a number that represents how long each “frame” is. Remember that 1000 milliseconds = 1 second. Divide by fps to get the number of milliseconds each “frame” should take!

delay = (1000/fps)-timeTaken

If (delay > 0) Then

Program.Delay(delay)

EndIf

Page 14: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Import JGT020...

It looks something like this, inside the game loop...

If (currentState = DISPLAY_LEVEL) Then

ElseIf (currentState = PLAY_GAME) Then

ElseIf (currentState = END_LEVEL) Then

EndIf

Page 15: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Work With This Program

Notice that the program has three states.The program starts in the first state, then jump to the second state, then to the third.How do we transition? You’ll do this by changing the state variable in one loop to be equal to the next state.

Page 16: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Another Possible State Machine

If (currentState = INIT_LEVEL) Then

initLevel()

ElseIf (currentState = INTRODUCE_LEVEL) Then

introduceLevel()

ElseIf (currentState = PLAY_LEVEL) Then

playLevel()

ElseIf (currentState = END_LEVEL) Then

endLevel()

EndIf

Page 17: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Subroutine initLevelSub initLevel

' Each time we start a new level, we add 1 to the level number

levelNumber = levelNumber + 1

...

EndSub

• What other initialization might we need here?

Page 18: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Back To JGT020

Sub displayLevelText

levelText = Shapes.AddText("Level " + currentLevel)

Shapes.Move(levelText, GraphicsWindow.Width / 2 - 60, GraphicsWindow.Height / 2 - 20)

Shapes.ShowShape(levelText)

EndSub

Page 19: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

What About This?

Sub hideLevelText

Shapes.HideShape(levelText)

EndSub

Page 20: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Using the State Machine

displayLevelText() Program.Delay(5000) hideLevelText() currentState = PLAY_GAMEThis last part is needed to transition the state machine to the PLAY_GAME state.

Page 21: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

HomeworkCreate subroutine(s) that handle the PLAY_GAME state. Use the homework from last week to help you. Can you change its color? Its size? Can you click on it somehow with the mouse? Can you use the keyboard?Create subroutine(s) that handle the END_LEVEL state. Maybe you can put up text that says “Level Completed”

Page 22: CRE Programming Club Class 8 Robert Eckstein and Robert Heard

Next Time...

We’re going to learn about what happens when shapes collide.