Skip to content

Commit 58fb623

Browse files
committed
evaluation
1 parent a3b42d5 commit 58fb623

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

Gravity.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from tkinter import *
88
from Body import Body
99
from Seed import Seed
10-
10+
import pyrebase
11+
import json
12+
firebaseConfig = json.load(open('firebaseConfig.json'))
13+
firebase = pyrebase.initialize_app(firebaseConfig)
14+
db = firebase.database()
1115
pygame.init()
1216

1317
root = Tk()
@@ -69,7 +73,7 @@ def selectRadio():
6973

7074
display_label = Label(root, text = 'Display Settings')
7175
presets_label = Label(root, text = 'Presets')
72-
76+
seed_text_location = (550, 10)
7377
randomNoMomentum_label = Label(root, text = 'Number of bodies', fg = disabledColor)
7478
seed_label = Label(root, text = 'Seed', fg = disabledColor)
7579

@@ -133,7 +137,22 @@ def selectRadio():
133137
background_color = pygame.Color(30, 30, 30)
134138

135139
bodies = []
136-
140+
maxEvalSeed, maxEval = "", 0
141+
secondEvalSeed, secondEval = "", 0
142+
thirdEvalSeed, thirdEval = "", 0
143+
def evaluateState(bodies):
144+
maxBody = bodies[0]
145+
for body in bodies:
146+
if body.mass > maxBody.mass:
147+
maxBody = body
148+
value = 0
149+
for body in bodies:
150+
if body != maxBody:
151+
dist = Body.findDisplayDistance(body.pos, maxBody.pos)
152+
if(dist > 450):
153+
continue
154+
value += (2025**(-1))*dist**2-0.444*dist+100
155+
return value
137156
# planet formation
138157
def generateRandomBodies(numBodies, massRange):
139158
return [Body((random.randint(0, screenWidth), random.randint(0, screenHeight)),\
@@ -208,7 +227,7 @@ def moveCamera(dx=0, dy=0):
208227

209228
pressed = pygame.key.get_pressed()
210229

211-
clock.tick(fps)
230+
# clock.tick(fps)
212231

213232
# drawing the background
214233
screen.fill(background_color)
@@ -353,10 +372,28 @@ def moveCamera(dx=0, dy=0):
353372
# 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
354373
if not backUp:
355374
pygame.draw.line(screen, launch_line_info[0], launch_line_info[1], launch_line_info[2], launch_line_width)
356-
375+
if frame_count >= 500 and len(bodies) > 0:
376+
evaluation = evaluateState(bodies)
377+
db.child('seeds').child(seed.seed).child('score').set(evaluation)
378+
db.child('seeds').child(seed.seed).child('seed').set(seed.raw)
379+
if(db.child('highscore').child('score').get().val()<evaluation):
380+
db.child('highscore').child('score').set(evaluation)
381+
db.child('highscore').child('seed').set(seed.raw)
382+
print(seed.raw)
383+
seed = Seed(Seed.generateRandom())
384+
if(seed.raw in db.child('seeds').shallow().get().val()):
385+
seed = Seed(Seed.generateRandom())
386+
bodies.clear()
387+
random.seed(seed.seed)
388+
bodies = generateRandomBodies(int(randomNoMomentum_numBodies), (8*10**22, 4*10**23))
389+
frame_count = 0
357390
frame_label_text = "Frame: {}".format(frame_count)
358391
frame_label = game_font.render(frame_label_text, 1, WHITE)
359392
screen.blit(frame_label, frame_text_location)
360393

394+
seed_label_text = "Seed: {}".format(seed.raw)
395+
seed_label = game_font.render(seed_label_text, 1, WHITE)
396+
screen.blit(seed_label, seed_text_location)
397+
361398
pygame.display.update()
362399
pygame.display.flip()

firebaseConfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"apiKey": "AIzaSyC2tRFAgjRVcsgDUSKdOptKQ_Aiw2QGKxQ",
3+
"authDomain": "gravity-sim-seeds.firebaseapp.com",
4+
"databaseURL": "https://gravity-sim-seeds-default-rtdb.firebaseio.com",
5+
"projectId": "gravity-sim-seeds",
6+
"storageBucket": "gravity-sim-seeds.appspot.com",
7+
"messagingSenderId": "738256469892",
8+
"appId": "1:738256469892:web:fb99a5060db0c4da67a7d1"
9+
}

0 commit comments

Comments
 (0)