Python lecture 10
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
228
On Slideshare
228
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Python & Perl Lecture 10 Department of Computer Science Utah State University
  • 2. Outline PyGame advanced topics ● User input ● Loading sound and images ●
  • 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. Pygame examples ● http://www.pygame.org/download.shtml
  • 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. Goals ● Creating a graphics window ● Loading images and sound files ● Rendering TTF text ● Basic event and mouse handling
  • 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. 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. 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. Game Object Classes
  • 11. Fist class
  • 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. 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. Initialize Everything
  • 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. 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. 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. 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. 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. Update the Sprites Sprite groups have an update() method, which simply calls the update method for all the sprites it contains.
  • 21. Draw The Entire Scene
  • 22. Aliens
  • 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