小学生python游戏编程arcade----基本知识4角色动画

发布于:2022-11-09 ⋅ 阅读:(6) ⋅ 点赞:(0) ⋅ 评论:(0)

前言

接上篇文章小学生python游戏编程arcade----基本知识1、2,连接如下:小学生python游戏编程arcade----基本知识小学生python游戏编程arcade----基本知识2,小学生python游戏编程arcade----基本知识3

角色动画

1、角色动画

1.1角色动画类

1.1.1 定义,初始图片及公共变量

class PlayerCharacter(arcade.Sprite):
“”“角色类”“”
def init(self):
super().init()
# 默认面向右
self.character_face_direction = R_facing
# 当前图片
self.cur_texture = 0
self.scale = PLAYER_scaling

    # 解色状态
    self.jumping = False
    self.climbing = False
    self.is_on_ladder = False

    # 文件路径
    main_path = "images/female_Person/femalePerson"

    # Load textures for idle standing
    self.idle_texture_pair = load_texture_pair(f"{main_path}_idle.png")
    self.jump_texture_pair = load_texture_pair(f"{main_path}_jump.png")
    self.fall_texture_pair = load_texture_pair(f"{main_path}_fall.png")

    # 加载行走图片
    self.walk_textures = []
    for i in range(8):
        texture = load_texture_pair(f"{main_path}_walk{i}.png")
        self.walk_textures.append(texture)

    # 加载爬行图片
    self.climbing_textures = []
    texture = arcade.load_texture(f"{main_path}_climb0.png")
    self.climbing_textures.append(texture)
    texture = arcade.load_texture(f"{main_path}_climb1.png")
    self.climbing_textures.append(texture)

    # 设置初始图片
    self.texture = self.idle_texture_pair[0]    
