Download presentation
Presentation is loading. Please wait.
Published byLinda Ray Modified over 9 years ago
1
CRE Programming Club Class 8 Robert Eckstein and Robert Heard
2
Introduction to Game Design Games have to be fun. Games have be challenging. The best games are easy to learn and difficult to master.
3
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.
4
What State Are You In? Laying Down Sitting Walking Jumping Running Biking How do you go from one state to another?
5
The State Machine Games 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.
6
Transitions and Events State 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.
8
playGame = “True” While (playGame = "True") ‘ Do something inside the loop... ‘ And something else... ‘ What happens if playGame is set to “False”? EndWhile A Simple Game Loop
9
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.
10
Creating Delays Remember 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.
11
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
12
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
13
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
14
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
15
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.
16
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
17
Subroutine initLevel Sub 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?
18
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
19
What About This? Sub hideLevelText Shapes.HideShape(levelText) EndSub
20
Using the State Machine displayLevelText() Program.Delay(5000) hideLevelText() currentState = PLAY_GAME This last part is needed to transition the state machine to the PLAY_GAME state.
21
Homework Create 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”
22
Next Time... We’re going to learn about what happens when shapes collide.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.