Python & Perl
Lecture 10

Department of Computer Science
Utah State University
Outline
PyGame advanced topics
● User input
● Loading sound and images
●
Collision detection
●

●

Pygame uses Rect objects to store and manipulate
rectangular areas
Colliderect()
test if two rec...
Pygame examples

●

http://www.pygame.org/download.shtml
Chimp game
●

●

In the pygame examples there is a simple example
named, "chimp".
This example simulates a punchable monke...
Goals
●

Creating a graphics window

●

Loading images and sound files

●

Rendering TTF text

●

Basic event and mouse ha...
Import Modules

There is a special pygame module named "locals". This module contains a
subset of pygame. The members of t...
Load Images
●

This function takes the name of an image to
load and a colorkey for the image. A
colorkey is used in graphi...
Load Sound
●

●

This function checks to see if the
pygame.mixer
module
was
imported
correctly. If not, it returns a small...
Game Object Classes
Fist class
Fist class
●

●

●

●

●

We create a class to represent the players fist
The __init__ function is called when new instanc...
Chimp class
●

This class will move the chimp back and forth across the screen.

●

When the monkey is punched, he will sp...
Initialize Everything
Create The Background
●

●

●

creates a new surface for us that is the
same size as the display window
The convert with n...
Put Text On The Background, Centered
●

●

We need to find the
centered position of the
text on our display
We create a "R...
Display The Background While Setup
Finishes
●

●

Show our background while we wait for
the other resources to load
This w...
Prepare Game Object
●

●

●

We create all the objects that the game is
going to need
We create an instance of each of our...
Handle All Input Events
●

●

●

First we get all the available Events
from pygame and loop through each of
them
check to ...
Update the Sprites

Sprite groups have an update() method, which simply calls the update method for all
the sprites it con...
Draw The Entire Scene
Aliens
Reading & References
●
●

●

www.pygame.org
http://www.pygame.org/docs/ref/rect.html#pygame.Rect.colli
derect
Ch 05 M. L. ...
Upcoming SlideShare
Loading in...5
×

Python lecture 10

72

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
72
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python lecture 10

  1. 1. Python & Perl Lecture 10 Department of Computer Science Utah State University
  2. 2. Outline PyGame advanced topics ● User input ● Loading sound and images ●
  3. 3. Collision detection ● ● Pygame uses Rect objects to store and manipulate rectangular areas Colliderect() test if two rectangles overlap colliderect(Rect) -> bool ● Returns true if any portion of either rectangle overlap
  4. 4. Pygame examples ● http://www.pygame.org/download.shtml
  5. 5. Chimp game ● ● In the pygame examples there is a simple example named, "chimp". This example simulates a punchable monkey moving around a small screen with promises of riches and reward
  6. 6. Goals ● Creating a graphics window ● Loading images and sound files ● Rendering TTF text ● Basic event and mouse handling
  7. 7. Import Modules There is a special pygame module named "locals". This module contains a subset of pygame. The members of this module are commonly used constants and functions that have proven useful to put into your program's global namespace. This locals module includes functions like "Rect" to create a rectangle object, and many constants like "QUIT”
  8. 8. Load Images ● This function takes the name of an image to load and a colorkey for the image. A colorkey is used in graphics to represent a color of the image that is transparent ● Platform independent pathname ● Loads image safely ● set the colorkey for the image
  9. 9. Load Sound ● ● This function checks to see if the pygame.mixer module was imported correctly. If not, it returns a small class instance that has a dummy play method It creates a full path to the sound image, and loads the sound file inside a try/except block. Then returns the loaded Sound object.
  10. 10. Game Object Classes
  11. 11. Fist class
  12. 12. Fist class ● ● ● ● ● We create a class to represent the players fist The __init__ function is called when new instances of this class are created. The first thing we do is be sure to call the __init__ function for our base class Sprite The update method is called once per frame The update() method for the fist moves the fist to the location of the mouse pointer. It also offsets the fist position slightly if the fist is in the "punching" state.
  13. 13. Chimp class ● This class will move the chimp back and forth across the screen. ● When the monkey is punched, he will spin around to exciting effect ● ● ● This class is also derived from the base Sprite class While initializing, the class also sets the attribute "area" to be the size of the display screen. update function for the chimp simply looks at the current "dizzy" state, which is true when the monkey is spinning from a punch. It calls either the _spin or _walk method.
  14. 14. Initialize Everything
  15. 15. Create The Background ● ● ● creates a new surface for us that is the same size as the display window The convert with no arguments will make sure our background is the same format as the display window, which will give us the fastest results Fill the entire background with a solid white-ish color. Fill takes an RGB triplet as the color argument.
  16. 16. Put Text On The Background, Centered ● ● We need to find the centered position of the text on our display We create a "Rect" object from the text dimensions, which allows us to easily assign it to the screen center.
  17. 17. Display The Background While Setup Finishes ● ● Show our background while we wait for the other resources to load This will blit our entire background onto the display window
  18. 18. Prepare Game Object ● ● ● We create all the objects that the game is going to need We create an instance of each of our sprite classes. And lastly we create a sprite Group which will contain all our sprites RenderPlain :This sprite group can draw all the sprites it contains to the screen. It is called RenderPlain because there are actually more advanced Render groups
  19. 19. Handle All Input Events ● ● ● First we get all the available Events from pygame and loop through each of them check to see if the mouse button was pressed or released If the button was pressed, we ask the fist object if it has collided with the chimp. We play the appropriate sound effect, and if the monkey was hit, we tell him to start spinning (by calling his punched() method)
  20. 20. Update the Sprites Sprite groups have an update() method, which simply calls the update method for all the sprites it contains.
  21. 21. Draw The Entire Scene
  22. 22. Aliens
  23. 23. Reading & References ● ● ● www.pygame.org http://www.pygame.org/docs/ref/rect.html#pygame.Rect.colli derect Ch 05 M. L. Hetland. Beginning Python From Novice to Professional, 2nd Ed., APRESS
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×