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:
parent
410ebbf428
commit
9b59fee1fb
|
|
@ -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)
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -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
15
ui.py
|
|
@ -3,7 +3,7 @@ from tkinter import *
|
|||
from PIL import Image
|
||||
from PIL.ImageTk import PhotoImage
|
||||
|
||||
from poisson import poisson
|
||||
from data import load
|
||||
|
||||
|
||||
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()
|
||||
app = App(root, bg='black', scrollregion=(0, 0, width, height))
|
||||
app.frame.pack(expand=True, fill=BOTH)
|
||||
|
||||
k = poisson(width, height, 160)
|
||||
|
||||
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))
|
||||
for (x, y), radius in state['stars'].items():
|
||||
app.canvas.create_oval(
|
||||
x - radius, y - radius,
|
||||
x + radius, y + radius,
|
||||
|
|
|
|||
Loading…
Reference in New Issue