Skip to content

Instantly share code, notes, and snippets.

@msanatan
Created May 31, 2017 18:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msanatan/f2ffba48e12c4c1a623b55294c94019a to your computer and use it in GitHub Desktop.
Save msanatan/f2ffba48e12c4c1a623b55294c94019a to your computer and use it in GitHub Desktop.
Pygame example with moving circles
import pygame
import sys
SCREEN_SIZE = WIDTH, HEIGHT = (640, 480)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 50, 50)
GREEN = (50, 255, 50)
CIRCLE_RADIUS = 30
# Initialization
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('Circles')
fps = pygame.time.Clock()
paused = False
# Ball setup
ball_pos1 = [50, 50]
ball_pos2 = [50, 240]
ball_pos3 = [50, 430]
def update():
ball_pos1[0] += 5
ball_pos2[0] += 3
ball_pos3[0] += 1
def render():
screen.fill(BLACK)
pygame.draw.circle(screen, RED, ball_pos1, CIRCLE_RADIUS, 0)
pygame.draw.circle(screen, WHITE, ball_pos2, CIRCLE_RADIUS, 0)
pygame.draw.circle(screen, GREEN, ball_pos3, CIRCLE_RADIUS, 0)
pygame.display.update()
fps.tick(60)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
paused = not paused
if not paused:
update()
render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment