Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Reference: www.pygame.org. The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)"— Presentation transcript:

1 Reference: www.pygame.org

2 The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables) 1. Erase the screen 2. Actual drawing code (using variables) 3. Flip 4. Repeat [Brain absorbs info while new screen is being drawn in steps 1-4]

3 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

4 Events and pygame Pygame 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)]

5 A few more pygame tidbits Clock objects  tick  get_fps Sounds / Music  loading and playing

6 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.htmlhttp://oldcomputers.net/appleiigs.html Alienware image: http://www.dell.com/us/p/alienware-area51-alx/pd?refid=alienware-area-51-alx Do these run at the same speed?

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

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

9 Example [Adjust moving circle example to run at a constant rate] [Animated "Bjorn" character (Reiner's tileset)]  http://www.reinerstilesets.de/

10 Parting Thought [Playing background music and sound effects]


Download ppt "Reference: www.pygame.org. The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)"

Similar presentations


Ads by Google