Update game2

main
Anna Marija Redžepova 2024-03-03 16:37:26 +00:00
parent e4044183b5
commit 2b09f7d005
1 changed files with 45 additions and 31 deletions

76
game2
View File

@ -27,24 +27,21 @@ PURPLE = (128, 0, 128)
GREEN = (0, 255, 0)
# Load additional sound files
if os.path.exists("wearethechampions.mp3"):
win_sound = pygame.mixer.Sound("wearethechampions.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("boing.wav"):
boing_sound = pygame.mixer.Sound("boing.wav")
if os.path.exists("glassbreaking.mp3"):
block_sound = pygame.mixer.Sound("glassbreaking.mp3")
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")
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")
sound_files = {
"wearethechampions.mp3": "win_sound",
"waltzn2.mp3": "bg_music",
"dishesfall.mp3": "fall_sound",
"boing.wav": "boing_sound",
"glassbreaking.mp3": "block_sound",
"lookatthisdude.mp3": "death_sound",
"chew.wav": "chew_sound",
"meow.mp3": "meow_sound",
"rimshot.mp3": "rimshot_sound"
}
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))
@ -67,21 +64,19 @@ 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
purple_cat_original_image = pygame.image.load("cat.png").convert_alpha()
orange_cat_original_image = pygame.image.load("orangecat.jpg").convert_alpha()
cool_tomato_original_image = pygame.image.load("tomato.png").convert_alpha()
confused_lemon_original_image = pygame.image.load("lemon.jpg").convert_alpha()
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": "Purple Cat", "image": pygame.transform.scale(purple_cat_original_image, (CHARACTER_WIDTH, CHARACTER_HEIGHT))},
{"name": "Orange Cat", "image": pygame.transform.scale(orange_cat_original_image, (CHARACTER_WIDTH, CHARACTER_HEIGHT))},
{"name": "Cool Tomato", "image": pygame.transform.scale(cool_tomato_original_image, (CHARACTER_WIDTH, CHARACTER_HEIGHT))},
{"name": "Confused Lemon", "image": pygame.transform.scale(confused_lemon_original_image, (CHARACTER_WIDTH, CHARACTER_HEIGHT))}
]
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))
@ -190,6 +185,21 @@ def draw_heart(surface, color, pos):
(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 not game_over:
for event in pygame.event.get():
@ -210,11 +220,11 @@ while not game_over:
# Randomly spawn an object (cupcake, football, or trollface)
spawn_type = random.choice(["cupcake", "football", "trollface"])
if spawn_type == "cupcake":
falling_cupcakes.append(Cupcake())
falling_cupcakes.append(FallingObject(cupcake_image, CUPCAKE_WIDTH, CUPCAKE_HEIGHT, 5))
elif spawn_type == "football":
falling_footballs.append(Football())
falling_footballs.append(FallingObject(football_image, FOOTBALL_WIDTH, FOOTBALL_HEIGHT, 5))
else:
falling_trollfaces.append(Trollface())
falling_trollfaces.append(FallingObject(trollface_image, TROLLFACE_WIDTH, TROLLFACE_HEIGHT, 5))
# Move the paddle based on flags
if move_left and paddle.left > 0:
@ -222,6 +232,10 @@ while not game_over:
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]