1

I am currently trying to create a text that can be updated whenever I hit a button. After going through the documentation, I saw the arcade.draw_text function is returning a text_sprite object, but it seems unable to change the text of that object.

Am I doing it completely the wrong way? Or there is a trick I did not get into yet?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
windsound
  • 706
  • 4
  • 9
  • 31

1 Answers1

1

Yes, you can do it. Following script changes text on mouse click:

import arcade

class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 400)
        self.text = 'Waiting for click...'

    def on_draw(self):
        arcade.start_render()
        arcade.draw_text(self.text, 300, 200, arcade.color.RED, 30, anchor_x='center')

    def on_mouse_release(self, x, y, button, key_modifiers):
        self.text = 'Clicked!'

MyGame()
arcade.run()

Text before click:

enter image description here

Text after click:

enter image description here

Alderven
  • 7,569
  • 5
  • 26
  • 38
  • Sorry for forgetting to accept your great reply! – windsound Jan 20 '21 at 20:12
  • With the new arcade object-oriented text, you can create it in the init function, draw it in the on_draw method, and you can change its .text property to the desired text. This is from 50-100 times faster than the draw_text method. – Ethan Chan Jul 22 '22 at 16:19