簡體   English   中英

不能只移動一個 object

[英]Can't move only one object

我正在嘗試為場景中的立方體編程基本運動,但關鍵是我只是實現了移動所有立方體或無立方體,我不知道失敗到底在哪里,我試着做一個 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,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]), 1.0, 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, 0, 1, 1, 0)
        c.move()
        pygame.display.flip()
        pygame.time.wait(10)

main()

我找到了一個解決方案(立方體代表動物,這就是原因,因為我有一只兔子 class: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, x, y, w, h, c):
    if c == 0:
       glColor3f(0,1.0,0)    
    elif c == 1:
       glColor3f(1.0,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]), 1.0, 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, i, g, 0.1, 0.1, n_rabbits, 1)

n_rabbits = n_rabbits+1
r = rabbit(s, i, g, 0, 0, n_rabbits, 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, 0.1, 0.1, 1)
    r.move()

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


    main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM