小学生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.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次。
可以关注我,点赞我、评论我、收藏我啦。

Learn and use Python and PyGame to design and build cool arcade games. In Program Arcade Games: With Python and PyGame, Second Edition, Dr. Paul Vincent Craven teaches you how to create fun and simple quiz games; integrate and start using graphics; animate graphics; integrate and use game controllers; add sound and bit-mapped graphics; and build grid-based games. After reading and using this book, you'll be able to learn to program and build simple arcade game applications using one of today's most popular programming languages, Python. You can even deploy onto Steam and other Linux-based game systems as well as Android, one of today's most popular mobile and tablet platforms. You'll learn: How to create quiz games How to integrate and start using graphics How to animate graphics How to integrate and use game controllers How to add sound and bit-mapped graphics How to build grid-based games Audience This book assumes no prior programming knowledge. Table of Contents Chapter 1: Before Getting Started… Chapter 2: Create a Custom Calculator Chapter 3: What Is a Computer Language? Chapter 4: Quiz Games and If Statements Chapter 5: Guessing Games with Random Numbers and Loops Chapter 6: Introduction to Graphics Chapter 7: Back to Looping Chapter 8: Introduction to Lists Chapter 9: Introduction to Animation Chapter 10: Functions Chapter 11: Controllers and Graphics Chapter 12: Bitmapped Graphics and Sound Chapter 13: Introduction to Classes Chapter 14: Introduction to Sprites Chapter 15: Libraries and Modules Chapter 16: Searching Chapter 17: Array-Backed Grids Chapter 18: Sorting Chapter 19: Exceptions Chapter 20: Recursion Chapter 21: Formatting Chapter 22: Exercises
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信息化未来

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值