Pygame |遵循抛物线曲线

时间:2016-02-22 01:50:26

标签: python pygame

我目前正在为亚轨道飞行进行射弹运动模拟。目前,该程序以抛物线的形状绘制点并连接点之间的线。然后红色方块沿着抛物线的路径行进以模拟火箭沿着预计的轨迹行进,但问题是,该程序非常错误,过于复杂并占用了比我想要的更多的代码行。

红色方块搜索列表中的第一个点并缩小两者之间的距离,在x值匹配后,它会搜索列表中的下一组坐标。

x_coord = []
y_coord = []
x_neg = []
y_neg = []
rocketx = 0
rockety = 0 
points_created = False
zoomx = 200
zoomy = 100
rocketSpeed = 0.1
coord_count = 0
rocketx = 0
rockety = display_height

while equation == True:
    len_coordx = len(x_coord)
    display.fill(white)

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

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                zoomx += 10
                del x_coord[:]
                del x_neg[:]
                del y_coord[:]
                points_created = False
            if event.key == K_LEFT:
                zoomx -= 10
                del x_coord[:]
                del x_neg[:]
                del y_coord[:]
                points_created = False
            if event.key == K_UP:
                zoomy += 2
                del x_coord[:]
                del x_neg[:]
                del y_coord[:]
                points_created = False
            if event.key == K_DOWN:
                zoomy -= 2
                del x_coord[:]
                del x_neg[:]
                del y_coord[:]
                points_created = False

    if points_created == False:
        for x in[float(j) / 100 for j in range(0, 200, 10)]:
            y = x**2 *zoomy + display_height/4
            y_coord.append(y)
            x_coord.append(x*zoomx + display_width/2)

        for x in x_coord:
            x_neg.append(x*-1 + display_width)

        points_created = True
        coord_count_neg = len(x_neg) - 1
        rocketx = x_neg[coord_count_neg] - 1
        rockety = y_coord[coord_count_neg] - 1


    if len(x_coord) > 0:
        for i in range(len_coordx):
            pygame.draw.rect(display, black, [x_coord[i], y_coord[i], 3, 3])
            pygame.draw.rect(display, black, [x_neg[i], y_coord[i], 3, 3])
            if i > 0:
                pygame.draw.line(display, black, (x_coord[i-1], y_coord[i-1]), (x_coord[i], y_coord[i]), 3)
                pygame.draw.line(display, black, (x_neg[i-1], y_coord[i-1]), (x_neg[i], y_coord[i]), 3)


    if coord_count_neg != 0:
        distance = math.sqrt((x_neg[coord_count_neg] - rocketx)**2 + (y_coord[coord_count_neg] - rockety)**2)

        newCoordx = round((((rocketSpeed*(x_neg[coord_count_neg] - rocketx))/distance) + rocketx),1)
        newCoordy = round((((rocketSpeed*(y_coord[coord_count_neg] - rockety))/distance) + rockety),1)
        rocketx = newCoordx
        rockety = newCoordy

        if rocketx >= x_neg[coord_count_neg]:
            coord_count_neg -= 1

    else:            

        distance = math.sqrt((x_coord[coord_count] - rocketx)**2 + (y_coord[coord_count] - rockety)**2)

        newCoordx = round((((rocketSpeed*(x_coord[coord_count] - rocketx))/distance) + rocketx),1)
        newCoordy = round((((rocketSpeed*(y_coord[coord_count] - rockety))/distance) + rockety),1)
        rocketx = newCoordx
        rockety = newCoordy

        if rocketx >= x_coord[coord_count]:
            coord_count += 1
        pygame.draw.rect(display,red,[rocketx, rockety, 10,10])


    pygame.draw.rect(display,red,[rocketx, rockety, 10,10])


pygame.display.update()

我如何才能更有效率地在更少的线条中实现同样的目标?

0 个答案:

没有答案