From effc9402350b2da19392851b34d37d6790f30de0 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Fri, 12 Apr 2024 19:11:50 -0700 Subject: [PATCH] Move stars code into own module for now. --- README.md | 23 +++++++++++++++++++++++ data.py | 39 +++++++++++---------------------------- stars.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ui.py | 6 +++--- 4 files changed, 90 insertions(+), 31 deletions(-) create mode 100644 stars.py diff --git a/README.md b/README.md index 17aef9d..f1fc651 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,26 @@ keep historical data in it rather than just the current state, eh? No 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? + + diff --git a/data.py b/data.py index 27070a8..64288a1 100644 --- a/data.py +++ b/data.py @@ -20,24 +20,18 @@ from random import randint, expovariate, seed import sqlite3 from poisson import poisson +import stars 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 -def initialize_db(filename): +def initialize_db_tables(filename): conn = open_db(filename) c = conn.cursor() - for statement in CREATE_TABLES: + for statement in stars.TABLES: c.execute(statement) c.close() conn.commit() @@ -67,27 +61,16 @@ def generate_stars(width, height, minimum_distance_between_stars): 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() + conn = initialize_db_tables(filename) + stars.init_db(conn) -def stars(): - global conn - c = conn.cursor() - c.execute('select x, y, radius from stars') - yield from c.fetchall() - c.close() +##def stars(): +## global conn +## c = conn.cursor() +## c.execute('select x, y, radius from stars') +## yield from c.fetchall() +## c.close() if __name__ == '__main__': diff --git a/stars.py b/stars.py new file mode 100644 index 0000000..5fee24e --- /dev/null +++ b/stars.py @@ -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 . +# +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() diff --git a/ui.py b/ui.py index d062548..11b7723 100755 --- a/ui.py +++ b/ui.py @@ -20,7 +20,7 @@ # from tkinter import * -import data +import data, stars class App: @@ -62,10 +62,10 @@ class App: data.open_db() 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) -for x, y, radius in data.stars(): +for x, y, radius in stars.iter_stars(data.conn): app.canvas.create_oval( x - radius, y - radius, x + radius, y + radius,