import pygame import sys import random import math # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 FPS = 60 GRAVITY = 0.8 JUMP_VELOCITY = -15 BULLET_SPEED = 10 RELOAD_TIME = 500 # 0.5 second MAX_BULLETS = 1 # Colors WHITE = (255, 255, 255) BLUE = (0, 150, 255) GREEN = (0, 255, 0) YELLOW = (255, 255, 0) BLACK = (0, 0, 0) CRIMSON = (220, 20, 60) RED = (255, 0, 0) # Setup screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("HAYDENS GAME(SOCOOL)") clock = pygame.time.Clock() # Fonts font = pygame.font.Font(None, 74) small_font = pygame.font.Font(None, 36) # Player setup player_size = 50 player = pygame.Rect(100, HEIGHT - 150, player_size, player_size) player_velocity = pygame.Vector2(0, 0) on_ground = False # Mirror player setup mirror_player = pygame.Rect(WIDTH - 150, HEIGHT - 150, player_size, player_size) mirror_velocity = pygame.Vector2(0, 0) # Platforms platforms = [ pygame.Rect(0, HEIGHT - 40, WIDTH, 40), pygame.Rect(200, 450, 200, 20), pygame.Rect(500, 350, 200, 20), pygame.Rect(100, 250, 200, 20), ] # Barriers (added to surround the entire screen) barriers = [ pygame.Rect(0, 0, WIDTH, 20), # Top barrier pygame.Rect(0, 0, 20, HEIGHT), # Left barrier pygame.Rect(WIDTH - 20, 0, 20, HEIGHT), # Right barrier pygame.Rect(0, HEIGHT - 20, WIDTH, 20) # Bottom barrier ] # Bullets list bullets = [] bullet_count = MAX_BULLETS reloading = False reload_timer = 0 # Game state flag game_started = False def handle_input(): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] or keys[pygame.K_a]: player_velocity.x = -5 elif keys[pygame.K_RIGHT] or keys[pygame.K_d]: player_velocity.x = 5 else: player_velocity.x = 0 def apply_gravity(): global on_ground player_velocity.y += GRAVITY if player_velocity.y > 10: player_velocity.y = 10 def move_and_collide(): global on_ground on_ground = False all_colliders = platforms + barriers # Horizontal movement player.x += int(player_velocity.x) for collider in all_colliders: if player.colliderect(collider): if player_velocity.x > 0: player.right = collider.left elif player_velocity.x < 0: player.left = collider.right # Vertical movement player.y += int(player_velocity.y) for collider in all_colliders: if player.colliderect(collider): if player_velocity.y > 0: player.bottom = collider.top player_velocity.y = 0 on_ground = True elif player_velocity.y < 0: player.top = collider.bottom player_velocity.y = 0 def move_mirror_player(): # Mirror mimics movement mirror_velocity.x = player_velocity.x mirror_velocity.y += GRAVITY if mirror_velocity.y > 10: mirror_velocity.y = 10 all_colliders = platforms + barriers # Horizontal movement mirror_player.x += int(mirror_velocity.x) for collider in all_colliders: if mirror_player.colliderect(collider): if mirror_velocity.x > 0: mirror_player.right = collider.left elif mirror_velocity.x < 0: mirror_player.left = collider.right # Vertical movement mirror_player.y += int(mirror_velocity.y) for collider in all_colliders: if mirror_player.colliderect(collider): if mirror_velocity.y > 0: mirror_player.bottom = collider.top mirror_velocity.y = 0 elif mirror_velocity.y < 0: mirror_player.top = collider.bottom mirror_velocity.y = 0 def shoot_bullet(): global bullet_count, reloading, reload_timer if bullet_count > 0 and not reloading: mouse_x, mouse_y = pygame.mouse.get_pos() direction = pygame.Vector2(mouse_x - player.centerx, mouse_y - player.centery) if direction.length() != 0: direction.normalize_ip() bullet_velocity = direction * BULLET_SPEED bullet = { 'rect': pygame.Rect(player.right, player.centery - 5, 10, 10), 'velocity': bullet_velocity, 'bounce_count': 0 } bullets.append(bullet) # Mirror shoots towards the player mirror_direction = pygame.Vector2(player.centerx - mirror_player.centerx, player.centery - mirror_player.centery) if mirror_direction.length() != 0: mirror_direction.normalize_ip() mirror_bullet_velocity = mirror_direction * BULLET_SPEED mirror_bullet = { 'rect': pygame.Rect(mirror_player.right, mirror_player.centery - 5, 10, 10), 'velocity': mirror_bullet_velocity, 'bounce_count': 0 } bullets.append(mirror_bullet) bullet_count -= 1 if bullet_count == 0: reloading = True reload_timer = pygame.time.get_ticks() def update_bullets(): all_colliders = platforms + barriers for bullet in bullets[:]: bullet['rect'].x += bullet['velocity'].x bullet['rect'].y += bullet['velocity'].y for collider in all_colliders: if bullet['rect'].colliderect(collider): # Increment the bounce count when bullet hits a platform if collider in platforms: bullet['bounce_count'] += 1 # Bounce horizontally if (bullet['rect'].left <= collider.right and bullet['rect'].right >= collider.right) or (bullet['rect'].right >= collider.left and bullet['rect'].left <= collider.left): bullet['velocity'].x *= -1 # Bounce vertically if (bullet['rect'].top <= collider.bottom and bullet['rect'].bottom >= collider.bottom) or (bullet['rect'].bottom >= collider.top and bullet['rect'].top <= collider.top): bullet['velocity'].y *= -1 # Despawn after certain chances, guaranteed after the 5th bounce if bullet['bounce_count'] == 1: despawn_chance = 0.5 elif bullet['bounce_count'] == 2: despawn_chance = 0.25 elif bullet['bounce_count'] == 3: despawn_chance = 0.1 elif bullet['bounce_count'] == 4: despawn_chance = 0.1 elif bullet['bounce_count'] >= 5: despawn_chance = 1 else: despawn_chance = 0 if random.random() < despawn_chance: bullets.remove(bullet) break # Remove bullet if it goes off-screen if bullet['rect'].x > WIDTH or bullet['rect'].y > HEIGHT or bullet['rect'].x < 0 or bullet['rect'].y < 0: bullets.remove(bullet) def start_screen(): screen.fill(CRIMSON) title_text = font.render("Hayden's Gun Game", True, YELLOW) title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 3)) screen.blit(title_text, title_rect) instructions_text = small_font.render("Press any key to start", True, WHITE) instructions_rect = instructions_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 100)) screen.blit(instructions_text, instructions_rect) pygame.display.flip() # Game loop running = True while running: clock.tick(FPS) if not game_started: start_screen() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: game_started = True break else: screen.fill(BLACK) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player_velocity.y = JUMP_VELOCITY mirror_velocity.y = JUMP_VELOCITY # Mirror also jumps if event.type == pygame.MOUSEBUTTONDOWN and event.button == pygame.BUTTON_LEFT: shoot_bullet() handle_input() apply_gravity() move_and_collide() move_mirror_player() update_bullets() # Reload bullets after delay if reloading: if pygame.time.get_ticks() - reload_timer >= RELOAD_TIME: bullet_count = MAX_BULLETS reloading = False # Draw players pygame.draw.rect(screen, BLUE, player) pygame.draw.rect(screen, RED, mirror_player) # Draw platforms for platform in platforms: pygame.draw.rect(screen, GREEN, platform) # Draw barriers for barrier in barriers: pygame.draw.rect(screen, GREEN, barrier) # Draw bullets for bullet in bullets: tip = (bullet['rect'].right, bullet['rect'].centery) back_top = (bullet['rect'].left, bullet['rect'].top) back_bottom = (bullet['rect'].left, bullet['rect'].bottom) pygame.draw.polygon(screen, YELLOW, [tip, back_top, back_bottom]) pygame.display.flip() pygame.quit() sys.exit()