简体   繁体   English

如何在 Pygame 中进行字符跳转?

[英]How to make a character jump in 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.我一直在看Pygame教程学习模块,到了教练教你如何让你的角色跳跃的部分。 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

At the started Jumpcount is set 10.开始时Jumpcount设置为 10。

Jumpcount = 10

The jump runs until Jumpcount is less or equal -10.跳转一直运行到Jumpcount小于或等于 -10。 Therefore, a jump takes exactly 21 cycles:因此,一次跳转恰好需要 21 个周期:

if Jumpcount >= -10:

Neg is "signe" of Jumpcount . NegJumpcount的“符号”。 It is 1 if Jumpcount is greater or equal to zero and -1 otherwise:如果Jumpcount大于或等于 0,则为 1,否则为 -1:

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

In each frame, the player's y coordinate is changed by the quadratic function (Jumpcount ** 2) * 0.5 .在每一帧中,玩家的y坐标由二次 function (Jumpcount ** 2) * 0.5改变。

y -= (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.由于此项乘以Neg ,如果Jumpcount大于 0 则为正,如果Jumpcount为 0 则为 0,否则小于 0。
When the amount of Jumpcount is large, the change in the y coordinate is greater than when the amount is small. Jumpcount的量大时, y坐标的变化比量小的时候大。 See the values for (Jumpcount ** 2) * 0.5 * Neg in the 21 cycles:查看 21 个周期中(Jumpcount ** 2) * 0.5 * Neg的值:

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

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这些值的总和为 0。因此y坐标在末尾与开头具有相同的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM