Trajectory Plotter - main.py
unknown
python
a year ago
9.1 kB
8
Indexable
import pygame, sys, data, functions, math pygame.init() #CONSTANTS BACKGROUND = pygame.image.load("Star_Background.jpg") FPS = 60 WINDOW_WIDTH = 1280 WINDOW_HEIGHT = 800 font1 = pygame.font.Font(None, 32) font2 = pygame.font.Font("BELL.TTF", 100) font2.set_underline(True) font3 = pygame.font.Font("BELL.TTF", 28) font4 = pygame.font.Font("BELL.TTF", 80) font4.set_underline(True) home_page_text1 = font2.render("Trajectory Plotter", True, (255, 255, 255)) home_page_text2 = font1.render("Press any button to continue... ", True, (255, 255, 255)) info_page_text = font4.render("Welcome to the Trajectory Plotter!", True, (255, 255, 255)) error_text = font1.render("Error: Invalid Input", True, (255, 0, 0)) paragraph = ( "This program is used to plot Hohmann interplanetary transfers.", "All planet radii and orbital speeds are relative to real life", "however, planets have been scalled up by 1000x to make them more", "visible.", " ", "Insert the names for the home planet and target planet in their corresponding", "boxes and hit the plot button to see the trajectory of your space ship!", "Ensure to spell planet names correctly.", " ", "Clicking on a planetary body focuses on it for easy tracking.", "Use the UP and DOWN arrow keys to zoom in and out.", "Use the LEFT and RIGHT arrow keys to speed up or slow down time.") #VARIABLES running = True home_page = True plot_page = False info_page = False plotted = False scalar = 1 centre = (640, 400) focus = data.Sun time_scalar = 1 error_counter = 0 num_boxes = 4 #Sets up the output window WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("Trajectory Plotter") while running: #------------------------HOME PAGE------------------------ while home_page: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() #Continues to info page if any button is clicked if (event.type == pygame.MOUSEBUTTONDOWN) or (event.type == pygame.KEYDOWN): info_page = True home_page = False WINDOW.blit(BACKGROUND, (0, 0)) WINDOW.blit(home_page_text1, (640 - (home_page_text1.get_width() / 2), 350)) WINDOW.blit(home_page_text2, (640 - (home_page_text2.get_width() / 2), 480)) pygame.display.update() #------------------------INFO PAGE------------------------ while info_page: data.info_button.active = False for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() #Continues to plot page if any button is clicked if (event.type == pygame.MOUSEBUTTONDOWN) or (event.type == pygame.KEYDOWN): plot_page = True info_page = False #Displays info page and its relevent text WINDOW.blit(BACKGROUND, (0, 0)) WINDOW.blit(info_page_text, (640 - (info_page_text.get_width() / 2), 100)) for line in paragraph: info_text = font1.render(line, True, (255, 255, 255)) WINDOW.blit(info_text, (640 - (info_text.get_width() / 2), 300 + (30 * paragraph.index(line)))) pygame.display.update() #------------------------PLOT PAGE------------------------ while plot_page: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: #triggers event if mouse pressed for obj in data.boxes: if pygame.Rect(obj.xcoord, obj.ycoord, obj.width, obj.height).collidepoint(event.pos): obj.clicked = True obj.input_text = "" plotted = False num_boxes = 4 else: obj.clicked = False for obj in data.planets: #checks to see if planet has been clicked on if (math.sqrt( (abs(centre[0] + obj.get_planet_position_x(scalar) - event.pos[0]))**2 + (abs(centre[1] + obj.get_planet_position_y(scalar) - event.pos[1]))**2) <= obj.planet_radius / (scalar * 10000)): focus = obj break else: pass elif event.type == pygame.KEYDOWN: #triggers event if key pressed for obj in data.boxes[0:2]: if obj.clicked: if event.key == pygame.K_RETURN: #saves entered value obj.active = False elif event.key == pygame.K_BACKSPACE: #deletes last character obj.input_text = obj.input_text[:-1] else: if len(obj.input_text ) < 10: #limits text to 10 characters obj.input_text += event.unicode #adds character to text obj.input_text = obj.input_text.replace( ".", "") obj.input_text = obj.input_text.replace( " ", "") obj.input_text = obj.input_text.capitalize() else: pass else: pass if event.key == pygame.K_DOWN: #zooms out (a little bit) scalar *= 1.2 elif event.key == pygame.K_UP: #zooms in (a little bit) scalar /= 1.2 elif event.key == pygame.K_LEFT: #speeds down time time_scalar /= 2 elif event.key == pygame.K_RIGHT: #speeds up time time_scalar *= 2 mouse_position = pygame.mouse.get_pos() #gets mouse position for obj in data.boxes: #checks if mouse is over box if pygame.Rect(obj.xcoord, obj.ycoord, obj.width, obj.height).collidepoint(mouse_position): if obj.active is False: #changes box colour if mouse is over box obj.colour = max(obj.colour[0] - 50, 0), max(obj.colour[1] - 50, 0), max(obj.colour[2] - 50, 0) obj.active = True else: if obj.active is True: obj.colour = max(obj.colour[0] + 50, 0), max(obj.colour[1] + 50, 0), max(obj.colour[2] + 50, 0) obj.active = False centre = (640 - focus.get_planet_position_x(scalar), #sets new centre 400 - focus.get_planet_position_y(scalar)) WINDOW.blit(BACKGROUND, (0, 0)) functions.draw_planet(centre, scalar, data.planets, WINDOW) if data.plot_button.clicked == True: #validates inputs if data.home_planet_box.input_text in data.planet_names and data.target_planet_box.input_text in data.planet_names and data.target_planet_box.input_text != data.home_planet_box.input_text: plotted = True else: error_counter = 255 plotted = False data.plot_button.clicked = False if data.info_button.clicked == True: #shows info page info_page = True plot_page = False data.info_button.clicked = False if data.end_plot_button.clicked == True: #ends plotting num_boxes = 4 plotted = False data.end_plot_button.clicked = False if error_counter > 0: #displays error message error_text.set_alpha(error_counter) WINDOW.blit(error_text, (640 - (error_text.get_width() / 2), 100)) error_counter -= 1 if plotted == True: #plots trajectory functions.draw_plot( centre, scalar, data.planet_names[ data.home_planet_box.input_text.capitalize()], data.planet_names[ data.target_planet_box.input_text.capitalize()], WINDOW) num_boxes = 5 functions.draw_box(font1, font3, data.boxes, num_boxes, WINDOW) pygame.display.update() if plotted == False: functions.travel_time(data.planets, time_scalar) pygame.time.Clock().tick(FPS)
Editor is loading...
Leave a Comment