碰到边缘就反弹的金币收集游戏,译者:lixingqiu

"""
碰到边缘就反弹的金币收集游戏,译者:lixingqiu,这是用Arcade街机模块制作的一个游戏示例。
演示了如何实例化角色,实例化角色列表,类的继承,角色的移动等。当前最新版本是2.0.0b4。
安装方法:pip install arcade==2.0.0b4 (2019/2/27),注释翻译:www.lixingqiu.com
arcade模块是由Paul Vincent Craven开发并维护,专业用来开发街机游戏。

"""

import random
import arcade
import os

# 常量定义
SPRITE_SCALING_PLAYER = 0.5
SPRITE_SCALING_COIN = 0.2
COIN_COUNT = 50

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "碰到边缘就反弹的金币收集游戏,译者:lixingqiu"


class Coin(arcade.Sprite):

    def __init__(self, filename, sprite_scaling):

        super().__init__(filename, sprite_scaling)

        self.change_x = 0
        self.change_y = 0

    def update(self):

        # 移动金币
        self.center_x += self.change_x
        self.center_y += self.change_y

        # 碰到边缘就反弹
        if self.left < 0:
            self.change_x *= -1

        if self.right > SCREEN_WIDTH:
            self.change_x *= -1

        if self.bottom < 0:
            self.change_y *= -1

        if self.top > SCREEN_HEIGHT:
            self.change_y *= -1


class MyGame(arcade.Window):
    """ 继承自Window类的MyGame类"""

    def __init__(self):
        """ 初始化器 """
        # 调用父类的初始化器
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 
        # 所有角色列表定义
        self.all_sprites_list = None
        self.coin_list = None

        # 定义玩家角色相关变量
        self.player_sprite = None
        self.score = 0

        # 隐藏鼠标
        self.set_mouse_visible(False)

        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        """ 设置游戏 """

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

        # 得分
        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 = 50
        self.all_sprites_list.append(self.player_sprite)

        # 实例化一些金币
        for i in range(50):

            # 创建金币对象
            coin = Coin("images/coin_01.png", SPRITE_SCALING_COIN)

            # 随机设定坐标
            coin.center_x = random.randrange(SCREEN_WIDTH)
            coin.center_y = random.randrange(SCREEN_HEIGHT)
            coin.change_x = random.randrange(-3, 4)
            coin.change_y = random.randrange(-3, 4)

            # 增加到所有角色列表
            self.all_sprites_list.append(coin)
            self.coin_list.append(coin)

    def on_draw(self):
        """ 重画方法 """
        arcade.start_render()
        self.all_sprites_list.draw()

        # 放得分文本
        output = f"Score: {self.score}"
        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)

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

        # Move the center of the player sprite to match the mouse x, y
        self.player_sprite.center_x = x
        self.player_sprite.center_y = y

    def update(self, delta_time):
        """ 游戏逻辑更新 """

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

        # 玩家所操作角色和金币的碰撞检测
        hit_list = arcade.check_for_collision_with_list(self.player_sprite,self.coin_list)

        # 碰到的金币被删除并加分
        for coin in hit_list:
            coin.kill()
            self.score += 1


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


if __name__ == "__main__":
    main()