File size: 4,116 Bytes
ba7b9c7 | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import pygame
import math
from Const import *
pygame.init()
class Celestial_Core:
def __init__(self, x, y, mass):
self.x = x
self.y = y
self.mass = mass
self.orbit = []
self.vx = 0
self.vy = 0
def draw(self, win, shift_x , shift_y, Scale):
pass
def attraction(self, other):
other_x, other_y = other.x, other.y
distance_x = other_x - self.x
distance_y = other_y - self.y
distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
force = G * self.mass * other.mass / distance**2
theta = math.atan2(distance_y, distance_x)
force_x = math.cos(theta) * force
force_y = math.sin(theta) * force
return force_x, force_y
class Planet(Celestial_Core):
def __init__(self, x, y, mass, radius, circ_draw, color):
super().__init__(x, y, mass)
self.radius = radius
self.circ_draw = circ_draw
self.color = color
def draw(self, win, shift_x , shift_y, Scale):
x = (self.x+shift_x) * Scale + WIDTH / 2
y = (self.y+shift_y) * Scale + HEIGHT / 2
if len(self.orbit) > 8:
for point in self.orbit:
x, y = point
x = (x+ shift_x) * Scale + WIDTH / 2
y = (y+ shift_y) * Scale + HEIGHT / 2
# pygame.draw.lines(win, self.color, False, self.orbit, 2)
if(x>0):
pygame.draw.circle(win, self.color, (x, y), self.circ_draw)
class SpaceShip(Celestial_Core) :
def __init__(self, x, y, mass, radius, color, fuel = 300_000):
super().__init__(x, y, mass+fuel)
self.radius = radius
self.color = color
self.fuel = fuel
self.reactor1 = False
self.reactor2 = False
self.reactor3 = False
# Destructor :
def __del__(self):
pass
def ChangeR(self,s_reactor):
if(s_reactor == 1):
self.reactor1 = not self.reactor1
if(s_reactor == 2):
self.reactor2 = not self.reactor2
if(s_reactor == 3):
self.reactor3 = not self.reactor3
def apply_action(self, action):
"""Applies the action chosen by the agent."""
# Action 0: Turn off all reactors
if action == 0:
self.reactor1 = False
self.reactor2 = False
self.reactor3 = False
# Action 1: Activate right reactor (disable others for simplicity now)
elif action == 1:
self.reactor1 = True
self.reactor2 = False
self.reactor3 = False
# Action 2: Activate left reactor
elif action == 2:
self.reactor1 = False
self.reactor2 = True
self.reactor3 = False
# Action 3: Activate main/forward reactor
elif action == 3:
self.reactor1 = False
self.reactor2 = False
self.reactor3 = True
# Action 4: Activate right & left reactors
elif action == 4:
self.reactor1 = True
self.reactor2 = True
self.reactor3 = False
# Action 5: Activate right & forward reactors
elif action == 5:
self.reactor1 = True
self.reactor2 = False
self.reactor3 = True
# Action 6: Activate left & forward reactors
elif action == 6:
self.reactor1 = False
self.reactor2 = True
self.reactor3 = True
else:
print('Error in action')
def accelerator(self):
if(self.fuel>0):
# self.fuel -=1
# self.mass -=1
return ACCELERATING_RATE
else:
self.reactor1 = False
self.reactor2 = False
self.reactor3 = False
return 0
def motor_off(self):
return not (self.reactor1 or self.reactor2 or self.reactor3)
def draw(self, win, shift_x , shift_y, Scale):
x = (self.x+shift_x) * Scale + WIDTH / 2
y = (self.y+shift_y) * Scale + HEIGHT / 2
if len(self.orbit) > 2:
updated_points = []
for point in self.orbit:
x, y = point
x = (x+ shift_x) * Scale + WIDTH / 2
y = (y+ shift_y) * Scale + HEIGHT / 2
updated_points.append((x, y))
pygame.draw.lines(win, WHITE, False, updated_points, 1)
if(x>0):
pygame.draw.circle(win, self.color, (x, y), self.radius)
if(self.reactor1):
pygame.draw.circle(win, YELLOW, (x+12,y),3)
if(self.reactor2):
comp_x = 12 * math.cos(2.*math.pi/3.)
comp_y = 12 * math.sin(2.*math.pi/3.)
pygame.draw.circle(win, YELLOW, (x+comp_x,y+comp_y),3)
if(self.reactor3):
comp_x = 12 * math.cos(-2.*math.pi/3.)
comp_y = 12 * math.sin(-2.*math.pi/3.)
pygame.draw.circle(win, YELLOW, (x+comp_x,y+comp_y),3) |