1.1.2 更新函数 update_animation(self, delta_time: float = 1 / 60)
    def update_animation(self, delta_time: float = 1 / 60):

        # 面向转换
        if self.change_x < 0 and self.character_face_direction == R_facing:
            self.character_face_direction = L_facing
        elif self.change_x > 0 and self.character_face_direction == L_facing:
            self.character_face_direction = R_facing

        # 爬行动画
        if self.is_on_ladder:
            self.climbing = True
        if not self.is_on_ladder and self.climbing:
            self.climbing = False
        if self.climbing and abs(self.change_y) > 1:
            self.cur_texture += 1
            if self.cur_texture > 7:
                self.cur_texture = 0
        if self.climbing:
            self.texture = self.climbing_textures[self.cur_texture // 4]
            return

        # 跳动画
        if self.change_y > 0 and not self.is_on_ladder:
            self.texture = self.jump_texture_pair[self.character_face_direction]
            return
        elif self.change_y < 0 and not self.is_on_ladder:
            self.texture = self.fall_texture_pair[self.character_face_direction]
            return

        # Idle animation
        if self.change_x == 0:
            self.texture = self.idle_texture_pair[self.character_face_direction]
            return

        # 行走动画
        self.cur_texture += 1
        if self.cur_texture > 7:
            self.cur_texture = 0
        self.texture = self.walk_textures[self.cur_texture][
            self.character_face_direction
        ]

1.2 player类的使用

1.2.1 setup中
    # 添加角色
    单图片.
    image_source = "images/1-1.png"
    self.player_sprite = arcade.Sprite(image_source, PLAYER_scaling)
    类
    self.player_sprite = PlayerCharacter()
1.2.2 按键处理
def on_key_press(self, key, modifiers):
    """每当按下键时调用."""
    if key == arcade.key.UP or key == arcade.key.W:
        self.up_pressed = True
    elif key == arcade.key.DOWN or key == arcade.key.S:
        self.down_pressed = True
    elif key == arcade.key.LEFT or key == arcade.key.A:
        self.left_pressed = True
    elif key == arcade.key.RIGHT or key == arcade.key.D:
        self.right_pressed = True
    self.process_keychange() 
1.2.3 更新处理

可以一次更新多种动画
self.scene.update_animation(delta_time, [‘wanjia’])

1.3 效果图

在这里插入图片描述

1.4 代码实现

# _*_ coding: UTF-8 _*_
# 开发人员: Administrator
# 开发时间:2022/11/5 16:59

import arcade
from arcadegame.seting import *
import os


def load_texture_pair(filename):
    """ 调用文件 """
    return [
        arcade.load_texture(filename),
        arcade.load_texture(filename, flipped_horizontally=True),
    ]


class PlayerCharacter(arcade.Sprite):
    """角色类"""
    def __init__(self):
        super().__init__()
        # 默认面向右
        self.character_face_direction = R_facing
        # 当前图片
        self.cur_texture = 0
        self.scale = PLAYER_scaling

        # 解色状态
        self.jumping = False
        self.climbing = False
        self.is_on_ladder = False

        # 文件路径
        main_path = "images/female_Person/femalePerson"

        # Load textures for idle standing
        self.idle_texture_pair = load_texture_pair(f"{main_path}_idle.png")
        self.jump_texture_pair = load_texture_pair(f"{main_path}_jump.png")
        self.fall_texture_pair = load_texture_pair(f"{main_path}_fall.png")

        # 加载行走图片
        self.walk_textures = []
        for i in range(8):
            texture = load_texture_pair(f"{main_path}_walk{i}.png")
            self.walk_textures.append(texture)

        # 加载爬行图片
        self.climbing_textures = []
        texture = arcade.load_texture(f"{main_path}_climb0.png")
        self.climbing_textures.append(texture)
        texture = arcade.load_texture(f"{main_path}_climb1.png")
        self.climbing_textures.append(texture)

        # 设置初始图片
        self.texture = self.idle_texture_pair[0]

        # Hit box will be set based on the first image used. If you want to specify
        # a different hit box, you can do it like the code below.
        # set_hit_box = [[-22, -64], [22, -64], [22, 28], [-22, 28]]
        self.hit_box = self.texture.hit_box_points

    def update_animation(self, delta_time: float = 1 / 60):

        # 面向转换
        if self.change_x < 0 and self.character_face_direction == R_facing:
            self.character_face_direction = L_facing
        elif self.change_x > 0 and self.character_face_direction == L_facing:
            self.character_face_direction = R_facing

        # 爬行动画
        if self.is_on_ladder:
            self.climbing = True
        if not self.is_on_ladder and self.climbing:
            self.climbing = False
        if self.climbing and abs(self.change_y) > 1:
            self.cur_texture += 1
            if self.cur_texture > 7:
                self.cur_texture = 0
        if self.climbing:
            self.texture = self.climbing_textures[self.cur_texture // 4]
            return

        # 跳动画
        if self.change_y > 0 and not self.is_on_ladder:
            self.texture = self.jump_texture_pair[self.character_face_direction]
            return
        elif self.change_y < 0 and not self.is_on_ladder:
            self.texture = self.fall_texture_pair[self.character_face_direction]
            return

        # Idle animation
        if self.change_x == 0:
            self.texture = self.idle_texture_pair[self.character_face_direction]
            return

        # 行走动画
        self.cur_texture += 1
        if self.cur_texture > 7:
            self.cur_texture = 0
        self.texture = self.walk_textures[self.cur_texture][
            self.character_face_direction
        ]


class MyGame(arcade.Window):
    def __init__(self):
        # 初始化窗体
        super().__init__(SCREEN_width, SCREEN_height, SCREEN_title)
        self.scene = None
        self.player_sprite = None

        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)
        # 上下左右按键状态
        self.left_pressed = False
        self.right_pressed = False
        self.up_pressed = False
        self.down_pressed = False
        self.jump_needs_reset = False

        arcade.set_background_color(arcade.csscolor.BLUE)

        self.physics_engine = None
        self.camera = None  # 摄象机
        self.gui_camera = None  # 第二摄象机
        self.score = 0
        # 加载声音
        self.collect_coin_sound = arcade.load_sound("sounds/coin2.wav")
        self.jump_sound = arcade.load_sound("sounds/s3.wav")
        self.game_over = arcade.load_sound(":resources:sounds/gameover1.wav")

        # 设置地图
        self.tile_map = None
        # 是否重新设置分数
        self.reset_score = True
        # 地图的最右边
        self.end_of_map = 0
        # 关卡级别
        self.level = 1

    def setup(self):
        # 摄象机
        self.camera = arcade.Camera(self.width, self.height)
        self.gui_camera = arcade.Camera(self.width, self.height)

        # 初始化场景
        # self.scene = arcade.Scene()
        #
        map_name = f"地图\家1.json"  # jia{self.level}.json"

        # 层属性
        layer_options = {
            LAYER_platforms: {
                "use_spatial_hash": True,
            },
            LAYER_tree: {
                "use_spatial_hash": False,
            },
            'fz': {
                "use_spatial_hash": True,
            },
            'tizi': {
                "use_spatial_hash": True,
            },
        }

        # 读地图文件
        self.tile_map = arcade.load_tilemap(map_name, TILE_Scaling, layer_options)
        print(self.tile_map.properties)
        # print(self.tile_map.get_tilemap_layer())
        # 使用我们的TileMap初始化场景,这将自动添加所有层,以正确的顺序在场景中显示为SpriteList。
        self.scene = arcade.Scene.from_tilemap(self.tile_map)
        print('精灵列表', self.scene['tizi'])

        if self.reset_score:
            self.score = 0
        self.reset_score = True

        self.end_of_map = self.tile_map.width * GRID_pixel_size


        # 添加角色.
        self.wanjia = PlayerCharacter()
        self.wanjia.center_x = PLAYER_start_x
        self.wanjia.center_y = PLAYER_start_y
        self.scene.add_sprite_list_after("wj", LAYER_platforms)  # 添加精灵列表,把玩家放在哪一层,前后层在此关健
        self.scene.add_sprite("wj",self.wanjia)
        # self.physics_engine = arcade.PhysicsEnginePlatformer(
        #     self.wanjia, gravity_constant=GRAVITY, walls=self.scene[LAYER_tree]
        # )  # 此处的walls=指定支撑图层
        self.physics_engine = arcade.PhysicsEngineSimple(
            self.wanjia,
            walls=self.scene['fz'])

        # # 设置背景色
        # if self.tile_map.background_color:
        #     arcade.set_background_color(self.tile_map.background_color)

    def on_draw(self):
        self.clear()
        self.camera.use()  # 摄象机
        self.scene.draw()  # 摄相机与scence的顺序不能放错,否则不会移动
        self.gui_camera.use()

        # 在屏幕上绘制分数,用屏幕滚动
        score_text = f"得分: {self.score}"
        arcade.draw_text(score_text, 10, 500, arcade.csscolor.RED, 18, )

    def process_keychange(self):
        """ 当我们向上/向下改变键或上/下梯子时调用.     """
        if not self.up_pressed and not self.down_pressed:
            self.wanjia.change_y = 0
        elif self.down_pressed:
            self.wanjia.change_y = -PLAYER_movement_speed
        elif self.up_pressed:
            self.wanjia.change_y = PLAYER_movement_speed

        # 左右
        if self.right_pressed and not self.left_pressed:
            self.wanjia.change_x = PLAYER_movement_speed
        elif self.left_pressed and not self.right_pressed:
            self.wanjia.change_x = -PLAYER_movement_speed
        else:
            self.wanjia.change_x = 0

    def on_key_press(self, key, modifiers):
        """每当按下键时调用."""
        if key == arcade.key.UP or key == arcade.key.W:
            self.up_pressed = True
        elif key == arcade.key.DOWN or key == arcade.key.S:
            self.down_pressed = True
        elif key == arcade.key.LEFT or key == arcade.key.A:
            self.left_pressed = True
        elif key == arcade.key.RIGHT or key == arcade.key.D:
            self.right_pressed = True

        self.process_keychange()

    def on_key_release(self, key, modifiers):
        """键盘释放时"""
        if key == arcade.key.UP or key == arcade.key.W:
            self.up_pressed = False
            self.jump_needs_reset = False
        elif key == arcade.key.DOWN or key == arcade.key.S:
            self.down_pressed = False
        elif key == arcade.key.LEFT or key == arcade.key.A:
            self.left_pressed = False
        elif key == arcade.key.RIGHT or key == arcade.key.D:
            self.right_pressed = False

        self.process_keychange()

    def center_camera_to_player(self):
        """摄相机随角色移动"""
        screen_center_x = self.wanjia.center_x - (self.camera.viewport_width / 2)
        screen_center_y = self.wanjia.center_y - (self.camera.viewport_height / 2)

        # 防止相机出界
        if screen_center_x < 0:
            screen_center_x = 0
        if screen_center_y < 0:
            screen_center_y = 0
        player_centered = screen_center_x, screen_center_y
        # print(player_centered)
        self.camera.move_to(player_centered)
        # print(self.wanjia.center_x)

    def on_update(self, delta_time):
        """运动和游戏逻辑"""
        self.physics_engine.update()  # 运用引擎移动角色

        # 掉下或level达到最后一关时,游戏结束,或重新开始
        if self.wanjia.center_y < -100 or self.level==3:
            self.wanjia.center_x = PLAYER_start_x
            self.wanjia.center_y = PLAYER_start_y
            arcade.play_sound(self.game_over)

        self.process_keychange()
        # 更新动画
        self.scene.update_animation(delta_time, ['wj'])

        # 是否走到地图尽头
        if self.wanjia.center_x >= self.end_of_map:
            # 关卡升级
            self.level += 1
            # 不需重新积分
            self.reset_score = False
            # 加载下个场景
            self.setup()

        self.center_camera_to_player()   # 摄象机


def main():
    """主程序"""
    window = MyGame()
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()

源码获取

关注博主后,私聊博主免费获取
需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主

今天是以此模板持续更新此育儿专栏的第 14/50次。
可以关注我,点赞我、评论我、收藏我啦。