I have a pretty complicated program that makes a ninja run across the screen. When you press down he slides and comes back up. And when you press up he should jump then come down and run. But the ninja keeps jumping and the only way to get out of it is if you slide when you are exactly on the ground. Its fine that he only slides when on the ground but IDK why he keeps bouncing.
I Don't know how to fix this but I have tried a couple thins and switches in the code but it didn't help. The following code is complicated and has to include images. I don't expect you to get images and test it, I just want pros to look over it and see if they can see something I didn't.
I think this is the main part in the code with problem:
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
Here is the whole Code:
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()
I'm sorry for putting entire code in but IDK where the error is happening. I hope that somebody can help. This is the Hardest Program I've Ever Made so it is very confusing. Feel free to comment on other things that are messing it up. And THANK YOU SO MUCH to all the people nice enough to just go help noobs like me. Thank You, A Random Guy On The Internet.