Python 3D 立方体交互:使用键盘控制移动和旋转

本文将带您使用 Python 和 Pygame 库创建一个可交互的 3D 立方体,您可以通过键盘控制立方体的移动和旋转。

代码实现

import pygame
import sys

# 初始化 Pygame
pygame.init()

# 设置窗口大小和标题
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("可操作的立方体")

# 定义立方体的顶点坐标
vertices = [
    (-1, 1, 1), (1, 1, 1), (1, -1, 1), (-1, -1, 1),
    (-1, 1, -1), (1, 1, -1), (1, -1, -1), (-1, -1, -1)
]

# 定义立方体的面
faces = [
    (0, 1, 2, 3),   # 前面
    (1, 5, 6, 2),   # 右面
    (5, 4, 7, 6),   # 后面
    (4, 0, 3, 7),   # 左面
    (4, 5, 1, 0),   # 上面
    (3, 2, 6, 7)    # 下面
]

# 定义立方体的初始位置、大小和旋转角度
cube_position = pygame.math.Vector3(0, 0, 0)
cube_scale = 100
cube_rotation = pygame.math.Vector3(0, 0, 0)

# 定义相机的初始位置和旋转角度
camera_position = pygame.math.Vector3(0, 0, -1000)
camera_rotation = pygame.math.Vector3(0, 0, 0)

# 定义移动和旋转的速度
movement_speed = 5
rotation_speed = 0.03

# 渲染立方体
def render_cube():
    for face in faces:
        vertex_list = []
        for index in face:
            vertex = vertices[index]
            # 缩放
            scaled_vertex = pygame.math.Vector3(vertex) * cube_scale
            # 旋转
            rotated_vertex = scaled_vertex.rotate(cube_rotation)
            # 平移
            translated_vertex = rotated_vertex + cube_position
            vertex_list.append(translated_vertex)

        pygame.draw.polygon(screen, (255, 255, 255), vertex_list)

# 主循环
while True:
    # 清屏
    screen.fill((0, 0, 0))

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # 获取键盘输入
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:  # 前移
        cube_position += pygame.math.Vector3(0, 0, movement_speed)
    if keys[pygame.K_s]:  # 后移
        cube_position += pygame.math.Vector3(0, 0, -movement_speed)
    if keys[pygame.K_a]:  # 左移
        cube_position += pygame.math.Vector3(-movement_speed, 0, 0)
    if keys[pygame.K_d]:  # 右移
        cube_position += pygame.math.Vector3(movement_speed, 0, 0)
    if keys[pygame.K_q]:  # 上移
        cube_position += pygame.math.Vector3(0, movement_speed, 0)
    if keys[pygame.K_e]:  # 下移
        cube_position += pygame.math.Vector3(0, -movement_speed, 0)
    if keys[pygame.K_LEFT]:  # 左旋转
        cube_rotation += pygame.math.Vector3(0, -rotation_speed, 0)
    if keys[pygame.K_RIGHT]:  # 右旋转
        cube_rotation += pygame.math.Vector3(0, rotation_speed, 0)
    if keys[pygame.K_UP]:  # 上旋转
        cube_rotation += pygame.math.Vector3(-rotation_speed, 0, 0)
    if keys[pygame.K_DOWN]:  # 下旋转
        cube_rotation += pygame.math.Vector3(rotation_speed, 0, 0)

    # 渲染立方体
    render_cube()

    # 更新屏幕显示
    pygame.display.flip()

代码解读

  1. 初始化 Pygame: 导入 Pygame 库并初始化。
  2. 创建窗口: 设置窗口大小和标题。
  3. 定义立方体:
    • vertices: 定义立方体的八个顶点坐标。
    • faces: 定义立方体的六个面的顶点索引。
  4. 定义初始参数:
    • cube_position: 立方体的初始位置。
    • cube_scale: 立方体的初始大小。
    • cube_rotation: 立方体的初始旋转角度。
    • camera_position: 相机的初始位置。
    • camera_rotation: 相机的初始旋转角度。
  5. 定义移动和旋转速度: 设置键盘控制移动和旋转的速度。
  6. 渲染立方体函数 render_cube():
    • 循环遍历每个面,计算每个顶点在屏幕上的坐标。
    • 使用 pygame.draw.polygon() 绘制每个面。
  7. 主循环:
    • 清空屏幕。
    • 处理事件,包括退出事件。
    • 获取键盘输入,根据按键控制立方体的移动和旋转。
    • 调用 render_cube() 渲染立方体。
    • 更新屏幕显示。

使用方法

运行上述代码后,将会弹出一个窗口,您可以通过键盘上下左右以及 WASD 键来控制立方体的移动和旋转。

  • WASD 键: 控制立方体的前后左右移动。
  • 上下左右键: 控制立方体的旋转。

您也可以通过修改代码中的 cube_positioncube_scalecube_rotation 变量来调整立方体的初始位置、大小和旋转角度。类似地,您可以修改 camera_positioncamera_rotation 变量来改变相机的初始位置和旋转角度,从而改变您观察立方体的视角。

扩展

您可以尝试添加更多功能,例如:

  • 鼠标控制: 使用鼠标控制立方体的旋转和缩放。
  • 灯光效果: 添加灯光效果来增强立方体的视觉效果。
  • 纹理贴图: 使用纹理贴图来装饰立方体的表面。

希望本文能帮助您入门 3D 图形编程,并为您提供一个使用 Python 和 Pygame 创建 3D 交互效果的简单示例。

Python 3D 立方体交互:使用键盘控制移动和旋转

原文地址: https://www.cveoy.top/t/topic/hN3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录