Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t让攻击动画在使用python的arcade库中工作_Python_Animation_Arcade - Fatal编程技术网

Can';t让攻击动画在使用python的arcade库中工作

Can';t让攻击动画在使用python的arcade库中工作,python,animation,arcade,Python,Animation,Arcade,我使用的是arcade.academy的教程,游戏目前运行良好(一个家伙四处收集硬币和得分),但我希望在用户按下Q时出现攻击动画,这样我可以将一些硬币变成敌人,制作一个小RPG,增加我的知识 这是我到目前为止得到的,但由于某种原因,self.is_攻击布尔值要么没有触发,要么没有正确连接 这是我到目前为止得到的 import arcade import random import os SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 SCREEN_TITLE =

我使用的是arcade.academy的教程,游戏目前运行良好(一个家伙四处收集硬币和得分),但我希望在用户按下Q时出现攻击动画,这样我可以将一些硬币变成敌人,制作一个小RPG,增加我的知识

这是我到目前为止得到的,但由于某种原因,self.is_攻击布尔值要么没有触发,要么没有正确连接

这是我到目前为止得到的

import arcade
import random
import os

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Move with a Sprite Animation Example"

COIN_SCALE = 0.5
COIN_COUNT = 50
CHARACTER_SCALING = 1

MOVEMENT_SPEED = 5
UPDATES_PER_FRAME = 7

# Constants used to track if the player is facing left or right
RIGHT_FACING = 0
LEFT_FACING = 1


def load_texture_pair(filename):
    """
    Load a texture pair, with the second being a mirror image.
    """
    return [
        arcade.load_texture(filename, scale=CHARACTER_SCALING),
        arcade.load_texture(filename, scale=CHARACTER_SCALING, mirrored=True)
    ]


