You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

132 lines
3.5 KiB

#!/usr/bin/env python3
import sys
import pygame
from classes.bucket import Bucket
from classes.player import Player
pygame.init()
# Define screen size
size = width, height = 640, 480
speed = [1, 1]
# Set up screen
screen = pygame.display.set_mode(size)
# Define ball
ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()
# Define ground
ground = pygame.Surface((width, height / 4))
ground.fill((0, 100, 0))
groundrect = pygame.Rect(0, (height / 4 * 3), width, (height / 4))
# Define sky
sky = pygame.Surface((width, height / 4 * 3))
sky.fill((155, 155, 255))
skyrect = pygame.Rect(0, 0, width, (height / 4 * 3))
# Define a bucket.
bucket = Bucket(pygame, (204, 204, 204), (50, 50)).getBucket()
bucketrect = bucket.get_rect()
bucketrect.bottomleft = (width * 0.9, (height / 4 * 3))
# Define a player.
player = Player(pygame, (0, 0, 0), (60, 100)).getPlayer()
playerrect = player.get_rect()
playerrect.bottomleft = (10, (height / 4 * 3))
playerspeed = 0
playerHasBucket = False
playerScore = 0
# Score text
scoreFont = pygame.font.SysFont(pygame.font.get_default_font(), 40)
while True:
# Handle events
for event in pygame.event.get():
pressedKeys = pygame.key.get_pressed()
# Handle keydown
if event.type == pygame.KEYDOWN:
if pressedKeys[pygame.K_q]:
sys.exit()
if pressedKeys[pygame.K_RIGHT]:
playerspeed = 1
if pressedKeys[pygame.K_LEFT]:
playerspeed = -1
if pressedKeys[pygame.K_SPACE]:
if playerrect.colliderect(bucketrect):
# Move bucket to player hand
bucketrect.top = bucketrect.top - 60
bucketrect.left = playerrect.right
playerHasBucket = True
# Handle keyup
if event.type == pygame.KEYUP:
# Stop player if no key is pressed
if (pressedKeys[pygame.K_RIGHT] + pressedKeys[pygame.K_LEFT]) == 0:
playerspeed = 0
if event.type == pygame.QUIT:
sys.exit()
# Move player
playerrect = playerrect.move((playerspeed, 0))
# Move ball
ballrect = ballrect.move(speed)
# Move bucket if player moves
if playerHasBucket:
bucketrect = bucketrect.move((playerspeed, 0))
# If player get rekt by the ball, move player back to start position
if playerrect.colliderect(ballrect):
playerScore = 0
playerrect.left = 0
playerHasBucket = False
bucketrect.bottomleft = (width * 0.9, (height / 4 * 3))
# If player reach goal, count score and restore bucket
if playerrect.left < 0 and playerHasBucket:
playerScore = playerScore + 1
playerHasBucket = False
bucketrect.bottomleft = (width * 0.9, (height / 4 * 3))
# Direct ball movement
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > (height / 4 * 3):
speed[1] = -speed[1]
# Fill screen buffer with black color
screen.fill((0, 0, 0))
# Draw ground on buffer
screen.blit(ground, groundrect)
# Draw sky on buffer
screen.blit(sky, skyrect)
# Draw ball on buffer
screen.blit(ball, ballrect)
# Draw bucket
screen.blit(bucket, bucketrect)
# Draw player
screen.blit(player, playerrect)
scoreText = scoreFont.render("Score: " + str(playerScore), True, (0, 0, 0))
screen.blit(scoreText, pygame.Rect(0, 0, 40, 20))
# Upload buffer to real screen
pygame.display.flip()