Skip to content

Instantly share code, notes, and snippets.

@jul
Created Dec 12, 2021
Embed
What would you like to do?
gof triangulaire on the way
import PySimpleGUI as sg
from operator import itemgetter
### uitliser ipython -i argv[0] pour être en interactif et faire joujou
can = sg.Canvas(size=(400,600))
status=sg.Text('status')
win = sg.Window("titre",[[can,],[status,],], finalize=True)
c = can.TKCanvas
from time import sleep
SZ=50
ORD=1
def save(canvas, name="save"):
global ORD
with open("%s-%04d.ps"% (name, ORD), "w") as f:
ORD+=1
f.write(canvas.postscript())
def cell(x,y, **kw):
hs=0.866 # heigth of an equilateral triangle
xr,yr=x*SZ+SZ,y*hs*SZ+SZ
# triangle compliqué à dessiner
r=SZ/2
i=c.create_oval(xr-r,yr-r,xr+r,yr+r,
fill=kw.get("state","red"), outline="white",
tags=(
"os:%s" % (kw.get("old_state", "dead")),
"p:%d,%d" % (x,y),
"rp:%d,%d,%d" % (xr,yr,r),
"bb:%d,%d,%d,%d" % (xr-r, yr-r, xr+r, yr+r),
))
sleep(.01)
c.update()
save(c)
return i
def get_info(c,tag):
e = c.gettags(tag)
sw=dict(
p = lambda s:("pos", tuple(map(int,s.split(",")))),
rp = lambda s:("real_pos", tuple(map(int,s.split(",")))),
os = lambda s:("old_state", s),
bb = lambda s:("box", tuple(map(int,s.split(",")))),
)
return dict( sw[k](v) for k,v in map(lambda s:s.split(":"), e))
def get_neighbour(x,y):
gi = lambda *x: get_info(c,( "p:%d,%d" % x) if len(x)==2 else x)
return set(
map(itemgetter("pos"),
map(gi, c.find_overlapping(*gi(2,2)["box"])
)))-{(x,y)}
for x in range(0,10,2):
for y in range(10):
save(c)
cell(
x/2-1/2*(y%2),
y-2/3,
state = (
(x,y)==(4,4) and "yellow" or "red" )
if 2 < x <= 6 and 2 < y < 6 or (x,y)==(2,4)
else "lightgreen",
outline="black")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment