I am making a platform game, so platforms are obviously important, but I have arrived at a problem. I am trying to make my character jump on my platform. I have managed to make it jump onto the platform and move left and right and fall off but once I am on the platform I can't jump. There is also another problem where if the sprite touches the bottom of the platform it will float up until its on top which it would ideally just be prevented from getting through. This issue isn't as important.
def level1(): global running, jumping1, neg1 global x1, y1 , x2, y2, xChange1, yChange1, xChange2, yChange2, jump1, jump2, jumping1, jumping2, isJump while running: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: xChange1 = -5 if event.key == pygame.K_RIGHT: xChange1 = 5 if event.key == pygame.K_UP: if not jumping1: jumping1 = True if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: xChange1 = 0 if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: xChange2 = -5 elif event.key == pygame.K_d: xChange2 = 5 elif event.key == pygame.K_w: if not jumping2: jumping2 = True if event.type == pygame.KEYUP: if event.key == pygame.K_a or event.key == pygame.K_d: xChange2 = 0 if jumping1: if jump1 >= -jumpCount: neg1 = 1 if jump1 <0: neg1 = -1 y1 -= (jump1 **2) * 0.5 * neg1 jump1 -= 1 else: jump1 = jumpCount jumping1 = False if jumping2: if jump2 >= -jumpCount: neg1 = 1 if jump2 <0: neg1 = -1 y2 -= (jump2 **2) * 0.5 * neg1 jump2 -= 1 else: jump2 = jumpCount jumping2 = False x1 += xChange1 x2 += xChange2 y1 += yChange1 y2 += yChange2 p1Rect = player1_image.get_rect(x=x1 + xChange1,y=y1 + yChange1) if p1Rect.colliderect(Level1Platform1Rect): print('player 1 platform 1 collision') jump1 = 0 y1 += -5 if not p1Rect.colliderect(Level1Platform1Rect): y1 += 5 screen.blit(game_image,(0,0)) character1(x1,y1) character2(x2,y2) gameFloor(fx,fy) gameFloor2(0,100) gameFloor2(0,500) gameFloor2(350,300) if x1 < 0: x1 = 0 if x1 > WIDTH - Char1WIDTH: x1 = WIDTH - Char1WIDTH if y1 < 0: y1 = 0 if y1 > HEIGHT - Char1HEIGHT : y1 = HEIGHT - Char1HEIGHT if x2 < 0: x2 = 0 if x2 > WIDTH - Char2WIDTH: x2 = WIDTH - Char2WIDTH if y2 < 0: y2 = 0 if y2 > HEIGHT - Char2HEIGHT: y2 = HEIGHT - Char2HEIGHT pygame.display.flip()
Here is a link to a video of what my program currently looks like: https://kapwi.ng/w/8mZ7VNhC
Here's a link to my full program: https://trinket.io/python/f0c581dd5d
https://stackoverflow.com/questions/66486193/sprite-cant-jump-when-on-platform March 05, 2021 at 11:06AM
没有评论:
发表评论