Python 我的角色在这个pygame脚本中停止了跳跃,为什么?

Python 我的角色在这个pygame脚本中停止了跳跃,为什么?,python,pygame,Python,Pygame,这就是问题的全部所在,在早期版本中,跳转函数正常工作,但由于为精灵的pygame窗口添加了固定边框,因此无法再跳转,下面是完整的代码,尽管大部分代码都不相关,请注意变量名和含义: #Imports import pygame import sys import math #Pygame Initialisation pygame.init() windowWidth=500 windowHeight=500 win = pygame.display.set_mode((windowWidt

这就是问题的全部所在,在早期版本中,跳转函数正常工作,但由于为精灵的pygame窗口添加了固定边框,因此无法再跳转,下面是完整的代码,尽管大部分代码都不相关,请注意变量名和含义:

#Imports

import pygame
import sys
import math

#Pygame Initialisation

pygame.init()
windowWidth=500
windowHeight=500
win = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption("ScreenShove WIP")

#Movement Initialisation ((c) means constant)

x=250       #X Position
y=434       #Y Position
dx=0        #Perceived Change in X
dy=0        #Perceived Change in Y
width=40    #Character Width (c)
height=60   #Character Height (c)
ax=3        #X axis acceleration (c)
ay=20       #Y axis acceleration (c)
xprev=0     #Previous X Position
yprev=0     #Previous Y Position
isJump=False#Is the sprite performing a jump?
dylock=0    #Counts the number of frames dy has been 0 in a jump
border=5    #Border size (c)

run = True
while run:

pygame.time.delay(20) #50 ish fps
#Add 1 indentation for all the following lines
#Exit Check

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

#Get Keypresses

keys = pygame.key.get_pressed()

#Y Movement Decay

assumeddx=x-xprev   #Recalculates "dx" based on previous positions.
assumeddy=y-yprev   #Recalculates "dy" based on previous positions.
xprev=x             #Fixes "xprev" and "yprev" for the next frame.
yprev=y

if isJump == True:                                          #If the sprite is performing a jump:
    if keys[pygame.K_s] and y<windowHeight-height-border:   #If the "s" key is pressed:
        dy+=math.ceil(ay/2)                                 #Increase the change in Y by half the Y axis acceleration constant
    if assumeddy==0 or dy==0:                               #If the calculated or perceived change in Y is 0:
        if dylock>=2:                                       #If the change in Y is equal to 0 at least twice this jump:
            isJump=False                                    #Declare the sprite has finished performing a jump
            dylock=0                                        #Reset the counter
            dy=0                                            #Set the perceived change in Y to 0
        else:                                               #Otherwise:
            dylock+=1                                       #Increase the counter by 1
    dy+=1                                                   #Increase the perceived change in Y by 1.
else:                                                       #Otherwise:
    dy=0                                                    #Set the perceived chnage in Y to 0.

#X Movement Decay

if dx>0:                                                    #If the perceived change in X is positive:
    dx-=1                                                   #Reduce it by 1
if dx<0:                                                    #If the perceived change in X is negative:
    dx+=1                                                   #Increase it by 1

#User Movement Changes

if keys[pygame.K_a] and x>=dx+border and dx>-10:                #If the "a" key is pressed and the sprite is in a valid position and the change in X exceeds -10:
    dx-=ax                                                      #Reduce the change in X by the X axis acceleration constant
elif keys[pygame.K_d] and x<windowWidth-width-border and dx<10: #If the "d" key is pressed and the sprite is in a valid position and the change in X does not exceed 10:
    dx+=ax                                                      #Increase the change in X by the X axis acceleration constant
if keys[pygame.K_w]:                                            #If the "w" key is pressed:
    if y >= dy+border and isJump==False and jumplock==False:    #If the sprite is in a valid position, is not performing a jump already and the jump key isn't locked:
        dy-=ay                                                  #Decrease the change in Y by the Y axis acceleration constant
        isJump = True                                           #Declare the sprite is performing a jump.
    jumplock=True                                               #Lock the jump key.
else:                                                           #Otherwise:
    jumplock=False                                              #Unlock the jump key.

#Border Fix for Velocity

if dx<0:                                                        #If the change in X is positive
    if x>=-dx+border:                                           #If the sprite is in a valid position
        x+=dx                                                   #Increase the X position by the change in X
    else:                                                       #Otherwise:
        dx=0                                                    #Set the change in X to 0
        x=border                                                #Set the X position to the nearest valid X position
if dx>0:
    if x<=windowWidth-width-border:
        x+=dx
    else:
        dx=0
        x=windowWidth-width-border
if dy>0:
    if y<=windowHeight-height-border:
        y+=dy
    else:
        dy=0
        y=windowHeight-height-border

#Border Fix for Position

if x<0+border:
    x=border
if x>windowWidth-width-border:
    x=windowWidth-width-border
if y>windowHeight-height-border:
    y=windowHeight-height-border

#Debugging

print("P:",x,y)
print("V:",dx,dy)
print("Jumping:",str(isJump))
print("Jump Locked:",str(jumplock))

#Draw to Screen

win.fill((0,0,0))
pygame.draw.rect(win, (0, 200 ,0), (x,y,width,height))
pygame.display.update()

#No more indentations

#Exit

pygame.quit()
sys.exit()
p表示位置

V表示速度或位置变化

两者都以“X Y”表示

跳跃是布尔值“isJump”和 跳转锁定是布尔值“跳转锁定”。

而不是执行此操作:

if dy>0:
    if y<=windowHeight-height-border:
        y+=dy
    else:
        dy=0
        y=windowHeight-height-border
如果dy>0:

如果不提这一点,那么在开发某些代码时,您确实可以从更好的结构中获益匪浅。如果添加或删除代码开始导致bug,这是一个很好的提示,表明结构需要改进。我认为这是因为这一行的计算结果为false。你的调试器给你提示了吗<代码>如果键[pygame.K_s]和y实际上什么都没做:(我认为这和位置或速度部分的边界固定有关。
if dy>0:
    if y<=windowHeight-height-border:
        y+=dy
    else:
        dy=0
        y=windowHeight-height-border
if y<=windowHeight-height-border:
    y+=dy
elif dy>0:
    dy=0
    y=windowHeight-height-border