Use SQLite rather than Pickle.
This commit is contained in:
parent
25e5723661
commit
9ba1c7fb31
17
README.md
17
README.md
|
|
@ -20,3 +20,20 @@ current parameters.
|
||||||
The "server" isn't a server at all yet. It just generates the starfield
|
The "server" isn't a server at all yet. It just generates the starfield
|
||||||
so the UI script doesn't have to do it each time. I'm just using pickle
|
so the UI script doesn't have to do it each time. I'm just using pickle
|
||||||
for data format, eventually it could use JSON (ick) or SQLite (nice.)
|
for data format, eventually it could use JSON (ick) or SQLite (nice.)
|
||||||
|
|
||||||
|
|
||||||
|
Aesthetics: maybe have a sprinkling of background stars behind the
|
||||||
|
"real" stars (that are part of the game.)
|
||||||
|
|
||||||
|
Toggle fullscreen.
|
||||||
|
|
||||||
|
Controls? Tabs? HUD/OSD (heads up display/on screen display)
|
||||||
|
|
||||||
|
|
||||||
|
Fri Apr 12 12:27:56 PDT 2024
|
||||||
|
I think I will use SQLite as teh interchange file format. It's language
|
||||||
|
agnostic (i mean, SQL, sure, but you can use it from any lang) and I can
|
||||||
|
keep historical data in it rather than just the current state, eh? No
|
||||||
|
one will freak out?
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
87
data.py
87
data.py
|
|
@ -17,16 +17,83 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with game. If not see <http://www.gnu.org/licenses/>.
|
# along with game. If not see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
'''
|
from random import randint, expovariate, seed
|
||||||
I could use SQLite for a data file format, or just pickle.
|
import sqlite3
|
||||||
'''
|
from poisson import poisson
|
||||||
|
|
||||||
from pickle import load as pload, dump
|
|
||||||
|
|
||||||
def save(fn, game_state):
|
FILENAME = './game.sqlite'
|
||||||
with open(fn, 'wb') as f:
|
MINIMUM_DISTANCE_BETWEEN_STARS = 160
|
||||||
dump(game_state, f, protocol=0)
|
WIDTH, HEIGHT = 10240, 7680
|
||||||
|
|
||||||
def load(fn):
|
|
||||||
with open(fn, 'rb') as f:
|
CREATE_TABLES = '''\
|
||||||
return pload(f)
|
create table stars (x INTEGER, y INTEGER, radius INTEGER)
|
||||||
|
'''.splitlines(False)
|
||||||
|
|
||||||
|
conn = None
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_db(filename):
|
||||||
|
conn = open_db(filename)
|
||||||
|
c = conn.cursor()
|
||||||
|
for statement in CREATE_TABLES:
|
||||||
|
c.execute(statement)
|
||||||
|
c.close()
|
||||||
|
conn.commit()
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def close_db():
|
||||||
|
if conn:
|
||||||
|
conn.close()
|
||||||
|
conn = None
|
||||||
|
else:
|
||||||
|
raise RuntimeError('db connection already closed')
|
||||||
|
|
||||||
|
|
||||||
|
def open_db(filename=FILENAME):
|
||||||
|
global conn
|
||||||
|
if conn is not None:
|
||||||
|
raise RuntimeError('db connection already open')
|
||||||
|
conn = sqlite3.connect(filename)
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def generate_stars(width, height, minimum_distance_between_stars):
|
||||||
|
for x, y in poisson(width, height, minimum_distance_between_stars):
|
||||||
|
yield x, y, round(1 + expovariate(1))
|
||||||
|
|
||||||
|
|
||||||
|
def main(filename):
|
||||||
|
print('Initializing db file', filename)
|
||||||
|
conn = initialize_db(filename)
|
||||||
|
|
||||||
|
print('Generating data.')
|
||||||
|
seed(23)
|
||||||
|
c = conn.cursor()
|
||||||
|
for values in generate_stars(
|
||||||
|
WIDTH,
|
||||||
|
HEIGHT,
|
||||||
|
MINIMUM_DISTANCE_BETWEEN_STARS,
|
||||||
|
):
|
||||||
|
c.execute('insert into stars values (?, ?, ?)', values)
|
||||||
|
c.close()
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def stars():
|
||||||
|
global conn
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute('select x, y, radius from stars')
|
||||||
|
yield from c.fetchall()
|
||||||
|
c.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
if not pathlib.Path(FILENAME).exists():
|
||||||
|
main(FILENAME)
|
||||||
|
else:
|
||||||
|
print(FILENAME, 'already exists! Not overwriting.')
|
||||||
|
|
|
||||||
Binary file not shown.
7803
game.state
7803
game.state
File diff suppressed because it is too large
Load Diff
23
server.py
23
server.py
|
|
@ -17,26 +17,3 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with game. If not see <http://www.gnu.org/licenses/>.
|
# along with game. If not see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
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)
|
|
||||||
|
|
|
||||||
13
ui.py
13
ui.py
|
|
@ -20,7 +20,7 @@
|
||||||
#
|
#
|
||||||
from tkinter import *
|
from tkinter import *
|
||||||
|
|
||||||
from data import load
|
import data
|
||||||
|
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
|
|
@ -59,18 +59,17 @@ class App:
|
||||||
print(event)
|
print(event)
|
||||||
|
|
||||||
|
|
||||||
|
data.open_db()
|
||||||
|
|
||||||
state = load('game.state')
|
|
||||||
width, height = state['width'], state['height']
|
|
||||||
|
|
||||||
root = Tk()
|
root = Tk()
|
||||||
app = App(root, bg='black', scrollregion=(0, 0, width, height))
|
app = App(root, bg='black', scrollregion=(0, 0, data.WIDTH, data.HEIGHT))
|
||||||
app.frame.pack(expand=True, fill=BOTH)
|
app.frame.pack(expand=True, fill=BOTH)
|
||||||
|
|
||||||
for (x, y), radius in state['stars'].items():
|
for x, y, radius in data.stars():
|
||||||
app.canvas.create_oval(
|
app.canvas.create_oval(
|
||||||
x - radius, y - radius,
|
x - radius, y - radius,
|
||||||
x + radius, y + radius,
|
x + radius, y + radius,
|
||||||
fill='yellow',
|
fill='yellow',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
app.frame.mainloop()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue