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 /src/gui | |
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
Diffstat (limited to 'src/gui')
-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 |
3 files changed, 126 insertions, 0 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 |