python - 如何添加随机颜色?

标签 python opengl pygame pyopengl

现在我有了立方体,我接下来要做的就是向每个立方体添加随机颜色,但我在每个立方体上制作它时遇到困难。正如您在我的代码输出中看到的,立方体的每个面都具有相同的颜色,而侧面不同。我在代码中尝试了不同的方法,但有时立方体的颜色都是相同的。

我想让立方体有不同的颜色。

我想要的输出:

enter image description here

代码如下:

import pygame
import random
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = ((0.5, -0.5, -0.5), (0.5, 0.5, -0.5), (-0.5, 0.5, -0.5), (-0.5, -0.5, -0.5),
             (0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, 0.5))
edges = ((0,1), (0,3), (0,4), (2,1),(2,3), (2,7), (6,3), (6,4),(6,7), (5,1), (5,4), (5,7))

surfaces = (
    (0,1,2,3),
    (3,2,7,6),
    (6,7,5,4),
    (4,5,1,0),
    (1,5,7,2),
    (4,0,3,6)
    )

colors = (
    (1,0,0),
    (0,1,0),
    (0,0,1),
    (0,1,0),
    (1,1,1),
    (0,1,1),
    (1,0,0),
    (0,1,0),
    (0,0,1),
    (1,0,0),
    (1,1,1),
    (0,1,1),
    )

def Cube():
    glBegin(GL_QUADS)
    x = 0
    for surface in surfaces:
        x+=1
        for vertex in surface:
            glColor3fv(colors[x])
            glVertex3fv(verticies[vertex])
    glEnd()
    
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()
    
def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(60, (display[0]/display[1]), 0.1, 500)
    button_down = False
    glEnable(GL_DEPTH_TEST)

    glMatrixMode(GL_MODELVIEW)  
    modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
    
    glTranslatef(0.0,0.0, -5)

    while True:
        glPushMatrix()
        glLoadIdentity()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    glTranslatef(-1, 0, 0)
                if event.key == pygame.K_RIGHT:
                    glTranslatef(1, 0, 0)
                if event.key == pygame.K_UP:
                    glTranslatef(0, 1, 0)
                if event.key == pygame.K_DOWN:
                    glTranslatef(0, -1, 0)
            if event.type == pygame.MOUSEMOTION:
                if button_down == True:
                    glRotatef(event.rel[1], 1, 0, 0)
                    glRotatef(event.rel[0], 0, 1, 0)

        for event in pygame.mouse.get_pressed():
            if pygame.mouse.get_pressed()[0] == 1:
                button_down = True
            elif pygame.mouse.get_pressed()[0] == 0:
                button_down = False

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        glMultMatrixf(modelMatrix)
        modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

        glLoadIdentity()
        glTranslatef(0, 0, -5)
        glMultMatrixf(modelMatrix)
        
        for i in range(5):
            glPushMatrix()
            glTranslate(-2+i, 0, 0)
            Cube()
            glPopMatrix()
        
        glPopMatrix()        
        pygame.display.flip()
        pygame.time.wait(10)

main()

最佳答案

定义 6 种颜色的数组:

colors = [(1,0,0), (0,1,0), (0,0,1), (1,1,0), (1,0,1), (0,1,1)]

不要不要为每个顶点设置颜色:

def Cube():
    glBegin(GL_QUADS)
    x = 0
    for surface in surfaces:
        x+=1
        for vertex in surface:
            # glColor3fv(colors[x])           <--- DELTETE
            glVertex3fv(verticies[vertex])
    glEnd()

但为每个立方体设置颜色:

for i in range(5):
    glPushMatrix()
    glTranslate(-2+i, 0, 0)
    glColor3fv(colors[i])                   # <--- INSERT
    Cube()
    glPopMatrix()

关于python - 如何添加随机颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72414831/

相关文章:

c++ - 将 OpenGL 转换为 PNG

java - 重新创建数组后的 NPE

python - 检测鼠标是否离开 Pygame 窗口

python - 为什么我的pygame声音文件无法播放?

python - 如何让 sash_place() 在启动时工作?

python - 确定哪个 Tkinter 小部件位于顶部(或可见)

c++ - 使用复杂的显示功能会阻止 glutKeyboardFunc 工作

python - 带有 Python 绑定(bind)的 C lib,两者都想要渲染

python - 扩展 NLP 实体提取

python - Django __init__ 查询集