角色面向左或右造型示例

"""
Sprite Facing Left or Right
角色面向左或右造型示例,这个程序需要街机模块支持。2019-2-28最新版本安装方法:
在命令提示符中输入以下命令:pip install arcade==2.0.0b4
如果缺少ffmpeg,则再次用pip命令安装即可。
Arcade模块是用来制作街机游戏的一个库。
"""

import arcade      # 导入街机模块
import os          # 导入os模块

SPRITE_SCALING = 0.5 # 常量定义,角色比例

SCREEN_WIDTH = 800   # 常量定义,屏幕宽度
SCREEN_HEIGHT = 600  # 常量定义,屏幕高度
SCREEN_TITLE = "角色面向左或右造型示例"

MOVEMENT_SPEED = 5

TEXTURE_LEFT = 0
TEXTURE_RIGHT = 1

class Player(arcade.Sprite):

    def __init__(self):
        super().__init__()
        # 注释翻译:lixingqiu
        # 加载一个向左造型图和一个向右造型图
        # mirrored=True 这会让图像镜像翻转
        texture = arcade.load_texture("images/character.png", mirrored=True, scale=SPRITE_SCALING)
        self.textures.append(texture)
        texture = arcade.load_texture("images/character.png", scale=SPRITE_SCALING)
        self.textures.append(texture)

        # 缺省是朝右方向的
        self.set_texture(TEXTURE_RIGHT) #  TEXTURE_RIGHT就是1
        
    def update(self):
        self.center_x += self.change_x
        self.center_y += self.change_y

        # 根据change_x的值决定使用哪个造型
        if self.change_x < 0:
            self.set_texture(TEXTURE_LEFT)
        if self.change_x > 0:
            self.set_texture(TEXTURE_RIGHT)

        if self.left < 0:  #  边界检测,如果最左x坐标小于0则让它等于0
            self.left = 0
        elif self.right > SCREEN_WIDTH - 1:
            self.right = SCREEN_WIDTH - 1

        if self.bottom < 0:
            self.bottom = 0
        elif self.top > SCREEN_HEIGHT - 1:
            self.top = SCREEN_HEIGHT - 1


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

    def __init__(self, width, height, title):
        """
        Initializer
        """

        # 调用父类初始化方法
        super().__init__(width, height, title)

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

        # 所有角色列表的定义
        self.all_sprites_list = None

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

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

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

        # 实例化所有角色列表
        self.all_sprites_list = arcade.SpriteList()

        # 实例化玩家对象
        self.player_sprite = Player()
        self.player_sprite.center_x = SCREEN_WIDTH / 2  # 屏幕x中央
        self.player_sprite.center_y = SCREEN_HEIGHT / 2 # 屏幕y中央
        self.all_sprites_list.append(self.player_sprite)# 添加到所有角色列表

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

        # 开始重画所有
        arcade.start_render()

        # 正式地画所有角色.
        self.all_sprites_list.draw()

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

        # 所有角色更新坐标等等
        self.all_sprites_list.update()

    def on_key_press(self, key, modifiers):
        """按键检测 """

        if key == arcade.key.UP: # 按向上方向箭头则change_y为正值
            self.player_sprite.change_y = MOVEMENT_SPEED
        elif key == arcade.key.DOWN:
            self.player_sprite.change_y = -MOVEMENT_SPEED
        elif key == arcade.key.LEFT:
            self.player_sprite.change_x = -MOVEMENT_SPEED
        elif key == arcade.key.RIGHT:
            self.player_sprite.change_x = MOVEMENT_SPEED

    def on_key_release(self, key, modifiers):
        """松键检测 """

        if key == arcade.key.UP or key == arcade.key.DOWN:
            self.player_sprite.change_y = 0
        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
            self.player_sprite.change_x = 0


def main():
    """ Main method """
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()