import pygame import sys # Initialize Pygame pygame.init() # Set up the screen WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Simple Platformer") # Colors WHITE = (255, 255, 255) BLUE = (0, 0, 255) # Player properties player_width = 50 player_height = 50 player_x = WIDTH // 2 - player_width // 2 player_y = HEIGHT // 2 - player_height // 2 player_speed = 5 player_jump_power = 10 player_jump = False player_jump_count = 10 player_rect = pygame.Rect(player_x, player_y, player_width, player_height) # Platform properties platform_width = WIDTH platform_height = 20 platform_x = 0 platform_y = HEIGHT - platform_height platform_rect = pygame.Rect(platform_x, platform_y, platform_width, platform_height) # Game loop running = True while running: screen.fill(WHITE) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not player_jump: player_jump = True # Player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_rect.x -= player_speed if keys[pygame.K_RIGHT]: player_rect.x += player_speed # Player jumping if player_jump: if player_jump_count >= -10: neg = 1 if player_jump_count < 0: neg = -1 player_rect.y -= (player_jump_count ** 2) * 0.5 * neg player_jump_count -= 1 else: player_jump = False player_jump_count = 10 # Check for collisions if player_rect.colliderect(platform_rect): player_jump = False player_jump_count = 10 player_rect.y = platform_rect.top - player_height # Draw player and platform pygame.draw.rect(screen, BLUE, player_rect) pygame.draw.rect(screen, BLUE, platform_rect) # Update the display pygame.display.flip() # Cap the frame rate pygame.time.Clock().tick(30) # Quit Pygame pygame.quit() sys.exit()