2016-04-27 42 views
0

我想用Python/PyGame做一個簡單的平臺遊戲。我似乎無法弄清楚編程跳躍機制的簡單方法。我達到了角色(藍色方塊)將沿着拋物線移動的點,但它只在某個x座標處的拋物線中移動。運行源代碼,你會明白我的意思。理想情況下,該塊將像馬里奧動作一樣在超級馬里奧比賽中移動。我會感謝任何幫助。如何將Quadratics編程爲簡單的平臺遊戲?

這裏是我的代碼:

import pygame, math 
pygame.init() 

screen = pygame.display.set_mode((1000, 700)) 
clock = pygame.time.Clock() 
pygame.display.set_caption('Platformer') 

def main(): 

    x = 0 
    y = 0 
    x_move = 0 
    jump = 0 

    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_RIGHT: 
        x_move = 5 
       if event.key == pygame.K_LEFT: 
        x_move = -5 
       if event.key == pygame.K_SPACE: 
        jump = 1 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT: 
        x_move = 0 
       if event.key == pygame.K_SPACE and y == 0: 
        jump = 0 

     if jump == 1: 
      y = -0.01 * (x ** 2) + (x/50) + 300 
      y = round(y) 
     if y < 0: 
      y = 0 

     x += x_move 

     screen.fill((40,40,40)) 
     pygame.draw.rect(screen, (0,0,255), (x+500, 650-y, 50, 50)) 

     pygame.display.update() 
     clock.tick(60) 


main() 
+1

我發現有點難以看到你想要做什麼。爲什麼y座標在跳躍時依賴於x?它應該依賴於時間嗎?即'y = s + 1/2 * a * t ** 2',其中t是跳躍以來的時間,s是初始y座標,a是重力? – xthestreams

回答