如何添加随机颜色?

发布于 2025-02-02 12:44:22 字数 3249 浏览 1 评论 0原文

现在我有了立方体,下一步要做的就是将每个立方体添加为随机颜色,但是我在每个立方体上都遇到了困难。正如您在代码的输出中看到的那样,立方体的每个面都具有相同的颜色,并且侧面不同。我在代码中尝试了不同的事情,但是有时候,多维数据集都是相同的颜色。

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

我所需的输出:

“在此处输入图像描述”

这是代码:

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()

Now that I have cubes, my next thing to do is to add to each cube a random color, but I am having trouble making it on each cube. As you can see in the output of my code, every face of the cube has the same color, and the sides are different. I have tried different things in my code, but sometimes the cubes are all the same color.

I want to make the cubes have different colors.

My Desired Output:

enter image description here

Here's the code:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2025-02-09 12:44:22

定义一个6种颜色的数组:

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

do 不是为每个顶点设置颜色:

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()

“”

Define an array of 6 colors:

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

Do not set the color for every vertex:

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()

but set the color for every cube:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文