Unformatted text preview:

Unit 9 pyGame Special thanks to Roy McElmurry John Kurkowski Scott Shawcroft Ryan Tucker Paul Beck for their work Except where otherwise noted this work is licensed under http creativecommons org licenses by nc sa 3 0 Exercise Whack a mole Goal Let s create a whack a mole game where moles pop up on screen periodically The user can click a mole to whack it This leads to A sound is played The player gets 1 point A new mole appears elsewhere on the screen The number of points is displayed at the top of the screen 2 What is pyGame A set of Python modules to make it easier to write games home page http pygame org documentation http pygame org docs ref pyGame helps you do the following and more Sophisticated 2 D graphics drawing functions Deal with media images sound F X music nicely Respond to user input keyboard joystick mouse Built in classes to represent common game objects 3 pyGame at a glance pyGame consists of many modules of code to help you cdrom font movie cursors image sndarray display joystick surfarray draw key time event mouse transform To use a given module import it For example import pygame from pygame import from pygame display import 4 Game fundamentals sprites Onscreen characters or other moving objects collision detection Seeing which pairs of sprites touch event An in game action such as a mouse or key press event loop Many games have an overall loop that waits for events to occur updates sprites redraws screen 5 A basic skeleton pygame template py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 from pygame import from pygame sprite import pygame init starts up pyGame screen display set mode width height display set caption window title create set up sprites the overall event loop while True e event wait if e type QUIT pygame quit break update sprites etc screen fill 255 255 255 display update pause until event occurs shuts down pyGame white background redraw screen 6 Initializing pyGame To start off our game we must pop up a graphical window Calling display set mode creates a window The call returns an object of type Surface which we will call screen We can call methods on the screen later Calling display set caption sets the window s title from pygame import pygame init starts up pyGame screen display set mode width height display set caption title pygame quit 7 Surfaces screen display set mode width height a surface In Pygame every 2D object is an object of type Surface The screen object each game character images etc Useful methods in each Surface object Surface width height constructs new Surface of given size fill red green blue paints surface in given color rgb 0255 get width get height returns the dimensions of the surface returns a Rect object representing the x y w h bounding this surface after changing any surfaces callsurface display update blit surface coords drawsmust another onto this surface at the given coordinates get rect 8 Sprites Sprites Onscreen characters or other moving objects A sprite has data behavior such as its position and size on the screen an image or shape for its appearance the ability to collide with other sprites whether it is alive or on screen right now might be part of certain groups enemies food In pyGame each type of sprite is represented as a subclass of the class pygame sprite Sprite 9 A rectangular sprite from pygame import from pygame sprite import class name Sprite def init self constructor Sprite init self self image Surface width height self rect Rect leftX topY width height other methods if any Important fields in every sprite image the image or shape to draw for this sprite a Surface as with screen you can fill this or draw things onto it rect position and size of where to draw the sprite a Rect Important methods update kill alive 10 Rect methods clip rect crops this rect s size to bounds of given rect collidepoint p True if this Rect contains the point colliderect rect True if this Rect touches the rect collidelist list True if this Rect touches any rect in the list collidelistall list True if this Rect touches all rects in the list contains rect True if this Rect completely contains the rect copy returns a copy of this rectangle inflate dx dy grows size of rectangle by given offsets move dx dy rather shiftsthan position of rectangle by given Many methods mutating return a newoffsets rect union rect ip smallest rectangle that contains this and To mutate use in place version e g move ip rect 11 A Sprite using an image from pygame import from pygame sprite import class name Sprite def init self constructor Sprite init self self image image load filename convert self rect self image get rect move x y other methods if any When using an image you load it from a file with image load and then use its size to define the rect field Any time you want a sprite to move on the screen you must change the state of its rect field 12 Setting up sprites When creating a game we think about the sprites What sprites are there on the screen What data behavior should each one keep track of Are any sprites similar If so maybe they share a class For our Whack a Mole game class Mole Sprite 13 Sprite groups name Group sprite1 sprite2 To draw sprites on screen put them into a Group Useful methods of each Group object draw surface draws all sprites in group on a Surface update calls every sprite s update method my mole1 Mole create a Mole object my mole2 Mole all sprites Group my mole1 other mole2 in the event loop all sprites draw screen 14 Events event driven programming When the overall program is a series of responses to user actions or events event loop aka main loop animation loop Many games have an overall loop to do the following wait for an event to occur or wait a certain interval of time update all game objects location etc redraw the screen repeat 15 The event loop In an event loop you wait for something to happen and then depending on the kind of event you process it while True e event wait wait for an event if e type QUIT pygame quit exit the game break elif e type type code to handle some other type of events elif 16 Mouse events Mouse actions lead to events with specific types press button down MOUSEBUTTONDOWN release button MOUSEBUTTONUP move the cursor MOUSEMOTION At any point you can call mouse get pos which returns the mouse s current position as an x y tuple e event wait if e type MOUSEMOTION pt mouse get pos x y pt 17 Collision detection collision detection Examining pairs of sprites to see if they are touching each


View Full Document

UW CSE 142 - Study Notes

Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?