game/ui.py

67 lines
1.6 KiB
Python

from fractions import Fraction as ratio
from tkinter import *
from PIL import Image
from PIL.ImageTk import PhotoImage
from poisson import poisson
class App:
'''
A canvas with scrolling support.
'''
def __init__(self, master=None, *canvas_args, **canvas_kw):
frame = self.frame = Frame(root, bg='green')
frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
C = self.canvas = Canvas(frame, *canvas_args, **canvas_kw)
scrollY = self.scrollY = Scrollbar(
frame,
orient=VERTICAL,
command=C.yview,
)
C['yscrollcommand'] = scrollY.set
scrollX = self.scrollX = Scrollbar(
frame,
orient=HORIZONTAL,
command=C.xview,
)
C['xscrollcommand'] = scrollX.set
C.grid(row=0, column=0, sticky=N+S+E+W)
scrollY.grid(row=0, column=1, sticky=N+S)
scrollX.grid(row=1, column=0, sticky=E+W)
C.bind("<Configure>", self.handle_canvas_resize)
def handle_canvas_resize(self, event):
print(event)
from random import randint, expovariate, seed
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))
app.canvas.create_oval(
x - radius, y - radius,
x + radius, y + radius,
fill='yellow',
)