import pygame import sys import math # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 FPS = 60 GRAVITY = 0.8 JUMP_VELOCITY = -15 BULLET_SPEED = 10 # 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) # Crimson color for title screen # Setup screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("UltraBluePilled") 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 # 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), ] # Bullets list bullets = [] # 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 # Horizontal player.x += int(player_velocity.x) for platform in platforms: if player.colliderect(platform): if player_velocity.x > 0: player.right = platform.left elif player_velocity.x < 0: player.left = platform.right # Vertical player.y += int(player_velocity.y) for platform in platforms: if player.colliderect(platform): if player_velocity.y > 0: player.bottom = platform.top player_velocity.y = 0 on_ground = True elif player_velocity.y < 0: player.top = platform.bottom player_velocity.y = 0 # Function to shoot a bullet towards the mouse def shoot_bullet(): mouse_x, mouse_y = pygame.mouse.get_pos() # Get the direction from player to mouse direction = pygame.Vector2(mouse_x - player.centerx, mouse_y - player.centery) # Normalize the direction direction.normalize_ip() # Create the bullet with the same direction and speed bullet_velocity = direction * BULLET_SPEED bullet = { 'rect': pygame.Rect(player.right, player.centery - 5, 10, 10), 'velocity': bullet_velocity } bullets.append(bullet) # Start screen function def start_screen(): screen.fill(CRIMSON) # Change the title screen background to crimson # Draw the title title_text = font.render("UltraBluePilled", True, YELLOW) title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 3)) screen.blit(title_text, title_rect) # Draw instructions 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: # Show the start screen start_screen() # Check for key press to start the game for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: game_started = True break # Exit the loop and start the game else: screen.fill(BLACK) # Set the in-game background to black # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Jump if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: player_velocity.y = JUMP_VELOCITY # Shoot bullet when left mouse button is clicked if event.type == pygame.MOUSEBUTTONDOWN and event.button == pygame.BUTTON_LEFT: shoot_bullet() handle_input() apply_gravity() move_and_collide() # Move bullets for bullet in bullets[:]: bullet['rect'].x += bullet['velocity'].x bullet['rect'].y += bullet['velocity'].y if bullet['rect'].x > WIDTH or bullet['rect'].y > HEIGHT or bullet['rect'].x < 0 or bullet['rect'].y < 0: bullets.remove(bullet) # Draw player (square) pygame.draw.rect(screen, BLUE, player) # Draw platforms for platform in platforms: pygame.draw.rect(screen, GREEN, platform) # Draw bullets as yellow triangles pointing to mouse 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()