为什么我的角色不会在 pygame/python 中跳跃 [英] Why won't my character jump in pygame/python

查看:64
本文介绍了为什么我的角色不会在 pygame/python 中跳跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我制作的第一个 Pygame 游戏.

This is my first Pygame game that I'm making.

我打算制作一个带有基本方块/方块的 2 人格斗游戏.

I am planning on making a 2 player fighting game with basic squares/blocks.

我现在正在尝试实现跳转到第一个字符,在我看来,所有逻辑看起来都是正确的,目前我没有任何错误.但是,当我尝试按 w 跳转时,没有任何反应!

I am now trying to implement jumping into the first character and in my eyes all the logic looks correct and I have got no errors at the moment. However, when I try pressing w to jump, nothing happens!

感谢所有帮助.(P.S 如果你想知道为什么我的脚本中没有类,我似乎无法理解类,它们让我很困惑:P)谢谢,这是我的代码:

All help appreciated. (P.S if you're wondering why I have no classes in my script, I cannot seem to get my head around classes and they confuse me a lot :P) Thanks and here's my code:

import pygame

pygame.init()

#Sets up 8 bit colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
lightblue = (180,235,255)
grassgreen =(20,200,50)

#Sets up pygame window
gameDisplay = pygame.display.set_mode((1000,600))
pygame.display.set_caption('Block Fighter')

gameExit = False

#Variables
x = 50
y = 480
x_change = 0
y_change = 0

clock = pygame.time.Clock()

#Main Game Loop
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                x_change = -5
            if event.key == pygame.K_d:
                x_change = 5
            if event.key == pygame.K_w:
                y_change = -5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_d:
                x_change = 0

    if y <= 480:
        onGround = False
    else:
        onGround = True
    if onGround == False:
        y_change = 5
    if onGround == True:
        y_change = 0


    x += x_change
    y += y_change

    gameDisplay.fill(lightblue)
    pygame.draw.rect(gameDisplay, grassgreen, [1000,600,-1000,-100])
    pygame.draw.rect(gameDisplay, black, [x,y,20,20])
    pygame.display.update()

    clock.tick(40)

pygame.quit()
quit()

推荐答案

当你按w跳跃时,游戏会将y_change的值改为-5为故意的.处理完事件队列后,它继续执行此块:

When you press w to jump, the game will change the value of y_change to -5 as intended. When the event queue has been processed, it goes on to this block:

if y <= 480:
        onGround = False
else:
    onGround = True
if onGround == False:
    y_change = 5
if onGround == True:
    y_change = 0

此时尚未模拟您的物理,因此您的角色仍在地面上.换句话说,onGround 仍然是 True.因此,if onGround == True: y_change = 0 代码被执行,并且 y_change 再次被设置回 0.

Your physics has not been simulated yet at this point, so your character is still on the ground. In other words, onGround is still True. Therefore, the if onGround == True: y_change = 0 code gets executed, and y_change is set back to 0 again.

在这发生之后,您的物理模拟发生,并且 y_change 现在为 0.这就是您的角色不跳跃的原因.你需要重新设计你的代码,以便游戏在检查他是否在地上时意识到你的角色开始跳跃.祝您游戏愉快!

After this has happened, your physics simulation happens, and y_change is now 0. This is why your character does not jump. You need to redesign your code so that the game realizes that your character is starting to jump when it's checking whether he's on the ground or not. Good luck with your game!

这篇关于为什么我的角色不会在 pygame/python 中跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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