Browse Source

Copy of example program with some comments and modifications

master
Elis Axelsson 4 years ago
parent
commit
897e660ecd
  1. BIN
      ball.bmp
  2. 52
      watersteal.py

BIN
ball.bmp

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

52
watersteal.py

@ -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…
Cancel
Save