简体   繁体   中英

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. 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.

Is my use of pygame.rect.move() incorrect?

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.
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. Same issue.

m not using .convert() at the end of an image import because it breaks the images.

In what way? You probably want the alpha version 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

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.

Or you can use the rect properties

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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