Tufts OpenCourseware
Search
Author: Ming Y Chow

Tufts OpenCourseWare
Introduction to Game Development
M. Chow
Spring 2012

 

Pygame

The Game Loop

  • The key to a game's operation
  • Responsible for keeping track of the current frame and for requesting periodic screen updates
  • The pseudocode / idea:
    while (game is running) {
      while (isPaused && isRunning) {
        sleep(someInterval);
      }
      updateState();
      processInputs();
      updateAI();
      updatePhysicsAndCollisions();
      updateAnimations();
      playSounds();
      updateVideo();
      drawGraphics();
      ...
      ...
      ...
    }
    
  • Inputs or none, the game loop must run

Pygame

  • Set of Python module
  • Circa 2000
  • Highly portable
  • Includes support for graphics, OpenGL, UI, mouse, keyboard, joystick, fonts, time, sound, etc.
  • Built on top of the Simple DirectMedia Layer (SDL), a cross-platform open source multimedia library
  • Fairly low level for writing games
  • Official reference: http://pygame.org/docs/ref/pygame.html

Initializing Pygame

  • import pygame, sys, os
  • from pygame.locals import *
  • pygame.init()

Setting Up the Display

  • window = pygame.display.set_mode((SOME_SCREEN_WIDTH, SOME_SCREEN_HEIGHT))
  • pygame.display.set_caption('SOME WINDOW TITLE')
  • screen = pygame.display.get_surface()
  • Methods and reference: http://www.pygame.org/docs/ref/display.html

Pygame Surface

Screen Modes

  • pygame.FULLSCREEN - full screen
  • pygame.HWSURFACE - Hardware accelerated, only in FULLSCREEN
  • pygame.OPENGL - Enable OpenGL
  • pygame.RESIZABLE - You can resize the display window
  • pygame.NOFRAME - Display window has no border or controls
  • pygame.DOUBLEBUF - double buffering support; recommended for HWSURFACE or OPENGL

Event Handling

  • Reacting to user activity (particularly the mouse and keyboard)
  • pygame.event.get() - Get events from the queue
  • Event types:
    • QUIT
    • ACTIVEEVENT
    • KEYUP
    • KEYDOWN
    • MOUSEBUTTONUP
    • MOUSEBUTTONDOWN
    • MOUSEMOTION
    • JOYAXISMOTION
    • JOYHATMOTION
    • JOYBALLMOTION
    • JOYBUTTONUP
    • JOYBUTTONDOWN
    • VIDEORESIZE
    • VIDEOEXPOSE
    • USEREVENT
  • Handling keyboard events
    • 	if event.type == KEYDOWN: # or use KEYUP
      		# Get the key pressed via event.key
      		print event.key
      
  • Handling mouse events
    • 	if event.type == MOUSEBUTTONDOWN: # or use MOUSEBUTTONUP or MOUSEMOTION
      		# Get the location of the mouse pointer via pygame.mouse.get_loc()
      		print pygame.mouse.get_loc()
      
  • List of keycodes and key methods: http://www.pygame.org/docs/ref/key.html
  • List of mouse methods: http://www.pygame.org/docs/ref/mouse.html

Implementing the Game Loop

  • As simple as a while loop that runs forever

Image Loading

  • pygame.image.load() - Load image
  • pygame.image.load().convert() or pygame.image.load().convert_alpha()- A better option to load image; convert image to best pixel and color format
  • screen.blit() - Draw image (and provide x-y location to draw the image)
  • pygame.display.flip() - Update the entire surface
  • Example:
    img = pygame.image.load("krusty.jpg")
    screen.blit(img, (10, 10))
    pygame.display.flip()
    
  • In the above example, .py file and krusty.jpg must be in the same folder. You can also specify path to image.
  • In addition, the background is black color