"""
带动画人物行走角色示例。本程序通过操作一个小人的上下左右移动去收集金币。
"""
import arcade
import random
import os
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "带动画人物行走角色示例: 改编及注释:李兴球"
COIN_SCALE = 1.0
COIN_COUNT = 50
MOVEMENT_SPEED = 5
class MyGame(arcade.Window):
""" 定义游戏类. """
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 = None
def setup(self):
self.all_sprites_list = arcade.SpriteList()
self.coin_list = arcade.SpriteList()
# 实例化动画步行角色对象
self.score = 0
self.player = arcade.AnimatedWalkingSprite() # 动画走动角色类
character_scale = 0.45 # 角色缩放比例
self.player.stand_right_textures = []
self.player.stand_right_textures.append(arcade.load_texture("images/Pico walk1.png",
scale=character_scale))
self.player.stand_left_textures = []
self.player.stand_left_textures.append(arcade.load_texture("images/Pico walk1.png",
scale=character_scale, mirrored=True))
self.player.walk_right_textures = []
self.player.walk_right_textures.append(arcade.load_texture("images/Pico walk1.png",
scale=character_scale))
self.player.walk_right_textures.append(arcade.load_texture("images/Pico walk2.png",
scale=character_scale))
self.player.walk_right_textures.append(arcade.load_texture("images/Pico walk3.png",
scale=character_scale))
self.player.walk_right_textures.append(arcade.load_texture("images/Pico walk4.png",
scale=character_scale))
self.player.walk_left_textures = []
self.player.walk_left_textures.append(arcade.load_texture("images/Pico walk1.png",
scale=character_scale, mirrored=True))
self.player.walk_left_textures.append(arcade.load_texture("images/Pico walk2.png",
scale=character_scale, mirrored=True))
self.player.walk_left_textures.append(arcade.load_texture("images/Pico walk3.png",
scale=character_scale, mirrored=True))
self.player.walk_left_textures.append(arcade.load_texture("images/Pico walk4.png",
scale=character_scale, mirrored=True))
self.player.texture_change_distance = 20
self.player.center_x = SCREEN_WIDTH // 2
self.player.center_y = SCREEN_HEIGHT // 2
self.player.scale = 0.8
self.all_sprites_list.append(self.player) # 增加到所有角色列表
for i in range(COIN_COUNT):
coin = coin = arcade.Sprite("images/coin_01.png", 0.21)
coin.center_x = random.randrange(SCREEN_WIDTH) # 给金币分配随机x坐标
coin.center_y = random.randrange(SCREEN_HEIGHT) # 给金币分配随机y坐标
self.coin_list.append(coin) # 增加到金币角色列表
self.all_sprites_list.append(coin) # 增加到所有角色列表
# 设置背景颜色
arcade.set_background_color(arcade.color.AMAZON)
def on_draw(self):
"""
渲染屏幕
"""
# 在画其它角色之前此代码要先执行
arcade.start_render()
# 画所有的角色
self.all_sprites_list.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.change_y = MOVEMENT_SPEED
elif key == arcade.key.DOWN:
self.player.change_y = -MOVEMENT_SPEED
elif key == arcade.key.LEFT:
self.player.change_x = -MOVEMENT_SPEED
elif key == arcade.key.RIGHT:
self.player.change_x = MOVEMENT_SPEED
def on_key_release(self, key, modifiers):
"""
键盘松开某键时调用此方法
"""
if key == arcade.key.UP or key == arcade.key.DOWN:
self.player.change_y = 0
elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
self.player.change_x = 0
def update(self, delta_time):
""" 移动与游戏逻辑 """
self.all_sprites_list.update() # 所有角色更新
self.all_sprites_list.update_animation() # 所有角色更新动画
# 玩家角色和金币列表之间的碰撞检测
hit_list = arcade.check_for_collision_with_list(self.player, self.coin_list)
# 把碰到的金币删除,并加分
for coin in hit_list:
coin.kill()
self.score += 1
def main():
""" 主要方法"""
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) # 实例化一个游戏
window.setup()
arcade.run()
if __name__ == "__main__":
main()
