21
Pygame Dick Steflik

Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

Embed Size (px)

Citation preview

Page 1: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

Pygame

Dick Steflik

Page 2: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 3: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

Pygame Intro # Import a library of functions called 'pygame'import pygame# Initialize the game enginepygame.init()# Set the width and height of the screenscreen=pygame.display.set_mode(700,500)# Put a title on the windowpygame.display.set_caption(“One Cool Game")#Loop until the user clicks the close button.done=False# Used to manage how fast the screen updatesclock=pygame.time.Clock()# -------- Main Program Loop -----------while done==False:

# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENTfor event in pygame.event.get(): # User did something

if event.type == pygame.QUIT: # If user clicked closedone=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 secondclock.tick(20)

Pygame.quit()

Page 4: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

Events

QUIT noneACTIVEEVENT gain, stateKEYDOWN unicode, key, modKEYUP key, modMOUSEMOTION pos, rel, buttonsMOUSEBUTTONUP pos, buttonMOUSEBUTTONDOWN pos, buttonJOYAXISMOTION joy, axis, valueJOYBALLMOTION joy, ball, relJOYHATMOTION joy, hat, valueJOYBUTTONUP joy, buttonJOYBUTTONDOWN joy, buttonVIDEORESIZE size, w, hVIDEOEXPOSE noneUSEREVENT code

Page 5: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

Event Methods

pygame.event.pump — internally process pygame event handlerspygame.event.get — get events from the queuepygame.event.poll — get a single event from the queuepygame.event.wait — wait for a single event from the queuepygame.event.peek — test if event types are waiting on the queuepygame.event.clear — remove all events from the queuepygame.event.event_name — get the string name from and event idpygame.event.set_blocked — control which events are allowed on the queuepygame.event.set_allowed — control which events are allowed on the queuepygame.event.get_blocked — test if a type of event is blocked from the queuepygame.event.set_grab — control the sharing of input devices with other applicationspygame.event.get_grab — test if the program is sharing input devicespygame.event.post — place a new event on the queuepygame.event.Event — create a new event object

Page 6: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 7: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 8: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 9: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 10: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 11: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 12: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 13: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 14: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 15: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 16: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 17: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.

Page 18: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 19: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 20: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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

Page 21: Pygame Dick Steflik. pygame Python module developed for writing games in python Cross platform Written on top of the Simple DirectMedia Layer, wrappers

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.