From 5762b39c1834b48fcb3922fa364f341d915a7003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20Marija=20Red=C5=BEepova?= Date: Sat, 2 Mar 2024 14:42:57 +0000 Subject: [PATCH] game2 --- game2 | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 game2 diff --git a/game2 b/game2 new file mode 100644 index 0000000..0875d1d --- /dev/null +++ b/game2 @@ -0,0 +1,116 @@ +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() \ No newline at end of file