import pygame def level_selector(screen_height, screen_width, screen): # Colors PURP = 130, 50, 70 ACTIVE_COLOR = (0, 255, 0) # Load images level_portal_original = pygame.image.load("assets/Portal.png") level_portal_top = level_portal_original.copy() lobby_bg = pygame.image.load("assets/LobbyBG.png") # Load font font = pygame.font.Font(None, 25) # You can adjust the font size and style here # Create surfaces for text text_surface_level1 = font.render("Level 1", True, PURP) text_surface_level2 = font.render("Level 2", True, PURP) text_surface_level3 = font.render("Level 3", True, PURP) # Define positions for the portals portal_top_position = (screen_width // 2 - level_portal_top.get_width() // 2, 150) portal_bottom_left_position = (screen_width // 3 - level_portal_original.get_width() // 2, screen_height // 2 + 60) portal_bottom_right_position = ( 2 * screen_width // 3 - level_portal_original.get_width() // 2, screen_height // 2 + 60) # Draw text positions text_rect_level1 = text_surface_level1.get_rect(center=(portal_bottom_left_position[0] + level_portal_original.get_width() // 2, portal_bottom_left_position[1] + level_portal_original.get_height() + 15)) text_rect_level2 = text_surface_level2.get_rect(center=(portal_bottom_right_position[0] + level_portal_original.get_width() // 2, portal_bottom_right_position[1] + level_portal_original.get_height() + 15)) text_rect_level3 = text_surface_level3.get_rect(center=(portal_top_position[0] + level_portal_top.get_width() // 2, portal_top_position[1] + level_portal_top.get_height() + 15)) # Load player sprite class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() # Load original images self.original_images = { "up": [pygame.image.load(f"sprites/BaseCharacter/up{num}.png").convert_alpha() for num in range(1, 5)], "down": [pygame.image.load(f"sprites/BaseCharacter/down{num}.png").convert_alpha() for num in range(1, 5)], "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["down"][0] self.rect = self.image.get_rect() self.rect.center = (screen_width // 2, screen_height // 2) self.speed = 2 self.direction = "down" self.standing_frames = { "up": self.images["up"][0], "down": self.images["down"][0], "left": self.images["left"][0], "right": self.images["right"][0] } def update(self, keys): 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" elif keys[pygame.K_UP]: self.rect.y -= self.speed self.direction = "up" elif keys[pygame.K_DOWN]: self.rect.y += self.speed self.direction = "down" else: # If no key is pressed, set direction to last pressed direction # and reset animation index to 0 to show the first frame self.image = self.standing_frames[self.direction] self.index = 0 # Ensure player stays within screen bounds self.rect.x = max(0, min(self.rect.x, screen_width - self.rect.width)) self.rect.y = max(0, min(self.rect.y, screen_height - self.rect.height)) # Animate player self.index += 0.2 if self.index >= len(self.images[self.direction]): self.index = 0 self.image = self.images[self.direction][int(self.index)] # Create player sprite player = Player() # Create sprite group for player all_sprites = pygame.sprite.Group() all_sprites.add(player) # Main game loop running = True clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: return "quit" elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return "display_menu" # Display menu when Escape key is pressed # Draw the background image first screen.blit(lobby_bg, (0, 0)) # Get pressed keys keys = pygame.key.get_pressed() # Update player position and animation based on pressed keys player.update(keys) # Draw portals screen.blit(level_portal_top, portal_top_position) screen.blit(level_portal_original, portal_bottom_left_position) screen.blit(level_portal_original, portal_bottom_right_position) # Draw text onto portals screen.blit(text_surface_level1, text_rect_level1) screen.blit(text_surface_level2, text_rect_level2) screen.blit(text_surface_level3, text_rect_level3) # Check if player overlaps with any portal and if space key is pressed if player.rect.colliderect(pygame.Rect(portal_top_position, level_portal_top.get_size())) and keys[pygame.K_SPACE]: return "level_3" elif player.rect.colliderect(pygame.Rect(portal_bottom_left_position, level_portal_original.get_size())) and keys[pygame.K_SPACE]: return "level_1" elif player.rect.colliderect(pygame.Rect(portal_bottom_right_position, level_portal_original.get_size())) and keys[pygame.K_SPACE]: return "level_2" # Check if player overlaps with any portal for visual indication if player.rect.colliderect(pygame.Rect(portal_top_position, level_portal_top.get_size())): pygame.draw.rect(screen, ACTIVE_COLOR, pygame.Rect(portal_top_position, level_portal_top.get_size()), 5) if player.rect.colliderect(pygame.Rect(portal_bottom_left_position, level_portal_original.get_size())): pygame.draw.rect(screen, ACTIVE_COLOR, pygame.Rect(portal_bottom_left_position, level_portal_original.get_size()), 5) if player.rect.colliderect(pygame.Rect(portal_bottom_right_position, level_portal_original.get_size())): pygame.draw.rect(screen, ACTIVE_COLOR, pygame.Rect(portal_bottom_right_position, level_portal_original.get_size()), 5) # Draw all sprites all_sprites.draw(screen) # Update the display pygame.display.flip() # Cap the frame rate clock.tick(60) # Quit Pygame pygame.quit()