简体   繁体   English

我使用pygame.rect.move是否有问题?

[英]Is there an issue with my use of pygame.rect.move?

I am using pygame.rect.move() to move a button to a certain position on instantiation. 我正在使用pygame.rect.move()将按钮移动到实例化的某个位置。 Then from the list of objects to blit that exists the menu button is called and all functions that need to be called each frame. 然后从存在的对象列表中调用菜单按钮,并在每一帧中调用所有需要调用的功能。 The button does NOT move. 该按钮不会移动。

The rect should perfectly fit the button. rect应该完全适合按钮。

Is my use of pygame.rect.move() incorrect? 我对pygame.rect.move()不正确吗?

Here's the class: 这是课程:

class menuButton(mainMenu):
    def setpos(self):
        self.r = self.r.move(self.p)
    def __init__(self,i,e,s,p, ai, ao,sa):
        self.t = "Menu Button"
        self.i = pygame.image.load(i)
        self.r = self.i.get_rect()
        self.e = e
        self.ai = ai
        self.ao = ao
        self.p = p
        self.a = 0
        self.i.set_alpha(sa)
        pygame.transform.scale(self.i,s)
        objects.append(self)
        print "%s has been instantiated." % (self.t)
    def logic(self):
        pass
    def animations(self):
        if self.ai == "FADE_IN":
            if(self.i.get_alpha() < 255):
                self.i.set_alpha(self.i.get_alpha() + 1)

    def update(self):
        self.r = self.i.get_rect()
        r = True
        for obj in objects:
            if isinstance(self,type(obj)) == False:
                r = False
        if r == True:
            if self.a == 0:
                self.setpos()
                print self.r
            self.a += 1
            self.logic()
            self.animations()
            screen.blit(self.i,self.r)
            pygame.display.flip()

I'm not using .convert() at the end of an image import because it breaks the images. 我在图像导入的末尾没有使用.convert(),因为它会破坏图像。
I'm not using sprites because I want more control. 我不使用精灵,因为我想要更多控制。
I have also written test programs and it seems to have worked, and I've re-written the main menu button class 3 times. 我还编写了测试程序,并且似乎有效,并且我已经将主菜单按钮类重写了3次。 Same issue. 同样的问题。

m not using .convert() at the end of an image import because it breaks the images. m在图像导入结束时不使用.convert(),因为它会破坏图像。

In what way? 用什么方式? You probably want the alpha version http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha 您可能需要Alpha版本http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha

I'm not using sprites because I want more control. 我不使用精灵,因为我想要更多控制。

You can derive from Sprite , then you can add anything you want, yet still use sprite.Group s 您可以从Sprite派生,然后可以添加所需的任何内容,但仍使用sprite.Group

A side note on the names, a Menu would contain Buttons. 关于名称的旁注,菜单将包含按钮。 But buttons deriving from menu doesn't make sense. 但是从菜单派生的按钮没有任何意义。

move() returns a new rect that has moved, move_ip() modifies the existing rect. move()返回已移动的新rect, move_ip()修改现有rect。

Or you can use the rect properties 或者您可以使用rect属性

def setpos(self):
    self.r.topleft = self.p

What are you doing with this code? 您在用这段代码做什么?

    r = True
    for obj in objects:
        if isinstance(self,type(obj)) == False:
            r = False
    if r == True:
        if self.a == 0:
            self.setpos()
            print self.r
        self.a += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM