Created
May 3, 2018 20:44
-
-
Save lisadean/a19ec304d4e89d1f24b79d1d5cb0ed00 to your computer and use it in GitHub Desktop.
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 | |
def main(): | |
canvas_width = 800 | |
canvas_height = 600 | |
pygame.init() | |
screen = pygame.display.set_mode((canvas_width, canvas_height)) | |
pygame.display.set_caption("Ball Example") | |
ball_y = 100 | |
ball_x = 100 | |
ball_radius = 50 | |
ball_dir_x = 5 | |
ball_dir_y = 5 | |
done = False | |
while not done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
screen.fill((0, 0, 0)) | |
pygame.draw.circle(screen, (255, 0, 0), (ball_x, ball_y), ball_radius, 0) | |
pygame.display.update() | |
ball_x += ball_dir_x | |
ball_y += ball_dir_y | |
if ball_x + ball_radius > canvas_width: | |
ball_dir_x = -ball_dir_x | |
if ball_y + ball_radius > canvas_height: | |
ball_dir_y = -ball_dir_y | |
if ball_x - ball_radius < 0: | |
ball_dir_x = -ball_dir_x | |
if ball_y - ball_radius < 0: | |
ball_dir_y = -ball_dir_y | |
pygame.quit() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment