arcade演示爆炸效果的例子

arcade演示爆炸效果的例子

"""
演示爆炸效果的例子
"""
import random
import arcade
import os

SPRITE_SCALING_PLAYER = 0.5
SPRITE_SCALING_COIN = 0.2
SPRITE_SCALING_LASER = 0.8
COIN_COUNT = 50

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "演示爆炸效果的例子,译:lixingqiu"

BULLET_SPEED = 5

EXPLOSION_TEXTURE_COUNT = 60


class Explosion(arcade.Sprite):
    """ 创建可爆炸的角色 """ 

    def __init__(self, texture_list):
        super().__init__("images/explosion/explosion0000.png")

        # 第一帧
        self.current_texture = 0      # 这相当于造型索引号
        self.textures = texture_list  # 这相当于每帧图片列表

    def update(self):

        # 更新每帧的图片,到了最后一帧后就会删除自己。
        self.current_texture += 1
        if self.current_texture < len(self.textures):
            self.set_texture(self.current_texture)
        else:
            self.kill()


class MyGame(arcade.Window):
    """ Main application class. """

    def __init__(self):
        """ Initializer """
        # 调用父类的初始化器
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        # 设置工作目录 not by lixingqiu
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # 定义要用到的角色列表们
        self.player_list = None
        self.coin_list = None
        self.bullet_list = None
        self.explosions_list = None

        # 定义玩家变量
        self.player_sprite = None
        self.score = 0

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

        # 加载声音对象
        # self.gun_sound = arcade.sound.load_sound("sounds/laser1.wav")
        # self.hit_sound = arcade.sound.load_sound("sounds/phaseJump1.wav")
        # 设置背景颜色
        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):

        """ 设置与实例化游戏中要用到的变量 """

        # 角色列表
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()
        self.bullet_list = arcade.SpriteList()
        self.explosions_list = arcade.SpriteList()

        # Pre-load the animation frames. We don't do this in the __init__ because it
        # takes too long and would cause the game to pause.
        # 预装爆炸效果的动画帧图,不在初始化方法中加载的原因是这要花太多的时间会引起游戏卡
        self.explosion_texture_list = []

        for i in range(EXPLOSION_TEXTURE_COUNT):
            # 加载从 explosion0000.png 到 explosion0270.png 的所有图片为爆炸效果动画帧            
            texture_name = f"images/explosion/explosion{i:04d}.png"
            self.explosion_texture_list.append(arcade.load_texture(texture_name))

        # 设置玩家的初始得分
        self.score = 0

        # 实例化玩家对象
        self.player_sprite = arcade.Sprite("images/character.png", SPRITE_SCALING_PLAYER)
        self.player_sprite.center_x = 50
        self.player_sprite.center_y = 70
        self.player_list.append(self.player_sprite)

        # 创建金币
        for coin_index in range(COIN_COUNT):

            # 实例化一个金币对象
            coin = arcade.Sprite("images/coin_01.png", SPRITE_SCALING_COIN)

            # 放置到随机坐标
            coin.center_x = random.randrange(SCREEN_WIDTH)
            coin.center_y = random.randrange(150, SCREEN_HEIGHT)

            # 增加到金币列表
            self.coin_list.append(coin)

        # 设置背景颜色
        arcade.set_background_color(arcade.color.AMAZON)

    def on_draw(self):
        """
        Render the screen.
        """

        # 此命令要在所有命之前
        arcade.start_render()

        # 画所有的角色
        self.coin_list.draw()
        self.bullet_list.draw()
        self.player_list.draw()
        self.explosions_list.draw()

        # 显示得分情况文本
        arcade.draw_text(f"Score: {self.score}", 10, 20, arcade.color.WHITE, 14)

    def on_mouse_motion(self, x, y, dx, dy):
        """
        鼠标指针移动事件
        """
        self.player_sprite.center_x = x

    def on_mouse_press(self, x, y, button, modifiers):
        """
        单击鼠标时调用此方法
        """

        # 枪声
        # arcade.sound.play_sound(self.gun_sound)

        # 创建一子弹
        bullet = arcade.Sprite("images/laserBlue01.png", SPRITE_SCALING_LASER)

        # 图像是朝向的,所以要旋转它 
        bullet.angle = 90

        # 给它一个y速度
        bullet.change_y = BULLET_SPEED

        # 放到玩家坐标
        bullet.center_x = self.player_sprite.center_x
        bullet.bottom = self.player_sprite.top

        # 加到子弹角色列表
        self.bullet_list.append(bullet)

    def update(self, delta_time):
        """ Movement and game logic """

        # 调用子弹列表和爆炸效果列表更新
        self.bullet_list.update()
        self.explosions_list.update()

        # 遍历每颗子弹做碰撞检测
        for bullet in self.bullet_list:

            # 子弹和所有金币的碰撞检测
            hit_list = arcade.check_for_collision_with_list(bullet, self.coin_list)

            # 如果碰到了,生成爆炸效果
            if len(hit_list) > 0:
                explosion = Explosion(self.explosion_texture_list)
                explosion.center_x = hit_list[0].center_x
                explosion.center_y = hit_list[0].center_y
                self.explosions_list.append(explosion)
                bullet.kill()

            #每个金币都删除并加分
            for coin in hit_list:
                coin.kill()
                self.score += 1

                # 击中声效
                # arcade.sound.play_sound(self.hit_sound)

            #  子弹离屏,删除它
            if bullet.bottom > SCREEN_HEIGHT:
                bullet.kill()


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


if __name__ == "__main__":
    main()
李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。