2016-04-08 48 views
-1

我想做一個簡單的自上而下駕駛模擬器,你按住向上箭頭鍵移動並使用右/左箭頭鍵來操縱。理想情況下,如果您要同時按住向上鍵和向左或向右鍵,汽車會循環移動。一個簡單的駕駛遊戲的問題

無論方向如何,汽車應該在屏幕上移動相同的距離。我設計了一組方程來計算給定方向(以度爲單位)的x和y座標。它把每個動作看成一個直角三角形。斜邊是汽車不管方向而移動的設定距離。另外兩邊是達到特定斜邊長度所需的x和y值。它使用餘弦函數找到一邊,畢達哥拉斯定理找到最後一邊。

我在方格紙上測試過它,每次都會移動相同的距離,而不管方向如何。問題在於汽車不能在一圈內移動(如果你繼續轉向)。默認方向是0度,所以當您按住向上鍵時,汽車會直線向上移動。如果您開始順時針轉動(右箭頭鍵),汽車將開始向右彎曲。但在某個時候它不會圍成一圈。嘗試運行代碼,這將是有道理的。

*的方向轉換爲弧度,因爲這是Python使用

import pygame, math 

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

# The center of the sceen 
x = 475 
y = 325 

drive = 0 # 0 = not moving, 1 = moving 
turn = 0 # 1 = clockwise, -1 = counter-clockwise 

d = 0 

def move(d, c): 
    d = math.radians(d) 
    a = math.cos(d) * c 
    b = math.sqrt((c**2) - (a**2)) 

    return a, b 


def main(): 
    while True: 
     global x, y, drive, turn, d 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_UP: 
        drive = 1 
       if event.key == pygame.K_RIGHT: 
        turn = 1 
       if event.key == pygame.K_LEFT: 
        turn = -1 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_UP: 
        drive = 0 
       if event.key == pygame.K_RIGHT: 
        turn = 0 
       if event.key == pygame.K_LEFT: 
        turn = 0 


     if drive == 1: 
      if turn == 1 and d != 359: # Turn Clockwise 
       d += 4 
      if turn == 1 and d == 359: 
       d = 0 
      if turn == -1 and d != 0: # Turn Counter Clockwise 
       d -= 4 
      if turn == -1 and d == 0: 
       d = 359 


     ''' move()[0] = a 
      move()[1] = b ''' 
     if drive == 1: 
      if d >= 0 and d < 90: 
       x += move(d, 6)[1] 
       y -= move(d, 6)[0] 


      if d >= 90 and d < 180: 
       x += move(d-90, 6)[0] 
       y += move(d-90, 6)[1] 


      if d >= 180 and d < 270: 
       x -= move(d-90, 6)[1] 
       y += move(d-90, 6)[0] 


      if d >= 270 and d < 360: 
       x -= move(d-180, 6)[1] 
       y += move(d-180, 6)[0] 



     screen.fill((40,40,40)) 
     pygame.draw.rect(screen, (0,0,255), (round(x, 0), round(y, 0), 50, 50)) 


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


main() 
+0

在您的代碼中,如果d> 360'或d <0'會發生什麼?我想這會發生很多,例如,'d + = 4'永遠不會等於359,因此如果足夠順時針旋轉,則'd> 360'。 – wflynny

+0

我沒有意識到這一點。感謝您的關注。它根本不會移動。 –

回答

1

繼我上面的評論,看起來如果你改變這樣的:

if drive == 1: 
     if turn == 1 and d != 359: # Turn Clockwise 
      d += 4 
     if turn == 1 and d == 359: 
      d = 0 
     if turn == -1 and d != 0: # Turn Counter Clockwise 
      d -= 4 
     if turn == -1 and d == 0: 
      d = 359 

​​

它不停止移動。但是,您的規則可能是急劇簡化。從sin(-y) = -sin(y)cos(-x) = cos(x)開始,通過更新您的x,y座標直接使用cos/sin來使用三角函數的全部功能。您的整個腳本可能如下所示:

def main(): 
    # define these here since you aren't modifying them outside of main 
    x = 475 
    y = 325 
    drive = 0 # 0 = not moving, 1 = moving 
    turn = 0 # 1 = clockwise, -1 = counter-clockwise 
    # set to -90 since 0 points east. 
    d = -90 

    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_UP: 
        drive = 1 
       elif event.key == pygame.K_RIGHT: 
        turn = 1 
       elif event.key == pygame.K_LEFT: 
        turn = -1 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_UP: 
        drive = 0 
       elif event.key in (pygame.K_RIGHT, pygame.K_LEFT): 
        turn = 0 

     if drive == 1: 
      d += turn * 4 

      x += 6 * math.cos(math.radians(d)) 
      y += 6 * math.sin(math.radians(d)) 

     screen.fill((40,40,40)) 
     pygame.draw.rect(screen, (0,0,255), (round(x, 0), round(y, 0), 50, 50)) 

     pygame.display.update() 
     clock.tick(20) 
+0

謝謝!這確實有很大的幫助。它完美地工作,但我不太明白d + = turn * 4這條線。此外,爲什麼東0度而不是北? –