tictac.py

Created by schraf

Created on May 03, 2026

1.56 KB


from math import *
import ion, kandinsky as kd
from time import sleep

W, H = 320, 222
PAD_L, PAD_B = 10, 16
CHART_W = W - PAD_L - 4
CHART_H = H - PAD_B - 20
BG  = (240, 240, 245)
AX  = (80,  80,  80)
BAR = (60,  110, 200)
FG  = (20,  20,  20)
MUT = (60,  60,  60)

DIVS = [1, 2, 3, 4, 6, 9, 12, 18, 36]

def conv(a, b):
    c = [0] * (len(a) + len(b) - 1)
    for i, x in enumerate(a):
        for j, y in enumerate(b):
            c[i + j] += x * y
    return c

def dist(d, n):
    die = [0] + [1] * d
    acc = [1]
    for _ in range(n):
        acc = conv(acc, die)
    total = d ** n
    return [(i, acc[i]) for i in range(len(acc)) if acc[i]], total

def draw(d, n):
    kd.fill_rect(0, 0, W, H, BG)
    pts, total = dist(d, n)
    max_p = max(v for _, v in pts)
    step = CHART_W // 36
    bar_w = max(1, step - 1)

    kd.fill_rect(PAD_L - 1, 4, 1, H - PAD_B - 4, AX)
    kd.fill_rect(PAD_L - 1, H - PAD_B, CHART_W + 5, 1, AX)

    for s, v in pts:
        bh = int(v / max_p * CHART_H)
        x  = PAD_L + (s - 1) * step
        y  = H - PAD_B - bh
        kd.fill_rect(x, y, bar_w, bh, BAR)

    kd.draw_string("des={} faces={}".format(n,d), 2, 2, FG, BG)

    for lbl, s in [("1", 1), ("18", 18), ("36", 35)]:
        x = PAD_L + (s - 1) * step
        kd.draw_string(lbl, x, H - PAD_B + 1, MUT, BG)
    sleep(.2)

idx = 0
while True:
    d = DIVS[idx]
    n = 36 // d
    draw(d, n)
    while True:
        if ion.keydown(ion.KEY_UP):
            idx = (idx + 1) % len(DIVS); break
        if ion.keydown(ion.KEY_DOWN):
            idx = (idx - 1) % len(DIVS); break

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.