""" ----------------------------------------- Project: Escape Drake Standard: 91896 (2.7) School: Tauranga Boys' Collage Auther: Hunter Moss Date: May 2024 Python: 3.11.2 ----------------------------------------- """ import pygame from pygame.locals import * 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') # Game Variables tile_size = 50 game_over = False main_menu = True level = 1 max_levels = 3 # Define colours white = 255, 255, 255 # Load images background_img = pygame.image.load('Images/Background.png') # Player variables player_size = tile_size player_x = screen_width // 2 - player_size // 2 player_y = screen_height // 2 - player_size // 2 player_x_speed = 0 player_y_speed = 0 last_direction = "right" # Keep track of the last direction for flipping sprite player_direction = "right" # Keep track of player's movement direction # Load player images def load_player_images(prefix, num_images): return [pygame.transform.scale(pygame.image.load(f'Images/Player/{prefix} ({i}).png'), (player_size, player_size)) for i in range(1, num_images + 1)] idle_imgs = load_player_images("idle", 16) run_imgs = load_player_images("run", 20) jump_imgs = load_player_images("jump", 30) def draw_grid(): for line in range(0, 20): pygame.draw.line(screen, white, (0, line * tile_size), (screen_width, line * tile_size)) pygame.draw.line(screen, white, (line * tile_size, 0), (line * tile_size, screen_height)) def draw_player(x, y): global last_direction, player_direction if player_y < screen_height - player_size: # If player is jumping if player_direction == "left": screen.blit(pygame.transform.flip(jump_imgs[0], True, False), (x, y)) # Use flipped image for left direction else: screen.blit(jump_imgs[0], (x, y)) else: # Player is on the ground if player_x_speed == 0: # Player is idle current_player_imgs = idle_imgs else: # Player is running current_player_imgs = run_imgs if player_x_speed < 0: # Player is moving left last_direction = "left" player_direction = "left" elif player_x_speed > 0: # Player is moving right last_direction = "right" player_direction = "right" if player_direction == "left": # Flip sprite if player is moving left current_player_imgs = [pygame.transform.flip(img, True, False) for img in current_player_imgs] screen.blit(current_player_imgs[0], (x, y)) def move_player(): global player_x, player_y, player_x_speed, player_y_speed keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT]: if keys[pygame.K_LEFT]: player_x_speed = -5 if keys[pygame.K_RIGHT]: player_x_speed = 5 else: player_x_speed = 0 if keys[pygame.K_UP] or keys[pygame.K_SPACE]: if player_y >= screen_height - player_size: # Only jump if player is on the ground player_y_speed = -10 # Apply gravity player_y_speed += 0.5 # Update player position player_x += player_x_speed player_y += player_y_speed # Screen collision if player_x <= 0: player_x = 0 elif player_x >= screen_width - player_size: player_x = screen_width - player_size if player_y <= 0: player_y = 0 player_y_speed = 0 elif player_y >= screen_height - player_size: player_y = screen_height - player_size player_y_speed = 0 # Main game loop run = True clock = pygame.time.Clock() while run: screen.blit(background_img, (0, 0)) draw_grid() move_player() draw_player(player_x, player_y) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.display.update() clock.tick(30) pygame.quit()