Move stars code into own module for now.

This commit is contained in:
Simon Forman 2024-04-12 19:11:50 -07:00
parent 9ba1c7fb31
commit effc940235
4 changed files with 90 additions and 31 deletions

View File

@ -37,3 +37,26 @@ keep historical data in it rather than just the current state, eh? No
one will freak out? one will freak out?
That was easy.
I don't want to spend too much time fiddling with the widgets and UI,
it's fun but it's a bit of a rabbit hole, eh?
The basic model is that you get a state db from the server, explore it
with the client and queue up an order list to send back to the server for
processing, which then returns the new state db.
What format for the order codes?
Thun code?
THe db tables and game data types should be compatible with Prolog?
I don't want to have a bunch of Python objects modelling the game
entities if possible, what I mean is, it would be neat if the db rows
(tuples of Python ints & strings) and the Tk widgets (canvas items, etc.)
could be the bulk of the internal game, uh, stuff. Not everything needs
to be a class?

39
data.py
View File

@ -20,24 +20,18 @@
from random import randint, expovariate, seed from random import randint, expovariate, seed
import sqlite3 import sqlite3
from poisson import poisson from poisson import poisson
import stars
FILENAME = './game.sqlite' FILENAME = './game.sqlite'
MINIMUM_DISTANCE_BETWEEN_STARS = 160
WIDTH, HEIGHT = 10240, 7680
CREATE_TABLES = '''\
create table stars (x INTEGER, y INTEGER, radius INTEGER)
'''.splitlines(False)
conn = None conn = None
def initialize_db(filename): def initialize_db_tables(filename):
conn = open_db(filename) conn = open_db(filename)
c = conn.cursor() c = conn.cursor()
for statement in CREATE_TABLES: for statement in stars.TABLES:
c.execute(statement) c.execute(statement)
c.close() c.close()
conn.commit() conn.commit()
@ -67,27 +61,16 @@ def generate_stars(width, height, minimum_distance_between_stars):
def main(filename): def main(filename):
print('Initializing db file', filename) print('Initializing db file', filename)
conn = initialize_db(filename) conn = initialize_db_tables(filename)
stars.init_db(conn)
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(): ##def stars():
global conn ## global conn
c = conn.cursor() ## c = conn.cursor()
c.execute('select x, y, radius from stars') ## c.execute('select x, y, radius from stars')
yield from c.fetchall() ## yield from c.fetchall()
c.close() ## c.close()
if __name__ == '__main__': if __name__ == '__main__':

53
stars.py Normal file
View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2024 Simon Forman
#
# This file is part of game
#
# game is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# game is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with game. If not see <http://www.gnu.org/licenses/>.
#
from random import randint, expovariate, seed
import sqlite3
from poisson import poisson
MINIMUM_DISTANCE_BETWEEN_STARS = 160
WIDTH, HEIGHT = 10240, 7680
TABLES = [
'create table stars (x INTEGER, y INTEGER, radius INTEGER)',
]
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 init_db(conn):
print('Generating star 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 iter_stars(conn):
c = conn.cursor()
c.execute('select x, y, radius from stars')
yield from c.fetchall()
c.close()

6
ui.py
View File

@ -20,7 +20,7 @@
# #
from tkinter import * from tkinter import *
import data import data, stars
class App: class App:
@ -62,10 +62,10 @@ class App:
data.open_db() data.open_db()
root = Tk() root = Tk()
app = App(root, bg='black', scrollregion=(0, 0, data.WIDTH, data.HEIGHT)) app = App(root, bg='black', scrollregion=(0, 0, stars.WIDTH, stars.HEIGHT))
app.frame.pack(expand=True, fill=BOTH) app.frame.pack(expand=True, fill=BOTH)
for x, y, radius in data.stars(): for x, y, radius in stars.iter_stars(data.conn):
app.canvas.create_oval( app.canvas.create_oval(
x - radius, y - radius, x - radius, y - radius,
x + radius, y + radius, x + radius, y + radius,