about summary refs log tree commit diff
path: root/main.py
blob: 65bdf1d34185f883ac5eb2f282041e5d1526d613 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sys
import math
from random import randint

from engine import Engine
from fuel import Fuel
from rocket import Rocket
from atmosphere import Atmosphere
from body import Body
from universe import Universe
from simulation import Simulation

import pygame
from pygame.locals import *

def main(argv):
    rocket = Rocket(name="starship", 
                    rocket_mass=240000, #thrust=245000
                    engine=Engine(name="raptor", thrust=2.3E6, flow_rate=1000), #https://en.wikipedia.org/wiki/SpaceX_Raptor
                    engine_number=33,
                    fuel_type=Fuel(name="methane", energy_density=None),
                    fuel_mass=4000000,
                    drag_coefficient=1.18,
                    cross_sectional_area=(math.pi * (9**2))
                    )
    
    body = Body(name="earth",
                density=5.51,
                radius=6371000,
                atmosphere=Atmosphere(
                                      avg_sea_level_pressure=101325,
                                      molar_mass_air=0.02896,
                                      standard_temp=288.15
                                     )
                )
    
    universe = Universe(name="conventional",
                        G=6.67E-11,
                        plank=None
                        )
    
    simulation = Simulation(universe, body, rocket)

    pygame.init()
    pygame.display.set_caption("OSLS - Overly Simple Launch Simulator")
    clock = pygame.time.Clock()

    SCREEN_WIDTH = 1024
    SCREEN_HEIGHT = 720

    simulation_display = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
    while(True):
        draw_simulation(simulation_display, simulation)
        pygame.display.update()

        for event in pygame.event.get(): 
            if event.type == pygame.QUIT:  
                pygame.quit()  
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    quit()
                else:
                    handle_key_press(simulation, event.key)

        delta = clock.tick(60) / 1000 #60fps #are we using delta in the simulation tick everywhere needed?
        print("delta: " + str(delta))
        simulation.tick(delta=delta)

        #TODO: draw floor, flame
        #TODO: add support for rocket stages
        #TODO: do max load on rocket so it blows up
        #TODO: allow for x movement, speed, accel etc
        #TODO: allow multilanguage api for landing algorithms etc

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 draw_simulation(simulation_display: type[pygame.Surface], simulation: type[Simulation]) -> None:        
        #draw background
        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.y))

        #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.y > 30000:
            for _ in range(100):
                simulation_display.set_at((randint(0, simulation_display.get_width()), randint(0, simulation_display.get_height())), (255, 255, 255))
        #draw stats text
        font = pygame.font.SysFont("Comic Sans MS", 30)

        simulation_display.blit(font.render("Time: {:.0f}s".format(simulation.time), False, (255, 255, 255)),(0,0))
        simulation_display.blit(font.render("Altitude: {:.0f}m".format(simulation.y), False, (255, 255, 255)),(0,40))
        simulation_display.blit(font.render("Speed: {:.0f}m/s".format(simulation.speed_y), False, (255, 255, 255)),(0,80))
        simulation_display.blit(font.render("Acceleration: {:.2f}m/s2".format(simulation.acceleration_y), False, (255, 255, 255)),(0,120))
        simulation_display.blit(font.render("Fuel: {:.0f}kg".format(simulation.rocket.fuel_mass), False, (255, 255, 255)),(0,160))

        #draw rocket
        rocket_height = 90
        rocket_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(), rocket_width, None, None)
        rocket_y = calculate_rocket_y_based_on_y_speed_accel(simulation_display.get_height(), rocket_height, simulation.speed_y, simulation.acceleration_y)

        rocket_color = (244, 67, 54)

        flame_radius = 10
        flame_color = (255, 125, 100)

        pygame.draw.rect(simulation_display, rocket_color, pygame.Rect(rocket_x, rocket_y, rocket_width, rocket_height))
        if simulation.rocket.engines_on and simulation.rocket.fuel_mass > 0:
            pygame.draw.circle(simulation_display, flame_color, (rocket_x + (rocket_width / 2), rocket_y + rocket_height + flame_radius), flame_radius)

def handle_key_press(simulation, key):
    if key == pygame.K_SPACE:
        simulation.rocket.engines_on = not simulation.rocket.engines_on
    elif key == pygame.K_LEFT:
        sys.exit(0)
    elif key == pygame.K_RIGHT:
        sys.exit(0)

if __name__ == "__main__":
    main(sys.argv)