Created
May 31, 2017 18:22
-
-
Save msanatan/f2ffba48e12c4c1a623b55294c94019a to your computer and use it in GitHub Desktop.
Pygame example with moving circles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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