Python收集金币游戏不会在墙上

"""
本程序演示一个收集金币的小程序.所有的金币都不会在墙上.这是用arcade街机游戏模块制作的样例小程序.

"""
import arcade
import random
import os

SPRITE_SCALING = 0.5
SPRITE_SCALING_COIN = 0.2

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Python收集金币游戏不会在墙上_改编:李兴球"

NUMBER_OF_COINS = 50

MOVEMENT_SPEED = 5


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

    def __init__(self, width, height, title):
        """
        初始化方法,这里是定义一些游戏的属性.
        """
        super().__init__(width, height, title)

        # 设置程序的工作目录 ,使用python -m运行的时候需要,否则可以去掉.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # 定义角色列表
        self.all_sprites_list = None
        self.coin_list = None

        # 设置游戏属性的一些初始值
        self.score = 0
        self.player_sprite = None
        self.wall_list = None
        self.physics_engine = None

    def setup(self):
        """ 设置游戏变量的具体值. """

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

        # 实例化玩家
        self.score = 0
        self.player_sprite = arcade.Sprite("images/princess.png", SPRITE_SCALING*0.4)
        self.player_sprite.center_x = 50
        self.player_sprite.center_y = 64

        # -- 实例化墙
        for y in range(0, 800, 200):
            for x in range(100, 700, 64):
                wall = arcade.Sprite("images/boxCrate_double.png", SPRITE_SCALING)
                wall.center_x = x
                wall.center_y = y
                self.wall_list.append(wall)

        # -- 随机放一些金币
        for i in range(NUMBER_OF_COINS):

            # 创建金币实例
            coin = arcade.Sprite("images/coin_01.png", SPRITE_SCALING_COIN)

            # 金币放置成功的逻辑变量
            coin_placed_successfully = False

            # 直到成功才退出while循环
            while not coin_placed_successfully:
                # 放置到随机位置
                coin.center_x = random.randrange(SCREEN_WIDTH)
                coin.center_y = random.randrange(SCREEN_HEIGHT)

                # 检测是否和墙有碰撞
                wall_hit_list = arcade.check_for_collision_with_list(coin, self.wall_list)

                # 检测是否和其它金币有重叠
                coin_hit_list = arcade.check_for_collision_with_list(coin, self.coin_list)

                if len(wall_hit_list) == 0 and len(coin_hit_list) == 0:
                    # 如果都是0,放置成功
                    coin_placed_successfully = True

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

        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)

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

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

        # 画其它角色之前这句代码要执行
        arcade.start_render()

        # 画所有的角色
        self.wall_list.draw()
        self.coin_list.draw()
        self.player_sprite.draw()
        
        # 放置文本
        output = f"得分: {self.score}"
        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)

    def on_key_press(self, key, modifiers):
        """按键事件. """

        if key == arcade.key.UP:
            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 update(self, delta_time):
        """ 游戏逻辑 """
 
        # 检测玩家是否有没有碰到金币
        coin_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
        for coin in coin_hit_list:
            coin.kill()
            self.score += 1
        self.physics_engine.update()


def main():
    """ 主要函数 """
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()