Pygame RPG – Next Stage Display

This is tutorial number 13 in our Pygame RPG series.

Over the course of the last few tutorials we built an entire level system with a fixed number of enemies per level. In order to “advance” to the next stage we have tied the “advance stage” code to the keyboard key “n”. There are many other ways of doing so, like through the use of drop down or map selection menu, but this is the way we’ll be exploring.

Our current system is too “bland” and gives no visual indicator that the stage has been advanced or even which stage we are on. To solve all these problems we’ll be creating a special display animation that plays when we advance to the next stage.

The “Stage Display” that we create is basically going to be a large text object that moves across the screen horizontally when we advance to the next stage in our Pygame RPG. A short clip on it’s Animation is included at the end of the tutorial.


Status Bar Class

We’re going to be using the largest font object that we created in the last tutorial along with the color object. If you don’t anything about fonts in Pygame, be sure to follow up with our dedicated tutorial on it.

We initialize the StageDisplay object at the “-100” position on the x-axis. This keeps it safely out of the window, allowing for us next piece of code to run smoothly. Setting the y-position to 100 will set it somewhere near the top of the screen.

class StageDisplay(pygame.sprite.Sprite):
      def __init__(self):
            super().__init__()
            self.text = headingfont.render("STAGE: " + str(handler.stage) , True , color_dark)
            self.rect = self.text.get_rect()
            self.posx = -100
            self.posy = 100
            self.display = False

The move_display() is incharge of “moving” the display across the screen. Every time it is called, it will move the Stage Display object 5 pixels to the right (it travels from left to right).

It’s working is quite simple. Is creates a text string from the words “STAGE” and a character representing the current stage number. It then checks to see if it has covered the length of the window, upon which it will either continue to move forward, or destroy itself.

      def move_display(self):
            # Create the text to be displayed
            self.text = headingfont.render("STAGE: " + str(handler.stage) , True , color_dark)
            if self.posx < 700:
                  self.posx += 5
                  displaysurface.blit(self.text, (self.posx, self.posy))
            else:
                  self.display = False
                  self.kill()

...
...
handler = EventHandler()
stage_display = StageDisplay()           

Create the stage_display object only after all the other objects. This is becuase it’s __init__ method relies on a variable from the EventHandler class. If the stage_display object is created before the handler() object, there will be on error.


Game Loop Changes

In order to integrate the “Stage Clear Display” into our game, we need to add into the same place as our next_stage() function. This ensures that the displays activates alongside the advancement of the next stage.

while True:
        ...
        ...
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_n:
                  if handler.battle == True and len(Enemies) == 0:
                        handler.next_stage()
                        stage_display = StageDisplay()
                        stage_display.display = True

This is another essential piece required which will continue to move the Stage Display across the screen while it’s display variable is set to True. Remember, calling the move method on the stage_display object only moves it 5 pixels.

    # Render stage display
    if stage_display.display == True:
          stage_display.move_display()

Here’s a short clip showcasing the transition between stages after pressing the “N” button.


Next Section

The next few sections will deal with adding more features and mechanics into our game. We still lack several major RPG elements, such as a variety of different attacks, a status bar, Player experience and many more.

If you want the code for this tutorial, you’ll have to wait for the next Code Review. Otherwise you can always add the above code into the code from previous code reviews.


This marks the end of the Pygame RPG – Next Stage Display 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
Inline Feedbacks
View all comments