""" ----------------------------------------- Project: CyberQuest Standard: 91896 (2.7) School: Tauranga Boys Collage Auther: Gilbert Jepsen Date: May 2024 Python: 3.11.9 ----------------------------------------- """ # Imports import pygame import sys # Constants screen_size = (1201, 1000) color = (255, 255, 255) tile_size = 50 gravity = 0.75 # Initialize pygame pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((screen_size)) pygame.display.set_caption('CyberQuest platformer') # Load images background_image = pygame.image.load('Gilbert/Images/Background.png') background_image = pygame.transform.scale(background_image, screen_size) player_image = pygame.image.load('Gilbert/Player_Images/Idle__000.png') player_image = pygame.transform.scale(player_image, (50, 50)) class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.idle_image = pygame.image.load('Gilbert/Player_Images/Idle__000.png') self.idle_image = pygame.transform.scale(self.idle_image, (50, 50)) self.run_images = [pygame.image.load(f'Gilbert/Player_Images/Run__{i:03}.png') for i in range(10)] self.run_images = [pygame.transform.scale(image, (50, 50)) for image in self.run_images] self.jump_images = [pygame.image.load(f'Gilbert/Player_Images/Jump__{i:03}.png') for i in range(10)] self.jump_images = [pygame.transform.scale(image, (50, 50)) for image in self.jump_images] self.image = self.idle_image self.rect = self.image.get_rect() self.rect.center = (100, screen_size[1] - 130) self.speed = 5 # self.vel_y = 0 self.jumped = False self.last_direction = 'right' self.frame_index = 0 def update(self): # Apply gravity self.vel_y += gravity self.rect.y += self.vel_y # Check for collisions with window borders if self.rect.bottom >= screen_size[1]: self.rect.bottom = screen_size[1] self.vel_y = 0 self.jumped = False # Check for keyboard input keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] or keys[pygame.K_UP]: if not self.jumped: self.vel_y = -15 self.jumped = True self.frame_index = 0 if keys[pygame.K_LEFT]: self.rect.x -= self.speed self.last_direction = 'left' elif keys[pygame.K_RIGHT]: self.rect.x += self.speed self.last_direction = 'right' # Keep the player within the window boundaries self.rect.x = max(0, min(self.rect.x, screen_size[0] - self.rect.width)) # Update animation frames if self.jumped: if self.last_direction == 'left': self.image = pygame.transform.flip(self.jump_images[self.frame_index], True, False) else: self.image = self.jump_images[self.frame_index] self.frame_index += 1 if self.frame_index >= len(self.jump_images): self.frame_index = len(self.jump_images) - 1 elif keys[pygame.K_LEFT]: self.frame_index += 1 if self.frame_index >= len(self.run_images): self.frame_index = 0 self.image = pygame.transform.flip(self.run_images[self.frame_index], True, False) self.last_direction = 'left' elif keys[pygame.K_RIGHT]: self.frame_index += 1 if self.frame_index >= len(self.run_images): self.frame_index = 0 self.image = self.run_images[self.frame_index] self.last_direction = 'right' else: if self.last_direction == 'left': self.image = pygame.transform.flip(self.idle_image, True, False) else: self.image = self.idle_image # Create the player instance player = Player() all_sprites = pygame.sprite.Group() all_sprites.add(player) # Main loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Fill the screen with the color screen.fill(color) # Blit the background image onto the screen screen.blit(background_image, (0, 0)) # Update all sprites all_sprites.update() # Draw all sprites all_sprites.draw(screen) #GRID for x in range(0, screen.get_width(), tile_size): pygame.draw.line(screen, (0, 0, 0), (x, 0), (x, screen.get_height())) for y in range(0, screen.get_height(), tile_size): pygame.draw.line(screen, (0, 0, 0), (0, y), (screen.get_width(), y)) # Update the display pygame.display.update() # Cap the frame rate clock.tick(60) pygame.quit() sys.exit()