[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Rockhunt



andrew baker wrote:
What's self.idealxpix and self.idealypix?

Those are the X and Y coordinates where the sprite "wants" to be.

Here's the class:

class CaveObject(pygame.sprite.Sprite):
    """one square in the onscreen cave (image, x, y)"""
    def __init__(self, image, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.left = x * cellwidth
        self.rect.top = y * cellheight
        self.idealxpix = self.rect.left
        self.idealypix = self.rect.top
        self.update()
    def update(self):
        "move the object on screen smoothly"
        xoffset = 0
        yoffset = 0
        if self.rect.left < self.idealxpix:
            xoffset = 6
        elif self.rect.left > self.idealxpix:
            xoffset = -6
        if self.rect.top < self.idealypix:
            yoffset = 6
        elif self.rect.top > self.idealypix:
            yoffset = -6
        self.rect.left = self.rect.left + xoffset
        self.rect.top = self.rect.top + yoffset
        if abs(self.rect.top - self.idealypix) < 6:
            self.idealypix = self.rect.top
        if abs(self.rect.left - self.idealxpix) < 6:
            self.idealxpix = self.rect.left
        self.check_wrap()
    def change_image(self, image):
        self.image = image
    def check_wrap(self):
        "check if object is out of boundaries and wrap if so"
        if self.rect.left > (23 * cellwidth):
            self.rect.left -= (24 * cellwidth)
            self.idealxpix -= (24 * cellwidth)
            self.x -= 24
            newcave.changed.append([self.x,self.y])
        elif self.rect.left < (-2 * cellwidth):
            self.rect.left += (24 * cellwidth)
            self.idealxpix += (24 * cellwidth)
            self.x += 24
            newcave.changed.append([self.x,self.y])
        if self.rect.top > (16 * cellheight):
            self.rect.top -= (17 * cellheight)
            self.idealypix -= (17 * cellheight)
            self.y -= 17
            newcave.changed.append([self.x,self.y])
        elif self.rect.top < (-2 * cellheight):
            self.rect.top += (17 * cellheight)
            self.idealypix += (17 * cellheight)
            self.y += 17
            newcave.changed.append([self.x,self.y])