1

I'm making some kind of RPG-styled game and I am trying to implement the Zombie looking direction. I have a Zombie class and Zombies are supposed to follow the player. I determine the Zombie direction (where he is going) by calculating the degree of the (player position - zombie position) to a vector, which is parallel to the x-axis and by using this degree in some if-elif blocks I determine the Zombie-moving. (for example "self.vel = vec(zombieSpeed,0)")

The code is working fine, but when the Zombie crosses one of the degrees of 22.5 + 45* the zombiedirection is changing in the frequency of the game update, because in the next followObject(...)-call the velocity of the zombie changes to another direction.

At first: I made a variable vec at the top of the Zombie class:

vec = pg.math.Vector2

Now the Zombie follow Object, where the moving speed and direction is set:

def followObject(self, entity):
    degree = (self.game.player.newPos - self.newPos).angle_to(vec(1,0))

    if (degree > -22.5 and degree <= 22.5):
        self.vel = vec(zombieSpeed,0)
    elif (degree > 22.5 and degree <= 67.5):
        self.vel = vec(zombieSpeed,-zombieSpeed)
    elif (degree > 67.5 and degree <= 112.5):
        self.vel = vec(0,-zombieSpeed)
    elif (degree > 112.5 and degree <= 157.5):
        self.vel = vec(-zombieSpeed,-zombieSpeed)
    elif (degree > 157.5 and degree <= 180) or (degree <= -180 and degree >= -157.5):
        self.vel = vec(-zombieSpeed,0)
    elif (degree > -157.5 and degree <= -112.5):
        self.vel = vec(-zombieSpeed,zombieSpeed)
    elif (degree > -112.5 and degree <= -67.5):
        self.vel = vec(0,zombieSpeed)
    elif (degree > -67.5 and degree <= -22.5):
        self.vel = vec(zombieSpeed,zombieSpeed)
    if self.vel.x != 0 and self.vel.y != 0:
        self.vel *= math.sqrt(2)/2

And I created this Method to determine, where characters have to look:

def getDirectionLooking(self, oldPos, newPos):

    vecPosition = newPos - oldPos
    degree = vecPosition.angle_to(vec(1,0))
    if self.isMoving == True:
        if (degree >= -45 and degree <= 45):
            return 'r' #character right sprites
        elif (degree > 45 and degree < 135):
            return 'b' #character back sprites
        elif (degree >= 135 and degree <= 180) or (degree >= -180 and degree <= -135):
            return 'l' #character left sprites
        elif (degree < -45 and degree > -135):
            return 'f' #character front sprites
    else:
        return self.oldDir

This is the update function for the Zombie, which is repeated every gametick:

def update(self):
    print(self.newDir,end=' ')
    self.followObject(self.followEntity) #followEntity is the playerinstance
    self.oldPos = vec(self.newPos)
    self.newPos += self.vel * self.game.dt
    self.newDir = self.getDirectionLooking(self.oldPos, self.newPos)

    self.animationUpdate(self.newDir)

    self.rect.x = self.newPos.x
    self.collideWithWalls('x')
    self.rect.y = self.newPos.y
    self.collideWithWalls('y')

So I need a method, where I can make the Zombie follow the character without this 'bug'. But I still want the Zombie to only be able to move to 8 directions.

output of Zombie direction:

 l f f f f f f f f f f l l f f f l f f l l f l f 

Here is the project:

Link to the project

Uuser
  • 49
  • 3
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. You haven't shown us the problem from the debugging you've done, nor have you isolated the problem into a reproducible form. Also, your link (another Stack Overflow no-no) is broken. – Prune Feb 08 '17 at 19:17
  • @Prune I think everything in my game is needed, since there is a map, which is needed, a camera, which is connected with the player, who is also needed, because my Zombie follows the Player. Do you have any recommendations for a fileuploadwebsite? – Uuser Feb 08 '17 at 19:55
  • No recommendation; see the posting guidelines. Your debugging so far should have included a stub routine to replace the camera module and tracing output to exhibit the problem points. – Prune Feb 08 '17 at 19:59
  • @Prune I added a better description and a new link, maybe you can run it now – Uuser Feb 08 '17 at 20:31
  • Again, please read the posting guidelines. Your posting doesn't contain all the code needed to reproduce the problem. Do *not* use a link. Also, the link you provide is to an non-secure site. – Prune Feb 08 '17 at 22:16
  • It is not possible to post all the code I have, this is just too much... (5 Classes and Sprites, how should I be able to post sprites here...) I can link my project. I would like to post it on trustworth site, if my site is not. But how should I manage those sprites? I just cant post them here. And the project wont work without them – Uuser Feb 09 '17 at 16:30
  • This is not a matter of making the entire project work. This is about focusing in on the code causing the problem. If you know the problem is within the code you posted, then supply the driver code that will complete the example. If the problem is *not* contained within the posted code, then we don't have enough information to help you. – Prune Feb 09 '17 at 16:52
  • I hope now it is possible to understand – Uuser Feb 09 '17 at 17:00
  • You should provide a minimal example that we can actually run (and delete that shady link). Remove everything that is not needed to display the error/bug, and to replace pictures you can just use `pygame.Surface`s and fill them with some color, e.g. `my_surface.fill((30, 140, 50))`. Also, Pygame's math.Vector2 is said to be buggy and I'd recommend to use [Pymunk's Vec2d](http://www.pymunk.org/en/latest/_modules/pymunk/vec2d.html#Vec2d) class instead. – skrx Feb 11 '17 at 13:33
  • I take the advice against pygame.math.Vector2 back, it seems to work correctly now. – skrx Feb 16 '17 at 15:50
  • I thank you for your effort, but I worked on a solution, which works fine. I don't think I need help on this issue anymore. I'd like to answer it myself but I used a completely different approach, Nevertheless I really appreciate, that you helped me looking for a solution, even though my answer did not accorded to the question-conventions. I will use these tipps for my next question. – Uuser Feb 20 '17 at 15:01

0 Answers0