code a 3d game
- User
code a 3d game
Assistant
3D Maze Runner Game with PyGame
Here's a complete 3D maze game with textures, lighting, and a first-person perspective:
import pygame import math import random import sys from pygame.locals import * # Initialize pygame pygame.init() # Screen dimensions SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 HALF_HEIGHT = SCREEN_HEIGHT // 2 # Set up the display screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("3D Maze Runner") # Colors SKY_COLOR = (135, 206, 235) GROUND_COLOR = (34, 139, 34) WALL_COLOR = (139, 69, 19) PLAYER_COLOR = (255, 0, 0) EXIT_COLOR = (0, 255, 0) TEXT_COLOR = (255, 255, 255) MINIMAP_BG = (0, 0, 0, 180) # Game constants FOV = math.pi / 3 # Field of view HALF_FOV = FOV / 2 MAX_DEPTH = 16 MOVE_SPEED = 0.05 ROTATION_SPEED = 0.04 CELL_SIZE = 64 MINIMAP_SCALE = 0.2 # Maze dimensions MAZE_WIDTH = 10 MAZE_HEIGHT = 10 # Generate a maze def generate_maze(width, height): # Create a grid filled with walls (1) maze = [[1 for _ in range(width)] for _ in range(height)] # Carve paths def carve(x, y): maze[y][x] = 0 # Mark as path # Define directions: up, right, down, left directions = [(0, -2), (2, 0), (0, 2), (-2, 0)] random.shuffle(directions) for dx, dy in directions: nx, ny = x + dx, y + dy if 0 <= nx < width and 0 <= ny < height and maze[ny][nx] == 1: maze[y + dy//2][x + dx//2] = 0 # Remove wall between carve(nx, ny) # Start carving from a random point (must be odd coordinates) start_x, start_y = 1, 1 carve(start_x, start_y) # Create an exit exit_x, exit_y = width - 2, height - 2 maze[exit_y][exit_x] = 2 # Mark as exit return maze # Create the maze maze = generate_maze(MAZE_WIDTH, MAZE_HEIGHT) # Player starting position and direction player_x = 1.5 * CELL_SIZE player_y = 1.5 * CELL_SIZE player_angle = 0 # Textures for walls wall_textures = [] for i in range(4): texture = pygame.Surface((64, 64)) for y in range(64): for x in range(64): # Create a wood-like texture pattern if (x // 8 + y // 8) % 2 == 0: color = (139 + random.randint(-20, 20), 69 + random.randint(-20, 20), 19 + random.randint(-20, 20)) else: color = (120 + random.randint(-20, 20), 50 + random.randint(-20, 20), 10 + random.randint(-20, 20)) texture.set_at((x, y), color) wall_textures.append(texture) # Font for text rendering font = pygame.font.SysFont(None, 36) # Game state game_won = False game_time = 0 # Create a clock to control frame rate clock = pygame.time.Clock() # Main game loop running = True while running: # Calculate frame time frame_time = clock.tick(60) / 1000.0 # Only update game time if the game is still running if not game_won: game_time += frame_time # Event handling for event in pygame.event.get(): if event.type == QUIT: running = False elif event.type == KEYDOWN: if event.key == K_ESCAPE: running = False if game_won and event.key == K_r: # Reset game maze = generate_maze(MAZE_WIDTH, MAZE_HEIGHT) player_x = 1.5 * CELL_SIZE player_y = 1.5 * CELL_SIZE player_angle = 0 game_won = False game_time = 0 # Player movement (if game not won) if not game_won: keys = pygame.key.get_pressed() # Rotation if keys[K_LEFT]: player_angle -= ROTATION_SPEED if keys[K_RIGHT]: player_angle += ROTATION_SPEED # Movement move_x, move_y = 0, 0 if keys[K_UP] or keys[K_w]: move_x += math.cos(player_angle) * MOVE_SPEED * CELL_SIZE move_y += math.sin(player_angle) * MOVE_SPEED * CELL_SIZE if keys[K_DOWN] or keys[K_s]: move_x -= math.cos(player_angle) * MOVE_SPEED * CELL_SIZE move_y -= math.sin(player_angle) * MOVE_SPEED * CELL_SIZE if keys[K_a]: move_x += math.cos(player_angle - math.pi/2) * MOVE_SPEED * CELL_SIZE move_y += math.sin(player_angle - math.pi/2) * MOVE_SPEED * CELL_SIZE if keys[K_d]: move_x += math.cos(player_angle + math.pi/2) * MOVE_SPEED * CELL_SIZE move_y += math.sin(player_angle + math.pi/2) * MOVE_SPEED * CELL_SIZE # Collision detection new_x = player_x + move_x new_y = player_y + move_y # Only move if the new position is not inside a wall grid_x, grid_y = int(new_x / CELL_SIZE), int(new_y / CELL_SIZE) if 0 <= grid_x < MAZE_WIDTH and 0 <= grid_y < MAZE_HEIGHT: if maze[grid_y][grid_x] != 1: # Not a wall player_x, player_y = new_x, new_y # Check if player reached the exit exit_x, exit_y = (MAZE_WIDTH - 2) * CELL_SIZE, (MAZE_HEIGHT - 2) * CELL_SIZE if math.sqrt((player_x - exit_x)**2 + (player_y - exit_y)**2) < CELL_SIZE / 2: game_won = True # Clear the screen screen.fill((0, 0, 0)) # Draw sky and ground pygame.draw.rect(screen, SKY_COLOR, (0, 0, SCREEN_WIDTH, HALF_HEIGHT)) pygame.draw.rect(screen, GROUND_COLOR, (0, HALF_HEIGHT, SCREEN_WIDTH, HALF_HEIGHT)) # Raycasting ray_angle = player_angle - HALF_FOV for ray in range(SCREEN_WIDTH): # Cast ray sin_a = math.sin(ray_angle) cos_a = math.cos(ray_angle) # Vertical check vx, vy, v_dist = 0, 0, MAX_DEPTH * CELL_SIZE if cos_a != 0: sign_x = 1 if cos_a > 0 else -1 x_offset = (CELL_SIZE - player_x % CELL_SIZE) if sign_x > 0 else (player_x % CELL_SIZE) for depth in range(MAX_DEPTH): v_depth = (x_offset + depth * CELL_SIZE) / abs(cos_a) vy = player_y + v_depth * sin_a vx = player_x + sign_x * (x_offset + depth * CELL_SIZE) # Check if we hit a wall grid_x, grid_y = int(vx / CELL_SIZE), int(vy / CELL_SIZE) if 0 <= grid_x < MAZE_WIDTH and 0 <= grid_y < MAZE_HEIGHT: if maze[grid_y][grid_x] != 0: v_dist = v_depth break # Horizontal check hx, hy, h_dist = 0, 0, MAX_DEPTH * CELL_SIZE if sin_a != 0: sign_y = 1 if sin_a > 0 else -1 y_offset = (CELL_SIZE - player_y % CELL_SIZE) if sign_y > 0 else (player_y % CELL_SIZE) for depth in range(MAX_DEPTH): h_depth = (y_offset + depth * CELL_SIZE) / abs(sin_a) hx = player_x + h_depth * cos_a hy = player_y + sign_y * (y_offset + depth * CELL_SIZE) # Check if we hit a wall grid_x, grid_y = int(hx / CELL_SIZE), int(hy / CELL_SIZE) if 0 <= grid_x < MAZE_WIDTH and 0 <= grid_y < MAZE_HEIGHT: if maze[grid_y][grid_x] != 0: h_dist = h_depth break # Get the closest wall dist = min(v_dist, h_dist) # Fix fisheye effect dist = dist * math.cos(player_angle - ray_angle) # Calculate wall height wall_height = min(int(CELL_SIZE * SCREEN_HEIGHT / (dist + 0.0001)), SCREEN_HEIGHT * 2) # Draw walls if dist < MAX_DEPTH * CELL_SIZE: # Determine which wall we hit (vertical or horizontal) is_vertical = v_dist < h_dist # Get texture coordinate if is_vertical: tex_x = int(vy % CELL_SIZE) else: tex_x = int(hx % CELL_SIZE) # Select texture based on wall type wall_type = 0 if is_vertical: grid_x, grid_y = int(vx / CELL_SIZE), int(vy / CELL_SIZE) if 0 <= grid_x < MAZE_WIDTH and 0 <= grid_y < MAZE_HEIGHT: wall_type = maze[grid_y][grid_x] else: grid_x, grid_y = int(hx / CELL_SIZE), int(hy / CELL_SIZE) if 0 <= grid_x < MAZE_WIDTH and 0 <= grid_y < MAZE_HEIGHT: wall_type = maze[grid_y][grid_x] # Get texture slice texture = wall_textures[wall_type - 1] if wall_type > 0 else wall_textures[0] tex_slice = pygame.transform.scale( texture.subsurface(tex_x, 0, 1, CELL_SIZE), (1, wall_height) ) # Apply lighting based on distance brightness = max(100, 255 - dist * 1.5) tex_slice.fill((brightness, brightness, brightness), special_flags=pygame.BLEND_RGB_MULT) # Draw the wall slice screen.blit(tex_slice, (ray, HALF_HEIGHT - wall_height // 2)) # Move to next ray ray_angle += FOV / SCREEN_WIDTH # Draw minimap minimap_size = int(min(SCREEN_WIDTH, SCREEN_HEIGHT) * MINIMAP_SCALE) minimap_x = SCREEN_WIDTH - minimap_size - 10 minimap_y = 10 # Draw minimap background minimap_surface = pygame.Surface((minimap_size, minimap_size), pygame.SRCALPHA) minimap_surface.fill(MINIMAP_BG) screen.blit(minimap_surface, (minimap_x, minimap_y)) # Draw maze on minimap cell_size = minimap_size / max(MAZE_WIDTH, MAZE_HEIGHT) for y in range(MAZE_HEIGHT): for x in range(MAZE_WIDTH): rect = pygame.Rect( minimap_x + x * cell_size, minimap_y + y * cell_size, cell_size, cell_size ) if maze[y][x] == 1: # Wall pygame.draw.rect(screen, WALL_COLOR, rect) elif maze[y][x] == 2: # Exit pygame.draw.rect(screen, EXIT_COLOR, rect) else: # Path pygame.draw.rect(screen, (100, 100, 100), rect, 1) # Draw player on minimap player_minimap_x = minimap_x + (player_x / CELL_SIZE) * cell_size player_minimap_y = minimap_y + (player_y / CELL_SIZE) * cell_size pygame.draw.circle(screen, PLAYER_COLOR, (int(player_minimap_x), int(player_minimap_y)), int(cell_size / 3)) # Draw player direction direction_x = player_minimap_x + math.cos(player_angle) * cell_size direction_y = player_minimap_y + math.sin(player_angle) * cell_size pygame.draw.line(screen, PLAYER_COLOR, (player_minimap_x, player_minimap_y), (direction_x, direction_y), 2) # Draw UI # Draw compass compass_radius = 30 compass_x = SCREEN_WIDTH // 2 compass_y = SCREEN_HEIGHT - compass_radius - 10 pygame.draw.circle(screen, (50, 50, 50), (compass_x, compass_y), compass_radius, 2) # Draw compass needle needle_x = compass_x + math.cos(player_angle) * (compass_radius - 5) needle_y = compass_y + math.sin(player_angle) * (compass_radius - 5) pygame.draw.line(screen, PLAYER_COLOR, (compass_x, compass_y), (needle_x, needle_y), 3) # Draw compass directions n_x = compass_x + math.cos(0) * (compass_radius - 15) n_y = compass_y + math.sin(0) * (compass_radius - 15) text = font.render("N", True, TEXT_COLOR) screen.blit(text, (n_x - text.get_width()//2, n_y - text.get_height()//2)) # Draw time time_text = font.render(f"Time: {game_time:.1f}s", True, TEXT_COLOR) screen.blit(time_text, (10, 10)) # Draw instructions controls = font.render("Arrow Keys/WASD: Move | Mouse: Look | ESC: Quit", True, TEXT_COLOR) screen.blit(controls, (SCREEN_WIDTH//2 - controls.get_width()//2, SCREEN_HEIGHT - 30)) # Draw win message if game_won: win_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA) win_surface.fill((0, 0, 0, 180)) screen.blit(win_surface, (0, 0)) win_text = font.render("Congratulations! You escaped the maze!", True, (255, 215, 0)) time_text = font.render(f"Your time: {game_time:.1f} seconds", True, (255, 215, 0)) restart_text = font.render("Press R to restart", True, (255, 255, 255)) screen.blit(win_text, (SCREEN_WIDTH//2 - win_text.get_width()//2, SCREEN_HEIGHT//2 - 60)) screen.blit(time_text, (SCREEN_WIDTH//2 - time_text.get_width()//2, SCREEN_HEIGHT//2)) screen.blit(restart_text, (SCREEN_WIDTH//2 - restart_text.get_width()//2, SCREEN_HEIGHT//2 + 60)) # Update the display pygame.display.flip() # Quit pygame pygame.quit() sys.exit()How to Play
- Use the Arrow Keys or WASD to move through the maze
- Press ESC to quit the game
- Find the green exit in the maze to win
- After winning, press R to restart the game
Features
- 3D Maze Environment: First-person perspective with textured walls
- Procedural Maze Generation: Each game creates a new random maze
- Minimap: Shows your position and the maze layout
- Compass: Helps with orientation
- Win Condition: Reach the green exit to win
- Restart Functionality: Play again after winning
- Time Tracking: See how fast you complete the maze
The game uses raycasting to create a pseudo-3D environment similar to early first-person games. The textures and lighting create depth and atmosphere, while the minimap and compass help with navigation.
Enjoy exploring the maze!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.