Add pygame_04.03.24.
parent
a31df629e0
commit
56bd8aab47
|
@ -0,0 +1,410 @@
|
|||
import pygame
|
||||
import sys
|
||||
import os
|
||||
import random
|
||||
|
||||
# Initialize Pygame
|
||||
pygame.init()
|
||||
pygame.mixer.init()
|
||||
|
||||
# Constants
|
||||
WIDTH, HEIGHT = 800, 600
|
||||
PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10
|
||||
PADDLE_SPEED = 10
|
||||
BRICK_WIDTH, BRICK_HEIGHT = 80, 20
|
||||
BRICK_ROWS = 4
|
||||
BRICK_COLS = 10
|
||||
LINE_HEIGHT = 5
|
||||
CUPCAKE_WIDTH, CUPCAKE_HEIGHT = 30, 30
|
||||
FOOTBALL_WIDTH, FOOTBALL_HEIGHT = 40, 40
|
||||
TROLLFACE_WIDTH, TROLLFACE_HEIGHT = 50, 50
|
||||
|
||||
# Colors
|
||||
WHITE = (255, 255, 255)
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0)
|
||||
PURPLE = (128, 0, 128)
|
||||
GREEN = (0, 255, 0)
|
||||
|
||||
# Check if sound files exist before loading
|
||||
if os.path.exists("boing.wav"):
|
||||
boing_sound = pygame.mixer.Sound("boing.wav")
|
||||
if os.path.exists("wearethechampions.mp3"):
|
||||
win_sound = pygame.mixer.Sound("wearethechampions.mp3")
|
||||
if os.path.exists("lookatthisdude.mp3"):
|
||||
death_sound = pygame.mixer.Sound("lookatthisdude.mp3")
|
||||
if os.path.exists("waltzn2.mp3"):
|
||||
bg_music = pygame.mixer.Sound("waltzn2.mp3")
|
||||
if os.path.exists("dishesfall.mp3"):
|
||||
fall_sound = pygame.mixer.Sound("dishesfall.mp3")
|
||||
if os.path.exists("glassbreaking.mp3"):
|
||||
block_sound = pygame.mixer.Sound("glassbreaking.mp3")
|
||||
if os.path.exists("waltzn2.mp3"):
|
||||
chew_sound = pygame.mixer.Sound("waltzn2.mp3")
|
||||
if os.path.exists("meow.mp3"):
|
||||
meow_sound = pygame.mixer.Sound("meow.mp3")
|
||||
if os.path.exists("rimshot.mp3"):
|
||||
rimshot_sound = pygame.mixer.Sound("rimshot.mp3")
|
||||
|
||||
# Load additional sound files
|
||||
sound_files = {
|
||||
win_sound: pygame.mixer.Sound("wearethechampions.mp3"),
|
||||
bg_music: pygame.mixer.Sound("waltzn2.mp3"),
|
||||
fall_sound: pygame.mixer.Sound("dishesfall.mp3"),
|
||||
boing_sound: pygame.mixer.Sound("boing.wav"),
|
||||
block_sound: pygame.mixer.Sound("glassbreaking.mp3"),
|
||||
death_sound: pygame.mixer.Sound("lookatthisdude.mp3"),
|
||||
chew_sound: pygame.mixer.Sound("chew.wav"),
|
||||
meow_sound: pygame.mixer.Sound("meow.mp3"),
|
||||
rimshot_sound: pygame.mixer.Sound("rimshot.mp3")
|
||||
}
|
||||
|
||||
for file, name in sound_files.items():
|
||||
if os.path.exists(file):
|
||||
globals()[name] = pygame.mixer.Sound(file)
|
||||
|
||||
# Create the game window
|
||||
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
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 cupcake image
|
||||
cupcake_original_image = pygame.image.load("cupcake.png").convert_alpha()
|
||||
cupcake_image = pygame.transform.scale(cupcake_original_image, (CUPCAKE_WIDTH, CUPCAKE_HEIGHT))
|
||||
|
||||
# Load football image
|
||||
football_original_image = pygame.image.load("football.png").convert_alpha()
|
||||
football_image = pygame.transform.scale(football_original_image, (FOOTBALL_WIDTH, FOOTBALL_HEIGHT))
|
||||
|
||||
# Load trollface image
|
||||
trollface_original_image = pygame.image.load("trollface.png").convert_alpha()
|
||||
trollface_image = pygame.transform.scale(trollface_original_image, (TROLLFACE_WIDTH, TROLLFACE_HEIGHT))
|
||||
|
||||
# Load character images
|
||||
characters = {
|
||||
"Purple Cat": pygame.image.load("cat.png").convert_alpha(),
|
||||
"Orange Cat": pygame.image.load("orangecat.jpg").convert_alpha(),
|
||||
"Cool Tomato": pygame.image.load("tomato.png").convert_alpha(),
|
||||
"Confused Lemon": pygame.image.load("lemon.jpg").convert_alpha()
|
||||
}
|
||||
|
||||
# Constants for character selection
|
||||
CHARACTER_WIDTH, CHARACTER_HEIGHT = 100, 100
|
||||
CHARACTER_SPACING = 50
|
||||
CHARACTER_Y = HEIGHT // 2 - CHARACTER_HEIGHT // 2
|
||||
CHARACTER_OPTIONS = [{"name": name, "image": pygame.transform.scale(image, (CHARACTER_WIDTH, CHARACTER_HEIGHT))}
|
||||
for name, image in characters.items()]
|
||||
SELECT_CHAR_FONT = pygame.font.Font(None, 36)
|
||||
SELECT_CHAR_TEXT = SELECT_CHAR_FONT.render("Select your character:", True, WHITE)
|
||||
SELECT_CHAR_TEXT_POS = SELECT_CHAR_TEXT.get_rect(center=(WIDTH // 2, HEIGHT // 4))
|
||||
|
||||
# Variables for character selection
|
||||
selected_character_index = 0
|
||||
selected_character_rect = pygame.Rect(0, 0, CHARACTER_WIDTH + 10, CHARACTER_HEIGHT + 10)
|
||||
selected_character_rect.center = (WIDTH // 2, CHARACTER_Y + CHARACTER_HEIGHT // 2)
|
||||
|
||||
# Function to draw character options
|
||||
def draw_character_options():
|
||||
character_x = (WIDTH - (CHARACTER_WIDTH + CHARACTER_SPACING) * len(CHARACTER_OPTIONS)) // 2
|
||||
for i, option in enumerate(CHARACTER_OPTIONS):
|
||||
character_rect = pygame.Rect(character_x, CHARACTER_Y, CHARACTER_WIDTH, CHARACTER_HEIGHT)
|
||||
pygame.draw.rect(screen, WHITE, character_rect, 2)
|
||||
if i == selected_character_index:
|
||||
pygame.draw.rect(screen, RED, character_rect, 2)
|
||||
screen.blit(option["image"], (character_x + 5, CHARACTER_Y + 5))
|
||||
character_x += CHARACTER_WIDTH + CHARACTER_SPACING
|
||||
|
||||
# Function to handle character selection
|
||||
def handle_character_selection():
|
||||
global selected_character_index
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT:
|
||||
selected_character_index = (selected_character_index - 1) % len(CHARACTER_OPTIONS)
|
||||
elif event.key == pygame.K_RIGHT:
|
||||
selected_character_index = (selected_character_index + 1) % len(CHARACTER_OPTIONS)
|
||||
elif event.key == pygame.K_RETURN:
|
||||
return CHARACTER_OPTIONS[selected_character_index]["image"]
|
||||
|
||||
# Function to create a button
|
||||
def draw_button():
|
||||
play_again_text = font.render("Play Again", True, WHITE)
|
||||
play_again_rect = play_again_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
|
||||
pygame.draw.rect(screen, GREEN, play_again_rect, border_radius=5)
|
||||
screen.blit(play_again_text, play_again_rect)
|
||||
return play_again_rect
|
||||
|
||||
# Main loop for character selection screen
|
||||
while True:
|
||||
screen.fill(BLACK)
|
||||
screen.blit(SELECT_CHAR_TEXT, SELECT_CHAR_TEXT_POS)
|
||||
draw_character_options()
|
||||
pygame.display.flip()
|
||||
selected_character_image = handle_character_selection()
|
||||
if selected_character_image:
|
||||
break
|
||||
|
||||
# Set the selected character image
|
||||
cat_image = selected_character_image
|
||||
CAT_WIDTH, CAT_HEIGHT = cat_image.get_size()
|
||||
|
||||
# Create the paddle
|
||||
paddle = pygame.Rect((WIDTH - PADDLE_WIDTH) // 2, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT)
|
||||
|
||||
# Create the ball (cat)
|
||||
cat_rect = cat_image.get_rect(center=(WIDTH // 2, HEIGHT // 2))
|
||||
cat_speed = [5, 5] # Cat movement speed
|
||||
|
||||
# Create death line
|
||||
line = pygame.Rect(0, HEIGHT - LINE_HEIGHT, WIDTH, LINE_HEIGHT)
|
||||
|
||||
# Create bricks
|
||||
bricks = []
|
||||
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)
|
||||
|
||||
# Create a list to hold falling cupcakes
|
||||
falling_cupcakes = []
|
||||
|
||||
# Create a list to hold falling footballs
|
||||
falling_footballs = []
|
||||
|
||||
# Create a list to hold falling trollfaces
|
||||
falling_trollfaces = []
|
||||
|
||||
# Scoring
|
||||
score = 0
|
||||
font = pygame.font.Font(None, 36)
|
||||
|
||||
# Lives
|
||||
lives = 9 # Start with 9 lives
|
||||
|
||||
# Game Over flag and text
|
||||
game_over = False
|
||||
game_started = False
|
||||
game_over_text = font.render("Game Over", True, WHITE)
|
||||
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
|
||||
move_left = False
|
||||
move_right = False
|
||||
|
||||
# Event to spawn falling objects
|
||||
SPAWN_OBJECTS = pygame.USEREVENT + 1
|
||||
pygame.time.set_timer(SPAWN_OBJECTS, 5000) # Spawn objects every 5 seconds
|
||||
|
||||
# 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)])
|
||||
|
||||
class FallingObject:
|
||||
def __init__(self, image, width, height, speed):
|
||||
self.image = image
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.x = random.randint(0, WIDTH - self.width)
|
||||
self.y = 0 - self.height
|
||||
self.speed = speed
|
||||
|
||||
def update(self):
|
||||
self.y += self.speed # Adjust the falling speed as needed
|
||||
|
||||
def draw(self, surface):
|
||||
surface.blit(self.image, (self.x, self.y))
|
||||
|
||||
# Main game loop
|
||||
while True:
|
||||
while not game_over and not winning:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT:
|
||||
move_left = True
|
||||
elif event.key == pygame.K_RIGHT:
|
||||
move_right = True
|
||||
elif event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_LEFT:
|
||||
move_left = False
|
||||
elif event.key == pygame.K_RIGHT:
|
||||
move_right = False
|
||||
elif event.type == SPAWN_OBJECTS:
|
||||
# Randomly spawn an object (cupcake, football, or trollface)
|
||||
spawn_type = random.choice(["cupcake", "football", "trollface"])
|
||||
if spawn_type == "cupcake":
|
||||
falling_cupcakes.append(FallingObject(cupcake_image, CUPCAKE_WIDTH, CUPCAKE_HEIGHT, 5))
|
||||
elif spawn_type == "football":
|
||||
falling_footballs.append(FallingObject(football_image, FOOTBALL_WIDTH, FOOTBALL_HEIGHT, 5))
|
||||
else:
|
||||
falling_trollfaces.append(FallingObject(trollface_image, TROLLFACE_WIDTH, TROLLFACE_HEIGHT, 5))
|
||||
|
||||
# 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
|
||||
|
||||
# Update cat position based on speed
|
||||
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 boing_sound:
|
||||
boing_sound.play()
|
||||
if cat_rect.top <= 0 or cat_rect.colliderect(paddle):
|
||||
cat_speed[1] = -cat_speed[1]
|
||||
if boing_sound:
|
||||
boing_sound.play()
|
||||
|
||||
# Check for collision with bricks
|
||||
for brick in bricks[:]:
|
||||
if cat_rect.colliderect(brick):
|
||||
bricks.remove(brick)
|
||||
if block_sound:
|
||||
block_sound.play()
|
||||
score += 1
|
||||
|
||||
# Check for collision with cupcakes
|
||||
for cupcake in falling_cupcakes[:]:
|
||||
if paddle.colliderect(cupcake.x, cupcake.y, cupcake.width, cupcake.height):
|
||||
falling_cupcakes.remove(cupcake)
|
||||
if chew_sound:
|
||||
chew_sound.play()
|
||||
# Update cat size and speed
|
||||
cat_rect.width += 10
|
||||
cat_rect.height += 10
|
||||
cat_speed[0] += 1
|
||||
cat_speed[1] += 1
|
||||
|
||||
# Check for collision with footballs
|
||||
for football in falling_footballs[:]:
|
||||
if paddle.colliderect(football.x, football.y, football.width, football.height):
|
||||
falling_footballs.remove(football)
|
||||
if meow_sound:
|
||||
meow_sound.play()
|
||||
# Update cat size and speed
|
||||
cat_rect.width -= 10
|
||||
cat_rect.height -= 10
|
||||
cat_speed[0] -= 1
|
||||
cat_speed[1] -= 1
|
||||
|
||||
# Check for collision with trollfaces
|
||||
for trollface in falling_trollfaces[:]:
|
||||
if paddle.colliderect(trollface.x, trollface.y, trollface.width, trollface.height):
|
||||
falling_trollfaces.remove(trollface)
|
||||
if rimshot_sound:
|
||||
rimshot_sound.play()
|
||||
# Grow a random brick
|
||||
brick = random.choice(bricks)
|
||||
brick.width += 10
|
||||
brick.height += 5
|
||||
|
||||
# Check for loss of life
|
||||
if cat_rect.bottom >= HEIGHT:
|
||||
lives -= 1
|
||||
if fall_sound:
|
||||
fall_sound.play()
|
||||
cat_rect.center = (WIDTH // 2, HEIGHT // 2) # Reset cat position to the center
|
||||
if lives <= 0:
|
||||
game_over = True
|
||||
|
||||
# Check for winning condition
|
||||
if len(bricks) == 0:
|
||||
winning = True
|
||||
|
||||
# Clear the screen
|
||||
screen.fill(BLACK)
|
||||
|
||||
# Draw background image
|
||||
screen.blit(background_image, (0, 0))
|
||||
|
||||
# Draw paddle
|
||||
pygame.draw.rect(screen, GREEN, paddle)
|
||||
|
||||
# Draw bricks
|
||||
for brick in bricks:
|
||||
pygame.draw.rect(screen, WHITE, brick)
|
||||
|
||||
# Draw cat
|
||||
screen.blit(cat_image, cat_rect)
|
||||
|
||||
# Draw falling cupcakes
|
||||
for cupcake in falling_cupcakes:
|
||||
cupcake.update()
|
||||
cupcake.draw(screen)
|
||||
|
||||
# Draw falling footballs
|
||||
for football in falling_footballs:
|
||||
football.update()
|
||||
football.draw(screen)
|
||||
|
||||
# Draw falling trollfaces
|
||||
for trollface in falling_trollfaces:
|
||||
trollface.update()
|
||||
trollface.draw(screen)
|
||||
|
||||
# Draw heart bar representing lives
|
||||
for i in range(lives):
|
||||
draw_heart(screen, RED, (10 + i * 30, 10)) # Adjust the heart position and color as needed
|
||||
|
||||
# Draw score
|
||||
score_text = font.render(f"Score: {score}", True, PURPLE)
|
||||
screen.blit(score_text, (10, 50))
|
||||
|
||||
# Update the display
|
||||
pygame.display.flip()
|
||||
|
||||
# Cap the frame rate
|
||||
pygame.time.Clock().tick(60)
|
||||
|
||||
# Play appropriate sound and display text
|
||||
if game_over:
|
||||
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)
|
||||
|
||||
# Draw play again button
|
||||
play_again_rect = draw_button()
|
||||
pygame.display.flip()
|
||||
|
||||
# Check for button click
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||
if play_again_rect.collidepoint(event.pos):
|
||||
# Reset the game state
|
||||
game_over = False
|
||||
winning = False
|
||||
score = 0
|
||||
lives = 9
|
||||
cat_rect.center = (WIDTH // 2, HEIGHT // 2)
|
||||
bricks.clear()
|
||||
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)
|
||||
falling_cupcakes.clear()
|
||||
falling_footballs.clear()
|
||||
falling_trollfaces.clear()
|
Loading…
Reference in New Issue