不能只移动一个物体

问题描述

我正在尝试为场景中的多维数据集编程基本动作,但要点是,我只是实现了移动所有多维数据集或不移动任何多维数据集,我不知道到底是哪里失败了,我尝试执行glTranslate( self.x,self.y,0.0),然后再绘制立方体,主要是要更改“ x”和“ y”位置。

class rect:
    def __init__(self,x,y,width,height,c):
        self.xp=x
        self.yp=y
        if color == 0:
            glColor3f(0,1.0,0)    
        else:
            glColor3f(1.0,0)

        glPushmatrix()
        glTranslatef(self.xp,self.yp,0.0)
        glBegin(GL_QUADS)                                  # start drawing a rectangle
        glVertex2f(self.xp,self.yp)                                   # bottom left point
        glVertex2f(self.xp + width,self.yp)                           # bottom right point
        glVertex2f(self.xp + width,self.yp + height)                  # top right point
        glVertex2f(self.xp,self.yp + height)                          # top left point
        glEnd()
        glPopMatrix()
        glFlush()

    def move(self):
        self.xp = self.xp + 1

def main():

    pygame.init()
    display = (1920,1080)
    pygame.display.set_mode(display,DOUBLEBUF|OPENGL)

    gluPerspective(45,(display[0]/display[1]),50.0)

    glTranslatef(0.0,0.0,-5)

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

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        c = cube(0,1,0)
        c.move()
        pygame.display.flip()
        pygame.time.wait(10)

main()

解决方法

我找到了一个解决方案(立方体代表动物,所以这是原因,因为我有兔子类:D)

class rabbit:

def __init__(self,s,i,g,x,y,n,c):
    self.xp=x
    self.yp=y
    self.number =  n
    self.speed = s
    self.intelligence = i
    self.gender = SEXS[g]
    
def move(self):
    self.xp = self.xp + 0.001
   

def draw(self,w,h,c):
    if c == 0:
       glColor3f(0,1.0,0)    
    elif c == 1:
       glColor3f(1.0,0)
    
    glPushMatrix()
    glTranslatef(self.xp,self.yp,0.0)
    glBegin(GL_QUADS)                                 # start drawing a rectangle       
    glVertex2f(self.xp,self.yp)                                   # bottom left point
    glVertex2f(self.xp + w,self.yp)                           # bottom right point
    glVertex2f(self.xp + w,self.yp + h)                  # top right point
    glVertex2f(self.xp,self.yp + h)
    glEnd()
    glFlush()
    glPopMatrix()

def main():
n_rabbits=0

pygame.init()
display = (1920,1080)
pygame.display.set_mode(display,DOUBLEBUF|OPENGL)

gluPerspective(45,(display[0]/display[1]),50.0)

glTranslatef(0.0,0.0,-5)

rabbits_lists=[]

rabbits_drew=False

g = random.randint(0,1)
s = random.randint(0,10)
i = random.randint(0,10)
x = random.randint(0,300)
y = random.randint(0,300)

n_rabbits = n_rabbits+1
r1 = rabbit(s,0.1,n_rabbits,1)

n_rabbits = n_rabbits+1
r = rabbit(s,0)


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

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    r.draw(r.xp,r.yp,1)
    r.move()

    r1.draw(r1.xp,r1.yp,0)
    
    pygame.display.flip()
    pygame.time.wait(10)


    main()