Copy of example program with some comments and modifications
parent
c7ee615854
commit
897e660ecd
|
@ -0,0 +1,52 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys, pygame
|
||||
|
||||
pygame.init()
|
||||
|
||||
# Define screen size
|
||||
size = width, height = 640, 480
|
||||
|
||||
speed = [1, 1]
|
||||
|
||||
# Define colors
|
||||
black = 0, 0, 0
|
||||
white = 255, 255, 255
|
||||
|
||||
# Set up screen
|
||||
screen = pygame.display.set_mode(size)
|
||||
|
||||
# Define ball
|
||||
ball = pygame.image.load("ball.bmp")
|
||||
ballrect = ball.get_rect()
|
||||
|
||||
while True:
|
||||
# Handle events
|
||||
for event in pygame.event.get():
|
||||
# Handle keypresses
|
||||
if event.type == pygame.KEYDOWN:
|
||||
pressedKeys = pygame.key.get_pressed()
|
||||
|
||||
if pressedKeys[pygame.K_q]:
|
||||
sys.exit()
|
||||
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit()
|
||||
|
||||
# Move ball
|
||||
ballrect = ballrect.move(speed)
|
||||
|
||||
# Direct ball movement
|
||||
if ballrect.left < 0 or ballrect.right > width:
|
||||
speed[0] = -speed[0]
|
||||
if ballrect.top < 0 or ballrect.bottom > height:
|
||||
speed[1] = -speed[1]
|
||||
|
||||
# Fill screen buffer with black color
|
||||
screen.fill(black)
|
||||
|
||||
# Draw ball on buffer
|
||||
screen.blit(ball, ballrect)
|
||||
|
||||
# Upload buffer to real screen
|
||||
pygame.display.flip()
|
Loading…
Reference in New Issue