Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在游戏运行时更改文本内容-Python Arcade 2.4.3_Python_Python 3.x_Text Rendering_Arcade - Fatal编程技术网

在游戏运行时更改文本内容-Python Arcade 2.4.3

在游戏运行时更改文本内容-Python Arcade 2.4.3,python,python-3.x,text-rendering,arcade,Python,Python 3.x,Text Rendering,Arcade,我正在寻找一种在游戏运行时更改渲染文本的方法 我发现有人说只需更改文本变量就可以了 所以我试着这样做: import arcade WINDOW = {"width":800, "height": 600, "title": ""} class MyGame(arcade.Window): """ Main application class. "&

我正在寻找一种在游戏运行时更改渲染文本的方法

我发现有人说只需更改文本变量就可以了

所以我试着这样做:

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()
但是,文本仍然没有改变,但它可以检测到点击。

arcade.run()
运行循环,该循环在此循环中对绘图()执行
。如果代码以每秒25帧的速度运行,那么它将每秒执行25次。因此,当您单击鼠标时,鼠标释放()上的
将文本更改为
“单击!”
,然后在绘图()上的
将其更改回
“Hello world!”
,最后显示
“Hello world!”

您应该使用
中的
self.text=“Hello world!”
设置()中的(更好)只设置一次

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()

以下是单击鼠标更改文本的简短示例:

import arcade

class Game(arcade.Window):
    def __init__(self):
        super().__init__(400, 300)
        self.text = 'Waiting for click!'

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

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

Game()
arcade.run()
输出:


on_draw()
self.text
设置为
“Hello world!”
,因此您在
中对鼠标释放()所做的任何更改都将被丢弃。正如@PranavHosangadi已经说过的-您不应该将
self.text=“Hello world!”
放在
on_draw()
中,而应该放在
中。这样,它将仅在开始时设置为“Hello world!”
。稍后按钮会将其更改为“单击!”
。您必须记住,
arcade.run()
在每个循环中运行在绘图()上执行的循环。