OpenGL(Python)使用变量更改多维数据集的顶点

时间:2018-09-09 12:36:12

标签: python opengl 3d pygame

我在这里有一个小问题。我想用多个立方体制成一个“蛇形”。身体也应朝同一方向移动。我能够移动一个立方体。但是我不能在一个单元上制作多个立方体。所以我的想法是用变量定义坐标,这些变量会改变它们的值-以及坐标。首先,我尝试以变量开头设置坐标或顶点,但不适用于不同的多维数据集(以后也应使用“ n”实体)。如果滚动到“ def testcube():”,则我尝试了构建多维数据集的其他版本。也许有人知道如何用这种方式对立方体或顶点进行编程。因为在我的情况下,我得到一个值Error(“预期的12个字节的数组,得到96个字节的数组”)。我知道我的代码不是很好,但是我是编程人员的新手。

感谢您的帮助。如果您有任何疑问,请随时提问。

    import pygame
from pygame.locals import *

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

import random


xr = 0
yr = 0
zr = 0

bodies = 2


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



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

snakevertices = (
    (2, 0, 0),
    (2, 2, 0),
    (0, 2, 0),
    (0, 0, 0),
    (2, 0, 2),
    (2, 2, 2),
    (0, 0, 2),
    (0, 2, 2),
    )


snakebodies_vertices = (
    (4, 0, 0),
    (4, 4, 0),
    (0, 4, 0),
    (0, 0, 0),
    (4, 0, 4),
    (4, 4, 4),
    (0, 0, 4),
    (0, 4, 4),
    )

vertices_2 = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1),
    )

vertices_3 = (
    (3, -1, -1),
    (3, 1, -1),
    (1, 1, -1),
    (1, -1, -1),
    (3, -1, 1),
    (3, 1, 1),
    (1, -1, 1),
    (1, 1, 1),
    )



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

snakecolors = (
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    (0.1,1,0.1),
    )

littlecolors = (
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    (1,0.2,0),
    )


ground_vertices = (
    (-100, -1.1, 20),
    (100, -1.1, 20),
    (-100, -1.1, -300),
    (100, -1.1, -300),
    )

back_vertices = (
    (-100,-100 , 5),
    (100,100 , 5),
    (-100,-100 , 5),
    (100,100 , 5),
    )

def ground():
    glBegin(GL_QUADS)
    for vertex in ground_vertices:
        glColor3fv((0,0.5,0.5))
        glVertex3fv(vertex)

    glEnd()







def setsnakebodies_vertices(snakevertices_body):
    new_snakebodies = []
    x_snake_bodies = bodies + 1
    y_snake_bodies = bodies + 1
    z_snake_bodies = bodies + 1
    for snakevert in snakevertices:
        new_snakevert = []

        new_snakex = snakevert[0] + x_snake_bodies
        new_snakey = snakevert[1] + y_snake_bodies
        new_snakez = snakevert[2] + z_snake_bodies

        new_snakevert.append(new_snakex)
        new_snakevert.append(new_snakey)
        new_snakevert.append(new_snakez)

        new_snakebodies.append(new_snakevert)

    return new_snakebodies


def set_snakevertices(max_snakedistance):

    x_snake_change = 1 + xr#random.randrange(-1,10)
    y_snake_change = 1 + yr#random.randrange(-5,5)
    z_snake_change = 1 + zr#random.randrange(-15,-5)
    new_snakevertices = []


    for snakevert in snakevertices:
        new_snakevert = []

        new_snakex = snakevert[0] + x_snake_change
        new_snakey = snakevert[1] + y_snake_change
        new_snakez = snakevert[2] + z_snake_change

        new_snakevert.append(new_snakex)
        new_snakevert.append(new_snakey)
        new_snakevert.append(new_snakez)

        new_snakevertices.append(new_snakevert)

    return new_snakevertices


