23
Python & Perl Lecture 10 Department of Computer Science Utah State University

Python lecture 10

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Python lecture 10

Python & Perl

Lecture 10

Department of Computer ScienceUtah State University

Page 2: Python lecture 10

Outline● PyGame advanced topics● User input ● Loading sound and images

Page 3: Python lecture 10

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

Page 4: Python lecture 10

Pygame examples

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

Page 5: Python lecture 10

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

Page 6: Python lecture 10

Goals

● Creating a graphics window● Loading images and sound files● Rendering TTF text● Basic event and mouse handling

Page 7: Python lecture 10

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”

Page 8: Python lecture 10

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

Page 9: Python lecture 10

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.

Page 10: Python lecture 10

Game Object Classes

Page 11: Python lecture 10

Fist class

Page 12: Python lecture 10

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.

Page 13: Python lecture 10

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.

Page 14: Python lecture 10

Initialize Everything

Page 15: Python lecture 10

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.

Page 16: Python lecture 10

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.

Page 17: Python lecture 10

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

Page 18: Python lecture 10

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

Page 19: Python lecture 10

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)

Page 20: Python lecture 10

Update the Sprites

Sprite groups have an update() method, which simply calls the update method for all the sprites it contains.

Page 21: Python lecture 10

Draw The Entire Scene

Page 22: Python lecture 10

Aliens

Page 23: Python lecture 10

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

fessional, 2nd Ed., APRESS