找回密码
 中文实名注册
搜索
楼主: 吕泽

老奶奶摔倒了,你会······

[复制链接]

4

主题

3

回帖

384

积分

中级会员

积分
384
发表于 2026-1-25 15:39:26 | 显示全部楼层
import pygame
import math
import random
from pygame.locals import *

# 初始化Pygame
pygame.init()

# 游戏常量
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
FPS = 60

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 50, 50)
GREEN = (50, 255, 100)
BLUE = (50, 150, 255)
YELLOW = (255, 255, 50)
PURPLE = (180, 80, 220)
GRAY = (100, 100, 100)
DARK_GRAY = (50, 50, 50)
WALL_COLOR = (70, 70, 100)
FLOOR_COLOR = (30, 30, 50)
CEILING_COLOR = (20, 20, 40)

class Player:
    def __init__(self, x, y, angle):
        self.x = x
        self.y = y
        self.angle = angle
        self.speed = 0.05
        self.rotation_speed = 0.04
        self.move_direction = [0, 0]  # x, y方向移动
        self.rotation_direction = 0   # 旋转方向
        
    def update(self, maze):
        # 处理旋转
        self.angle += self.rotation_direction * self.rotation_speed
        
        # 处理移动
        if self.move_direction != [0, 0]:
            # 归一化移动向量
            length = math.sqrt(self.move_direction[0]**2 + self.move_direction[1]**2)
            dx = self.move_direction[0] / length
            dy = self.move_direction[1] / length
            
            # 计算新位置
            new_x = self.x + dx * self.speed
            new_y = self.y + dy * self.speed
            
            # 碰撞检测
            if not maze.is_wall(int(new_x), int(new_y)):
                self.x = new_x
                self.y = new_y

class Maze:
    def __init__(self, size):
        self.size = size
        self.grid = [[1 for _ in range(size)] for _ in range(size)]
        self.generate_maze()
        
    def generate_maze(self):
        # 使用递归回溯算法生成迷宫
        stack = []
        visited = [[False for _ in range(self.size)] for _ in range(self.size)]
        
        # 起点
        start_x, start_y = 1, 1
        self.grid[start_y][start_x] = 0
        visited[start_y][start_x] = True
        stack.append((start_x, start_y))
        
        # 方向数组 (dx, dy)
        directions = [(0, -2), (2, 0), (0, 2), (-2, 0)]
        
        while stack:
            current_x, current_y = stack[-1]
            
            # 获取未访问的邻居
            neighbors = []
            for dx, dy in directions:
                nx, ny = current_x + dx, current_y + dy
                if 0 < nx < self.size-1 and 0 < ny < self.size-1 and not visited[ny][nx]:
                    neighbors.append((nx, ny, dx//2, dy//2))
            
            if neighbors:
                # 随机选择一个邻居
                nx, ny, wx, wy = random.choice(neighbors)
                # 移除墙壁
                self.grid[current_y + wy][current_x + wx] = 0
                self.grid[ny][nx] = 0
                visited[ny][nx] = True
                stack.append((nx, ny))
            else:
                stack.pop()
               
        # 设置终点
        self.grid[self.size-2][self.size-2] = 0
   
    def is_wall(self, x, y):
        if x < 0 or x >= self.size or y < 0 or y >= self.size:
            return True
        return self.grid[y][x] == 1
   
    def is_exit(self, x, y):
        return int(x) == self.size-2 and int(y) == self.size-2

class Raycaster:
    def __init__(self, screen_width, screen_height):
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.fov = math.pi / 3  # 视野角度
        self.max_depth = 20
        
    def cast_rays(self, player, maze, screen):
        # 绘制天花板和地板
        pygame.draw.rect(screen, CEILING_COLOR, (0, 0, self.screen_width, self.screen_height//2))
        pygame.draw.rect(screen, FLOOR_COLOR, (0, self.screen_height//2, self.screen_width, self.screen_height//2))
        
        # 投射光线
        for x in range(self.screen_width):
            # 计算光线角度
            ray_angle = player.angle - self.fov/2 + (x / self.screen_width) * self.fov
            distance = 0
            hit_wall = False
            
            # 光线方向向量
            eye_x = math.cos(ray_angle)
            eye_y = math.sin(ray_angle)
            
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表