import pygame screen_width = 800 screen_height = 800 class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() # Load original images self.original_images = { "left": [pygame.image.load(f"sprites/BaseCharacter/left{num}.png").convert_alpha() for num in range(1, 5)], "right": [pygame.image.load(f"sprites/BaseCharacter/right{num}.png").convert_alpha() for num in range(1, 5)] } # Scale images self.images = {direction: [pygame.transform.scale(image, (image.get_width() * 2, image.get_height() * 2)) for image in images_list] for direction, images_list in self.original_images.items()} self.index = 0 self.image = self.images["right"][0] self.rect = self.image.get_rect() self.rect.center = (screen_width // 2, screen_height // 2) self.speed = 5 self.direction = "right" self.standing_frames = { "left": self.images["left"][0], "right": self.images["right"][0] } self.jump_power = -10 # Adjust as needed self.gravity = 0.5 # Adjust as needed self.vertical_speed = 0 self.on_ground = False def update(self, keys, platforms): if keys[pygame.K_LEFT]: self.rect.x -= self.speed self.direction = "left" elif keys[pygame.K_RIGHT]: self.rect.x += self.speed self.direction = "right" else: self.image = self.standing_frames[self.direction] self.index = 0 # Apply gravity if not self.on_ground: self.vertical_speed += self.gravity self.rect.y += self.vertical_speed # Jumping mechanics if keys[pygame.K_SPACE] and self.on_ground: self.vertical_speed = self.jump_power self.on_ground = False # Check if player is on the ground self.on_ground = False self.rect.y += 1 for platform in platforms: if self.rect.colliderect(platform.rect): self.on_ground = True self.rect.y = platform.rect.y - self.rect.height self.vertical_speed = 0 break self.rect.y -= 1 # Set standing frame when not moving if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT] and self.on_ground: self.image = self.standing_frames[self.direction] # Animate player self.index += 0.2 # Adjust animation speed if needed if self.index >= len(self.images[self.direction]): self.index = 0 self.image = self.images[self.direction][int(self.index)] class Platform(pygame.sprite.Sprite): def __init__(self, x, y, width, height): super().__init__() self.image = pygame.Surface((width, height)) self.image.fill((255, 255, 255)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y class End(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((50, 50)) # You can adjust the size as needed self.image.fill((255, 255, 0)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y class Kill(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((50, 800)) # You can adjust the size as needed self.image.fill((255, 0, 0)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y class Phones(pygame.sprite.Sprite): def __init__(self, x, y, size=(20, 50)): super().__init__() self.image = pygame.Surface(size) self.image.fill((255, 0, 255)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y