660 likes | 1.66k Views
Pygame. Dick Steflik. pygame. Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers around OS functions Windows, Unix, Linux, Android, iOS Can be used for multimedia development Distributed with python. Pygame Intro.
E N D
Pygame Dick Steflik
pygame • Python module developed for writing games in python • Cross platform • Written on top of the Simple DirectMedia Layer, wrappers around OS functions • Windows, Unix, Linux, Android, iOS • Can be used for multimedia development • Distributed with python
Pygame Intro # Import a library of functions called 'pygame' import pygame # Initialize the game engine pygame.init() # Set the width and height of the screen screen=pygame.display.set_mode(700,500) # Put a title on the window pygame.display.set_caption(“One Cool Game") #Loop until the user clicks the close button. done=False # Used to manage how fast the screen updates clock=pygame.time.Clock() # -------- Main Program Loop ----------- while done==False: # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done=True # set true to kill main loop # ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT # ALL GAME LOGIC SHOULD HERE # ALL CODE TO DRAW SHOULD GO HERE # Limit to 20 frames per second clock.tick(20) Pygame.quit()
Events QUIT none ACTIVEEVENT gain, state KEYDOWN unicode, key, mod KEYUP key, mod MOUSEMOTION pos, rel, buttons MOUSEBUTTONUP pos, button MOUSEBUTTONDOWN pos, button JOYAXISMOTION joy, axis, value JOYBALLMOTION joy, ball, rel JOYHATMOTION joy, hat, value JOYBUTTONUP joy, button JOYBUTTONDOWN joy, button VIDEORESIZE size, w, h VIDEOEXPOSE none USEREVENT code
Event Methods pygame.event.pump — internally process pygame event handlers pygame.event.get — get events from the queue pygame.event.poll — get a single event from the queue pygame.event.wait — wait for a single event from the queue pygame.event.peek — test if event types are waiting on the queue pygame.event.clear — remove all events from the queue pygame.event.event_name — get the string name from and event id pygame.event.set_blocked — control which events are allowed on the queue pygame.event.set_allowed — control which events are allowed on the queue pygame.event.get_blocked — test if a type of event is blocked from the queue pygame.event.set_grab — control the sharing of input devices with other applications pygame.event.get_grab — test if the program is sharing input devices pygame.event.post — place a new event on the queue pygame.event.Event — create a new event object
Surfaces • A memory buffer of pixels • Can reside in system memory or in special hardware memory that can be hardware accelerated • Used mostly with drawing functions
Geometric Drawing ; • Also known as line drawing • pygame.draw.rect — draw a rectangle shape • pygame.draw.polygon — draw a shape with any number of sides • pygame.draw.circle — draw a circle around a point • pygame.draw.ellipse — draw a round shape inside a rectangle • pygame.draw.arc— draw a partial section of an ellipse • pygame.draw.line — draw a straight line segment • pygame.draw.lines — draw multiple contiguous line segments • pygame.draw.aaline— draw fine antialiased lines • pygame.draw.aalines— draw a connected sequence of antialiased lines
Antialiasing • Also knowing as smoothing • Because we use bitmapped displays a line is really made up of pixels placed next to one another, This make a line look like its has jagged edges. At the lowest level of a graphics package there are some digital signal processing techniques applied to a line to make it look smooth
pygame.draw.rect • draw a rectangle shape • rect(Surface, color, Rect, width=0) -> Rect • Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled. • Keep in mind the Surface.fill() method works just as well for drawing filled rectangles. In fact the Surface.fill() can be hardware accelerated on some platforms with both software and hardware display modes.
pygame.draw.polygon() • draw a shape with any number of sides • polygon(Surface, color, pointlist, width=0) -> Rect • Draws a polygonal shape on the Surface. The pointlist argument is the vertices of the polygon. The width argument is the thickness to draw the outer edge. If width is zero then the polygon will be filled. • For aapolygon, use aalines with the ‘closed’ parameter.
pygame.draw.circle() • draw a circle around a point • circle(Surface, color, pos, radius, width=0) -> Rect • Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.
pygame.draw.ellipse() • draw a round shape inside a rectangle • ellipse(Surface, color, Rect, width=0) -> Rect • Draws an elliptical shape on the Surface. The given rectangle is the area that the circle will fill. The width argument is the thickness to draw the outer edge. If width is zero then the ellipse will be filled.
pygame.draw.arc() • draw a partial section of an ellipse • arc(Surface, color, Rect, start_angle, stop_angle, width=1) -> Rect • Draws an elliptical arc on the Surface. The rect argument is the area that the ellipse will fill. The two angle arguments are the initial and final angle in radians, with the zero on the right. The width argument is the thickness to draw the outer edge.
pygame.draw.line() • draw a straight line segment • line(Surface, color, start_pos, end_pos, width=1) -> Rect • Draw a straight line segment on a Surface. There are no endcaps, the ends are squared off for thick lines.
pygame.draw.lines() • draw multiple contiguous line segments • lines(Surface, color, closed, pointlist, width=1) -> Rect • Draw a sequence of lines on a Surface. The pointlist argument is a series of points that are connected by a line. If the closed argument is true an additional line segment is drawn between the first and last points. • This does not draw any endcaps or miter joints. Lines with sharp corners and wide line widths can have improper looking corners.
Pygame.draw.aaline() • draw fine antialiased lines • aaline(Surface, color, startpos, endpos, blend=1) -> Rect • Draws an anti-aliased line on a surface. This will respect the clipping rectangle. A bounding box of the affected area is returned as a rectangle. If blend is true, the shades will be be blended with existing pixel shades instead of overwriting them. This function accepts floating point values for the end points.
pygame.draw.aalines() • draw a connected sequence of antialiased lines • aalines(Surface, color, closed, pointlist, blend=1) -> Rect • Draws a sequence on a surface. You must pass at least two points in the sequence of points. The closed argument is a simple Boolean and if true, a line will be draw between the first and last points. The Boolean blend argument set to true will blend the shades with existing shades instead of overwriting them. This function accepts floating point values for the end points.
Sprites • a two-dimensional image or animation that is integrated into a larger scene • Used in many simple interactive video games • Usually a small .jpg, .gif or .png graphic
Pygame.sprite • Several data structures for containing and rendering sprites • Groups, RenderPlain • Methods for manipulating sprites • Kill a sprite, remove a sprite from a container,add a sprite to a container, draw a sprite on a surface • Several methods for detecting collisions between sprites • Groupcollide, spritecollide
RenderPlain • This is a container that is used to hold sprites that has the ability to render all of the sprites it contains. • Use the add method to add more sprites to the group • Use kill or remove to remove a sprite from a group
SpriteCollide • Detects a collision between a specific sprite and a group of sprites • Pygame.sprite.spritecollide(player, group, dokill) • If dokill is True when a collision is detected the sprite(s) collided with are removed from all groups they belong to. This make it very easy to make targer/shooter games.