2016-04-08 52 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度而不是北? –