Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user]...

10
Part II Reference: www.pygame.org Game Loops +

Transcript of Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user]...

Page 1: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

Part IIReference: www.pygame.org

Game Loops +

Page 2: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

The Game Loop

Animation / Game loop1. Update variables2. [Get input from the user] (GameLoop only)3. Draw (using variables)

1. Erase the screen2. Actual drawing code (using variables)3. Flip

4. Repeat [Brain absorbs info while new screen is being drawn in steps 1-4]

Page 3: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

Window input

(common to nearly all OS's)Application == Window (to the OS)

Application might have sub-windows Buttons, Menus, …

OS sends messages (events) to the App Upon creation Minimize / Maximize / Resize / Close Mouse / Keyboard / Gamepad events

Page 4: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

Events and pygamePygame gives us two choices:

Event Handling: Handle (new) events manually Ask pygame for a list of Event objects with

pygame.event.get() Iterate through it (hint: while or later a for loop)

If this event is important to our app, handle it. [We’ll do this after looking at sequences]

Device-polling: Let pygame process the events and then we poll pygame. Call pygame.event.pump()

Process all waiting events Saves result in status variables

Access the status variables Hybrid:

Use pygame.event.get. Handle events of interest Use device polling for the rest

[Example of each: moving circle, switch color w/ space, close button (w/ mouse over)]

Page 5: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

A few more pygame tidbits

Clock objects tick get_fps

Sounds / Music loading and playing

Page 6: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

A Game Loop "problem"

So…a game loop run on these would be faster / slower.

Character moving at 1 pixels / update. Suppose we write it on the IIgs What would it look like on the Alienware?

IIgs image: http://oldcomputers.net/appleiigs.htmlAlienware image: http://www.dell.com/us/p/alienware-area51-alx/pd?refid=alienware-area-51-alx

Do these run at the same speed?

Page 7: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

A Game Loop "problem", cont.Solution #1: Insert pauses

Set a desired frame rate (60fps == 1/60s per frame) Calculate time since last frame Insert a time.sleep if necessary (or use the clock

object) Advantage: simple Disadvantages:

If the computer can't reach 60 fps, there will be differences.

time.sleep pauses everything. We could do useful work during that pause AI calculations physics etc.

If we want to change the target fps, we have to change movement / rotation amounts everywhere in our code.

Page 8: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

A Game Loop "problem", cont.

Solution #2: Adjust movements based on dt Let the computer run as fast as possible Calculate dT (the time since last frame) Express desired movments speeds as a velocity (e.g.

100 pixels / s) Each frame, velocity * dT is the distance to move.

On a slow computer… On a fast computer…

This is what's done on most PC games and most modern consoles.

Disadvantages: we have to do an extra multiply every frame.

Page 9: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

Example

[Adjust moving circle example to run at a constant rate]

[Animated "Bjorn" character (Reiner's tileset)] http://www.reinerstilesets.de/

Page 10: Reference: . The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)

Parting Thought

[Playing background music and sound effects]