Python功能制作之3D方块

介绍

用python写一个黑窗口,窗口里面有一个白色的3D方块,左键按下后移动可以旋转以各个视角来看方块。

当然有需要的话,可以自己在代码中去更改颜色,直接通过RBG的参数进行更改即可。

做了两个函数:init[初始化]draw_cube[绘制方块]。

如果需要旋转,将清空缓冲上面的 **# glRotatef(1, 1, 1, 1)**取消注释即可。

这个函数的作用主要是刷新的帧率,比如填写120,就可以感觉到游戏里面卡顿的感觉了

需要安装的库

在终端执行:

pip install pygame

pip install PyOpenGL

源码:

python 复制代码
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *

# 初始化Pygame和OpenGL
def init():
    # 初始化Pygame库
    pygame.init()
    # 创建窗口800*600
    display = (800, 600)
    # 设置显示模式为OpenGl,并创建双缓冲的窗口对象
    pygame.display.set_mode(display, pygame.DOUBLEBUF | pygame.OPENGL)
    # 设置透视投影 45是视角的夹角,(display[0] / display[1])是窗口的宽高比,0.1是离裁剪的最近距离,50是裁剪的最远距离
    gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
    # 是xyz轴的平移量,z轴平移5
    glTranslatef(0.0, 0.0, -5)

# 绘制白色的3D正方体
def draw_cube():
    glBegin(GL_QUADS)
    # 设置颜色RGB
    glColor3f(1.0, 1.0, 1.0)
    """设置正方体的6个面的点"""
    glVertex3f(1.0, -1.0, -1.0)
    glVertex3f(1.0, 1.0, -1.0)
    glVertex3f(-1.0, 1.0, -1.0)
    glVertex3f(-1.0, -1.0, -1.0)

    glVertex3f(1.0, -1.0, 1.0)
    glVertex3f(1.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, 1.0)
    glVertex3f(-1.0, -1.0, 1.0)

    glVertex3f(1.0, -1.0, -1.0)
    glVertex3f(1.0, 1.0, -1.0)
    glVertex3f(1.0, 1.0, 1.0)
    glVertex3f(1.0, -1.0, 1.0)

    glVertex3f(-1.0, -1.0, -1.0)
    glVertex3f(-1.0, 1.0, -1.0)
    glVertex3f(-1.0, 1.0, 1.0)
    glVertex3f(-1.0, -1.0, 1.0)

    glVertex3f(1.0, 1.0, -1.0)
    glVertex3f(1.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, -1.0)

    glVertex3f(1.0, -1.0, -1.0)
    glVertex3f(1.0, -1.0, 1.0)
    glVertex3f(-1.0, -1.0, 1.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glEnd()

# 主循环
def main():
    init()
    running = True
    rotate = False
    last_mouse_pos = (0, 0)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                rotate = True
                last_mouse_pos = pygame.mouse.get_pos()
            elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                rotate = False

        # glRotatef(1, 1, 1, 1)  # 每帧旋转1度
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # 清空银色缓冲去和深度缓冲去,准备下一帧渲染
        draw_cube() # 重新绘制
        pygame.display.flip() # 渲染结果显示屏幕
        pygame.time.wait(10) # 等待10毫秒,控制每帧的渲染速度,通过参数调整帧率-->数值越大,越有游戏卡了的感觉

        if rotate:
            cur_mouse_pos = pygame.mouse.get_pos()
            diff_x = cur_mouse_pos[0] - last_mouse_pos[0]
            diff_y = cur_mouse_pos[1] - last_mouse_pos[1]
            glRotatef(diff_y * 0.1, 1, 0, 0)  # 根据鼠标纵向移动旋转y轴
            glRotatef(diff_x * 0.1, 0, 1, 0)  # 根据鼠标横向移动旋转x轴
            last_mouse_pos = cur_mouse_pos

    pygame.quit()

if __name__ == '__main__':
    main()

效果:

相关推荐
费弗里25 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(3)
python·dash
dme.41 分钟前
Javascript之DOM操作
开发语言·javascript·爬虫·python·ecmascript
加油吧zkf1 小时前
AI大模型如何重塑软件开发流程?——结合目标检测的深度实践与代码示例
开发语言·图像处理·人工智能·python·yolo
t_hj1 小时前
python规划
python
czhc11400756631 小时前
Linux 76 rsync
linux·运维·python
悠悠小茉莉2 小时前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
m0_625686552 小时前
day53
python
Real_man2 小时前
告别 requirements.txt,拥抱 pyproject.toml和uv的现代Python工作流
python
站大爷IP3 小时前
Python文件操作的"保险箱":with语句深度实战指南
python
运器1233 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程