0

I'm currently making a projectile motion simulation for sub-orbital flights. At the moment the program plots points in the shape of a parabola and connects lines between the points. A red square then travels the path of the parabola to simulate a rocket travelling the projected trajectory, the problem is however, that the program is very buggy, over-complicated and takes up more lines of code than i wanted it to.

The red square searches for the first point in a list and decreases the distance between the two, after the x values match, it then searches for the next set of coordinates in the list.

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()

How can i make this more efficient and achieve the same thing in less lines?

  • Working code should be placed on http://codereview.stackexchange.com – OneCricketeer Feb 22 '16 at 01:55
  • 1
    @cricket_007 It is possible that this particular question *might* be a fit for [Code Review](http://codereview.stackexchange.com/help/on-topic), the conjecture that *all* working code should automatically go to [Code Review](http://codereview.stackexchange.com/help/on-topic) is less than accurate. – nhgrif Feb 22 '16 at 01:57
  • @nhgrif no at all, clearly, but this question is a good candidate seeing as there has not been a question asked here other than "how can I make this better" – OneCricketeer Feb 22 '16 at 02:01

0 Answers0