Skip to content

Instantly share code, notes, and snippets.

@arpit-omprakash
Created June 1, 2020 15:20
Show Gist options
  • Save arpit-omprakash/d25b9941815b9021efb7ad442bb15fa8 to your computer and use it in GitHub Desktop.
Save arpit-omprakash/d25b9941815b9021efb7ad442bb15fa8 to your computer and use it in GitHub Desktop.
A simple moving ball
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)
ballImg = pygame.image.load('ball.jpg')
ballx = 10
bally = 10
direction = 'right'
while True:
DISPLAYSURF.fill(WHITE)
if direction == 'right':
ballx += 5
if ballx == 340:
direction = 'down'
elif direction == 'down':
bally += 5
if bally == 240:
direction = 'left'
elif direction == 'left':
ballx -= 5
if ballx == 10:
direction = 'up'
elif direction == 'up':
bally -= 5
if bally == 10:
direction = 'right'
DISPLAYSURF.blit(ballImg, (ballx, bally))
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