Copy of example program with some comments and modifications

master
Elis Axelsson 2017-07-15 17:03:03 +02:00
parent c7ee615854
commit 897e660ecd
2 changed files with 52 additions and 0 deletions

BIN
ball.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

52
watersteal.py Normal file
View File

@ -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()