""" ----------------------------------------- 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 import pickle import os from os import path # Constants screen_size = (1201, 1000) color = (255, 255, 255) tile_size = 50 gravity = 0.75 # Function to load level data def load_level_data(level_name): level_dir = "Gilbert/Code/Versions" level_path = os.path.join(level_dir, level_name) if os.path.exists(level_path): with open(level_path, 'rb') as file: loaded_data = pickle.load(file) return loaded_data else: print("Level data file not found:", level_path) return None # 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)) block_img = pygame.image.load('Gilbert/Images/Block.png') block_img = pygame.transform.scale(block_img, (tile_size, tile_size)) block2_img = pygame.image.load('Gilbert/Images/Block2.png') block2_img = pygame.transform.scale(block2_img, (tile_size, tile_size)) block3_img = pygame.image.load('Gilbert/Images/Block3.png') block3_img = pygame.transform.scale(block3_img, (tile_size, tile_size)) block4_img = pygame.image.load('Gilbert/Images/Block4.png') block4_img = pygame.transform.scale(block4_img, (tile_size, tile_size)) block5_img = pygame.image.load('Gilbert/Images/Block5.png') block5_img = pygame.transform.scale(block5_img, (tile_size, tile_size)) block6_img = pygame.image.load('Gilbert/Images/Block6.png') block6_img = pygame.transform.scale(block6_img, (tile_size, tile_size)) block7_img = pygame.image.load('Gilbert/Images/Block7.png') block7_img = pygame.transform.scale(block7_img, (tile_size, tile_size)) block8_img = pygame.image.load('Gilbert/Images/Block8.png') block8_img = pygame.transform.scale(block8_img, (tile_size, tile_size)) block9_img = pygame.image.load('Gilbert/Images/Block9.png') block9_img = pygame.transform.scale(block9_img, (tile_size, tile_size)) block10_img = pygame.image.load('Gilbert/Images/Block10.png') block10_img = pygame.transform.scale(block10_img, (tile_size, tile_size)) block11_img = pygame.image.load('Gilbert/Images/Block11.png') block11_img = pygame.transform.scale(block11_img, (tile_size, tile_size)) block12_img = pygame.image.load('Gilbert/Images/Block12.png') block12_img = pygame.transform.scale(block12_img, (tile_size, tile_size)) block13_img = pygame.image.load('Gilbert/Images/Block13.png') block13_img = pygame.transform.scale(block13_img, (tile_size, tile_size)) block14_img = pygame.image.load('Gilbert/Images/Block14.png') block14_img = pygame.transform.scale(block14_img, (tile_size, tile_size)) block15_img = pygame.image.load('Gilbert/Images/Block15.png') block15_img = pygame.transform.scale(block15_img, (tile_size, tile_size)) Acid_img = pygame.image.load('Gilbert/Images/Acid.png') Acid_img = pygame.transform.scale(Acid_img, (tile_size, tile_size)) Acid2_img = pygame.image.load('Gilbert/Images/Acid2.png') Acid2_img = pygame.transform.scale(Acid2_img, (tile_size, tile_size)) # Load lava images for animation flowing_lava_imgs = [ pygame.image.load('Gilbert/Images/Acid.png'), pygame.image.load('Gilbert/Images/Acid2.png') ] flowing_lava_imgs = [pygame.transform.scale(img, (tile_size, tile_size)) for img in flowing_lava_imgs] class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() # Load images 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_x = 0 self.vel_y = 0 self.jumped = False self.last_direction = 'right' self.frame_index = 0 def update(self, level_data): # Apply gravity self.vel_y += gravity self.rect.y += self.vel_y # Check for collisions with tiles on y-axis self.check_collision_y(level_data) # Check for keyboard input keys = pygame.key.get_pressed() if (keys[pygame.K_SPACE] or keys[pygame.K_UP]) and self.on_ground(level_data) and not self.jumped and self.vel_y == 0: self.vel_y = -15 self.jumped = True self.frame_index = 0 if keys[pygame.K_LEFT]: self.vel_x = -self.speed self.last_direction = 'left' elif keys[pygame.K_RIGHT]: self.vel_x = self.speed self.last_direction = 'right' else: self.vel_x = 0 # Update player's position on x-axis self.rect.x += self.vel_x self.check_collision_x(level_data) # Keep the player within the window boundaries self.rect.x = max(0, min(self.rect.x, screen_size[0] - self.rect.width)) self.rect.y = max(0, min(self.rect.y, screen_size[1] - self.rect.height)) # Limit to the bottom of the screen # Update animation frames self.update_animation(keys) def update_animation(self, keys): 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 def check_collision_y(self, level_data): for row in range(len(level_data)): for col in range(len(level_data[0])): tile = level_data[row][col] if tile != 0: tile_rect = pygame.Rect(col * tile_size, row * tile_size, tile_size, tile_size) if self.rect.colliderect(tile_rect): # Collision from above if self.vel_y > 0 and self.rect.bottom > tile_rect.top and self.rect.bottom < tile_rect.bottom: self.rect.bottom = tile_rect.top self.vel_y = 0 self.jumped = False # Collision from below elif self.vel_y < 0 and self.rect.top < tile_rect.bottom and self.rect.top > tile_rect.top: self.rect.top = tile_rect.bottom self.vel_y = 0 def check_collision_x(self, level_data): for row in range(len(level_data)): for col in range(len(level_data[0])): tile = level_data[row][col] if tile != 0: tile_rect = pygame.Rect(col * tile_size, row * tile_size, tile_size, tile_size) if self.rect.colliderect(tile_rect): if self.vel_x > 0: # Moving right self.rect.right = tile_rect.left elif self.vel_x < 0: # Moving left self.rect.left = tile_rect.right def on_ground(self, level_data): # Check if there is a solid tile below the player for col in range(len(level_data[0])): tile = level_data[self.rect.bottom // tile_size][col] if tile != 0: return True return False # Create the player instance player = Player() all_sprites = pygame.sprite.Group() all_sprites.add(player) # Function to draw tiles based on level data def draw_tiles(level_data): for row in range(len(level_data)): for col in range(len(level_data[0])): tile_id = level_data[row][col] if tile_id == 0: continue elif tile_id == 1: screen.blit(block_img, (col * tile_size, row * tile_size)) elif tile_id == 2: screen.blit(block2_img, (col * tile_size, row * tile_size)) elif tile_id == 3: screen.blit(block3_img, (col * tile_size, row * tile_size)) elif tile_id == 4: screen.blit(block4_img, (col * tile_size, row * tile_size)) elif tile_id == 5: screen.blit(block5_img, (col * tile_size, row * tile_size)) elif tile_id == 6: screen.blit(block6_img, (col * tile_size, row * tile_size)) elif tile_id == 7: screen.blit(block7_img, (col * tile_size, row * tile_size)) elif tile_id == 8: screen.blit(block8_img, (col * tile_size, row * tile_size)) elif tile_id == 9: screen.blit(block9_img, (col * tile_size, row * tile_size)) elif tile_id == 10: screen.blit(block10_img, (col * tile_size, row * tile_size)) elif tile_id == 11: screen.blit(block11_img, (col * tile_size, row * tile_size)) elif tile_id == 12: screen.blit(block12_img, (col * tile_size, row * tile_size)) elif tile_id == 13: screen.blit(block13_img, (col * tile_size, row * tile_size)) elif tile_id == 14: screen.blit(block14_img, (col * tile_size, row * tile_size)) elif tile_id == 15: screen.blit(block15_img, (col * tile_size, row * tile_size)) #Sprite elif tile_id == 101: screen.blit(Acid_img, (col * tile_size, row * tile_size)) elif tile_id == 102: screen.blit(Acid2_img, (col * tile_size, row * tile_size)) # Load level data level_name = 'Version-Tile-Collision_data' level_data = load_level_data(level_name) if level_data: print("Level data loaded successfully:", level_data) else: print("Failed to load level data.") # 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)) # Draw tiles draw_tiles(level_data) # Update all sprites all_sprites.update(level_data) # Pass level_data to the update method of Player # Draw all sprites all_sprites.draw(screen) # Draw grid lines 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() # frame rate clock.tick(60) pygame.quit() sys.exit()