Update game2

main
Anna Marija Redžepova 2024-03-02 17:09:28 +00:00
parent 080275d555
commit cc9f3ddbf8
1 changed files with 136 additions and 72 deletions

208
game2
View File

@ -1,116 +1,180 @@
import pygame import pygame
import sys import sys
import random import os
# Initialize Pygame # Initialize Pygame
pygame.init() pygame.init()
pygame.mixer.init()
# Set up the display # Constants
WIDTH, HEIGHT = 800, 600 WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT)) PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10
PADDLE_SPEED = 10
BRICK_WIDTH, BRICK_HEIGHT = 80, 20
BRICK_ROWS = 4
BRICK_COLS = 10
LINE_HEIGHT = 5
# Colors # Colors
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
BLACK = (0, 0, 0) BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Load images # Sounds
cat_image = pygame.image.load("cat.png").convert_alpha() wall_sound = None
football_image = pygame.image.load("football.png").convert_alpha() block_sound = None
heart_bar_image = pygame.image.load("heartbar.png").convert_alpha() death_sound = None
# Constants # Check if sound files exist before loading
if os.path.exists("wall.wav"):
wall_sound = pygame.mixer.Sound("wall.wav")
if os.path.exists("block.wav"):
block_sound = pygame.mixer.Sound("block.wav")
if os.path.exists("death.wav"):
death_sound = pygame.mixer.Sound("death.wav")
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Arkanoid")
# Load cat image
cat_original_image = pygame.image.load("cat.png").convert_alpha()
CAT_WIDTH, CAT_HEIGHT = 50, 50 CAT_WIDTH, CAT_HEIGHT = 50, 50
FOOTBALL_WIDTH, FOOTBALL_HEIGHT = 30, 30 cat_image = pygame.transform.scale(cat_original_image, (CAT_WIDTH, CAT_HEIGHT))
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 # Load heart bar image
score = 0 heart_bar_image = pygame.image.load("heartbar.png").convert_alpha()
lives = NUM_HEARTS HEART_WIDTH, HEART_HEIGHT = 15, 15 # Adjust heart size here
falling_footballs = [] HEART_SPACING = 20 # Adjust spacing between hearts here
falling_speed = 3
clock = pygame.time.Clock()
# Create the paddle # Create the paddle
paddle = pygame.Rect((WIDTH - PADDLE_WIDTH) // 2, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT) paddle = pygame.Rect((WIDTH - PADDLE_WIDTH) // 2, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT)
# Function to draw the paddle # Create the ball (cat)
def draw_paddle(): cat_rect = cat_image.get_rect(center=(WIDTH // 2, HEIGHT // 2))
pygame.draw.rect(screen, PADDLE_COLOR, paddle) cat_speed = [5, 5] # Cat movement speed
# Function to handle paddle movement # Create death line
def move_paddle(key): line = pygame.Rect(0, HEIGHT - LINE_HEIGHT, WIDTH, LINE_HEIGHT)
if key == pygame.K_LEFT:
paddle.x -= 5
elif key == pygame.K_RIGHT:
paddle.x += 5
# Function to draw the cat # Create bricks
def draw_cat(x, y): bricks = []
screen.blit(cat_image, (x, y)) for row in range(BRICK_ROWS):
for col in range(BRICK_COLS):
brick = pygame.Rect(col * (BRICK_WIDTH + 5), row * (BRICK_HEIGHT + 5), BRICK_WIDTH, BRICK_HEIGHT)
bricks.append(brick)
# Function to draw the football # Scoring
def draw_football(x, y): score = 0
screen.blit(football_image, (x, y)) font = pygame.font.Font(None, 36)
# Function to draw the hearts representing lives # Lives
def draw_hearts(): lives = 9 # Start with 9 lives
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 # Game Over flag and text
def check_collision(): game_over = False
global lives game_started = False
for football in falling_footballs: game_over_text = font.render("Game Over", True, WHITE)
if paddle.colliderect(football): game_over_rect = game_over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
falling_footballs.remove(football)
lives -= 1
# Function to create falling footballs # Flags for paddle movement
def create_falling_footballs(): move_left = False
x = random.randint(0, WIDTH - FOOTBALL_WIDTH) move_right = False
y = 0
falling_footballs.append(pygame.Rect(x, y, FOOTBALL_WIDTH, FOOTBALL_HEIGHT))
# Main game loop # Main game loop
running = True while not game_over:
while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False pygame.quit()
sys.exit()
# Key pressed
elif event.type == pygame.KEYDOWN: elif event.type == pygame.KEYDOWN:
move_paddle(event.key) if event.key == pygame.K_LEFT:
move_left = True
elif event.key == pygame.K_RIGHT:
move_right = True
# Key released
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
move_left = False
elif event.key == pygame.K_RIGHT:
move_right = False
# Move the paddle based on flags
if move_left and paddle.left > 0:
paddle.x -= PADDLE_SPEED
if move_right and paddle.right < WIDTH:
paddle.x += PADDLE_SPEED
# Move the cat
cat_rect.x += cat_speed[0]
cat_rect.y += cat_speed[1]
# Check for collision with walls
if cat_rect.left <= 0 or cat_rect.right >= WIDTH:
cat_speed[0] = -cat_speed[0]
if wall_sound:
wall_sound.play()
if cat_rect.top <= 0 or cat_rect.colliderect(paddle):
cat_speed[1] = -cat_speed[1]
if wall_sound:
wall_sound.play()
# Check for collision with bricks
for brick in bricks[:]:
if cat_rect.colliderect(brick):
bricks.remove(brick)
score += 1
if block_sound:
block_sound.play()
# Check for loss of life
if cat_rect.bottom >= HEIGHT:
lives -= 1
if death_sound:
death_sound.play()
if lives <= 0:
game_over = True
break
else:
cat_rect.center = (WIDTH // 2, HEIGHT // 2)
# Clear the screen # Clear the screen
screen.fill(BLACK) screen.fill(BLACK)
# Draw game elements # Draw paddle
draw_paddle() pygame.draw.rect(screen, WHITE, paddle)
draw_cat((WIDTH - CAT_WIDTH) // 2, HEIGHT - CAT_HEIGHT - 100)
draw_hearts()
# Move and draw falling footballs # Draw bricks
for football in falling_footballs: for brick in bricks:
football.y += falling_speed pygame.draw.rect(screen, WHITE, brick)
draw_football(football.x, football.y)
# Check for collisions between paddle and falling footballs # Draw cat
check_collision() screen.blit(cat_image, cat_rect)
# Create falling footballs at random intervals # Draw heart bar representing lives
if random.randint(0, 100) < 3: start_x = 10 # Adjust the starting x-coordinate to move the heart bar more to the left
create_falling_footballs() for i in range(lives):
heart_image = pygame.transform.scale(heart_bar_image, (HEART_WIDTH, HEART_HEIGHT))
screen.blit(heart_image, (start_x + i * (HEART_WIDTH + HEART_SPACING), 10))
# Draw score
score_text = font.render(f"Score: {score}", True, (128, 0, 128)) # Purple color
screen.blit(score_text, (10, 50))
# Update the display # Update the display
pygame.display.flip() pygame.display.flip()
# Cap the frame rate # Cap the frame rate
clock.tick(60) pygame.time.Clock().tick(60)
# Display Game Over text
screen.fill(BLACK)
screen.blit(game_over_text, game_over_rect)
pygame.display.flip()
# Wait for a few seconds before quitting
pygame.time.delay(1500)
# Quit Pygame
pygame.quit() pygame.quit()
sys.exit() sys.exit()