Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me...

24
Meet Pygame Noah Kantrowitz June 8, 2007

Transcript of Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me...

Page 1: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Meet PygameNoah Kantrowitz

June 8, 2007

Page 2: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Basics

•Cross-platform

•Based on SDL (don’t quote me on that)

•Handle input (keyboard, mouse) and output (graphics, audio)

Page 3: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Starting Up

•import pygame

•pygame.init()

• Cue music ...

Page 4: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Screen

• screen = pygame.display.set_mode((x, y))

• screen = pygame.display.get_surface()

• pygame.display.flip()

• pygame.display.update(dirty)

Page 5: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Surfaces

•The basic element of graphics

•pygame.image.load(file)

• .convert_alpha() vs .set_colorkey((r,g,b))

•dest.blit(src, (x, y))

Page 6: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Formats

•Native support for the following:

•SVG will need to worked out

•Stay tuned ...

JPEG PNG GIF BMP PCXTGA TIF LBM PBM XPM

Page 7: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Loop

while True: <timing> <event handling> <update phase> <draw phase>

Page 8: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Loop

while True: <timing> <event handling> <update phase> <draw phase>

Page 9: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Timing

•from pygame.time import Clock

•clock = Clock()

•delta = clock.tick(30)

Page 10: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Loop

while True: <timing> <event handling> <update phase> <draw phase>

Page 11: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Event Handling

for evt in pygame.event.get(): if evt.type == pygame.QUIT: sys.exit() ...

Page 12: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Event Handling

•Other types:QUIT KEYUP KEYDOWN

MOUSEMOTION MOUSEBUTTONUP MOUSEBUTTONDOWN

Page 13: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Key Events

•evt.key == pygame.K_a

•See the Pygame documentation for the full list of key constants.

Page 14: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Mouse Events

•evt.pos

•evt.button (for the button events)

Page 15: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Loop

while True: <timing> <event handling> <update phase> <draw phase>

Page 16: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Sprites

•pygame.sprite.Sprite

•class Foo(Sprite):

•Must call superclass __init__()

•.image, .rect

Page 17: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Groups

•pygame.sprite.Group

•RenderUpdates, OrderedUpdates

•.add(), .remove()

•.update(*args)

•More on these in a moment

Page 18: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

The Loop

while True: <timing> <event handling> <update phase> <draw phase>

Page 19: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Drawing

•.draw(dest)

•Dirty updates

•pygame.draw.update(dirty)

Page 20: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Rects

• pygame.Rect(left,top,width,height)

•surf.get_rect() Always at (0,0)

•Attributes: top bottom left right topleft bottomleft

topright bottomright midtop midbottom midleft midright

center centerx centery size width height

Page 21: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Collisions

•Rect-based

•spritecollide(sprite,group,kill)

•spritecollideany(sprite,group)

• groupcollide(group1,group2,kill1,kill2)

Page 22: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Sound

•pygame.mixer.Sound(file)

•.play(), .stop()

•pygame.mixer.music

Page 23: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

Text

•Try to avoid it for now.

Page 24: Meet Pygame Noah Kantrowitz June 8, 2007. The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,

More?

•http://www.pygame.org/docs

•Email me. ([email protected])