Pygame:精灵动画理论 - 需要反馈

发布于 2024-07-17 01:29:19 字数 640 浏览 3 评论 0原文

对我从某人那里得到的一些代码进行一些调整后,使角色图像根据其方向和上下左右输入移动,我将其放在一起:(希望代码不要太混乱)

角色移动代码 + IMG

Sprite 表仅纵向运行,因此基本上每个 sprite 部分都是不同的动作。 现在是否有一种方法可以制作一个与当前代码一起运行的代码,以便从一组“动作”向下循环以制作动画?

例如: “向左跑”是精灵 3。那么,在我们指定该列之后,是否可以循环运行动画的多少帧(假设是 4)来制作动画?

示例图片: 替代文本http://animania1.ca/ShowFriends/dev/example.jpg< /a>

After some tweaking of some code I got from someone to cause a characters images to move in regards to its direction and up down left right input I've put this together: (hope the code isn't too messy)

Character Move Code + IMG

The Sprite sheet only runs lengthwise, so basically each sprite section is a different action. Now would there be a way I could make a code that functions with the current one to cycle down from a set 'action' in order to make an animation?

For example:
'Run Left' is sprite 3. So then after we designate that column would it be possible to loop down how ever many frames of the run animation (lets say 4) in order to make an animation?

Example Picture:
alt texthttp://animania1.ca/ShowFriends/dev/example.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2024-07-24 01:29:19

这应该很容易。

如果将帧编号记录在变量中,则可以将其与必须获得要显示的动画帧编号的帧数进行模运算。

frame_count = 0
animation_frames = 4
while quit == False:
    # ...
    # snip
    # ...
    area = pygame.Rect(
        image_number * 100,
        (frame_count % animation_frames) * 150,
        100,
        150
    )
    display.blit(sprite, sprite_pos, area)
    pygame.display.flip()
    frame_count += 1

如果不同的动作具有不同的帧数,则在更新 image_number 时必须更新animation_frames。

另外,这假设可以从任何帧开始播放动画。 如果情况并非如此,您需要记录操作开始时的帧计数,并将其从模数之前的帧计数中取出:

    area = pygame.Rect(
        image_number * 100,
        ((frame_count - action_start_frame) % animation_frames) * 150,
        100,
        150
    )

有关事件处理的注释。 如果您按住左键,然后点击右键,但继续按住左键,则精灵会停止移动,因为您处理的最后一个事件是按键事件,尽管我仍然按住左键。

如果这不是您想要的,您可以通过记录您感兴趣的按键的向上/向下状态或使用 pygame.key.get_pressed 接口。

另一方面,您似乎目标是固定的帧速率,同时根据最后一帧所花费的时间确定精灵移动的距离。 在我看来,这可能并不理想。

2D 动作游戏通常需要以可预测的方式运行。 如果某些 CPU 繁重的进程在您计算机的后台启动,导致您的游戏不再能够每秒生成 60 帧,那么最好放慢速度,而不是让您的对象开始在帧之间跳过很长的距离。 想象一下,如果这种情况发生在像《合金弹头》这样的 2D 动作游戏中,你必须四处跳跃以避免子弹?

这也使得任何物理计算变得更加简单。 您必须根据游戏类型做出判断。

It should be easy.

If you record the frame number in a variable, you can modulo this with the number of frames you have to get an animation frame number to display.

frame_count = 0
animation_frames = 4
while quit == False:
    # ...
    # snip
    # ...
    area = pygame.Rect(
        image_number * 100,
        (frame_count % animation_frames) * 150,
        100,
        150
    )
    display.blit(sprite, sprite_pos, area)
    pygame.display.flip()
    frame_count += 1

If different actions have different numbers of frames, you'll have to update animation_frames when you update image_number.

Also, this assumes that it's ok to play the animation starting at any frame. If this is not the case, you'll need to record what the frame count was when the action started, and take this away from frame count before the modulo:

    area = pygame.Rect(
        image_number * 100,
        ((frame_count - action_start_frame) % animation_frames) * 150,
        100,
        150
    )

A note about your event handling. If you hold down, say, left, and tap right but keep holding down left, the sprite stops moving because the last event you processed was a keyup event, despite the fact that I'm still holding left.

If this is not what you want, you can get around it by either keeping a record of the up/down states of the keys you are interested in, or by using the pygame.key.get_pressed interface.

On another note, you appear to be aiming for a fixed frame rate, and at the same time determining how far to move your sprite based on the time taken in the last frame. In my opinion, this probably isn't ideal.

2D action games generally need to work in a predictable manner. If some CPU heavy process starts in the background on your computer and causes your game to no longer be able to churn out 60 frames a second, it's probably preferable for it to slow down, rather then have your objects start skipping huge distances between frames. Imagine if this happened in a 2D action game like Metal Slug where you're having to jump around avoiding bullets?

This also makes any physics calculations much simpler. You'll have to make a judgement call based on what type of game it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文