Python Arcade – Display Text
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
import arcade
class MainGame(arcade.Window):
def __init__( self ):
super ().__init__( 600 , 600 ,
title = "Text in Arcade" )
def on_draw( self ):
arcade.start_render()
arcade.draw_text( "GeeksforGeeks" , 120.0 , 300.0 ,
arcade.color.GREEN, 40 , 80 , 'left' )
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
import arcade
class MainGame(arcade.Window):
def __init__( self ):
super ().__init__( 600 , 600 , title = "Player Movement" )
self .x = 250
self .y = 250
self .score = 0
self .vel = 300
def on_draw( self ):
arcade.start_render()
arcade.draw_rectangle_filled( self .x, self .y, 50 , 50 ,
arcade.color.GREEN )
arcade.draw_text( 'Score :- ' + str ( self .score), 150.0 , 500.0 ,
arcade.color.RED, 20 , 180 , 'left' )
def on_update( self ,delta_time):
self .x + = self .vel * delta_time
if self .x> = 550 or self .x< = 50 :
self .score + = 10
self .vel * = - 1
MainGame()
arcade.run()
|
Output:
Last Updated :
23 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...