diff options
Diffstat (limited to 'src/main.py')
-rw-r--r-- | src/main.py | 89 |
1 files changed, 22 insertions, 67 deletions
diff --git a/src/main.py b/src/main.py index 5b6f673..8249535 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,5 @@ import sys import math -from random import randint from engine import Engine from fuel import Fuel @@ -14,6 +13,10 @@ from simulation import Simulation import pygame from pygame.locals import * +from gui.background_sprite import Sprite_Background +from gui.rocket_sprite import Sprite_Rocket +from gui.body_sprite import Sprite_Body + def main(argv): raptor_engine = Engine(name="raptor", isp=360, max_flow_rate=931) #https://en.wikipedia.org/wiki/SpaceX_Raptor methane_fuel = Fuel(name="methane") #TODO: more @@ -65,7 +68,6 @@ def main(argv): ) simulation = Simulation(universe, body, rocket) - simulation.rocket.current_stage().engines_on = True pygame.init() pygame.display.set_caption("OSLS - Overly Simple Launch Simulator") @@ -74,11 +76,23 @@ def main(argv): SCREEN_WIDTH = 1024 SCREEN_HEIGHT = 720 + sprite_group = pygame.sprite.Group() + + #cloud_sprite = Sprite_Cloud(simulation) #TODO + + floor = 100 #100px is the floor of the drawing (where the rocket stops and where the body surface is) + + sprite_group.add(Sprite_Background(simulation), + Sprite_Body(simulation, floor), + Sprite_Rocket(simulation, floor)) + simulation_display = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) paused = False while True: if not paused: - draw_simulation(simulation_display, simulation) + sprite_group.update(simulation_display.get_width(), simulation_display.get_height()) + sprite_group.draw(simulation_display) + draw_text_info(simulation_display, simulation) pygame.display.update() for event in pygame.event.get(): @@ -94,40 +108,18 @@ def main(argv): handle_key_press(simulation, event.key) delta = clock.tick(60) / 1000 #60fps #are we using delta in the simulation tick everywhere needed? - if not paused: #tick with pause messes up delta TODO: TODOODODODODODOOD TODO + if not paused: print("delta: " + str(delta)) simulation.tick(delta=delta) - #TODO: draw body sprite, rocket sprite, clouds sprites, etc. #TODO: implement gravity properly (x and y) + #TODO: implement apoapsis and periapsis calculation + #TODO: draw good rocket sprite, persistant clouds and star sprites, etc. + #TODO: do actual aerodynamic load so heading depends on that and not on speed #TODO: do max load on rocket so it blows up #TODO: allow multilanguage api for landing algorithms etc -def draw_simulation(simulation_display: type[pygame.Surface], simulation: type[Simulation]) -> None: - #draw background - def linear_gradient(start_color, end_color, length, value_at): - return [ - int(start_color[j] + (float(value_at)/(length-1))*(end_color[j]-start_color[j])) - for j in range(3) - ] - - def get_color_for_height(height: float) -> (int, int, int): - if height < 70000: - return linear_gradient((31,118,194), (0, 0, 0), 70000, int(height)) - else: - return (0, 0, 0) - - #gradient for atmosphere - simulation_display.fill(get_color_for_height(simulation.rocket_altitude())) - - #draw clouds and stars - #draw clouds (we need continuity TODO) - #if simulation.y < 20000 and randint(0, 100) < 5: - # pygame.draw.circle(simulation_display, (255, 255, 255), (randint(0, simulation_display.get_width()), randint(0, simulation_display.get_height())), 30) - #draw stars - if simulation.rocket_altitude() > 30000: - for _ in range(100): - simulation_display.set_at((randint(0, simulation_display.get_width()), randint(0, simulation_display.get_height())), (255, 255, 255)) +def draw_text_info(simulation_display: type[pygame.Surface], simulation: type[Simulation]) -> None: #draw stats text font = pygame.font.SysFont("Comic Sans MS", 30) @@ -152,43 +144,6 @@ def draw_simulation(simulation_display: type[pygame.Surface], simulation: type[S simulation_display.blit(font.render("Gimbal: {:.2f}deg".format(simulation.rocket.current_stage().gimbal), False, (255, 255, 255)),(0,600)) simulation_display.blit(font.render("Heading: {:.2f}deg".format(simulation.heading), False, (255, 255, 255)),(0,640)) - #draw rocket - first_stage_height = 90 #TODO - first_stage_width = 60 - - def calculate_rocket_y_based_on_y_speed_accel(display_height: int, rocket_height: int, speed_y: float, accel_y: float) -> int: - top = display_height / 5 - (rocket_height / 2) #in the case we are accelerating positively - bottom = display_height - (top * 2) - - return bottom - - def calculate_rocket_x_based_on_x_speed_accel(display_width: int, rocket_width: int, speed_x: float, accel_x: float) -> int: - return display_width / 2 - (rocket_width / 2) - - rocket_x = calculate_rocket_x_based_on_x_speed_accel(simulation_display.get_width(), first_stage_width, None, None) - rocket_y = calculate_rocket_y_based_on_y_speed_accel(simulation_display.get_height(), first_stage_height, simulation.speed_y, simulation.acceleration_y) - - rocket_color = (244, 67, 54) - - flame_radius = 10 - flame_color = (255, 125, 100) - - #TODO: Rotate rocket with heading - i = simulation.rocket.stages_spent - stage_height = first_stage_height / (i + 1) - stage_y = rocket_y + first_stage_height - stage_height - for _ in simulation.rocket.stages: - stage_width = first_stage_width / (i + 1) - stage_x = rocket_x + i * (stage_width / 2) - pygame.draw.rect(simulation_display, rocket_color, pygame.Rect(stage_x, stage_y, stage_width, stage_height)) - stage_y -= stage_height / 2 - stage_height /= 2 - i += 1 - - #draw flame - if simulation.rocket.current_stage().engines_on and simulation.rocket.current_stage().fuel_mass > 0: - pygame.draw.circle(simulation_display, flame_color, (rocket_x + (first_stage_width / 2), rocket_y + first_stage_height + flame_radius), flame_radius) - def handle_key_press(simulation, key): if key == pygame.K_x: simulation.rocket.current_stage().engines_on = not simulation.rocket.current_stage().engines_on |