class PlayerCharacter(arcade.Sprite):
    def __init__(self):

        # Set up parent class
        super().__init__()

        # Default to face-right
        self.character_face_direction = RIGHT_FACING

        # Used for flipping between image sequences
        self.cur_texture = 0
        self.attack_texture = 0

        # Track our state
        self.jumping = False
        self.climbing = False
        self.is_on_ladder = False
        self.is_attacking = False

        # Adjust the collision box. Default includes too much empty space
        # side-to-side. Box is centered at sprite center, (0, 0)
        self.points = [[-22, -64], [22, -64], [22, 28], [-22, 28]]

        # --- Load Textures ---

        # Images from Kenney.nl's Asset Pack 3
        main_path = "images/Sprites/rogue like character/rogue like"
        # main_path = "platform_tutorial/images/Female person/PNG/Poses/character_femalePerson"
        # main_path = "platform_tutorial/images/Male person/PNG/Poses/character_malePerson"
        # main_path = "platform_tutorial/images/Male adventurer/PNG/Poses/character_maleAdventurer"
        # main_path = "platform_tutorial/images/Zombie/PNG/Poses/character_zombie"
        # main_path = "platform_tutorial/images/Robot/PNG/Poses/character_robot"

        # Load textures for idle standing
        self.idle_texture_pair = load_texture_pair(f"{main_path} idle_Animation 1_0.png")

        # Load textures for walking
        self.walk_textures = []
        for i in range(6):
            texture = load_texture_pair(f"{main_path} run_Animation 1_{i}.png")
            self.walk_textures.append(texture)

        # Load textures for attacking
        self.attack_textures = []
        for i in range(10):
            texture = load_texture_pair(f"{main_path} attack_Animation 1_{i}.png")
            self.attack_textures.append(texture)

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

        # Figure out if we need to flip face left or right
        if self.change_x < 0 and self.character_face_direction == RIGHT_FACING:
            self.character_face_direction = LEFT_FACING
        elif self.change_x > 0 and self.character_face_direction == LEFT_FACING:
            self.character_face_direction = RIGHT_FACING

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

        # Walking animation
        self.cur_texture += 1
        if self.cur_texture > 5 * UPDATES_PER_FRAME:
            self.cur_texture = 0
        self.texture = self.walk_textures[self.cur_texture // UPDATES_PER_FRAME][self.character_face_direction]

        # Attacking animation
        if self.is_attacking:
            self.cur_texture += 1
            if self.cur_texture > 9 * UPDATES_PER_FRAME:
                self.cur_texture = 0
                self.is_attacking = False
            self.texture = self.attack_textures[self.cur_texture // UPDATES_PER_FRAME][self.character_face_direction]


class MyGame(arcade.Window):
    # Main application class.

    def __init__(self, width, height, title):

        # Initializer

        super().__init__(width, height, title)

        # Set the working directory (where we expect to find files) to the same
        # directory this .py file is in. You can leave this out of your own
        # code, but it is needed to easily run the examples using "python -m"
        # as mentioned at the top of this program.
        file_path = os.path.dirname(os.path.abspath(__file__))
        os.chdir(file_path)

        # Set up the game and initialize the variables.

        # Sprite lists
        self.player_list = None
        self.coin_list = None

        # Set up the player
        self.score = 0
        self.player = None
        self.is_attacking = False

    def setup(self):
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()

        # Set up the player
        self.score = 0
        self.player = PlayerCharacter()

        self.player.center_x = SCREEN_WIDTH // 2
        self.player.center_y = SCREEN_HEIGHT // 2
        self.player.scale = 0.8

        self.player_list.append(self.player)

        for i in range(COIN_COUNT):
            coin = arcade.AnimatedTimeSprite(scale=0.5)
            coin.center_x = random.randrange(SCREEN_WIDTH)
            coin.center_y = random.randrange(SCREEN_HEIGHT)

            coin.textures = []
            coin.textures.append(arcade.load_texture("images/items/Coingold.png", scale=COIN_SCALE))
            coin.textures.append(arcade.load_texture("images/items/Coingold.png", scale=COIN_SCALE))
            coin.textures.append(arcade.load_texture("images/items/Coingold.png", scale=COIN_SCALE))
            coin.textures.append(arcade.load_texture("images/items/Coingold.png", scale=COIN_SCALE))
            coin.textures.append(arcade.load_texture("images/items/Coingold.png", scale=COIN_SCALE))
            coin.textures.append(arcade.load_texture("images/items/Coingold.png", scale=COIN_SCALE))
            coin.cur_texture_index = random.randrange(len(coin.textures))

            self.coin_list.append(coin)

        # Set the background color
        arcade.set_background_color(arcade.color.AMAZON)

    def on_draw(self):

        # Render the screen.


        # This command has to happen before we start drawing
        arcade.start_render()

        # Draw all the sprites.
        self.coin_list.draw()
        self.player_list.draw()

        # Put the text on the screen.
        output = f"Score: {self.score}"
        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)

    def on_key_press(self, key, modifiers):

        # Called whenever a key is pressed.

        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
        elif key == arcade.key.Q:
            self.is_attacking = True

    def on_key_release(self, key, modifiers):

        # Called when the user releases a key.

        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
        elif key == arcade.key.Q:
            self.is_attacking = False

    def on_update(self, delta_time):
        # Movement and game logic 

        self.coin_list.update()
        self.coin_list.update_animation()
        self.player_list.update()
        self.player_list.update_animation()

        # Generate a list of all sprites that collided with the player.
        hit_list = arcade.check_for_collision_with_list(self.player, self.coin_list)

        # Loop through each colliding sprite, remove it, and add to the score.
        for coin in hit_list:
            coin.remove_from_sprite_lists()
            self.score += 1

def main():
    # Main method 
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    window.setup()
    arcade.run()


if __name__ == "__main__":
    main()
导入arcade
随机输入
导入操作系统
屏幕宽度=800
屏幕高度=600
SCREEN\u TITLE=“使用精灵动画示例移动”
硬币刻度=0.5
硬币计数=50
字符缩放=1
移动速度=5
每帧更新\u=7
#用于跟踪玩家是向左还是向右的常数
右向=0
左向=1
def加载纹理对(文件名):
"""
加载纹理对,第二个是镜像。
"""
返回[
arcade.load_纹理(文件名,比例=字符比例),
arcade.load\u纹理(文件名,比例=字符比例,镜像=真)
]
类玩家角色(arcade.Sprite):
定义初始化(自):
#设置父类
super()。\uuuu init\uuuuu()
#默认面朝右
self.character\u face\u direction=右侧
#用于在图像序列之间翻转
self.cur\u纹理=0
self.attack_纹理=0
#跟踪我们的状态
自我跳跃=错误
自我攀爬=错误
self.is_on_梯形图=错误
self.is_=False
#调整碰撞框。默认值包含太多的空白
#肩并肩。长方体在精灵中心居中,(0,0)
self.points=[-22,-64],[22,-64],[22,28],-22,28]]
#---加载纹理---
#来自Kenney.nl的资产包3的图片
main_path=“图像/精灵/类流氓角色/类流氓”
#main\u path=“平台教程/images/femalePerson/PNG/Poses/character\u femalePerson”
#main\u path=“平台教程/images/Male person/PNG/Poses/character\u Male person”
#main\u path=“平台教程/图像/男性冒险家/PNG/Poses/character\u男性冒险家”
#main\u path=“平台教程/images/Zombie/PNG/Poses/character\u Zombie”
#main\u path=“平台教程/images/Robot/PNG/Poses/character\u Robot”
#为空闲站立加载纹理
self.idle_texture_pair=加载_texture_pair(f“{main_path}idle_Animation 1_0.png”)
#加载用于行走的纹理
self.walk_纹理=[]
对于范围(6)中的i:
纹理=加载纹理对(f“{main_path}运行动画1{i}.png”)
self.walk\u纹理.附加(纹理)
#加载用于攻击的纹理
self.attack_纹理=[]
对于范围(10)内的i:
纹理=加载纹理对(f“{main_path}攻击\动画1{i}.png”)
self.attack_textures.append(纹理)
def更新动画(自身,增量时间:浮点=1/60):
#弄清楚我们是需要向左还是向右翻转面部
如果self.change\u x<0且self.character\u face\u direction==右向:
self.character\u face\u direction=左向
elif self.change\u x>0且self.character\u face\u direction==左向:
self.character\u face\u direction=右侧
#空闲动画
如果self.change_x==0和self.change_y==0:
self.texture=self.idle\u纹理\u对[self.character\u face\u direction]
返回
#行走动画
self.cur_纹理+=1
如果self.cur\u纹理>5*每帧更新一次:
self.cur\u纹理=0
self.texture=self.walk\u纹理[self.cur\u纹理//每帧更新一次][self.character\u面\u方向]
#攻击动画
如果self.u正在攻击:
self.cur_纹理+=1
如果self.cur\u纹理>9*每帧更新一次:
self.cur\u纹理=0
self.is_=False
self.texture=self.attack\u纹理[self.cur\u纹理//每帧更新一次][self.character\u面\u方向]
类MyGame(街机窗口):
#主应用程序类。
定义初始(自身、宽度、高度、标题):
#初始值设定项
超级()
#将工作目录(我们希望在其中查找文件)设置为相同的
#此.py文件所在的目录。你可以把这件事留给你自己
#代码,但需要使用“python-m”轻松运行示例
#如本课程顶部所述。
file_path=os.path.dirname(os.path.abspath(uu file_uu))
chdir(文件路径)
#设置游戏并初始化变量。
#精灵列表
self.player\u list=无
self.coin_list=无
#摆好球员
self.score=0
self.player=无
self.is_=False
def设置(自):
self.player_list=arcade.SpriteList()
self.coin_list=arcade.SpriteList()
#摆好球员
self.score=0
self.player=PlayerCharacter()
self.player.center\u x=屏幕宽度//2
self.player.center\u y=屏幕高度//2
self.player.scale=0.8
self.player\u list.append(self.player)
对于范围内的i(硬币计数):
硬币=拱廊。动画时间精灵(比例=0.5)
coin.center\u x=random.randrange(屏幕宽度)
coin.center\u y=random.randrange(屏幕高度)
coin.textures=[]
coin.textures.append(arcade.load_纹理(“images/items/Coingold.png”,scale=coin_scale))
coin.textures.append(arcade.load_纹理(“images/items/Coingold.png”,scale=coin_scale))
coin.textures.append(arcade.load_纹理(“images/items/Coingold.png”,scale=coin_scale))
coin.textures.append(arcade.load_纹理(“images/items/Coingold.png”),缩放=
self.player.is_attacking=True