In this article, we will learn How we can create buttons in Arcade using python.
Adding Buttons
In Arcade we can easily add buttons to our game.
For this, we will use some functions:
UIManager():
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
Now to create our button we are going to create a class named MainClass and inside this class, we are going to initialize one variable for the UIManager. After that, we will create our button using the UIFlatButton() then we will add this button in our UIManager. Then we will create an on_draw() function to draw our button.
Below is the implementation:
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() |
Output:
Adding functions in Button
Now we are going to create an on_buttonclick() function which will be called every time the user presses the button.
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() |
Output: