简单驾驶游戏的问题

Problems With a Simple Driving Game

我正在尝试制作一个简单的自上而下的驾驶模拟器,您可以在其中按住向上箭头键移动并使用 right/left 箭头键进行转向。理想情况下,如果同时按住向上键和向左或向右键,小车会绕圈移动。

无论方向如何,汽车每帧都应在屏幕上移动相同的距离。我设计了一组方程式来计算给定方向(以度为单位)的 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()

根据我上面的评论,如果您更改:

    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

    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) 以来,通过直接使用 cos/sin 更新 x,y 坐标,充分利用三角函数的强大功能。您的整个脚本可能如下所示:

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)