Separate state generation from UI.

Now the UI loads instantaneously (the starfield takes noticable time to
generate with the Poisson process.)
This commit is contained in:
Simon Forman 2024-04-11 21:46:43 -07:00
parent 410ebbf428
commit 9b59fee1fb
4 changed files with 7843 additions and 11 deletions

13
data.py Normal file
View File

@ -0,0 +1,13 @@
'''
I could use SQLite for a data file format, or just pickle.
'''
from pickle import load as pload, dump
def save(fn, game_state):
with open(fn, 'wb') as f:
dump(game_state, f, protocol=0)
def load(fn):
with open(fn, 'rb') as f:
return pload(f)

7803
game.state Normal file

File diff suppressed because it is too large Load Diff

23
server.py Normal file
View File

@ -0,0 +1,23 @@
from random import randint, expovariate, seed
from data import save
from poisson import poisson
MINIMUM_DISTANCE_BETWEEN_STARS = 160
WIDTH, HEIGHT = 10240, 7680
seed(23)
print('Generating data.')
state = dict(
width = WIDTH,
height = HEIGHT,
stars = {
(x, y): round(1 + expovariate(1))
for x, y in poisson(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS)
},
)
print('Saving game.state')
save('game.state', state)

15
ui.py
View File

@ -3,7 +3,7 @@ from tkinter import *
from PIL import Image from PIL import Image
from PIL.ImageTk import PhotoImage from PIL.ImageTk import PhotoImage
from poisson import poisson from data import load
class App: class App:
@ -44,21 +44,14 @@ class App:
from random import randint, expovariate, seed state = load('game.state')
width, height = state['width'], state['height']
seed(23)
width, height = 10240, 7680
root = Tk() root = Tk()
app = App(root, bg='black', scrollregion=(0, 0, width, height)) app = App(root, bg='black', scrollregion=(0, 0, width, height))
app.frame.pack(expand=True, fill=BOTH) app.frame.pack(expand=True, fill=BOTH)
k = poisson(width, height, 160) for (x, y), radius in state['stars'].items():
for x, y in k:
#x, y = randint(0, width - 1), randint(0, height - 1)
#radius = randint(2, 10) // 2
radius = round(1 + expovariate(1))
app.canvas.create_oval( app.canvas.create_oval(
x - radius, y - radius, x - radius, y - radius,
x + radius, y + radius, x + radius, y + radius,