import pygame import random # Initialize Pygame pygame.init() # Constants SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 PLAYER_WIDTH = 30 PLAYER_HEIGHT = 50 PLATFORM_WIDTH = 80 PLATFORM_HEIGHT = 10 WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAVITY = 0.5 JUMP_POWER = -10 MAX_SPEED_Y = 10 PLAYER_SPEED_X = 5 # Create the game window screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Parkour Game") clock = pygame.time.Clock() # Define the Player class class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface([PLAYER_WIDTH, PLAYER_HEIGHT]) self.image.fill(WHITE) self.rect = self.image.get_rect() self.rect.x = SCREEN_WIDTH // 2 self.rect.y = SCREEN_HEIGHT // 2 self.speed_x = 0 self.speed_y = 0 self.on_ground = False def update(self, platforms): self.speed_y += GRAVITY self.speed_y = min(self.speed_y, MAX_SPEED_Y) self.rect.x += self.speed_x # Check collision with platforms for platform in platforms: if pygame.sprite.collide_rect(self, platform): if self.speed_x > 0: self.rect.right = platform.rect.left elif self.speed_x < 0: self.rect.left = platform.rect.right self.rect.y += self.speed_y # Check collision with platforms self.on_ground = False for platform in platforms: if pygame.sprite.collide_rect(self, platform): if self.speed_y > 0: self.rect.bottom = platform.rect.top self.on_ground = True elif self.speed_y < 0: self.rect.top = platform.rect.bottom self.speed_y = 0 # Keep the player inside the screen if self.rect.left < 0: self.rect.left = 0 elif self.rect.right > SCREEN_WIDTH: self.rect.right = SCREEN_WIDTH # Define the Platform class class Platform(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface([PLATFORM_WIDTH, PLATFORM_HEIGHT]) self.image.fill(WHITE) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Create sprite groups all_sprites = pygame.sprite.Group() platforms = pygame.sprite.Group() # Create player player = Player() all_sprites.add(player) # Create platforms platform1 = Platform(0, SCREEN_HEIGHT - 20) platform2 = Platform(SCREEN_WIDTH // 2 - 40, SCREEN_HEIGHT // 2) platform3 = Platform(200, 400) platform4 = Platform(500, 300) platforms.add(platform1, platform2, platform3, platform4) all_sprites.add(platform1, platform2, platform3, platform4) # Main game loop running = True while running: 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 player.on_ground: player.speed_y = JUMP_POWER keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.speed_x = -PLAYER_SPEED_X elif keys[pygame.K_RIGHT]: player.speed_x = PLAYER_SPEED_X else: player.speed_x = 0 all_sprites.update(platforms) screen.fill(BLACK) all_sprites.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit()