Pygame RPG – Player Experience and Mana

This is tutorial number 14 in our Pygame RPG series.

It’s time to “fill out” our game a bit and add some new mechanics. Two things in particular that most RPG’s have is a Player leveling system as well as a variety of different special attacking moves to use. Before implementing either of these though, we need two other supporting features, “Mana” and “Experience”.

In this Pygame tutorial we’ll be explain how to correctly insert “Experience” and “Mana” into our RPG. Using these, or variants of these concepts you can easily add a ton of new and exciting mechanics into your game.


Player Variables

The first thing to do is add two new variables into the player class, self.mana and self.experiance.

        #Combat
        self.attacking = False
        self.cooldown = False
        self.attack_frame = 0
        self.health = 5
        self.experiance = 0
        self.mana = 0

Enemy Class

Usually in games, killing the enemy generates a certain (or random) amount of experience, mana or items drop. Considering this, it made sense for us to place the experience and mana code into the enemy class.

We’ll be exploring two slightly different methods, one of which we’ll use on Mana and one on Experience. The number of mana generated will be a randomized amount, whereas the experience will be a fixed amount per kill.

        self.direction = random.randint(0,1) # 0 for Right, 1 for Left
        self.vel.x = random.randint(2,6) / 2  # Randomised velocity of the generated enemy
        self.mana = random.randint(1, 3)  # Randomised mana amount obtained upon kill

Previously when our enemy object was hit by the Player, we would just use the kill() method to remove the Enemy from the game. In this and the upcoming tutorials we will be adding many more features all connected to the “death” of the enemy.

It’s often a good idea to create a limiter for certain variables such as Mana. In this case we’ll use an if statement to prevent it from incrementing if it reaches the value of 100. We won’t keep any kind of limit for experience though (yet), so it will increment freely if the enemy is killed.

      def update(self):
            # Checks for collision with the Player
            hits = pygame.sprite.spritecollide(self, Playergroup, False)

            # Activates upon either of the two expressions being true
            if hits and player.attacking == True:
                  if player.mana < 100: player.mana += self.mana # Release mana
                  player.experiance += 1   # Release expeiriance
                  self.kill()

            # If collision has occured and player not attacking, call "hit" function            
            elif hits and player.attacking == False:
                  player.player_hit()

And that’s all. No changes to the game loop required this time. There is however, a little problem. We have no way of actually monitoring our experience or mana, which almost completely ruins their purpose. This is something we will be rectifying in the next tutorial, so be sure to follow up.

Here’s a little sneak peek from the next section where we’ll be working on the status bar.

Pygame RPG Status Bar

Use the button below to advance to the next section!


This marks the end of the Pygame RPG – Player Experience and Mana Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments