Meet pygame
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Meet Pygame PowerPoint PPT Presentation


  • 69 Views
  • Uploaded on
  • Presentation posted in: General

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, audio). Starting Up. import pygame pygame.init() Cue music. The Screen. screen = pygame.display.set_mode((x, y))

Download Presentation

Meet Pygame

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Meet pygame

Meet Pygame

  • Noah Kantrowitz

  • June 8, 2007


The basics

The Basics

  • Cross-platform

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

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


Starting up

Starting Up

  • import pygame

  • pygame.init()

  • Cue music ...


The screen

The Screen

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

  • screen = pygame.display.get_surface()

  • pygame.display.flip()

  • pygame.display.update(dirty)


Surfaces

Surfaces

  • The basic element of graphics

  • pygame.image.load(file)

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

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


Formats

JPEG

PNG

GIF

BMP

PCX

TGA

TIF

LBM

PBM

XPM

Formats

  • Native support for the following:

  • SVG will need to worked out

  • Stay tuned ...


The loop

The Loop

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


The loop1

The Loop

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


Timing

Timing

  • from pygame.time import Clock

  • clock = Clock()

  • delta = clock.tick(30)


The loop2

The Loop

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


Event handling

Event Handling

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


Event handling1

QUIT

KEYUP

KEYDOWN

MOUSEMOTION

MOUSEBUTTONUP

MOUSEBUTTONDOWN

Event Handling

  • Other types:


Key events

Key Events

  • evt.key == pygame.K_a

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


Mouse events

Mouse Events

  • evt.pos

  • evt.button (for the button events)


The loop3

The Loop

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


Sprites

Sprites

  • pygame.sprite.Sprite

  • class Foo(Sprite):

  • Must call superclass __init__()

  • .image, .rect


Groups

Groups

  • pygame.sprite.Group

  • RenderUpdates, OrderedUpdates

  • .add(), .remove()

  • .update(*args)

  • More on these in a moment


The loop4

The Loop

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


Drawing

Drawing

  • .draw(dest)

  • Dirty updates

  • pygame.draw.update(dirty)


Rects

top

bottom

left

right

topleft

bottomleft

topright

bottomright

midtop

midbottom

midleft

midright

center

centerx

centery

size

width

height

Rects

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

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

  • Attributes:


Collisions

Collisions

  • Rect-based

  • spritecollide(sprite,group,kill)

  • spritecollideany(sprite,group)

  • groupcollide(group1,group2,kill1,kill2)


Sound

Sound

  • pygame.mixer.Sound(file)

  • .play(), .stop()

  • pygame.mixer.music


Meet pygame

Text

  • Try to avoid it for now.


Meet pygame

More?

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

  • Email me. ([email protected])


  • Login