cubef_casio.py

Created by mathieu-croslacoste

Created on September 15, 2025

3.04 KB

cubefield (casio script) on Numworks. Compressed.


from random import randint as R,random as A,choice as C
from math import*
from kandinsky import fill_rect as F,draw_string as D,set_pixel as S
from ion import keydown as K
from time import sleep as P
LSCR,HSCR=320,222
def getkey():
 for i in range(5):
  if K(i):return i
 return-1
try:from kandinsky import draw_line as L
except:
 def sign(x):return x>0 and 1 or x<0 and-1
 def L(x1,y1,x2,y2,c):
  x1,y1,x2,y2=[round(v)for v in(x1,y1,x2,y2)];dx,dy=x2-x1,y2-y1;sx,sy=sign(dx),sign(dy);dx,dy=abs(dx),abs(dy);err=dx-dy
  while 1:
   S(x1,y1,c)
   if x1==x2 and y1==y2:break
   e2=2*err
   if e2>-dy:err-=dy;x1+=sx
   if e2<dx:err+=dx;y1+=sy
def borne(val,vmin,vmax):return max(vmin,min(vmax,val))
HH=HSCR*2//5
CUBE_R=1
CAM_MUL=12
MAX_VX=.5
MAX_X=10
MAX_BANK=20
f=lambda rgb:(rgb>>16,(rgb>>8)&0xff,rgb&0xff)
PAL=[f(0xcbd1be),f(0x8f9389),f(0x52534c),f(0x26201d),f(0xe0a46e),f(0x91a47a),f(0x5d7643),f(0x4d533a),f(0xa93130),f(0x7a1338),f(0x834664),f(0x917692),f(0x160712),f(0x593084),f(0x3870be),f(0x579fb4)]
CGY=(128,)*3
try:dark=int(input("Dark Mode? 0/1\n"));dark=dark==1
except:dark=0
CPL,BG="k","w"
if dark:CPL,BG="r",(0,0,50)
CRD=(255,0,0)
class Cam:
 def __init__(s,x,y,z,bank):s.x,s.y=x,y;s.z=z;s.sin=sin(bank);s.cos=cos(bank)
 def transform(s,x,rel_y,z):x-=s.x;z-=s.z;x,z=(s.cos*x-s.sin*z,s.sin*x+s.cos*z);x=CAM_MUL*x/rel_y;z=CAM_MUL*z/rel_y;return(x+LSCR/2,HH-z)
def Horizon(cam):tan=int(LSCR/2*cam.sin/cam.cos);L(0,HH+tan,LSCR,HH-tan,CGY)
def Player():x,y=LSCR/2,HH+100;L(x,y-10,x-10,y,CPL);L(x,y-10,x+10,y,CPL);L(x-10,y,x+10,y,CPL)
def Cubes(cubes,cam):
 n=len(cubes)
 for i in range(n-1,-1,-1):
  wx,wy,clrId=cubes[i];depth=wy-cam.y
  if depth>=6:continue
  x,y=cam.transform(wx-CUBE_R,depth,0);size=CAM_MUL*2*CUBE_R/depth;scos=size*cam.cos;ssin=size*cam.sin;clr=PAL[clrId];
  L(x,y,x+scos,y-ssin,clr);L(x,y,x-ssin,y-scos,clr);L(x+scos-ssin,y-ssin-scos,x+scos,y-ssin,clr);L(x+scos-ssin,y-ssin-scos,x-ssin,y-scos,clr)
def render(x,y,z,vx,vy,cubes,score):bank=(vx*MAX_BANK/MAX_VX*pi/180);cam=Cam(x,y,z,bank);F(0,20,320,202,BG);Cubes(cubes,cam);Player();Horizon(cam);D("Score: "+str(score),2,2,(255,128,0),BG)
def collision(x,y,cubes):
 for c in cubes:
  if c[1]>=y+.4:break
  if abs(c[0]-x)<=CUBE_R:return 1
 return 0
def game():
 def gen(oy):
  for cy in range(oy,20):
   for cx in range(-MAX_X,MAX_X-CUBE_R):
    if R(1,12)==1:cubes.append((cx,cy+A(),R(0,len(PAL)-1)))
 F(0,0,320,222,BG);x,y,z=0,0,2.5;vx,vy=0,.05;cubes=[];score=1;k=None;gen(1)
 if dark:
  for i in range(99):S(R(0,320),R(0,20),(R(50,250),)*3)
 while 1:
  if collision(x,y,cubes):return score
  render(x,y,z,vx,vy,cubes,score);k=getkey();dir=(k==3)-(k==0)
#  vdir=(k==1)-(k==2);
  ax=.1*dir
  if ax==0:ax=-.3*vx
  vx=round(borne(vx+ax,-MAX_VX,MAX_VX),2)
  if abs(vx)<.1:vx=0
  x=borne(x+vx,-MAX_X,MAX_X);y+=vy
#  z=max(0,z+vdir*.1);
  score+=1;cubes=[c for c in cubes if c[1]>y+.2];P(.008)
  if len(cubes)<5:cubes=[[c[0],c[1]-y,c[2]]for c in cubes];gen(4);y=0
 cols=("r",(200,)*3,(40,)*3)
 for i in range(3000):F(R(0,315),R(0,212),R(10,12),R(5,6),C(cols));P((i%8==7)*.001)
 D("Game Over",115,102,CPL,BG)
print(game())

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.