def set_snakevertices_2(max_snakedistance):
    x_snake_change = 1 + xr#random.randrange(-1,10)
    y_snake_change = 1 + yr#random.randrange(-5,5)
    z_snake_change = 1 + zr#random.randrange(-15,-5)
    new_snakevertices_2 =  []
    for snakevert in vertices_2:
        new_snakevert = []

        new_snakex = snakevert[0] + x_snake_change
        new_snakey = snakevert[1] + y_snake_change
        new_snakez = snakevert[2] + z_snake_change

        new_snakevert.append(new_snakex)
        new_snakevert.append(new_snakey)
        new_snakevert.append(new_snakez)

        new_snakevertices_2.append(new_snakevert)

    return new_snakevertices_2

    """new_snakevertices_3 = []
    for snakevert in vertices_3:
        new_snakevert = []

        new_snakex = snakevert[0] + x_snake_change
        new_snakey = snakevert[1] + y_snake_change
        new_snakez = snakevert[2] + z_snake_change

        new_snakevert.append(new_snakex)
        new_snakevert.append(new_snakey)
        new_snakevert.append(new_snakez)

        new_snakevertices.append(new_snakevert)

    return new_snakevertices"""


def set_vertices(max_distance):
    x_value_change = random.randrange(-1,10)
    y_value_change = random.randrange(-5,5)
    z_value_change = random.randrange(-15,-5)
    new_vertices = []

    for vert in vertices:
        new_vert = []

        new_x = vert[0] + x_value_change
        new_y = vert[1] + y_value_change
        new_z = vert[2] + z_value_change

        new_vert.append(new_x)
        new_vert.append(new_y)
        new_vert.append(new_z)

        new_vertices.append(new_vert)

    return new_vertices





