import pygame import sys # Initialize Pygame pygame.init() # Set up the screen WIDTH = 800 HEIGHT = 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Platformer Game") # Set up colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) # Set up the player player_width = 50 player_height = 50 player_x = WIDTH // 2 - player_width // 2 player_y = HEIGHT - player_height player_speed = 5 player_jump_force = 10 player_jump = False player_jump_count = 10 # Set up platforms platforms = [ pygame.Rect(200, HEIGHT - 100, 100, 20), pygame.Rect(400, HEIGHT - 200, 100, 20), pygame.Rect(600, HEIGHT - 300, 100, 20) ] clock = pygame.time.Clock() # Game loop while True: screen.fill(WHITE) # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Handle player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x -= player_speed if keys[pygame.K_RIGHT]: player_x += player_speed # Handle player jumping if keys[pygame.K_SPACE] and not player_jump: player_jump = True if player_jump: if player_jump_count >= -10: neg = 1 if player_jump_count < 0: neg = -1 player_y -= (player_jump_count ** 2) * 0.5 * neg player_jump_count -= 1 else: player_jump = False player_jump_count = 10 # Apply gravity player_y += 5 # Collide with platforms for platform in platforms: if platform.colliderect((player_x, player_y, player_width, player_height)): if player_y < platform.y: player_y = platform.y - player_height player_jump = False player_jump_count = 10 else: player_y = platform.y + platform.height # Draw player pygame.draw.rect(screen, RED, (player_x, player_y, player_width, player_height)) # Draw platforms for platform in platforms: pygame.draw.rect(screen, BLACK, platform) pygame.display.flip() clock.tick(60)