Skip to content

Instantly share code, notes, and snippets.

@kgaughan
Created August 13, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgaughan/f8c8b3545cee3a17c351f88c6b360386 to your computer and use it in GitHub Desktop.
Save kgaughan/f8c8b3545cee3a17c351f88c6b360386 to your computer and use it in GitHub Desktop.
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