def Cube(vertices):
    glBegin(GL_QUADS)
    for surface in surfaces:
        x = 0
        for vertex in surface:
            x+=1
            glColor3fv(littlecolors[x])
            glVertex3fv(vertices[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
            glColor3fv((0,0,0))
    glEnd()


def Snakebodies(snakebodies_vertices):
    glBegin(GL_QUADS)
    for surface in surfaces:

        for vertex in surface:

            glColor3fv((0,0,1))
            glVertex3fv(snakebodies_vertices[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in littleedges:
        for vertex in edge:
            glColor3fv((0,0,0))
            glVertex3fv(snakebodies_vertices[vertex])

    glEnd()


def Snake(snakevertices):
    glBegin(GL_QUADS)
    for surface in surfaces:

        for vertex in surface:

            glColor3fv((0,1,0))
            glVertex3fv(snakevertices[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in littleedges:
        for vertex in edge:
            glColor3fv((0,0,0))
            glVertex3fv(snakevertices[vertex])

    glEnd()

    glBegin(GL_QUADS)                                       
    for surface in surfaces: 

        for vertex in surface:

            glColor3fv((0,1,0))
            glVertex3fv(vertices_2[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glColor3fv((0,0,0))
            glVertex3fv(vertices_2[vertex])

    glEnd()

    glBegin(GL_QUADS)                                       
    for surface in surfaces: 

        for vertex in surface:

            glColor3fv((0,1,0))
            glVertex3fv(vertices_3[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glColor3fv((0,0,0))
            glVertex3fv(vertices_3[vertex])

    glEnd()




def testcube():
    glBegin(GL_QUADS)                                   
    for surface in surfaces:
        for vertex in surface:
            glColor3fv((1,1,1))
            glVertex3fv((
                (1, -1, -1),
                (1, 1, -1),
                (-1, 1, -1),
                (-1, -1, -1),
                (1, -1, 1),
                (1, 1, 1),
                (-1, -1, 1),
                (-1, 1, 1),
                ))
    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glColor3fv((0,0,0))
            glVertex3f(
                (1, -1, -1),
                (1, 1, -1),
                (-1, 1, -1),
                (-1, -1, -1),
                (1, -1, 1),
                (1, 1, 1),
                (-1, -1, 1),
                (-1, 1, 1),
                )

    glEnd()

def bodiesadd(anzahl):
    global bodies
    bodies = bodies + anzahl
    print(bodies)
def main():
    global bodies
    global xr
    global yr
    global zr
    dirchange = ''
    richtung = 'Back'

    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 100.0)


    x_koor = 0
    y_koor = 0
    glTranslatef(x_koor, y_koor, -5)

    glRotatef(25, 0, 50, 0)
    object_passed = False


    max_distance = 100
    cube_dict = {}
    for x in range(1):
        cube_dict[x] = set_vertices(max_distance)


    max_snakedistance = 100
    snake_dict = {}
    for x in range(bodies):                                         
        snake_dict[x] = set_snakevertices(max_snakedistance)
    for x in range(bodies):                                         
        snake_dict[x] = set_snakevertices_2(max_snakedistance)


    snakevertices_body = 100
    bodies_dict = {}
    for x in range(bodies):
        bodies_dict[x] = setsnakebodies_vertices(snakevertices_body)

    while not object_passed:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:

                #neuer platz für essen
                if event.key == pygame.K_n:
                    for x in range(1):
                        cube_dict[x] = set_vertices(max_distance)
                if event.key == pygame.K_z:  
                    bodiesadd(1)



                #schlange steureung
                if event.key == pygame.K_a:
                    dirchange = 'Left' #xr += -1

                if event.key == pygame.K_d:
                    dirchange = 'Right' #xr += 1

                if event.key == pygame.K_w:
                    dirchange = 'Up'#yr += -1

                if event.key == pygame.K_s:
                    dirchange = 'Down'#yr += 1

                if event.key == pygame.K_o:
                    dirchange = 'Back'#zr += -1

                if event.key == pygame.K_l:
                    dirchange = 'Forward'#zr += 1




                if event.key == pygame.K_m:

                    for x in range(bodies):
                        bodies_dict[x] = setsnakebodies_vertices(snakevertices)

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    glTranslatef(0,0,1.0)

                if event.button == 5:
                    glTranslatef(0,0,-1.0)

        if dirchange == 'Left' and richtung != 'Right':
            richtung = dirchange
        if dirchange == 'Right' and richtung != 'Left':
            richtung = dirchange
        if dirchange == 'Up' and richtung != 'Down':
            richtung = dirchange
        if dirchange == 'Down' and richtung != 'Up':
            richtung = dirchange
        if dirchange == 'Back' and richtung != 'Forward':
            richtung = dirchange
        if dirchange == 'Forward' and richtung != 'Back':
            richtung = dirchange

        if richtung == 'Left':
            xr += -0.1
            for x in range(bodies):
                snake_dict[x] = set_snakevertices(max_snakedistance)

            for x in range(bodies):                                          
                snake_dict[x] = set_snakevertices_2(max_snakedistance)
        if richtung == 'Right':
            xr += 0.1
            for x in range(bodies):
                snake_dict[x] = set_snakevertices(max_snakedistance)
            for x in range(bodies):                                         
                snake_dict[x] = set_snakevertices_2(max_snakedistance)
        if richtung == 'Up':
            yr += 0.1
            for x in range(bodies):
                snake_dict[x] = set_snakevertices(max_snakedistance)
            for x in range(bodies):                                         
                snake_dict[x] = set_snakevertices_2(max_snakedistance)
        if richtung == 'Down':
            yr += -0.1
            for x in range(bodies):
                snake_dict[x] = set_snakevertices(max_snakedistance)
            for x in range(bodies):                                          
                snake_dict[x] = set_snakevertices_2(max_snakedistance)
        if richtung == 'Back':
            zr += -0.1
            for x in range(bodies):
                snake_dict[x] = set_snakevertices(max_snakedistance)
            for x in range(bodies):                                         
                snake_dict[x] = set_snakevertices_2(max_snakedistance)
        if richtung == 'Forward':
            zr += 0.1
            for x in range(bodies):
                snake_dict[x] = set_snakevertices(max_snakedistance)
            for x in range(bodies):                                         
                snake_dict[x] = set_snakevertices_2(max_snakedistance)









        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glTranslatef(x_koor,y_koor,0)
        ground()
        for each_cube in cube_dict:
            Cube(cube_dict[each_cube])
        for each_Snake in snake_dict:
            Snake(snake_dict[each_Snake])
        for each_body in bodies_dict:
            Snakebodies(bodies_dict[each_body])
        pygame.display.flip()
        pygame.time.wait(1)

main()
pygame.quit()
quit()

0 个答案:

没有答案