1 of 18

Marco Acea

Jonathan Perez

All code and credit goes to Ex TA Lukas Peraza

and the Pygame documentation!

2 of 18

First things first… what’s the difference?

  • You’re all familiar with tkinter, think of pygame as a more object oriented tkinter!
  • With Pygame you can represent images on the screen as an object… and the ‘canvas’ as an object… it’s really just a lot of objects.

3 of 18

Let’s take a look at some past projects!

4 of 18

Surfaces - Canvas, but make it fashion

  • Surfaces are like a canvas in tkinter.
  • The surface object has two important parameters:
    • pygame.Surface((width,height ))
  • There are two ways of adding color to a surface (let c be a surface we defined)
    • c.fill(r,g,b) #Applies a rgb color to a surface
    • c.blit(source,destination) #Source is another surface and destination is a pair of coordinates
  • pygame.image.load(path) #makes a surface out of an image

5 of 18

Starter Code

6 of 18

Events

7 of 18

What does this look like in code?

  • Note that event.type returns

what the event was, and we

we compare it to the events

we know about.

  • All the keys in Pygame!

8 of 18

Drawing

  • There are basic drawing methods like in tkinter that we can draw onto any surface. Images are drawn by using .blit like we mentioned before.

9 of 18

Drawing cont.

  • Once you finish ‘drawing’ or ‘blit’-ing something, you have to update the screen.

10 of 18

Starter Code!

Ex-TA Lukas’ Starter Code!!!

Search for the subsection:

“Framework”

11 of 18

Sprites

  • Sprites are by far one of the coolest parts of pygame, and really what sets it apart from tkinter!
  • Sprites are objects in your game, think characters, that can be updated in batches, you can ‘kill’ them and check for collision between sprites!

12 of 18

How to use Sprites

  • Extend the pygame.sprite.Sprite class (Think-

class Marco(pygame.sprite.Sprite): …

  • Call the superclass's init method
  • Create the update method
  • Assign the rect, image, and optionally the radius attributes (for circular collision)

13 of 18

Examples

14 of 18

Groups

  • A Group (pygame.sprite.Group) is an object which represents a collection of Sprites.
  • You can use groups to update each sprite in the group at the same time, or to draw each sprite in the group at the same time.

15 of 18

Groups

  • Here are some quick functions for the pygame.sprite.Group class

  • Learn about these function!

16 of 18

Group Collide!

  • pygame.sprite.groupcollide(group1,group2)
  • This will find collisions between all the Sprites in two groups.
  • Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect.
  • If either dokill argument is True, the colliding Sprites will be removed from their respective Group.
  • The collided argument is a callback function used to calculate if two sprites are colliding.

17 of 18

Sprite Collision

  • spritecollide(sprite, group, dokill, collided = None)
  • Works like groupCollide()

18 of 18

Animation Demo!