📜  Python Arcade – 显示文本

📅  最后修改于: 2022-05-13 01:55:32.613000             🧑  作者: Mango

Python Arcade – 显示文本

在本文中,我们将学习如何在Python向街机游戏添加文本。

添加简单文本

我们可以使用 draw_text()函数在拱廊中添加文本。

在下面的例子中,我们将创建一个名为 MainGame 的类,在这个类中,我们将创建一个 on_draw()函数,我们将在其中进行文本的渲染和绘制。然后我们将调用我们的 MainGame() 类和 arcade.run()函数。



下面是实现:

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


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


输出:

更新文本

在这个例子中,我们将分数显示为文本,并且我们希望每次玩家触摸屏幕边框时分数增加 10 分。为此,我们将在 MainGame 类中为玩家的 x 和 y 坐标、分数和速度初始化一些变量。之后,我们将创建 2 个函数:

  • on_draw():我们将在这个函数绘制我们的播放器和文本。
  • on_update():我们将通过增加速度来更新玩家的 x 坐标。然后我们将改变玩家的方向并在玩家越过屏幕边界时增加分数。在此之后,我们将调用 MainGame() 类。

下面是实现:

蟒蛇3

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

输出: