Update game2

main
Anna Marija Redžepova 2024-03-02 20:20:07 +00:00
parent fbbb59ba14
commit d65bef279d
1 changed files with 109 additions and 33 deletions

142
game2
View File

@ -1,6 +1,7 @@
import pygame import pygame
import sys import sys
import os import os
import random
# Initialize Pygame # Initialize Pygame
pygame.init() pygame.init()
@ -14,38 +15,46 @@ BRICK_WIDTH, BRICK_HEIGHT = 80, 20
BRICK_ROWS = 4 BRICK_ROWS = 4
BRICK_COLS = 10 BRICK_COLS = 10
LINE_HEIGHT = 5 LINE_HEIGHT = 5
CUPCAKE_WIDTH, CUPCAKE_HEIGHT = 30, 30
# Colors # Colors
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
BLACK = (0, 0, 0) BLACK = (0, 0, 0)
RED = (255, 0, 0) RED = (255, 0, 0)
PURPLE = (128, 0, 128)
# Sounds # Load additional sound files
wall_sound = None if os.path.exists("wearethechampions.mp3"):
block_sound = None win_sound = pygame.mixer.Sound("wearethechampions.mp3")
death_sound = None if os.path.exists("waltzn2.mp3"):
bg_music = pygame.mixer.Sound("waltzn2.mp3")
# Check if sound files exist before loading if os.path.exists("dishesfall.mp3"):
if os.path.exists("wall.wav"): fall_sound = pygame.mixer.Sound("dishesfall.mp3")
wall_sound = pygame.mixer.Sound("wall.wav") if os.path.exists("boing.wav"):
if os.path.exists("block.wav"): boing_sound = pygame.mixer.Sound("boing.wav")
block_sound = pygame.mixer.Sound("block.wav") if os.path.exists("glassbreaking.mp3"):
if os.path.exists("death.wav"): block_sound = pygame.mixer.Sound("glassbreaking.mp3")
death_sound = pygame.mixer.Sound("death.wav") if os.path.exists("lookatthisdude.mp3"):
death_sound = pygame.mixer.Sound("lookatthisdude.mp3")
if os.path.exists("chew.wav"):
chew_sound = pygame.mixer.Sound("chew.wav")
# Create the game window # Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT)) screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Arkanoid") pygame.display.set_caption("Arkanoid")
# Load beach background image
background_image = pygame.image.load("beach.jpg").convert()
background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
# Load cat image # Load cat image
cat_original_image = pygame.image.load("cat.png").convert_alpha() cat_original_image = pygame.image.load("cat.png").convert_alpha()
CAT_WIDTH, CAT_HEIGHT = 50, 50 CAT_WIDTH, CAT_HEIGHT = 50, 50
cat_image = pygame.transform.scale(cat_original_image, (CAT_WIDTH, CAT_HEIGHT)) cat_image = pygame.transform.scale(cat_original_image, (CAT_WIDTH, CAT_HEIGHT))
# Load heart bar image # Load cupcake image
heart_bar_image = pygame.image.load("heartbar.png").convert_alpha() cupcake_original_image = pygame.image.load("cupcake.png").convert_alpha()
HEART_WIDTH, HEART_HEIGHT = 15, 15 # Adjust heart size here cupcake_image = pygame.transform.scale(cupcake_original_image, (CUPCAKE_WIDTH, CUPCAKE_HEIGHT))
HEART_SPACING = 20 # Adjust spacing between hearts here
# 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)
@ -64,6 +73,29 @@ for row in range(BRICK_ROWS):
brick = pygame.Rect(col * (BRICK_WIDTH + 5), row * (BRICK_HEIGHT + 5), BRICK_WIDTH, BRICK_HEIGHT) brick = pygame.Rect(col * (BRICK_WIDTH + 5), row * (BRICK_HEIGHT + 5), BRICK_WIDTH, BRICK_HEIGHT)
bricks.append(brick) bricks.append(brick)
# Falling Cupcakes
class Cupcake:
def __init__(self):
self.width = CUPCAKE_WIDTH
self.height = CUPCAKE_HEIGHT
self.x = random.randint(0, WIDTH - self.width)
self.y = 0
self.speed = random.randint(1, 3)
def update(self):
self.y += self.speed
def draw(self, surface):
surface.blit(cupcake_image, (self.x, self.y))
# Create a list to hold falling cupcakes
falling_cupcakes = []
# Function to spawn falling cupcakes
def spawn_cupcakes():
if len(falling_cupcakes) < 2: # Maximum 2 cupcakes on screen
falling_cupcakes.append(Cupcake())
# Scoring # Scoring
score = 0 score = 0
font = pygame.font.Font(None, 36) font = pygame.font.Font(None, 36)
@ -77,10 +109,22 @@ game_started = False
game_over_text = font.render("Game Over", True, WHITE) game_over_text = font.render("Game Over", True, WHITE)
game_over_rect = game_over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2)) game_over_rect = game_over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# Winning flag and text
winning = False
winning_text = font.render("YOU WON!", True, WHITE)
winning_rect = winning_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# Flags for paddle movement # Flags for paddle movement
move_left = False move_left = False
move_right = False move_right = False
# Function to draw heart
def draw_heart(surface, color, pos):
# Draw filled heart
pygame.draw.polygon(surface, color, [(pos[0] + 5, pos[1] + 10), (pos[0] + 10, pos[1] + 5), (pos[0] + 15, pos[1] + 10),
(pos[0] + 20, pos[1] + 5), (pos[0] + 25, pos[1] + 10), (pos[0] + 15, pos[1] + 25),
(pos[0], pos[1] + 10)])
# Main game loop # Main game loop
while not game_over: while not game_over:
for event in pygame.event.get(): for event in pygame.event.get():
@ -113,35 +157,51 @@ while not game_over:
# Check for collision with walls # Check for collision with walls
if cat_rect.left <= 0 or cat_rect.right >= WIDTH: if cat_rect.left <= 0 or cat_rect.right >= WIDTH:
cat_speed[0] = -cat_speed[0] cat_speed[0] = -cat_speed[0]
if wall_sound: if boing_sound:
wall_sound.play() boing_sound.play()
if cat_rect.top <= 0 or cat_rect.colliderect(paddle): if cat_rect.top <= 0 or cat_rect.colliderect(paddle):
cat_speed[1] = -cat_speed[1] cat_speed[1] = -cat_speed[1]
if wall_sound: if boing_sound:
wall_sound.play() boing_sound.play()
# Check for collision with bricks # Check for collision with bricks
for brick in bricks[:]: for brick in bricks[:]:
if cat_rect.colliderect(brick): if cat_rect.colliderect(brick):
bricks.remove(brick) bricks.remove(brick)
score += 1
if block_sound: if block_sound:
block_sound.play() block_sound.play()
score += 1
# Check for collision with cupcakes
for cupcake in falling_cupcakes[:]:
if cat_rect.colliderect(cupcake.x, cupcake.y, cupcake.width, cupcake.height):
falling_cupcakes.remove(cupcake)
if chew_sound:
chew_sound.play()
lives += 1 # Restore one life
# Increase the size of the cat
cat_image = pygame.transform.scale(cat_original_image, (CAT_WIDTH + 10, CAT_HEIGHT + 10))
cat_rect = cat_image.get_rect(center=cat_rect.center)
# Check for loss of life # Check for loss of life
if cat_rect.bottom >= HEIGHT: if cat_rect.bottom >= HEIGHT:
lives -= 1 lives -= 1
if death_sound: if fall_sound:
death_sound.play() fall_sound.play()
cat_rect.center = (WIDTH // 2, HEIGHT // 2) # Reset cat position to the center
if lives <= 0: if lives <= 0:
game_over = True game_over = True
break
else: # Check for winning condition
cat_rect.center = (WIDTH // 2, HEIGHT // 2) if len(bricks) == 0:
winning = True
# Clear the screen # Clear the screen
screen.fill(BLACK) screen.fill(BLACK)
# Draw background image
screen.blit(background_image, (0, 0))
# Draw paddle # Draw paddle
pygame.draw.rect(screen, WHITE, paddle) pygame.draw.rect(screen, WHITE, paddle)
@ -152,27 +212,43 @@ while not game_over:
# Draw cat # Draw cat
screen.blit(cat_image, cat_rect) screen.blit(cat_image, cat_rect)
# Draw falling cupcakes
for cupcake in falling_cupcakes:
cupcake.update()
cupcake.draw(screen)
# Draw heart bar representing lives # Draw heart bar representing lives
start_x = 10 # Adjust the starting x-coordinate to move the heart bar more to the left
for i in range(lives): for i in range(lives):
heart_image = pygame.transform.scale(heart_bar_image, (HEART_WIDTH, HEART_HEIGHT)) draw_heart(screen, RED, (10 + i * 30, 10)) # Adjust the heart position and color as needed
screen.blit(heart_image, (start_x + i * (HEART_WIDTH + HEART_SPACING), 10))
# Draw score # Draw score
score_text = font.render(f"Score: {score}", True, (128, 0, 128)) # Purple color score_text = font.render(f"Score: {score}", True, PURPLE)
screen.blit(score_text, (10, 50)) screen.blit(score_text, (10, 50))
# Spawn falling cupcakes
spawn_cupcakes()
# Update the display # Update the display
pygame.display.flip() pygame.display.flip()
# Cap the frame rate # Cap the frame rate
pygame.time.Clock().tick(60) pygame.time.Clock().tick(60)
# Display Game Over text # Play appropriate sound and display text
screen.fill(BLACK) if game_over:
screen.blit(game_over_text, game_over_rect) if death_sound:
death_sound.play()
screen.blit(game_over_text, game_over_rect)
elif winning:
if win_sound:
win_sound.play()
screen.blit(winning_text, winning_rect)
pygame.display.flip() pygame.display.flip()
# Stop background music when game is over
if bg_music:
bg_music.stop()
# Wait for a few seconds before quitting # Wait for a few seconds before quitting
pygame.time.delay(1500) pygame.time.delay(1500)