• 小学生python游戏编程arcade----游戏界面按钮实现事件实现的三种方法


    前言

    接上篇文章小学生python游戏编程arcade----基本知识1、2,连接如下:小学生python游戏编程arcade----基本知识,小学生python游戏编程arcade----基本知识2,小学生python游戏编程arcade----基本知识3,
    小学生python游戏编程arcade----基本知识5 自动行走的敌人小学生python游戏编程arcade----6射击及子弹

    游戏界面按钮实现事件实现的三种方法

    1、按钮定义及事件3种方法

    1.1 按钮定义

    start_button = arcade.gui.UIFlatButton(text=“开始”, width=200)

    1.2三种事件引用方法
    1.2.1 类法

    class QuitButton(arcade.gui.UIFlatButton):
    def on_click(self, event: arcade.gui.UIOnClickEvent):
    arcade.exit()

    1.2.2 事件方法2,自定义方法函数
    start_button.on_click = self.on_click_start
    def on_click_start(self, event):
        print("自定义方法调用--开始:", event)
    
    • 1
    • 2
    • 3
    1.2.3 事件方法3,使用装饰器处理onclick事件

    使用装饰器处理onclick事件
    @settings_button.event(“on_click”)
    def on_click_settings(event):
    print(“装饰器处理onclick事件Settings:”, event)

    1.3 效果图

    在这里插入图片描述

    1.4 代码实现
    """
    按钮创建及处理按钮事件的三种方法
    """
    import arcade
    import arcade.gui
    
    
    # 事件方法1--定义类
    class QuitButton(arcade.gui.UIFlatButton):
        def on_click(self, event: arcade.gui.UIOnClickEvent):
            arcade.exit()
    
    
    class MyWindow(arcade.Window):
        def __init__(self):
            super().__init__(800, 600, "按钮事件三种方法", resizable=True)
    
            # 用于处理UI的UIManager.
            self.manager = arcade.gui.UIManager()
            self.manager.enable()
    
            # 设置背景色
            arcade.set_background_color(arcade.color.DARK_BLUE_GRAY)
    
            # 创建垂直BoxGroup以对齐按钮
            self.v_box = arcade.gui.UIBoxLayout()
    
            # Create the buttons
            start_button = arcade.gui.UIFlatButton(text="开始", width=200)
            self.v_box.add(start_button.with_space_around(bottom=20))
    
            settings_button = arcade.gui.UIFlatButton(text="设置", width=200)
            self.v_box.add(settings_button.with_space_around(bottom=20))
    
            # Again, method 1. Use a child class to handle events.
            quit_button = QuitButton(text="退出", width=200)
            self.v_box.add(quit_button)
    
            #  事件方法2,自定义方法函数
            start_button.on_click = self.on_click_start
    
            #  事件方法3,
            #使用装饰器处理onclick事件
            @settings_button.event("on_click")
            def on_click_settings(event):
                print("装饰器处理onclick事件Settings:", event)
    
            # 创建一个小部件来容纳v_box小部件,它将使按钮居中
            self.manager.add(
                arcade.gui.UIAnchorWidget(
                    anchor_x="center_x",
                    anchor_y="center_y",
                    child=self.v_box)
            )
    
        def on_click_start(self, event):
            print("自定义方法调用--开始:", event)
    
        def on_draw(self):
            self.clear()
            self.manager.draw()
    
    
    window = MyWindow()
    arcade.run()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    2、定义为类,以便游戏集成

    2.1 定义GuiGL类

    class GuiGL(object):
    def init(self):
    # 用于处理UI的UIManager.
    self.manager =UIManager()
    self.manager.enable()

        # 创建垂直BoxGroup以对齐按钮
        self.v_box = UIBoxLayout()
        # 创建一个小部件来容纳v_box小部件,它将使按钮居中
        self.manager.add(
           UIAnchorWidget(
                anchor_x="center_x",
                anchor_y="center_y",
                child=self.v_box)
        )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2.2 加个背景
        self.g = GuiGL()
        # 初始化场景
        self.scene = arcade.Scene()
        background = arcade.Sprite("images/坦克装甲车.jpg", 1)
        background.center_x = SCREEN_WIDTH / 2
        background.center_y = SCREEN_HEIGHT / 2
        self.scene.add_sprite("bg", background)
        使用时,可以把相关gui的都放在GuiGL这个类中,方便使用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.3 效果图

    在这里插入图片描述

    2.4 代码实现
    """
    按钮创建及处理按钮事件的三种方法
    """
    import arcade
    from arcade.gui import UIFlatButton,UIOnClickEvent,UIManager,UIBoxLayout,UIAnchorWidget
    
    
    # 事件方法1--定义类
    class QuitButton(UIFlatButton):
        def on_click(self, event: UIOnClickEvent):
            arcade.exit()
    
    class GuiGL(object):
        def __init__(self):
            # 用于处理UI的UIManager.
            self.manager =UIManager()
            self.manager.enable()
    
            # 创建垂直BoxGroup以对齐按钮
            self.v_box = UIBoxLayout()
            # 创建一个小部件来容纳v_box小部件,它将使按钮居中
            self.manager.add(
               UIAnchorWidget(
                    anchor_x="center_x",
                    anchor_y="center_y",
                    child=self.v_box)
            )
    
    
    SCREEN_WIDTH=1200
    SCREEN_HEIGHT=800
    
    
    class Game(arcade.Window):
        def __init__(self):
            super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "按钮事件三种方法", resizable=True)
            self.g = GuiGL()
            # 初始化场景
            self.scene = arcade.Scene()
            background = arcade.Sprite("images/坦克装甲车.jpg", 1)
            background.center_x = SCREEN_WIDTH / 2
            background.center_y = SCREEN_HEIGHT / 2
            self.scene.add_sprite("bg", background)
            # 设置背景色
            arcade.set_background_color(arcade.color.DARK_BLUE_GRAY)
            red_style = {
                "font_name": ("calibri", "arial"),
                "font_size": 15,
                "font_color": arcade.color.WHITE,
                "border_width": 2,
                "border_color": None,
                "bg_color": arcade.color.REDWOOD,
    
                # used if button is pressed
                "bg_color_pressed": arcade.color.WHITE,
                "border_color_pressed": arcade.color.RED,  # also used when hovered
                "font_color_pressed": arcade.color.RED,
            }
    
            #创建按钮
            start_button = UIFlatButton(text="开始", width=200, style=red_style)
            self.g.v_box.add(start_button.with_space_around(bottom=20)) # 按钮底部20
    
            settings_button = UIFlatButton(text="设置", width=200, style=red_style)
            self.g.v_box.add(settings_button.with_space_around(bottom=20))
    
            # Again, method 1. Use a child class to handle events.
            quit_button = QuitButton(text="退出", width=200)
            self.g.v_box.add(quit_button)
    
            #  事件方法2,自定义方法函数
            start_button.on_click = self.on_click_start
    
            #  事件方法3,
            #使用装饰器处理onclick事件
            @settings_button.event("on_click")
            def on_click_settings(event):
                print("装饰器处理onclick事件Settings:", event)
    
    
    
        def on_click_start(self, event):
            print("自定义方法调用--开始:", event)
    
        def on_draw(self):
            self.clear()
            self.scene.draw()
            self.g.manager.draw()
    
    
    
    window = Game()
    arcade.run()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    源码获取

    关注博主后,私聊博主免费获取
    需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主

    今天是以此模板持续更新此育儿专栏的第 18/50次。
    可以关注我,点赞我、评论我、收藏我啦。

  • 相关阅读:
    通过数据模板自动生成表格table
    监听器
    02-2、PyCharm中文乱码的三处解决方法
    并发编程 --- 信号量线程同步
    C/C++数据结构之Hash与BloomFilter详解
    正则表达式
    【Vue】Axios取消请求
    dflow入门5——Big step & Big parameter
    目标检测YOLO实战应用案例100讲-基于机器视觉的输电线路小目标检测和缺 陷识别(下)
    [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • 原文地址:https://blog.csdn.net/fqfq123456/article/details/127838392