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


    前言

    接上篇文章小学生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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    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
    • 3
    • 4
    • 5
    • 6
    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
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    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()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303

    源码获取

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

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

  • 相关阅读:
    离散数学 --- 命题逻辑 -- 命题符号化与命题公式
    tag单调栈-单调栈预备知识-lt.739. 每日温度
    react使用 Ant ui框架
    JVM基础:初识JVM
    Nginx proxy_pass 详解
    【附源码】Python计算机毕业设计面向移动平台的企业会议室预约系统
    甲骨文真的要开放Java EE?
    全网最全jmeter接口测试/接口自动化测试看这篇文章就够了:跨线程组传递jmeter变量及cookie的处理
    如何管理和维护组件库?
    Docker之最细的基础知识入门和底层架构解析
  • 原文地址:https://blog.csdn.net/fqfq123456/article/details/127753958