import pygame import sys import random # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 FPS = 60 WHITE = (255, 255, 255) BLACK = (0, 0, 0) BLUE = (135, 206, 235) YELLOW = (255, 255, 0) RED = (255, 0, 0) PLAYER_SIZE = (50, 70) BLOCK_SIZE = 50 GRAVITY = 1 JUMP_STRENGTH = -15 # Setup the display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Platformer Game") clock = pygame.time.Clock() # Player class class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load('human_character.png') # Use a human-like sprite image self.image = pygame.transform.scale(self.image, PLAYER_SIZE) self.rect = self.image.get_rect() self.rect.x, self.rect.y = WIDTH // 2, HEIGHT - PLAYER_SIZE[1] self.velocity_y = 0 self.on_ground = False def update(self): self.move() self.check_collision() def move(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.rect.x -= 5 if keys[pygame.K_RIGHT]: self.rect.x += 5 if keys[pygame.K_UP] and self.on_ground: self.velocity_y = JUMP_STRENGTH self.on_ground = False self.velocity_y += GRAVITY self.rect.y += self.velocity_y if self.rect.y >= HEIGHT - PLAYER_SIZE[1]: self.rect.y = HEIGHT - PLAYER_SIZE[1] self.velocity_y = 0 self.on_ground = True def check_collision(self): hits = pygame.sprite.spritecollide(self, platforms, False) if hits: self.rect.y = hits[0].rect.top - PLAYER_SIZE[1] self.velocity_y = 0 self.on_ground = True if pygame.sprite.spritecollide(self, obstacles, False) or self.rect.colliderect(lava.rect): self.rect.x, self.rect.y = WIDTH // 2, HEIGHT - PLAYER_SIZE[1] if pygame.sprite.spritecollide(self, goals, True): print("You Win!") pygame.quit() sys.exit() # Platform class class Platform(pygame.sprite.Sprite): def __init__(self, x, y, width, height, moving=False): super().__init__() self.image = pygame.image.load('dirt_block.png') # Use a dirt block sprite image self.image = pygame.transform.scale(self.image, (width, height)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.moving = moving self.direction = 1 def update(self): if self.moving: self.rect.x += self.direction if self.rect.right > WIDTH or self.rect.left < 0: self.direction *= -1 # Goal class class Goal(pygame.sprite.Sprite): def __init__(self, x, y, width, height): super().__init__() self.image = pygame.image.load('door.png') # Use a door sprite image self.image = pygame.transform.scale(self.image, (width, height)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Obstacle class (Moving Slime) class Obstacle(pygame.sprite.Sprite): def __init__(self, x, y, width, height): super().__init__() self.image = pygame.image.load('slime.png') # Use a slime sprite image self.image = pygame.transform.scale(self.image, (width, height)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.direction = random.choice([-1, 1]) def update(self): self.rect.x += self.direction * 2 if self.rect.right > WIDTH or self.rect.left < 0: self.direction *= -1 # Lava class class Lava(pygame.sprite.Sprite): def __init__(self, x, y, width, height): super().__init__() self.image = pygame.Surface((width, height)) self.image.fill(RED) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Cloud class class Cloud(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.image.load('cloud.png') # Use a cloud sprite image self.image = pygame.transform.scale(self.image, (100, 60)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Group setup all_sprites = pygame.sprite.Group() platforms = pygame.sprite.Group() obstacles = pygame.sprite.Group() goals = pygame.sprite.Group() clouds = pygame.sprite.Group() # Create instances player = Player() all_sprites.add(player) # Stairs and platforms for i in range(5): platform = Platform(50 + i * 50, HEIGHT - (i + 1) * 40, BLOCK_SIZE, BLOCK_SIZE) all_sprites.add(platform) platforms.add(platform) platform1 = Platform(300, 450, BLOCK_SIZE, BLOCK_SIZE, moving=True) platform2 = Platform(550, 350, BLOCK_SIZE, BLOCK_SIZE) platform3 = Platform(200, 250, BLOCK_SIZE, BLOCK_SIZE, moving=True) platform4 = Platform(400, 150, BLOCK_SIZE, BLOCK_SIZE) all_sprites.add(platform1, platform2, platform3, platform4) platforms.add(platform1, platform2, platform3, platform4) goal = Goal(750, 50, 50, 70) all_sprites.add(goal) goals.add(goal) # Obstacles obstacle1 = Obstacle(300, 550, BLOCK_SIZE, BLOCK_SIZE) obstacle2 = Obstacle(500, 350, BLOCK_SIZE, BLOCK_SIZE) all_sprites.add(obstacle1, obstacle2) obstacles.add(obstacle1, obstacle2) # Lava pits lava = Lava(0, HEIGHT - 20, WIDTH, 20) all_sprites.add(lava) # Clouds for i in range(5): cloud = Cloud(random.randint(0, WIDTH - 100), random.randint(0, HEIGHT // 2)) all_sprites.add(cloud) clouds.add(cloud) # Main game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False all_sprites.update() # Draw background screen.fill(BLUE) pygame.draw.circle(screen, YELLOW, (WIDTH - 80, 80), 50) clouds.draw(screen) all_sprites.draw(screen) pygame.display.flip() clock.tick(FPS) pygame.quit() sys.exit()