From 653e268d361888aa6094ece022a55a5347a79969 Mon Sep 17 00:00:00 2001 From: Shade-raid <55247492+Shade-raid@users.noreply.github.com> Date: Sun, 11 May 2025 16:53:20 +0400 Subject: [PATCH] Update main.py Refactored main game loop and bullet handling logic, fixed recursion bug --- main.py | 108 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/main.py b/main.py index cf19df3..65c2979 100644 --- a/main.py +++ b/main.py @@ -1,56 +1,60 @@ import pygame import os + +# Initialize Pygame modules +pygame.init() pygame.font.init() pygame.mixer.init() +# Screen dimensions WIDTH, HEIGHT = 900, 500 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) -pygame.display.set_caption("First Game!") +pygame.display.set_caption("Spaceship Shooter") +# Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 255, 0) -BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT) - -#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3') -#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3') - -HEALTH_FONT = pygame.font.SysFont('comicsans', 40) -WINNER_FONT = pygame.font.SysFont('comicsans', 100) - +# Game constants FPS = 60 VEL = 5 BULLET_VEL = 7 MAX_BULLETS = 3 SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40 +BORDER = pygame.Rect(WIDTH // 2 - 5, 0, 10, HEIGHT) +# Events YELLOW_HIT = pygame.USEREVENT + 1 RED_HIT = pygame.USEREVENT + 2 -YELLOW_SPACESHIP_IMAGE = pygame.image.load( - os.path.join('Assets', 'spaceship_yellow.png')) -YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale( - YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90) +# Fonts +HEALTH_FONT = pygame.font.SysFont('comicsans', 40) +WINNER_FONT = pygame.font.SysFont('comicsans', 100) -RED_SPACESHIP_IMAGE = pygame.image.load( - os.path.join('Assets', 'spaceship_red.png')) -RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale( - RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270) +# Sounds (make sure the files exist) +# BULLET_HIT_SOUND = pygame.mixer.Sound(os.path.join('Assets', 'Grenade+1.mp3')) +# BULLET_FIRE_SOUND = pygame.mixer.Sound(os.path.join('Assets', 'Gun+Silencer.mp3')) -SPACE = pygame.transform.scale(pygame.image.load( - os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT)) +# Load and transform images +YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'spaceship_yellow.png')) +YELLOW_SPACESHIP = pygame.transform.rotate( + pygame.transform.scale(YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90) +RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'spaceship_red.png')) +RED_SPACESHIP = pygame.transform.rotate( + pygame.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270) +SPACE = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT)) + +# Draw everything to the screen def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health): WIN.blit(SPACE, (0, 0)) pygame.draw.rect(WIN, BLACK, BORDER) - red_health_text = HEALTH_FONT.render( - "Health: " + str(red_health), 1, WHITE) - yellow_health_text = HEALTH_FONT.render( - "Health: " + str(yellow_health), 1, WHITE) + red_health_text = HEALTH_FONT.render(f"Health: {red_health}", 1, WHITE) + yellow_health_text = HEALTH_FONT.render(f"Health: {yellow_health}", 1, WHITE) WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10)) WIN.blit(yellow_health_text, (10, 10)) @@ -65,31 +69,31 @@ def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_hea pygame.display.update() - +# Handle yellow ship movement def yellow_handle_movement(keys_pressed, yellow): - if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT + if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # Left yellow.x -= VEL - if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT + if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # Right yellow.x += VEL - if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP + if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # Up yellow.y -= VEL - if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN + if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT: # Down yellow.y += VEL - +# Handle red ship movement def red_handle_movement(keys_pressed, red): - if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT + if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # Left red.x -= VEL - if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT + if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # Right red.x += VEL - if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP + if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # Up red.y -= VEL - if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN + if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT: # Down red.y += VEL - +# Move bullets and detect hits def handle_bullets(yellow_bullets, red_bullets, yellow, red): - for bullet in yellow_bullets: + for bullet in yellow_bullets[:]: bullet.x += BULLET_VEL if red.colliderect(bullet): pygame.event.post(pygame.event.Event(RED_HIT)) @@ -97,7 +101,7 @@ def handle_bullets(yellow_bullets, red_bullets, yellow, red): elif bullet.x > WIDTH: yellow_bullets.remove(bullet) - for bullet in red_bullets: + for bullet in red_bullets[:]: bullet.x -= BULLET_VEL if yellow.colliderect(bullet): pygame.event.post(pygame.event.Event(YELLOW_HIT)) @@ -105,15 +109,15 @@ def handle_bullets(yellow_bullets, red_bullets, yellow, red): elif bullet.x < 0: red_bullets.remove(bullet) - +# Show winner text def draw_winner(text): draw_text = WINNER_FONT.render(text, 1, WHITE) - WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() / - 2, HEIGHT/2 - draw_text.get_height()/2)) + WIN.blit(draw_text, (WIDTH // 2 - draw_text.get_width() // 2, + HEIGHT // 2 - draw_text.get_height() // 2)) pygame.display.update() pygame.time.delay(5000) - +# Main game loop def main(): red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) @@ -126,42 +130,41 @@ def main(): clock = pygame.time.Clock() run = True + while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False - pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS: bullet = pygame.Rect( - yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5) + yellow.x + yellow.width, yellow.y + yellow.height // 2 - 2, 10, 5) yellow_bullets.append(bullet) - #BULLET_FIRE_SOUND.play() + # BULLET_FIRE_SOUND.play() if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS: bullet = pygame.Rect( - red.x, red.y + red.height//2 - 2, 10, 5) + red.x, red.y + red.height // 2 - 2, 10, 5) red_bullets.append(bullet) - #BULLET_FIRE_SOUND.play() + # BULLET_FIRE_SOUND.play() if event.type == RED_HIT: red_health -= 1 - #BULLET_HIT_SOUND.play() + # BULLET_HIT_SOUND.play() if event.type == YELLOW_HIT: yellow_health -= 1 - #BULLET_HIT_SOUND.play() + # BULLET_HIT_SOUND.play() winner_text = "" if red_health <= 0: winner_text = "Yellow Wins!" - if yellow_health <= 0: winner_text = "Red Wins!" - if winner_text != "": + if winner_text: draw_winner(winner_text) break @@ -171,11 +174,10 @@ def main(): handle_bullets(yellow_bullets, red_bullets, yellow, red) - draw_window(red, yellow, red_bullets, yellow_bullets, - red_health, yellow_health) - - main() + draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health) + pygame.quit() +# Run the game if __name__ == "__main__": main()