Skip to content

Instantly share code, notes, and snippets.

@arpit-omprakash
Created June 1, 2020 15:21
Show Gist options
  • Save arpit-omprakash/5fde593ec2ce67d098072ebb5e36666b to your computer and use it in GitHub Desktop.
Save arpit-omprakash/5fde593ec2ce67d098072ebb5e36666b to your computer and use it in GitHub Desktop.
A simple script demonstrating animation in pygame
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 60
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((400,300), 0, 32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
box = pygame.Rect((10,10), (50,50))
direction = 'right'
while True:
DISPLAYSURF.fill(BLACK)
if direction == 'right':
box.left += 5
if box.left == 340:
direction = 'down'
elif direction == 'down':
box.top += 5
if box.top == 240:
direction = 'left'
elif direction == 'left':
box.left -= 5
if box.left == 10:
direction = 'up'
elif direction == 'up':
box.top -= 5
if box.top == 10:
direction = 'right'
pygame.draw.rect(DISPLAYSURF, WHITE, box)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment