Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Playing with some basic pygame: a simple full-screen bouncing box.
#/usr/bin/env python
import pygame
from pygame.locals import *
BOX_WIDTH = 100
BOX_HEIGHT = 100
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
def main():
pygame.init()
x, y = BOX_WIDTH, BOX_HEIGHT
dx, dy = 1, 1
s = pygame.display.set_mode((0, 0),
pygame.FULLSCREEN |
pygame.HWSURFACE |
pygame.DOUBLEBUF)
width, height = s.get_size()
clock = pygame.time.Clock()
pygame.event.set_allowed(None)
pygame.event.set_allowed((QUIT, KEYDOWN))
running = True
while running:
pygame.display.flip()
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
x += dx
if x > width or x < BOX_WIDTH / 2:
dx = -dx
y += dy
if y > height or y < BOX_HEIGHT / 2:
dy = -dy
s.fill(BLACK)
s.fill(WHITE,
(x - BOX_WIDTH / 2,
y - BOX_HEIGHT / 2,
BOX_WIDTH / 2,
BOX_HEIGHT / 2))
pygame.display.quit()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment