Assignment 1 commit: Moon & Apollo - code quality improvements and implementation of .env file
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from moon import Moon
|
||||
|
||||
if __name__ == '__main__':
|
||||
game = Moon()
|
||||
game.run()
|
||||
@@ -0,0 +1,76 @@
|
||||
import os
|
||||
|
||||
import pygame
|
||||
|
||||
from game.game import Game
|
||||
from numeric.compute import rot_2D, matmul
|
||||
|
||||
|
||||
class Moon(Game):
|
||||
def __init__(self,
|
||||
earth_position:tuple[int, int]=None,
|
||||
earth_size:int=None,
|
||||
moon_size:int=None,
|
||||
moon_distance:int=None,
|
||||
moon_speed:int=None):
|
||||
super().__init__()
|
||||
|
||||
if earth_position is None:
|
||||
self.bary_center = \
|
||||
(tuple(map(int, os.getenv("BARY_CENTER", "{0},{1}".format((self.size[0] // 2), (self.size[1] // 2)).split(",")))))
|
||||
else:
|
||||
self.bary_center = earth_position
|
||||
if earth_size is None:
|
||||
self.earth_size = int(os.getenv("EARTH_SIZE", "12"))
|
||||
else:
|
||||
self.earth_size = earth_size
|
||||
if moon_size is None:
|
||||
self.moon_size = int(os.getenv("MOON_SIZE", "8"))
|
||||
else:
|
||||
self.moon_size = moon_size
|
||||
if moon_distance is None:
|
||||
self.moon_distance = int(os.getenv("MOON_DISTANCE", "200"))
|
||||
else:
|
||||
self.moon_distance = moon_distance
|
||||
if moon_speed is None:
|
||||
self.moon_speed = float(os.getenv("MOON_SPEED", "1"))
|
||||
else:
|
||||
self.moon_speed = moon_speed
|
||||
|
||||
self.angle = 0
|
||||
self.moon_position = None
|
||||
|
||||
def update_game(self):
|
||||
self.angle = pygame.time.get_ticks() / 1000
|
||||
self.moon_position = self.get_orbit_position(self.moon_distance)
|
||||
return True
|
||||
|
||||
def get_orbit_position(self,
|
||||
distance:int,
|
||||
bary_center:tuple[int, int]=None,
|
||||
angle:float=None) \
|
||||
-> tuple[int, int]:
|
||||
"""
|
||||
Calculates the absolute position of the satellite on the orbit.
|
||||
:param distance: Relative distance from the primary body (radius).
|
||||
:param bary_center: The bary center of the orbit (in this simplified simulation a point).
|
||||
:param angle: Numeric value used to calculate the rotation matrix.
|
||||
:return: The absolute position of the satellite.
|
||||
"""
|
||||
if bary_center is None:
|
||||
bary_center = self.bary_center
|
||||
if angle is None:
|
||||
angle = self.angle
|
||||
|
||||
rot = rot_2D(angle * self.moon_speed)
|
||||
rotated = matmul(rot, [[distance], [0]])
|
||||
return (
|
||||
int(bary_center[0] + rotated[0][0]),
|
||||
int(bary_center[1] + rotated[1][0])
|
||||
)
|
||||
|
||||
def draw_game(self):
|
||||
self.screen.fill((30,30,30))
|
||||
pygame.draw.circle(self.screen, (0,0,255), self.bary_center, self.earth_size)
|
||||
pygame.draw.circle(self.screen, (255,255,255), self.moon_position, self.moon_size)
|
||||
pygame.display.flip()
|
||||
Reference in New Issue
Block a user