Python Arcade – 添加按钮
在本文中,我们将学习如何使用Python在 Arcade 中创建按钮。
添加按钮
在 Arcade 中,我们可以轻松地为游戏添加按钮。
为此,我们将使用一些函数:
界面管理器():
Syntax: arcade.gui.UIManager(window, auto_enable)
Parameters:
- window : Our game window
- auto_enable : Accepts a boolean value
UIBoxLayout():
Syntax: arcade.gui.UIBoxLayout(x, y, vertical, align, children, size)
Parameters:
- x : x coordinate of bottom left
- y : x coordinate of bottom left
- vertical : Layout children vertical (True) or horizontal (False)
- align : Align children in orthogonal direction (x: left, center, right / y: top, center, bottom)
- children : Initial children, more can be added
- size : A hint for UILayout, if this UIWidget would like to grow
UIFlatButton():
Syntax: arcade.gui.UIFlatButton( x, y, width, height, text, style)
Parameters:
- x : x-coordinate of widget.
- y : y-coordinate of widget.
- width : width of widget. Defaults to texture width if not specified.
- height : height of widget. Defaults to texture height if not specified.
- text : text to add to the button.
- style : Used to style the button
现在要创建我们的按钮,我们将创建一个名为 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()
输出: