import pygame def level_selector(screen_height, screen_width, screen, level_end): # Colors PURP = 130, 50, 70 ACTIVE_COLOR = (0, 255, 0) INACTIVE_COLOR = (255, 0, 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 font2 = pygame.font.Font(None, 20) # Choose your font and size # 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) text_surface = font2.render("LEVEL " + str(level_end) + " UNLOCKED", True, (255, 255, 255)) # Render text onto surface # 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 = text_surface.get_rect(center=(screen_width // 2, 425)) # Position the text 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 soldier sprite class Soldier(pygame.sprite.Sprite): def __init__(self): super().__init__() # Load original images self.original_images = { "up": [pygame.image.load(f"img/player/Up/up{num}.png").convert_alpha() for num in range(1, 5)], "down": [pygame.image.load(f"img/player/Down/down{num}.png").convert_alpha() for num in range(1, 5)], "left": [pygame.image.load(f"img/player/Left/left{num}.png").convert_alpha() for num in range(1, 5)], "right": [pygame.image.load(f"img/player/Right/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 soldier 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 soldier 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 soldier sprite soldier = Soldier() # Create sprite group for soldier all_sprites = pygame.sprite.Group() all_sprites.add(soldier) # 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 "title_screen" # Display menu when Escape key is pressed # Draw the background image first screen.blit(lobby_bg, (0, 0)) # Blit the text onto the screen screen.blit(text_surface, text_rect) # Get pressed keys keys = pygame.key.get_pressed() # Update soldier position and animation based on pressed keys soldier.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 soldier overlaps with any portal and if space key is pressed if level_end >= 3: if soldier.rect.colliderect(pygame.Rect(portal_top_position, level_portal_top.get_size())) and keys[pygame.K_SPACE]: return "level_3" if level_end >= 1: if soldier.rect.colliderect(pygame.Rect(portal_bottom_left_position, level_portal_original.get_size())) and keys[pygame.K_SPACE]: return "level_1" if level_end >= 2: if soldier.rect.colliderect(pygame.Rect(portal_bottom_right_position, level_portal_original.get_size())) and keys[pygame.K_SPACE]: return "level_2" if level_end == 4: return "level_4" # Check if soldier overlaps with any portal for visual indication if soldier.rect.colliderect(pygame.Rect(portal_top_position, level_portal_top.get_size())) and level_end >= 3: pygame.draw.rect(screen, ACTIVE_COLOR, pygame.Rect(portal_top_position, level_portal_top.get_size()), 5) elif soldier.rect.colliderect(pygame.Rect(portal_top_position, level_portal_top.get_size())): pygame.draw.rect(screen, INACTIVE_COLOR, pygame.Rect(portal_top_position, level_portal_top.get_size()), 5) if soldier.rect.colliderect(pygame.Rect(portal_bottom_left_position, level_portal_original.get_size())) and level_end >= 1: pygame.draw.rect(screen, ACTIVE_COLOR, pygame.Rect(portal_bottom_left_position, level_portal_original.get_size()), 5) elif soldier.rect.colliderect(pygame.Rect(portal_bottom_left_position, level_portal_original.get_size())): pygame.draw.rect(screen, INACTIVE_COLOR, pygame.Rect(portal_bottom_left_position, level_portal_original.get_size()), 5) if soldier.rect.colliderect(pygame.Rect(portal_bottom_right_position, level_portal_original.get_size())) and level_end >= 2: pygame.draw.rect(screen, ACTIVE_COLOR, pygame.Rect(portal_bottom_right_position, level_portal_original.get_size()), 5) elif soldier.rect.colliderect(pygame.Rect(portal_bottom_right_position, level_portal_original.get_size())): pygame.draw.rect(screen, INACTIVE_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()