📜  Python Arcade – 添加按钮

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

Python Arcade – 添加按钮

在本文中,我们将学习如何使用Python在 Arcade 中创建按钮。

添加按钮

在 Arcade 中,我们可以轻松地为游戏添加按钮。

为此,我们将使用一些函数:

界面管理器():

UIBoxLayout():

UIFlatButton():

现在要创建我们的按钮,我们将创建一个名为 MainClass 的类,在这个类中,我们将初始化 UIManager 的一个变量。之后,我们将使用 UIFlatButton() 创建我们的按钮,然后我们将在我们的 UIManager 中添加这个按钮。然后我们将创建一个 on_draw()函数来绘制我们的按钮。

下面是实现:

Python3
# Importing arcade module
import arcade
# Importing arcade gui
import arcade.gui
  
# Creating MainGame class
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Buttons")
  
        # Changing background color of screen
        arcade.set_background_color(arcade.color.BLUE)
  
        # Creating a UI MANAGER to handle the UI
        self.uimanager = arcade.gui.UIManager()
        self.uimanager.enable()
  
        # Creating Button using UIFlatButton
        start_button = arcade.gui.UIFlatButton(text="Start Game",
                                               width=200)
  
        # Adding button in our uimanager
        self.uimanager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="center_x",
                anchor_y="center_y",
                child=start_button)
        )
  
    # Creating on_draw() function to draw on the screen
  
    def on_draw(self):
        arcade.start_render()
          
        # Drawing our ui manager
        self.uimanager.draw()
  
  
# Calling MainGame class
MainGame()
arcade.run()


Python3
# Importing arcade module
import arcade
# Importing arcade gui
import arcade.gui
  
# Creating MainGame class
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Buttons")
  
        # Changing background color of screen
        arcade.set_background_color(arcade.color.BLUE)
  
        # Creating a UI MANAGER to handle the UI
        self.uimanager = arcade.gui.UIManager()
        self.uimanager.enable()
  
        # Creating Button using UIFlatButton
        start_button = arcade.gui.UIFlatButton(text="Start Game",
                                               width=200)
  
        # Assigning our on_buttonclick() function
        start_button.on_click = self.on_buttonclick
  
        # Adding button in our uimanager
        self.uimanager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="center_x",
                anchor_y="center_y",
                child=start_button)
        )
  
    # This function will be called everytime the user
    # presses the start button
    def on_buttonclick(self, event):
        print("Button is clicked")
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
          
        # Drawing our ui manager
        self.uimanager.draw()
  
  
# Calling MainGame class
MainGame()
arcade.run()


输出:



在按钮中添加功能

现在我们将创建一个 on_buttonclick()函数,每次用户按下按钮时都会调用该函数。

蟒蛇3

# Importing arcade module
import arcade
# Importing arcade gui
import arcade.gui
  
# Creating MainGame class
class MainGame(arcade.Window):
    def __init__(self):
        super().__init__(600, 600, title="Buttons")
  
        # Changing background color of screen
        arcade.set_background_color(arcade.color.BLUE)
  
        # Creating a UI MANAGER to handle the UI
        self.uimanager = arcade.gui.UIManager()
        self.uimanager.enable()
  
        # Creating Button using UIFlatButton
        start_button = arcade.gui.UIFlatButton(text="Start Game",
                                               width=200)
  
        # Assigning our on_buttonclick() function
        start_button.on_click = self.on_buttonclick
  
        # Adding button in our uimanager
        self.uimanager.add(
            arcade.gui.UIAnchorWidget(
                anchor_x="center_x",
                anchor_y="center_y",
                child=start_button)
        )
  
    # This function will be called everytime the user
    # presses the start button
    def on_buttonclick(self, event):
        print("Button is clicked")
  
    # Creating on_draw() function to draw on the screen
    def on_draw(self):
        arcade.start_render()
          
        # Drawing our ui manager
        self.uimanager.draw()
  
  
# Calling MainGame class
MainGame()
arcade.run()

输出: