Learning Outcomes
On completion of this page you will .....
Go to topOn completion of this page you will .....
Go to top#Import modules
import pygame, random, time
#Initialize pygame
pygame.init()
#Set display surface
WIN_WIDTH = 1200
WIN_HEIGHT = 800
gameWindow = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Catch the ball")
#Set FPS and clock
FPS = 60
clock = pygame.time.Clock()
#Set game values as constants
BALL_STARTING_VELOCITY = 10
BUFFER_DISTANCE = 100
BALL_STARTING_POSITION = WIN_WIDTH + BUFFER_DISTANCE
BALL_ACCELERATION = 1
KEY_STEPS = 5
PLAYER_SIZE = (90, 119)
BANNER_SIZE = (600, 80)
HEADER_HEIGHT = 80
YELLOW = (255, 255,0)
BLACK = (0, 0, 200)
#Set up the footballer sprite
player_image = pygame.image.load("Footballer1.png")
player_image =pygame.transform.scale(player_image, PLAYER_SIZE)
player_rect = player_image.get_rect()
player_rect.topright = (100, WIN_HEIGHT//2-60)
#Set up the ball sprite
ball_image = pygame.image.load("Football.png")
ball_rect = ball_image.get_rect()
ball_rect.x = WIN_WIDTH +100
#set up banner
banner_image = pygame.image.load("SoccerBanner.png")
banner_image =pygame.transform.scale(banner_image, BANNER_SIZE)
banner_rect = banner_image.get_rect()
banner_rect.center = (WIN_WIDTH//2, HEADER_HEIGHT//2)
score = 0
misses = 0
ball_velocity = BALL_STARTING_VELOCITY
#Set up the first starting position of the ball
ball_rect.topleft = (WIN_WIDTH +100, random.randint(HEADER_HEIGHT,WIN_HEIGHT))
#Set up the scoring text
fontVerdana = pygame.font.Font("Verdana.ttf",32)
score_text = fontVerdana.render("Score: " + str(score),True, BLACK, YELLOW)
score_rect = score_text.get_rect()
score_rect.bottomleft = (20, 60)
#Set up the misses text
misses_text = fontVerdana.render("Misses: " + str(misses),True,BLACK, YELLOW)
misses_rect = misses_text.get_rect()
misses_rect.bottomleft = (WIN_WIDTH - misses_rect.width - 40, 60)
#Set up the music
pygame.mixer.music.load("backgroundcheering.wav")
pygame.mixer.Sound("cheerforscoring.wav")
score_sound = pygame.mixer.Sound("cheerforscoring.wav")
#Start the main game loop
pygame.mixer.music.play(-1, 0.0)
active = True
while active:
#If the user has chosen to exit the game by pressing the quit button
for event in pygame.event.get():
if event.type == pygame.QUIT:
active = False
#Set up the next frame
gameWindow.fill(YELLOW)
#Actions to be taken if the ball has reached 100 pixels to the left of the window's left margin
if ball_rect.x < -100:
ball_rect.topleft = (WIN_WIDTH +100, random.randint(HEADER_HEIGHT,WIN_HEIGHT))
misses += 1
misses_text = fontVerdana.render("Misses: " + str(misses),True,BLACK, YELLOW)
ball_velocity += BALL_ACCELERATION
#Move the ball to the left by the amount of the value of ball_velocity ball_rect.x -= ball_velocity
#Move the goal keeper up or down using the UP and DOWN arrow keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player_rect.y -= KEY_STEPS
if event.key == pygame.K_DOWN:
player_rect.y += KEY_STEPS
#Actions taken when a collision occurs
if player_rect.colliderect(ball_rect):
score_sound.play()
ball_rect.y = random.randint(HEADER_HEIGHT+20,WIN_HEIGHT - 20)
ball_rect.x = WIN_WIDTH +100
score+=1
score_text = fontVerdana.render("Score: " + str(score),True, BLACK, YELLOW)
#Using the blit() function to position the images and the text
gameWindow.blit(player_image, player_rect)
gameWindow.blit(ball_image, ball_rect)
gameWindow.blit(score_text, score_rect)
gameWindow.blit(misses_text, misses_rect)
gameWindow.blit(banner_image, banner_rect)
#Draw a black line to separate the scoring area from the playing area
pygame.draw.line(gameWindow, BLACK,(0,80), (WIN_WIDTH, 80), 2)
#When game over inform the user and give the numbers of scores and misses
if misses > 20:
pygame.mixer.music.stop()
finish_text = fontVerdana.render("Game Over",True, BLACK, YELLOW)
finish_rect = finish_text.get_rect()
finish_rect.center = (WIN_WIDTH//2, WIN_HEIGHT//4)
finish_text1 = fontVerdana.render("Total score: " + str(score),True, BLACK, YELLOW)
finish_rect1 = finish_text1.get_rect()
finish_rect1.center = (WIN_WIDTH//2, WIN_HEIGHT//2)
finish_text2 = fontVerdana.render("Total misses: " +str(misses),True, BLACK, YELLOW)
finish_rect2 = finish_text2.get_rect()
finish_rect2.center = (WIN_WIDTH//2, WIN_HEIGHT//4 * 3)
option_text = fontVerdana.render("Press 'y' to play again or any key to exit",True, BLACK, YELLOW)
option_rect = option_text.get_rect()
option_rect.centerx = WIN_WIDTH//2
option_rect.y = WIN_HEIGHT - 60
score_text = fontVerdana.render("Score: ----" ,True, BLACK, YELLOW)
print(str(score))
print("Score: " + str(score))
misses_text = fontVerdana.render("Misses: ----",True,BLACK, YELLOW)
# misses_text = fontVerdana.render("Misses: " + str(misses),True,BLACK, YELLOW)
print(str(misses))
print("Misses: " + str(misses))
#Update the window display
gameWindow.blit(finish_text,finish_rect)
gameWindow.blit(finish_text1,finish_rect1)
gameWindow.blit(finish_text2,finish_rect2)
gameWindow.blit(option_text, option_rect)
gameWindow.blit(score_text, score_rect)
gameWindow.blit(misses_text, misses_rect)
pygame.display.update()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
active = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
score = 0
misses = 0
ball_rect.x = BALL_STARTING_POSITION
ball_rect.y = random.randint(HEADER_HEIGHT, WIN_HEIGHT -40)
ball_velocity = BALL_STARTING_VELOCITY
pygame.mixer.music.play(-1, 0.0)
waiting = False
else:
waiting = False
active = False
pygame.display.update()
pygame.display.update()
clock.tick(FPS)
pygame.quit()