116 lines
2.8 KiB
Plaintext
116 lines
2.8 KiB
Plaintext
import pygame
|
|
import sys
|
|
import random
|
|
|
|
# Initialize Pygame
|
|
pygame.init()
|
|
|
|
# Set up the display
|
|
WIDTH, HEIGHT = 800, 600
|
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
# Colors
|
|
WHITE = (255, 255, 255)
|
|
BLACK = (0, 0, 0)
|
|
|
|
# Load images
|
|
cat_image = pygame.image.load("cat.png").convert_alpha()
|
|
football_image = pygame.image.load("FOOT BALL.png").convert_alpha()
|
|
heart_bar_image = pygame.image.load("heart bar.png").convert_alpha()
|
|
|
|
# Constants
|
|
CAT_WIDTH, CAT_HEIGHT = 50, 50
|
|
FOOTBALL_WIDTH, FOOTBALL_HEIGHT = 30, 30
|
|
HEART_WIDTH, HEART_HEIGHT = 30, 30
|
|
PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10
|
|
PADDLE_COLOR = (255, 0, 0)
|
|
BALL_RADIUS = 10
|
|
LINE_HEIGHT = 5
|
|
NUM_HEARTS = 9
|
|
|
|
# Global variables
|
|
score = 0
|
|
lives = NUM_HEARTS
|
|
falling_footballs = []
|
|
falling_speed = 3
|
|
clock = pygame.time.Clock()
|
|
|
|
# Create the paddle
|
|
paddle = pygame.Rect((WIDTH - PADDLE_WIDTH) // 2, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT)
|
|
|
|
# Function to draw the paddle
|
|
def draw_paddle():
|
|
pygame.draw.rect(screen, PADDLE_COLOR, paddle)
|
|
|
|
# Function to handle paddle movement
|
|
def move_paddle(key):
|
|
if key == pygame.K_LEFT:
|
|
paddle.x -= 5
|
|
elif key == pygame.K_RIGHT:
|
|
paddle.x += 5
|
|
|
|
# Function to draw the cat
|
|
def draw_cat(x, y):
|
|
screen.blit(cat_image, (x, y))
|
|
|
|
# Function to draw the football
|
|
def draw_football(x, y):
|
|
screen.blit(football_image, (x, y))
|
|
|
|
# Function to draw the hearts representing lives
|
|
def draw_hearts():
|
|
for i in range(lives):
|
|
screen.blit(heart_bar_image, (WIDTH - (i + 1) * HEART_WIDTH, 0))
|
|
|
|
# Function to handle collisions between the paddle and falling footballs
|
|
def check_collision():
|
|
global lives
|
|
for football in falling_footballs:
|
|
if paddle.colliderect(football):
|
|
falling_footballs.remove(football)
|
|
lives -= 1
|
|
|
|
# Function to create falling footballs
|
|
def create_falling_footballs():
|
|
x = random.randint(0, WIDTH - FOOTBALL_WIDTH)
|
|
y = 0
|
|
falling_footballs.append(pygame.Rect(x, y, FOOTBALL_WIDTH, FOOTBALL_HEIGHT))
|
|
|
|
# Main game loop
|
|
running = True
|
|
while running:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
elif event.type == pygame.KEYDOWN:
|
|
move_paddle(event.key)
|
|
|
|
# Clear the screen
|
|
screen.fill(BLACK)
|
|
|
|
# Draw game elements
|
|
draw_paddle()
|
|
draw_cat((WIDTH - CAT_WIDTH) // 2, HEIGHT - CAT_HEIGHT - 100)
|
|
draw_hearts()
|
|
|
|
# Move and draw falling footballs
|
|
for football in falling_footballs:
|
|
football.y += falling_speed
|
|
draw_football(football.x, football.y)
|
|
|
|
# Check for collisions between paddle and falling footballs
|
|
check_collision()
|
|
|
|
# Create falling footballs at random intervals
|
|
if random.randint(0, 100) < 3:
|
|
create_falling_footballs()
|
|
|
|
# Update the display
|
|
pygame.display.flip()
|
|
|
|
# Cap the frame rate
|
|
clock.tick(60)
|
|
|
|
# Quit Pygame
|
|
pygame.quit()
|
|
sys.exit() |