Download presentation
Presentation is loading. Please wait.
Published byRonald Kennedy Modified over 8 years ago
1
(More) Event handling
2
Input and Windowed-OS’s Windowed OS’s (Windows, OSX, GUI-linux’s) send messages (events) when the user does something “on” the window: – Move mouse – Press a key – Close the window – Resize the window – Minimize / maximize The window notifies the program when these first happen. The application can respond to these (event handling)
3
Input and Windowed OS’s What we've been using so far… device-polling: get the current state of an input device. – “Is the ‘a’ key currently pressed? – Where is the mouse cursor?
4
Event handling in pygame A second way… Event Handling The pygame.event.get() function returns a list of new event objects. – Each object has a.type attribute (a variable) You can compare it to pygame.XYZ variables to determine its type. Depending on the type, the event object may have other Calling pygame.event.get() empties the event queue for your window.
5
Examples Write a program which: – Can be closed by clicking the ‘X’ or by pressing escape. – Can be resized / fullscreen-ed – Mouse-over and click-release on a "quit button".
6
A few more pygame tidbits Clock objects – tick – get_fps Sounds / Music – loading and playing
7
A Game Loop "problem" So…a game loop run on these would be faster / slower. Character moving at 100 pixels / s. – 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?
8
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. – Advantage: simple – Disadvantages: If the computer can't reach 60fps, there will be differences. time.sleep pauses everything. We could do useful work during that pause – AI calculations – physics – etc.
9
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.