diff options
author | Baitinq <you@example.com> | 2022-02-11 21:23:06 +0000 |
---|---|---|
committer | Baitinq <you@example.com> | 2022-02-11 21:39:49 +0000 |
commit | f4ac6cf687e5f66305de1b201f3948650ece35fb (patch) | |
tree | 71fbeec8431ee24768c1f63a8c273d5d0161c559 | |
parent | Implemented proper attitude calculation (instead of just y value) (diff) | |
download | OSLS-f4ac6cf687e5f66305de1b201f3948650ece35fb.tar.gz OSLS-f4ac6cf687e5f66305de1b201f3948650ece35fb.tar.bz2 OSLS-f4ac6cf687e5f66305de1b201f3948650ece35fb.zip |
Added first basic sprites
-rw-r--r-- | src/gui/background_sprite.py | 36 | ||||
-rw-r--r-- | src/gui/body_sprite.py | 27 | ||||
-rw-r--r-- | src/gui/rocket_sprite.py | 63 | ||||
-rw-r--r-- | src/main.py | 89 | ||||
-rw-r--r-- | src/simulation.py | 21 |
5 files changed, 158 insertions, 78 deletions
diff --git a/src/gui/background_sprite.py b/src/gui/background_sprite.py new file mode 100644 index 0000000..0579af8 --- /dev/null +++ b/src/gui/background_sprite.py @@ -0,0 +1,36 @@ +from random import randint + +import pygame +from pygame.locals import * + +from simulation import Simulation + +class Sprite_Background(pygame.sprite.Sprite): + def __init__(self, simulation: type[Simulation]): + super().__init__() + self.simulation = simulation + + def update(self, display_width, display_height): + self.image = pygame.Surface([display_width, display_height]) + + #TODO: put into its own functions drawBg() drawStars() + self.image.fill(self.get_color_for_height(self.simulation.rocket_altitude())) + + #draw stars, TODO: should be its own sprite and speed should actually matter + if self.simulation.rocket_altitude() > 30000: + for _ in range(100): + self.image.set_at((randint(0, display_width), randint(0, display_height)), (255, 255, 255)) + + self.rect = self.image.get_rect() + + def linear_gradient(self, 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(self, height: float) -> (int, int, int): + if height < 70000: + return self.linear_gradient((31,118,194), (0, 0, 0), 70000, int(height)) + else: + return (0, 0, 0) \ No newline at end of file diff --git a/src/gui/body_sprite.py b/src/gui/body_sprite.py new file mode 100644 index 0000000..708da03 --- /dev/null +++ b/src/gui/body_sprite.py @@ -0,0 +1,27 @@ +import pygame +from pygame.locals import * + +from simulation import Simulation +from body import Body + +class Sprite_Body(pygame.sprite.Sprite): + def __init__(self, simulation: type[Simulation], floor: int): + super().__init__() + self.simulation = simulation + self.body = self.simulation.body + self.floor = floor + + def update(self, display_width, display_height): + draw_height = self.floor + rocket_altitude = self.simulation.rocket_altitude() + body_radius = self.body.radius + + #draw circle, but in fixed point, not screen + self.image = pygame.Surface([display_width, draw_height], pygame.SRCALPHA) + self.image.fill((0, 155, 155)) + + top_of_floor = display_height - draw_height + rocket_altitude + #print("Top of floor: " + str(top_of_floor)) + + self.rect = self.image.get_rect() + self.rect = [0, top_of_floor] \ No newline at end of file diff --git a/src/gui/rocket_sprite.py b/src/gui/rocket_sprite.py new file mode 100644 index 0000000..101c227 --- /dev/null +++ b/src/gui/rocket_sprite.py @@ -0,0 +1,63 @@ +import pygame +from pygame.locals import * + +from simulation import Simulation +from rocket import Rocket + +class Sprite_Rocket(pygame.sprite.Sprite): + def __init__(self, simulation: type[Simulation], floor: int): + super().__init__() + self.simulation = simulation + self.rocket = self.simulation.rocket + self.floor = floor + + def update(self, display_width, display_height): + first_stage_height = 90 #TODO + first_stage_width = 60 + rocket_color = (244, 67, 54) + + flame_radius = 10 + flame_color = (255, 125, 100) + + total_rocket_height = flame_radius * 2 + i = self.simulation.rocket.stages_spent + stage_height = first_stage_height / (i + 1) + for _ in self.simulation.rocket.stages: + total_rocket_height += stage_height + stage_height /= 2 + i += 1 + + self.image = pygame.Surface([first_stage_width, total_rocket_height], pygame.SRCALPHA) + #self.image.fill((0, 255, 0)) + + i = self.simulation.rocket.stages_spent + stage_height = first_stage_height + stage_y = first_stage_height + for _ in self.simulation.rocket.stages: + stage_width = first_stage_width / (i + 1) + stage_x = i * (stage_width / 2) + + pygame.draw.rect(self.image, rocket_color, pygame.Rect(stage_x, total_rocket_height - stage_y - (flame_radius * 2), stage_width, stage_height)) + + stage_y += stage_height / 2 + stage_height /= 2 + + i += 1 + + #draw flame: TODO: Flame should show direction somehow (gimbal) + if self.simulation.rocket.current_stage().engines_on and self.simulation.rocket.current_stage().fuel_mass > 0: + pygame.draw.circle(self.image, flame_color, (first_stage_width / 2, total_rocket_height - flame_radius), flame_radius) + + + + #heading + self.image = pygame.transform.rotozoom(self.image, -self.simulation.heading, 1) + + self.rect = self.image.get_rect() + + rocket_x = display_width / 2 - (first_stage_width / 2) + rocket_y = display_height - self.floor#TODO: 100 is a const. SHARE THAT ACROSS THIGNS (FLOOR) + + self.rect.bottomleft = [rocket_x, rocket_y] + + \ No newline at end of file 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 diff --git a/src/simulation.py b/src/simulation.py index ffcdaeb..9f6c389 100644 --- a/src/simulation.py +++ b/src/simulation.py @@ -91,17 +91,6 @@ class Simulation(): self.speed_x = self.speed_x + (self.acceleration_x * delta) self.speed_y = self.speed_y + (self.acceleration_y * delta) - - print("Speed x: " + str(self.speed_x)) - print("Speed y: " + str(self.speed_y)) - - #TODO: WELL CALCULATED? (angle well?) - ref_vec = (0, 1) - acc_vec = (self.speed_x, self.speed_y) - dot = (acc_vec[0] * ref_vec[0]) + (acc_vec[1] * ref_vec[1]) - det = (acc_vec[0] * ref_vec[1]) - (acc_vec[1] * ref_vec[0]) - self.heading = math.degrees(math.atan2(det, dot)) - print("Heading: " + str(self.heading)) #update position based on velocity and delta self.x += self.speed_x * delta @@ -112,10 +101,20 @@ class Simulation(): self.y -= self.speed_y * delta self.speed_x = 0 self.speed_y = 0 + + print("Speed x: " + str(self.speed_x)) + print("Speed y: " + str(self.speed_y)) print("X: " + str(self.x)) print("Y: " + str(self.y)) + ref_vec = (0, 1) + acc_vec = (self.speed_x, self.speed_y) + dot = (acc_vec[0] * ref_vec[0]) + (acc_vec[1] * ref_vec[1]) + det = (acc_vec[0] * ref_vec[1]) - (acc_vec[1] * ref_vec[0]) + self.heading = math.degrees(math.atan2(det, dot)) + print("Heading: " + str(self.heading)) + print("Total Simulation Time: " + str(self.time)) print("") |