Upload
linda-ray
View
225
Download
0
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
CRE Programming Club
Class 8Robert 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.
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.
What State Are You In?
Laying DownSittingWalkingJumpingRunningBikingHow do you go from one state to another?
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.
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.
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
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.
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.
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
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
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
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
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.
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
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?
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
What About This?
Sub hideLevelText
Shapes.HideShape(levelText)
EndSub
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.
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”
Next Time...
We’re going to learn about what happens when shapes collide.