Arcade触发器例子角色自定义属性演示

"""
Arcade触发器例子角色自定义属性演示,还展示了any和all函数的用法。
操作方法:用鼠标指针操作小人,去碰撞金币,它们就会亮起来。
然后碰右下角的道具,它会把点亮的金币删除。译:lixingqiu
本程序需要Arcade模块运行,它是制作街机游戏的一个Python库。
any函数:可迭代序列中有一个数据用bool运算的结果为真,即为真。
all函数:可迭代序列中有一个数据用bool运算的结果为假,则为假,全部为真,则为真,要不怎么叫all呢。
>>> help(any)
Help on built-in function any in module builtins: 内置模块中的内置函数

any(iterable, /)
    Return True if bool(x) is True for any x in the iterable.可迭代序列中有一个bool运算为真则返回真
    
    If the iterable is empty, return False.如果可迭代序列是空的,返回假

>>> help(all)
Help on built-in function all in module builtins:

all(iterable, /)                            可迭代序列中所有bool运算为真才返回真
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.如果可迭代序列是空的,返回真

>>> 


"""

import arcade
import os

# 常量定义
SPRITE_SCALING_PLAYER = 0.5

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Arcade触发器例子角色自定义属性演示,改编:李兴球"


class MyGame(arcade.Window):
    """ Our custom Window Class"""

    def __init__(self):
        """ Initializer """
        # 调用父类的初始化方法新建一个窗口
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 
        # 角色列表定义
        self.player_list = None
        self.coin_list = None

        # 玩家角色定义
        self.player_sprite = None

        # 道具角色定义(触发器)
        self.trigger_sprite = None

        # 不显示鼠标指针
        self.set_mouse_visible(False)

        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        """ 设置游戏,初始化变量"""

        # 实例化角色列表
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()

        # 实例化角色
        self.player_sprite = arcade.Sprite("images/character.png", SPRITE_SCALING_PLAYER)
        self.player_sprite.center_x = 50         # 角色x坐标
        self.player_sprite.center_y = 150        # 角色的y坐标
        self.player_list.append(self.player_sprite)

        # 实例化一些金币
        for x in range(100, 800, 100):
            coin = arcade.Sprite("images/coin_01.png", scale=0.3, center_x=x, center_y=400)
            coin.intensity = 'dim'           # 这是增加的一个属性
            coin.alpha = 64                  # 透明度
            self.coin_list.append(coin)

        # 创建一个触发器
        self.trigger_sprite = arcade.Sprite("images/bumper.png", scale=0.5, center_x=750, center_y=50)

    def on_draw(self):
        """ Draw everything """
        arcade.start_render()
        self.coin_list.draw()
        self.trigger_sprite.draw()
        self.player_list.draw()

        # Put the instructions on the screen.
        instructions1 = "去碰金币,点亮它吧."
        arcade.draw_text(instructions1, 10, 90, arcade.color.WHITE, 14,font_name='simhei')
        instructions2 = "去碰右下角的道具把亮着的金币销毁吧."
        arcade.draw_text(instructions2, 10, 70, arcade.color.WHITE, 14,font_name='simhei' )

        # Query the property on the coins and show results.
        coins_are_bright = [coin.intensity == 'bright' for coin in self.coin_list]
        output_any = f"有金币被点亮了吗? : {any(coins_are_bright)}"
        arcade.draw_text(output_any, 10, 40, arcade.color.WHITE, 14,font_name='simhei')
        output_all = f"所有金币都被点亮了? : {all(coins_are_bright)}"
        arcade.draw_text(output_all, 10, 20, arcade.color.WHITE, 14,font_name='simhei')

    def on_mouse_motion(self, x, y, dx, dy):
        """ 处理鼠标移动 """

        # 把玩家的坐标放到鼠标指针的坐标
        self.player_sprite.center_x = x
        self.player_sprite.center_y = y

    def update(self, delta_time):
        """ 移动和游戏逻辑 """

        # 更新所有的金币
        self.coin_list.update()

        # 玩家在on_mouse_motion方法中,坐标已经实时更新

        # 创建所碰到的金币列表
        coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)

        # 遍历所碰到的金币,把它们点亮
        for coin in coins_hit_list:
            coin.intensity = 'bright'
            coin.alpha = 255

        hit_trigger = arcade.check_for_collision(self.player_sprite, self.trigger_sprite)
        if hit_trigger:
            # 返回亮了的金币列表
            intense_sprites = [sprite for sprite in self.coin_list if sprite.intensity == 'bright']
            for coin in intense_sprites:
                coin.kill()


def main():
    """ Main method """
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()