util.py

Created by aquaebm

Created on October 09, 2022

2.17 KB

Utility classes, functions and a decorator for simple game programming on your numworks…

Note that all my other scripts depend on this one, so you might want to load this onto your calculator first (and not modify it’s name or contents).


from kandinsky import *
from ion import keydown
from time import *
from random import *
BLACK=(0,0,0)
WHITE=(248,252,248)
DARK_PURPLE=(72,0,72)
CYAN=(0,252,248)
def fill_screen(col,x=0,y=0):
  fill_rect(x,y,320-(x<<1),222-(y<<1),col)
def draw_str_centered(txt,x,y,fg=BLACK,bg=WHITE):
  lines=txt.split("\n")
  start_y=y-len(lines)*9
  for i,line in enumerate(lines):
    draw_string(line,x-len(line)*5,start_y+18*i,fg,bg)
def game_func(f):
  def wrapper(**args):
    restart=True
    while restart:
      v=f(**args)
      draw_str_centered(
        "YOU {}\nRestart: OK\nQuit: ->"
          .format("WIN"if v else"LOSE"),
        160,111,args["col"],args["bg"]
      )
      while not keydown(4):
        if keydown(3):
          restart=False
          break
    fill_screen(WHITE)
  return wrapper
class KeyPressHandler:
  # Allows for spontaneous reaction to key presses in loops, as keydown(KEY)
  # would return True for several consecutive iterations when a key is pressed,
  # when all we need is a reaction to a single keypress, not a long press.
  def __init__(s,keys):
    # we keep a reference to keys to preserve the order when iterated on
    # as dicts are unordered.
    s.k=keys
    s.y=dict.fromkeys(keys,True)
  def __getitem__(s,i,k=keydown):
    y=s.y
    p,a=k(i),y[i]
    # p : pressed , a : allowed
    y[i]^=p==a
    # if pressed successfully, block further
    # keypress reactions until the button is released
    return p and a
  def __iter__(s):
    return map(s.__getitem__,s.k)
class Timer:
  def reset(s,dt):
    s.t=monotonic()
    s.dt=dt
  def __init__(s,it=1):
    s.reset(it)
  def __call__(s,m=monotonic):
    return m()-s.t>=s.dt
def comp_col(col):
  return [255^max(rgb,0)for rgb in col]
def windows(it,n):
  it=iter(it)
  l=[next(it)for _ in range(n)]
  yield l.copy()
  for e in it:
    l.pop(0)
    l.append(e)
    yield l.copy()
def wrap(x,imin,imax):
  return x+(imax-imin)*((x<imin)-(x>imax))
def disc(x,y,r,c,f=fill_rect):
  s=int(1.414213562373095*r)|1
  h=s>>1
  f(x-h,y-h,s,s,c)
  s=r*r
  for w in range(h+1,r):
    r=w*w
    while r+h*h>s:
      h-=1
    r=h<<1|1
    f(x-w,y-h,1,r,c)
    f(x-h,y-w,r,1,c)
    f(x+w,y-h,1,r,c)
    f(x-h,y+w,r,1,c)
    h-=1

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.