Please help me work out this bug!!!
It's 10:20 am Michigan time. I got up early to see if I could get a working version of my game up, and ran into a rather intresting (quickly reaching frustrating level) error in some of my mob sprite code. Please take a look and see if you can find anything wrong. The sprite is supose to turn right anytime it reaches the screen edge or if it collides with a wall sprite. The code for keeping it on the screen works perfectly, and is shown below.def update(self, time):
if time == 5:
self.lastx = self.positionx
self.lasty = self.positiony
self.positionx += self.dx
self.positiony += self.dy
if self.positionx > 589:
self.positionx = 589
self.dy = self.dx
self.dx = 0
self.direction = "Down"
if self.positionx < 1:
self.positionx = 1
self.dy = self.dx
self.dx = 0
self.direction = "Up"
if self.positiony > 379:
self.positiony = 379
self.dx = self.dy * -1
self.dy = 0
self.direction = "Left"
if self.positiony < 1:
self.positiony = 1
self.dx = self.dy * -1
self.dy = 0
self.direction = "Right"
self.rect.topleft = (self.positionx, self.positiony)
There there is the wall collide function, which sorts things out after the sprite hits a wall. Even though the following code is almost an exact copy of that above which does work, whenever a sprite hits a wall while traveling left or right on the screen, it bounces back the way it came.
def WallCollide(self):
self.positionx = self.lastx
self.positiony = self.lasty
if self.direction == "Right":
self.dy = self.dx
self.dx = 0
self.direction = "Down"
if self.direction == "Left":
self.dy = self.dx
self.dx = 0
self.direction = "Up"
if self.direction == "Down":
self.dx = self.dy * -1
self.dy = 0
self.direction = "Left"
if self.direction == "Up":
self.dx = self.dy * -1
self.dy = 0
self.direction = "Right"
For the life of me, I can not think of a single reason why two nearly identical code sections should produce two very different (one of which is highly undesirable) results. Any help with sorting this out would be appreciated. Thanks in advance.
(log in to comment)
Comments
I didn't look very deeply into your code, but try this:
def WallCollide(self):
self.positionx = self.lastx
self.positiony = self.lasty
if self.direction == "Right":
self.dy = self.dx
self.dx = 0
self.direction = "Down"
elif self.direction == "Left":
self.dy = self.dx
self.dx = 0
self.direction = "Up"
elif self.direction == "Down":
self.dx = self.dy * -1
self.dy = 0
self.direction = "Left"
elif self.direction == "Up":
self.dx = self.dy * -1
self.dy = 0
self.direction = "Right"
def WallCollide(self):
self.positionx = self.lastx
self.positiony = self.lasty
if self.direction == "Right":
self.dy = self.dx
self.dx = 0
self.direction = "Down"
elif self.direction == "Left":
self.dy = self.dx
self.dx = 0
self.direction = "Up"
elif self.direction == "Down":
self.dx = self.dy * -1
self.dy = 0
self.direction = "Left"
elif self.direction == "Up":
self.dx = self.dy * -1
self.dy = 0
self.direction = "Right"
I would add some asserts that show that self.direction is consistent with (self.dx, self.dy). For instance:
if self.direction == "Right": assert self.dx >= 0 and self.dy == 0