Created
June 26, 2020 17:42
-
-
Save shorinji/0b1158c9e925e195bc926c0a45c8819c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sys | |
import pygame | |
import math | |
from pygame.locals import * | |
WIDTH = 800 | |
HEIGHT = 600 | |
BLACK = Color(0, 0, 0) | |
BALL_COLOR = Color(255, 0, 0) | |
BALL_SIZE = 10 | |
ballPosition = 100, 100 | |
ballDirection = -5, 5 | |
pygame.init() | |
screen = pygame.display.set_mode([WIDTH, HEIGHT]) | |
while 1: | |
# handle events | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
if event.type == pygame.KEYDOWN: | |
if event.key == K_ESCAPE: | |
sys.exit() | |
# run game logic | |
newX = ballPosition[0] + ballDirection[0] | |
newY = ballPosition[1] + ballDirection[1] | |
if newX < 0 or newX >= WIDTH: | |
ballDirection = -ballDirection[0], ballDirection[1] | |
if newY < 0 or newY >= HEIGHT: | |
ballDirection = ballDirection[0], -ballDirection[1] | |
ballPosition = newX, newY | |
# render current frame | |
screen.fill(BLACK) | |
pygame.draw.circle(screen, BALL_COLOR, ballPosition, BALL_SIZE) | |
pygame.display.flip() | |
pygame.time.wait(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment