pythonpython-3.xtext-renderingarcade

Change the content of the text while the Game is running - Python Arcade 2.4.3


I'm looking for a way to change the rendered text while the game is running.

I found someone who says that simply change the text variable would work.

So I try this:

import arcade

WINDOW = {"width":800, "height": 600, "title": ""}

class MyGame(arcade.Window):
    """
    Main application class.
    """

    def __init__(self):

        # Call the parent class and set up the window
        super().__init__(WINDOW['width'], WINDOW['height'], WINDOW['title'])

        arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)

    def setup(self):
        """ Set up the game here. Call this function to restart the game. """
        pass

    def on_draw(self):
        """ Render the screen. """

        arcade.start_render()
        # Code to draw the screen goes here

        self.text = "Hello world!"
        arcade.draw_text(self.text, WINDOW['width'] / 3 + (WINDOW['width'] / 3 / 3) - 20, WINDOW['height'] / 2, arcade.csscolor.WHITE, 18)
    
    def on_mouse_release(self, x, y, button, key_modifiers):
        print("Clicked!")
        self.text = "Clicked!"

def main():
    """ Main method """
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

But still, The text didn't change but It can detect clicks.


Solution

  • arcade.run() runs loop which executes on_draw() in this loop. If code runs with speed 25 FPS (Frames Per Second) then it executes on_draw() 25 times in every second. So when you click mouse then on_mouse_release() changes text to "Clicked!" and later on_draw() changes it back to "Hello world!" and finally it displays "Hello world!"

    You should use self.text = "Hello world!" in __init__() or (better) in setup() to set it only once.

    import arcade
    
    WINDOW = {"width":800, "height": 600, "title": ""}
    
    class MyGame(arcade.Window):
        """
        Main application class.
        """
    
        def __init__(self):
    
            # Call the parent class and set up the window
            super().__init__(WINDOW['width'], WINDOW['height'], WINDOW['title'])
    
            arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
    
        def setup(self):
            """ Set up the game here. Call this function to restart the game. """
            
            self.text = "Hello world!"
    
        def on_draw(self):
            """ Render the screen. """
    
            arcade.start_render()
            # Code to draw the screen goes here
    
            arcade.draw_text(self.text, WINDOW['width'] / 3 + (WINDOW['width'] / 3 / 3) - 20, WINDOW['height'] / 2, arcade.csscolor.WHITE, 18)
        
        def on_mouse_release(self, x, y, button, key_modifiers):
            print("Clicked!")
            self.text = "Clicked!"
    
    def main():
        """ Main method """
        window = MyGame()
        window.setup()
        arcade.run()
    
    
    if __name__ == "__main__":
        main()