Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Body.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def checkForBodyCollision(lst):
# true if body1 is bigger and they are colliding
absorb = dist <= body1.displayRad + body2.displayRad*Body.collisionDistanceFactor and body1.mass >= body2.mass
# if body2 is bigger and they are colliding
if dist <= body2.displayRad + body1.displayRad*0.5 and body2.mass >= body1.mass:
if dist <= body2.displayRad + body1.displayRad*Body.collisionDistanceFactor and body2.mass >= body1.mass:
absorb = True
# swapping them so that body1 refers to the larger
temp = body2
Expand Down
51 changes: 46 additions & 5 deletions Gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from tkinter import *
from Body import Body
from Seed import Seed

import pyrebase
import json
firebaseConfig = json.load(open('firebaseConfig.json'))
firebase = pyrebase.initialize_app(firebaseConfig)
db = firebase.database()
pygame.init()

root = Tk()
Expand Down Expand Up @@ -69,7 +73,7 @@ def selectRadio():

display_label = Label(root, text = 'Display Settings')
presets_label = Label(root, text = 'Presets')

seed_text_location = (550, 10)
randomNoMomentum_label = Label(root, text = 'Number of bodies', fg = disabledColor)
seed_label = Label(root, text = 'Seed', fg = disabledColor)

Expand Down Expand Up @@ -133,7 +137,22 @@ def selectRadio():
background_color = pygame.Color(30, 30, 30)

bodies = []

maxEvalSeed, maxEval = "", 0
secondEvalSeed, secondEval = "", 0
thirdEvalSeed, thirdEval = "", 0
def evaluateState(bodies):
maxBody = bodies[0]
for body in bodies:
if body.mass > maxBody.mass:
maxBody = body
value = 0
for body in bodies:
if body != maxBody:
dist = Body.findDisplayDistance(body.pos, maxBody.pos)
if(dist > 450):
continue
value += (2025**(-1))*dist**2-0.444*dist+100
return value
# planet formation
def generateRandomBodies(numBodies, massRange):
return [Body((random.randint(0, screenWidth), random.randint(0, screenHeight)),\
Expand Down Expand Up @@ -208,7 +227,7 @@ def moveCamera(dx=0, dy=0):

pressed = pygame.key.get_pressed()

clock.tick(fps)
# clock.tick(fps)

# drawing the background
screen.fill(background_color)
Expand Down Expand Up @@ -353,10 +372,32 @@ def moveCamera(dx=0, dy=0):
# multiplying Body.max_speed by 7.5 shows how fast a body would travel in 0.25 seconds as opposed to 0.033 seconds, as this is easier to see when dragging the line
if not backUp:
pygame.draw.line(screen, launch_line_info[0], launch_line_info[1], launch_line_info[2], launch_line_width)

if frame_count >= 500 and len(bodies) > 0:
evaluation = evaluateState(bodies)
db.child('seeds').child(seed.seed).child('score').set(evaluation)
db.child('seeds').child(seed.seed).child('seed').set(seed.raw)
if(db.child('highscore').child('score').get().val()<evaluation):
print('New Highscore of ' + str(evaluation) + " with seed " + seed.raw)
db.child('highscore').child('score').set(evaluation)
db.child('highscore').child('seed').set(seed.raw)
print(seed.raw)
seed = Seed(Seed.generateRandom())
# seeds = db.child('seeds').shallow().get().val()
# print(len(seeds))
print(int(evaluation))
# if(seed.raw in seeds):
# seed = Seed(Seed.generateRandom())
bodies.clear()
random.seed(seed.seed)
bodies = generateRandomBodies(int(randomNoMomentum_numBodies), (8*10**22, 4*10**23))
frame_count = 0
frame_label_text = "Frame: {}".format(frame_count)
frame_label = game_font.render(frame_label_text, 1, WHITE)
screen.blit(frame_label, frame_text_location)

seed_label_text = "Seed: {}".format(seed.raw)
seed_label = game_font.render(seed_label_text, 1, WHITE)
screen.blit(seed_label, seed_text_location)

pygame.display.update()
pygame.display.flip()
9 changes: 9 additions & 0 deletions firebaseConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"apiKey": "AIzaSyC2tRFAgjRVcsgDUSKdOptKQ_Aiw2QGKxQ",
"authDomain": "gravity-sim-seeds.firebaseapp.com",
"databaseURL": "https://gravity-sim-seeds-default-rtdb.firebaseio.com",
"projectId": "gravity-sim-seeds",
"storageBucket": "gravity-sim-seeds.appspot.com",
"messagingSenderId": "738256469892",
"appId": "1:738256469892:web:fb99a5060db0c4da67a7d1"
}