Pygame RPG – Enemy Turning Tutorial

This is a sequel to the previous tutorial where we created the new Enemy in our Pygame RPG. The goal this time is to improve upon the Enemy class, and add in the ability for it to judge the player’s location and point in the proper direction. In short, we will be giving our enemy a turning ability in this Pygame RPG Tutorial.

We won’t be able to proceed to the next part, where he shoots out projectiles without working this out first.


Enemy Class

We’re just going to add an extra variable here called self.turning and initialize it to 0. A value of 0 means the enemy is not currently in a state of turning, whereas 1 means that he is. Basically it’s used to track the state of the enemy.

class Enemy2(pygame.sprite.Sprite):
      def __init__(self):
        super().__init__()   
        self.pos = vec(0,0)
        self.vel = vec(0,0)
        self.wait = 0
        self.wait_status = False
        self.turning = 0

Direction Check

There are two possible cases in which we would want the Enemy to turn around. The first is if the Player is behind the enemy, and the enemy is pointing forwards (this is the first if statement). The second is if the Player is in front of the Enemy, but the Player is pointing backwards. (This is the second if)

(By forward, I mean in the positive x direction, and by backwards, I mean in the negative x direction).

      def direction_check(self):
            if (player.pos.x - self.pos.x < 0 and self.direction == 0):
                  return 1
            elif (player.pos.x - self.pos.x > 0 and self.direction == 1):
                  return 1
            else:
                  return 0

If 1 is returned, then that means we need to change the direction of the enemy. If 0 is returned then that means


Turning the Enemy

Here, we are going to add a direction check onto the enemy, within the move function. If the direction_check() function returns 1, then the move function will call the turn function and set self.wait to 90. This ensures that the Enemy waits for 90 frames before turning. This is done to prevent the Enemy from turning around within a few milliseconds, which looks really weird.

      def move(self):
        ... 
        ...
        # Updates positon with new values
        if self.wait > 60:
              self.wait_status = True
        elif int(self.wait) <= 0:
              self.wait_status = False

         if (self.direction_check()):
                 self.turn()
                 self.wait = 90
                 self.turning = 1

The self.turning flag is also set to true, for the duration of the turn.

Here is the turn() function, which handles the Enemy turning. If it’s called, and the self.wait counter is still above 0, it simply decrements it by one and returns. Otherwise it turns the self.turning flag to 0 (to symbolize the end of the turn) and proceeds further on to flip the direction and the image being used.

      def turn(self):
            if self.wait > 0:
                self.wait -= 1
                return
            elif int(self.wait) <= 0:
                self.turning = 0
                
            if (self.direction):
                  self.direction = 0
                  self.image = pygame.image.load("enemy2.png")
            else:
                  self.direction = 1
                  self.image = pygame.image.load("enemy2_L.png")

To summarise, the turn()function is called 90 times, as we set self.wait to 90, when we began the turn. However, the turn is not executed until self.wait is back to 0. This prevents the enemy from turning around too quickly.

And keep in mind, that for a game running at 60 FPS, setting self.wait to 90 makes the enemy pause for 1.5 seconds, before turning around. ( 90 divided by 60).


Demonstration


Next Section

In the next section, we’ll put the finishing touches on our new Enemy, by giving it a special attack. It will have the ability to shoot out bolts, similar to that of the Player’s fireball. It’s an interesting mechanic, and adds extra depth to our enemy.


This marks the end of the Pygame RPG Enemy Turning Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments