Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, pygame, sys
- pygame.init()
- width, height = 300, 300
- screen = pygame.display.set_mode((width, height))
- screen.fill([0,0,0])
- def getArg(x, y):
- if y == 0:
- arg = 0
- if x < 0:
- arg = pi
- return arg
- elif x == 0:
- arg = pi/2
- if y < 0:
- arg = -(pi/2)
- return arg
- else:
- if y > 0 and x > 0:
- print("#1")
- return math.atan( math.fabs(y) / math.fabs(x) )
- elif y < 0 and x > 0:
- print("#2")
- return pi - math.atan( math.fabs(y) / math.fabs(x) )
- elif y > 0 and x < 0:
- print("#3")
- return -(math.atan( math.fabs(y) / math.fabs(x) ))
- elif y < 0 and x < 0:
- print("#4")
- return -(pi - math.atan( math.fabs(y) / math.fabs(x) ))
- else:
- return 0
- pi = math.pi
- G = (6.67 * math.pow(10, -11) )
- class Vector:
- def __init__(self, mod, arg):
- self.x = mod*math.cos(arg)
- self.y = mod*math.sin(arg)
- def getMod(self):
- return math.sqrt( (self.x*self.x) + (self.y*self.y) )
- def getArg(self):
- return getArg(self.x, self.y)
- def add(self, vector):
- self.x = self.x + vector.x
- self.y = self.y + vector.y
- def addXY(self, x, y):
- self.x = self.x + x
- self.y = self.y + y
- def getDist(self, vector):
- return math.sqrt( ( (self.x-vector.x)*(self.x-vector.x) ) + ( (self.y-vector.y)*(self.y-vector.y) ) )
- def getDiff(self, vector):
- x, y = (self.x-vector.x), (self.y-vector.y)
- temp = createVector(1, 3)
- temp.x, temp.y = x, y
- return temp
- def createVector(x, y):
- return Vector(math.sqrt( (x*x) + (y*y) ), getArg(x, y))
- counter = 0
- counter_limit = 10
- interval = 100
- class Particle:
- def __init__(self, pos, vel, mass, acc):
- self.pos = pos
- self.vel = vel
- self.mass = mass
- self.acc = acc
- def tick(self):
- if counter >= counter_limit:
- self.doGravity(earth)
- self.vel.add(self.acc)
- #self.pos.add(self.vel)
- else:
- self.pos.addXY(self.vel.x/counter_limit, self.vel.y/counter_limit)
- def doGravity(self, particle):
- dist = self.pos.getDist(particle.pos)*math.pow(10, 5.5)
- force = ((G*self.mass*particle.mass)/(dist*dist))
- acc = (force/self.mass)
- print(" ")
- print("acceleration ",acc)
- diffVec = self.pos.getDiff(particle.pos)
- print("difference: ", diffVec.x,",",diffVec.y)
- print("position: ", self.pos.x,",",self.pos.y)
- aArg = diffVec.getArg()
- aV = Vector(acc, aArg)
- self.acc = aV
- def draw(self, target):
- pygame.draw.circle(target, [255, 255, 255], [int(self.pos.x), int(self.pos.y)], 5)
- moon = Particle(createVector(150, 150-75), Vector(5, getArg(3, 0)), 7.34*math.pow(10, 22), createVector(0, 0))
- earth = Particle(createVector(150, 150), createVector(0, 0), 5.97*math.pow(10, 24), createVector(0, 0))
- def tickGame():
- moon.tick()
- screen.fill([0,0,0])
- moon.draw(screen)
- earth.draw(screen)
- pygame.display.flip()
- tickGame()
- #test = getArg(-3, 6)
- #print(math.degrees(test))
- while moon.pos.x > 0 and moon.pos.x < width and moon.pos.y > 0 and moon.pos.y < height and (moon.pos.getDist(earth.pos) > 10):
- counter = counter + 1
- tickGame()
- if counter >= counter_limit:
- counter = 0
- #print(" ")
- #print("Moon position: ", moon.pos.x, ", ", moon.pos.y)
- #print("Moon Velocity: ", moon.vel.x, ", ", moon.vel.y)
- #print("Moon Acceleration: ", moon.acc.x, ", ", moon.acc.y)
- pygame.time.wait(int(interval/counter_limit))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement