from math import * from kandinsky import * from kandinsky import fill_rect as F from random import * from ion import * from time import * BL=(0,0,0) WH=(255,)*3 GR=(230,)*3 RE=(155,0,0) BG=(240,)*3 FONT_BG=(0,)*3 FONT_C=(255,)*3 arith=["*","/","+","-"] colors=[(255,0,0),(0,255,0),(255,255,0),(0,0,255)] labels=["Multiplication","Division","Addition","Subtraction"] def dot(x,y,c,fg,bg,t,a,b): draw_string(c,0,0,GR,WH) fill_rect(x,y,9*t,18*t,bg) for v in range(18): for u in range(9): if get_pixel(u,v)==WH: fill_rect(x+u*t,y+v*t,t-a,t-b,fg) draw_string(" ",0,0,WH,WH) def aff(txt,x,y,t,fg,bg=WH,a=1,b=1): for (i,c) in enumerate(txt): dot(x+i*t*9,y,c,fg,bg,t,a,b) def clear_scr(): F(0,83,322,104,BG) def compute(n1,n2,func): if func==0: return str(int(n1*n2)) if func==1: return str(round(n1/n2,2)) if func==2: return str(int(n1+n2)) if func==3: return str(int(n1-n2)) def show_expression(): clear_scr() aff(str(int(num))+arith[func]+str(int(num_2))+"="+compute(num,num_2,func),10,105,3,FONT_C,FONT_BG) def reset_all(): global num,num_2 num=num_2=0 show_expression() num=num_2=0 func=0 speed_t=0.2 F(0, 0, 322, 222,(0,255,255)) F(0, 77, 322, 116, 'black') F(0, 83, 322, 104, BG) aff(str(int(num))+arith[func]+str(int(num_2))+"="+compute(num,num_2,func),20,105,3,(220,)*3,(0,)*3) while not keydown(KEY_EXE): oper=arith[func] if func==1 and num_2==0: num_2=1 if keydown(KEY_MULTIPLICATION): func=0 if keydown(KEY_DIVISION): func=1 if keydown(KEY_PLUS): func=2 if keydown(KEY_MINUS): func=3 for i,k in enumerate([KEY_MULTIPLICATION,KEY_DIVISION,KEY_PLUS,KEY_MINUS]): if keydown(k): clear_scr() F(0,0,322,50,colors[i]) aff(str(int(num))+arith[i]+str(int(num_2))+"="+compute(num,num_2,i),10,105,3,FONT_C,FONT_BG) aff(labels[i],20,5,2,FONT_C,FONT_BG) if keydown(KEY_UP): speed_t=max(speed_t-0.01,0.0005) sleep(speed_t) num+=1 show_expression() if keydown(KEY_DOWN): sleep(0.2) num-=1 show_expression() if keydown(KEY_LEFT): sleep(0.2) num_2-=1 if func==1 and num_2==0: num_2=-1 show_expression() if keydown(KEY_RIGHT): sleep(0.2) num_2+=1 if func==1 and num_2==0: num_2=1 show_expression() if keydown(KEY_BACKSPACE): sleep(0.2) reset_all() draw_string("Press [OK]",100,190) print("bg=",BG) print("font_c=",FONT_C)