Open In App

Python Arcade – Display Text

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn How we can add text to arcade games in Python.

Adding Simple Text

We can add text in the arcade using the draw_text() function.

Syntax: arcade.draw_text(text, x, y, color, size, width, align, font_name)

Parameters:

  • text: Text we want to display
  • x : x coordinate
  • y : y coordinate
  • color : color of the text
  • size : Size of the font
  • width : Width of the text
  • align : Alignment of the text
  • font_name : Name of the font

In the below example, we are going to create a class named MainGame, and inside this class, we will create an on_draw() function in which we will do the rendering and drawing of our text. Then we will call our MainGame() class and arcade.run() function.

Below is the implementation:

Python3




# Importing arcade module
import arcade
  
# Creating MainGame class       
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600,
                         title="Text in Arcade")
  
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
          
        # Drawing the text using draw_text()
        # draw_text function is used to draw 
        # text to the screen using Pyglet’s label.
        arcade.draw_text("GeeksforGeeks",120.0,300.0,
                         arcade.color.GREEN,40,80,'left')
  
# Calling MainGame class       
MainGame()
arcade.run()


Output:

Updating the Text

In this example, we are going to display the score as text and we want to increase the score by 10 points every time the player touches the border of the screen. For this, we are going to initialize some variables for the x and y coordinates, score, and velocity of the player in our MainGame class. After that, we will create 2 functions:

  • on_draw(): We will draw our player and text inside this function.
  • on_update(): We will update the x coordinate of the player by adding velocity. Then we will change the player’s direction and increase the score if the player crosses the boundaries of the screen. After this, we will call our MainGame() class.

Below is the implementation:

Python3




# Importing arcade module
import arcade
  
# Creating MainGame class       
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Player Movement")
  
        # Initializing the initial x and y coordinated
        self.x = 250 
        self.y = 250
  
        # Creating variable to store the score
        self.score = 0
  
        # Initializing a variable to store
        # the velocity of the player
        self.vel = 300
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
  
        # Drawing the rectangle using
        # draw_rectangle_filled function
        arcade.draw_rectangle_filled(self.x, self.y,50, 50,
                                     arcade.color.GREEN )
  
        # Drawing the text
        arcade.draw_text('Score :- '+str(self.score),150.0,500.0,
                         arcade.color.RED,20,180,'left')
          
    # Creating on_update function to
    # update the x coordinate
    def on_update(self,delta_time):
        self.x += self.vel * delta_time
  
        # Changing the direction of
        # movement if player crosses the screen
        # and increasing the score
        if self.x>=550 or self.x<=50:
            self.score += 10
            self.vel *= -1
          
# Calling MainGame class       
MainGame()
arcade.run()


Output:



Last Updated : 23 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads