Python:Pygame basics
From GPWiki[edit] IntroductionPygame is a Python module built on top of SDL. This tutorial will go through a Python script that initializes Pygame, sets a 640 by 480 display, wait for a key press, and exit. [edit] The ScriptPygameBasic.py
#!/usr/bin/env python
import pygame
from pygame.locals import *
# optional
if not pygame.font: print 'Warning, no fonts'
if not pygame.mixer: print 'Warning, no sound'
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Pygame Caption')
pygame.mouse.set_visible(0)
screen.fill((0, 0, 0))
pygame.display.flip()
while (pygame.event.wait().type != KEYDOWN): pass
[edit] Step by step explanationImport import pygame from pygame.locals import * Import the Pygame module, throwing an exception if unable to import. Check for fonts and sound # optional if not pygame.font: print 'Warning, no fonts' if not pygame.mixer: print 'Warning, no sound' Check to see if font support and sound is available, print a warning if not found.
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Pygame Caption')
pygame.mouse.set_visible(0)
Initialize Pygame. Set the display mode to 640 by 480, using the default/system color depth. Set the caption to 'Pygame Caption'. Set the mouse to be invisible. screen.fill((0, 0, 0)) pygame.display.flip() Fill the screen to black, color value (0, 0, 0). Update/flip the screen buffer. while (pygame.event.wait().type != KEYDOWN): pass While waiting for events that are not a key press, do nothing. This will loop until a key is pressed. |


