""" ----------------------------------------- Project: Escape Drake Standard: 91896 (2.7) School: Tauranga Boys' Collage Auther: Hunter Moss Date: May 2024 Python: 3.11.2 ----------------------------------------- """ import pygame import os pygame.init() # Screen Size screen_width = 1000 screen_height = 1000 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Escape Drake') # Load images background_img = pygame.image.load('Images/Background.png') # Constants tile_size = 50 # Player class class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.images = { 'idle': load_images('Images/Player', 'idle', 16), 'run': load_images('Images/Player', 'run', 20), 'jump': load_images('Images/Player', 'jump', 30) } self.image = self.images['idle'][0] self.rect = self.image.get_rect() self.rect.center = (screen_width // 2, screen_height // 2) self.rect.inflate_ip(-20, -20) # Adjust player size to fit tile size self.vel = pygame.math.Vector2(0, 0) self.speed = 5 self.gravity = 0.5 self.jump_power = -10 self.on_ground = False self.jumping = False self.facing_right = True self.frame = 0 self.animation_speed = 0.1 # Adjust animation speed def update(self): keys = pygame.key.get_pressed() # Horizontal movement if keys[pygame.K_LEFT]: self.vel.x = -self.speed self.facing_right = False elif keys[pygame.K_RIGHT]: self.vel.x = self.speed self.facing_right = True else: self.vel.x = 0 # Jumping if keys[pygame.K_UP] and self.on_ground: self.vel.y = self.jump_power self.on_ground = False self.jumping = True # Apply gravity self.vel.y += self.gravity # Update player position self.rect.x += self.vel.x self.rect.y += self.vel.y # Screen if self.rect.left < 0: self.rect.left = 0 if self.rect.right > screen_width: self.rect.right = screen_width if self.rect.top < 0: self.rect.top = 0 if self.rect.bottom > screen_height: self.rect.bottom = screen_height self.on_ground = True self.jumping = False self.vel.y = 0 # Ensure entire player sprite stays within the screen if self.rect.top < 0: self.rect.top = 0 if self.rect.bottom > screen_height: self.rect.bottom = screen_height if self.rect.left < 0: self.rect.left = 0 if self.rect.right > screen_width: self.rect.right = screen_width # Animation handling if self.jumping: self.image = self.images['jump'][0] elif self.vel.x != 0: self.frame = (self.frame + self.animation_speed) % len(self.images['run']) self.image = self.images['run'][int(self.frame)] else: self.frame = (self.frame + self.animation_speed) % len(self.images['idle']) self.image = self.images['idle'][int(self.frame)] # Flip sprite if not self.facing_right: self.image = pygame.transform.flip(self.image, True, False) # Load player images function def load_images(folder, prefix, count): return [pygame.transform.scale(pygame.image.load(os.path.join(folder, f"{prefix} ({i}).png")), (tile_size, tile_size)) for i in range(1, count + 1)] # Main game loop def main(): run = True clock = pygame.time.Clock() player = Player() while run: screen.blit(background_img, (0, 0)) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # Update player player.update() # Draw player screen.blit(player.image, player.rect) pygame.display.update() clock.tick(60) # Adjusted frame rate pygame.quit() if __name__ == "__main__": main()