我有一个非常复杂的程序,可以使忍者在屏幕上运行。当您按下时,他会滑动并重新上升。当您向上按时,他应该跳下来然后下来并奔跑。但是忍者会不断跳跃,而摆脱忍者的唯一方法就是当您正好在地面上滑动时。很好,他只有在地面上才能滑动,但IDK为什么他会不断弹跳。
我不知道如何解决此问题,但是我尝试了一些稀疏和代码切换,但没有帮助。以下代码很复杂,并且必须包含图像。我不希望您得到图像并对其进行测试,我只希望专业人士查看它,看看他们是否可以看到我没看到的东西。
我认为这是有问题的代码的主要部分:
if not (isjump):
if Counter > 17:
if keys[pygame.K_UP]:
isjump = True
isrunning = False
isslide = False
Counter = 0
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount = jumpCount -1
else:
isJump = False
jumpCount = 10
这是整个代码:
import pygame
import time
x = 60
y = 300
pygame.init()
Counter = 0
jumpCount = 10
imagerun = 0
imageslide = 0
imagejump = 0
isslide = False
isjump = False
ninjaheight = 128
ninjawidth = 128
isrunning = True
def redrawgame():
global isrunning
win.blit(bg, (bgX, 0)) # draws our first bg image
win.blit(bg, (bgX2, 0))
if isrunning == True:
win.blit(Run[imagerun],(x, y))
if isslide == True:
isrunning = False
win.blit(Slide[imageslide],(x, y))
if isjump == True:
win.blit(Jump[imagejump],(x, y))
pygame.display.update()
bg = (pygame.image.load('bg.jpg'))
bgX = 0
bgX2 = bg.get_width()
win = pygame.display.set_mode((1200, 500))
pygame.display.set_caption("First Game")
Run = [pygame.transform.scale(pygame.image.load('Run__0.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__1.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__2.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__3.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__4.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__5.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__6.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__7.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__8.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Run__9.png'), (ninjaheight, ninjawidth))]
Slide = [pygame.transform.scale(pygame.image.load('Slide__0.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__1.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__2.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__3.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__4.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__5.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__6.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__7.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__8.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Slide__9.png'), (ninjaheight, ninjawidth))]
Jump = [pygame.transform.scale(pygame.image.load('Jump__1.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__2.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__3.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__4.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__5.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__6.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__7.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__8.png'), (ninjaheight, ninjawidth)), pygame.transform.scale(pygame.image.load('Jump__9.png'), (ninjaheight, ninjawidth))]
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
time.sleep(0.025)
Counter += 1
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
if not isslide == True:
if Counter > 17:
if jumpCount == 10:
isrunning = False
isjump = False
isslide = True
Counter = 0
imageslide = 0
if isrunning == True:
if imagerun == 9:
imagerun = 0
else:
imagerun +=1
if isslide == True:
if imageslide == 9:
isslide = False
isrunning = True
else:
imageslide += 1
if not (isjump):
if Counter > 17:
if keys[pygame.K_UP]:
isjump = True
isrunning = False
isslide = False
Counter = 0
else:
isJump = False
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount = jumpCount -1
else:
isJump = False
jumpCount = 10
bgX -= 15.4 # Move both background images back
bgX2 -= 15.4
if bgX < bg.get_width() * -1: # If our bg is at the -width then reset its position
bgX = bg.get_width()
if bgX2 < bg.get_width() * -1:
bgX2 = bg.get_width()
redrawgame()
if isslide == False and isjump == False:
isrunning = True
pygame.quit()
对于将整个代码放入但发生错误的IDK感到很抱歉。我希望有人能提供帮助。这是我做过的最难的程序,所以非常令人困惑。随意评论其他混乱的事情。非常感谢所有的人,他们非常友善,可以帮助像我这样的菜鸟。谢谢您,互联网上的一个随便的家伙。