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

Re: [pygame] sprite groups and tilesheets



For scrolling games, keep a "logical" rect that represents where the
character is in the world and either:

1) keep a "display" rect that represents where the character is on the
screen.  update it on every movement
2) every frame, look at the character's logical rect and the current
logical offset of the screen and calculate (taking into consideration
the edges of the world) where the character should be drawn.

On Thu, Nov 13, 2008 at 3:20 AM, Michael Fiano <michael.fiano@xxxxxxxxx> wrote:
> On Tue, 11 Nov 2008 13:27:08 -0800 (PST)
> Devon Scott-Tunkin <djvonfunkin@xxxxxxxxx> wrote:
>
>> Here's a basic sprite class with a looping animation:
>>
>> class Bob(pygame.sprite.Sprite):
>>   def __init__(self):
>>     self.frame = 0
>>     self.images = load_sprite('bob.png')
>>     self.image = self.images[self.frame]
>>     self.rect = self.image.get_rect()
>>     self.animcounter = 0
>>     self.animspeed = 7
>>   def update(self):
>>     self.animcounter = (self.animcounter + 1)%self.animspeed
>>     if self.animcounter == 0:
>>       self.frame = (self.frame + 1)%len(self.images)
>>     self.image = self.images[self.frame]
>>
>>
>>
>> bob = Bob()
>> where bob.update() #would be called in the main loop
>
> Thanks, Devon. I finally created a real sprite and i'm updating its
> position with self.rect. Only problem is this bit of pseudo-code:
>
> if moving down
>        if self.rect.bottom > 3/4 of screen
>                scrollpos = 0,-1
>        else
>                self.rect.move_ip(0,1)
>
> What this does is scrolls the map if the rect is more than 75% down the
> screen. But its not keeping track of the rect position when the screen
> is scrolling. The player can run past the edge of map and avoid the
> collisions because the rect isnt in the player's position since the
> background moved. How can I move the rect AND the map like this?
>