0

I'm working through various PyGame introductory tutorials at the moment and I've just finished looking through the Chimp example (located here).

I was hoping someone could explain to me why the below code will not make the Surface object (the chimp image) bounce off the bottom of the display Surface and begin moving towards the top again:

 def _walk(self):
    newpos = self.rect.move((self.move,1))

    if self.rect.left < self.area.left or \
    self.rect.right > self.area.right or \
    self.rect.top < self.area.top or \
    self.rect.bottom > self.area.bottom:

        self.move = -self.move

        newpos = self.rect.move((self.move,-1))
        self.image = pygame.transform.flip(self.image,1,0)

    self.rect = newpos

It seems to move the object down the screen alright, but does not cause it to reverse position. Rather it gets stuck at the bottom of the display Surface.

I have seen some similar questions asked but I can't quite make heads or tails of the explanations, so my apologies in advance if people see this all the time.

Thanks in advance!

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
ThatCob
  • 33
  • 4
  • are your positions integers or floats? can move be a diagonal? can you provide a full example? – Jean-François Fabre Aug 20 '16 at 14:01
  • Sorry yes, the link I posted above contains the full code I'm working from - would you like me to copy in the full body here? Positions should be integers, and a move can be diagonal (the rect.move() function works on (x,y) axes). – ThatCob Aug 20 '16 at 16:16
  • Check out [this answer](http://stackoverflow.com/a/39054676/355230) to another seemingly unrelated pygame question. The object bounces off the edges of the window. – martineau Aug 20 '16 at 16:16
  • If see that you have adapted the `walk` method. Isn't it strange that you invert `self.move` and you apply 1 and then -1 as second parameter ? wouldn't that create a double negative effect, unseen in the original code because it is 0 there? – Jean-François Fabre Aug 20 '16 at 16:23
  • @Jean-FrançoisFabre ... I'm an idiot. I didn't even see the original setting where it was set up top as 1. Changing it to a variable, as with self.move, makes it work. Thank you so much! – ThatCob Aug 20 '16 at 16:38

0 Answers0