如何在 Pygame 中让角色跳跃? [英] How to make a character jump in Pygame?

查看:126
本文介绍了如何在 Pygame 中让角色跳跃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看 Pygame 教程来学习该模块,它来到了讲师向您展示如何让您的角色跳跃的部分.但是,我发现他们写的代码无法理解,他们也没有很好地解释.

I have been watching Pygame tutorials to learn the module, and it came to the portion where the instructor shows you how to make your character jump. However, I am finding it impossible to understand the code they put down, and they did not explain it very well.

谁能帮我分解下面的代码,以便我确切地了解发生了什么?是否有一种更简单的方法来为角色编码跳转?请记住,我已经将代码设置为按空格键激活此代码的位置.

Can anyone break down for me the code below so I understand exactly what is happening? And is there a simpler way of coding a jump for a character? Please keep in mind I already have the code set up to where pressing the spacebar makes this code activate.

Isjump = False
Jumpcount = 10

#code for spacebar activation here, turns Isjump to True# 

if Jumpcount >= -10:
    Neg = 1
    if Jumpcount < 0:
        Neg = -1
    y -= (Jumpcount ** 2) * 0.5 * Neg
    Jumpcount -= 1
else:
    Isjump = False
    Jumpcount = 10

推荐答案

在开始时 Jumpcount 设置为 10.

At the started Jumpcount is set 10.

Jumpcount = 10

跳转运行直到 Jumpcount 小于或等于 -10.因此,一次跳转正好需要 21 个周期:

The jump runs until Jumpcount is less or equal -10. Therefore, a jump takes exactly 21 cycles:

if Jumpcount >= -10:

Neg 是signe";跳转计数.如果 Jumpcount 大于或等于 0,则为 1,否则为 -1:

Neg is "signe" of Jumpcount. It is 1 if Jumpcount is greater or equal to zero and -1 otherwise:

Neg = 1
if Jumpcount < 0:
    Neg = -1

在每一帧中,玩家的y坐标被二次函数(Jumpcount ** 2) * 0.5改变.

In each frame, the player's y coordinate is changed by the quadratic function (Jumpcount ** 2) * 0.5.

y -= (Jumpcount ** 2) * 0.5 * Neg

由于本项乘以Neg,如果Jumpcount大于0则为正,如果Jumpcount为0则为0,否则为0小于0.
Jumpcount的数量大时,y坐标的变化大于数量小时.查看 21 个循环中 (Jumpcount ** 2) * 0.5 * Neg 的值:

Since this term is multiplied by Neg, it is positive if Jumpcount is greater than 0, and 0 if Jumpcount is 0, otherwise it is less than 0.
When the amount of Jumpcount is large, the change in the y coordinate is greater than when the amount is small. See the values for (Jumpcount ** 2) * 0.5 * Neg in the 21 cycles:

50.0, 40.5, 32.0, 24.5, 18.0, 12.5, 8.0, 4.5, 2.0, 0.5, 0.0, 
-0.5, -2.0, -4.5, -8.0, -12.5, -18.0, -24.5, -32.0, -40.5, -50.0

一开始,数值为正,玩家跳跃.最后数值为负,玩家摔倒.
这些值的总和为 0.因此 y 坐标在末尾与开头具有相同的值

At the beginning the values are positive and the player jumps. In the end the values are negative and the player falls down.
The sum of this values is 0. Therefore the y-coordinate has the same value at the end as at the beginning

这篇关于如何在 Pygame 中让角色跳跃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