func.py

Created by ziii

Created on December 20, 2023

4.33 KB


from kandinsky import fill_rect as f, set_pixel as s_p
from ion import *
from time import *

"""""
I imported fill_rect as f and set_pixel as s_p to save space,
you can do the same or change it in every program
"""""

def draw(arr,coo,cor,le,col,r,scale):
  a=r%2
  if cor[not a]+le[not a]>=coo[not a]+coo_r(arr[-1*(r//2)]*scale,r)[not a] and coo[not a]+coo_r(arr[-1+r//2]*scale,r)[not a]>=cor[not a]:
    for l in arr:
      for s in range(scale):
        if le[not a]>coo[not a]+coo_r(l,r)[not a]*scale+s-cor[not a]>=0 and cor[a]+le[a]>coo[a]+(coo_r(l,r)[a]-l[2]*(((r+1)//2)%2))*scale and coo[a]+(coo_r(l,r)[a]+l[2]*(((r-1)//2)%2))*scale>cor[a]:
            sq=[coo[0]+(coo_r(l,r)[0]-l[2]*(r==2))*scale+s*a,coo[1]+(coo_r(l,r)[1]-l[2]*(r==1))*scale+s*(not a),coo[0]+(coo_r(l,r)[0]+l[2]*(r==0))*scale+s*a,coo[1]+(coo_r(l,r)[1]+l[2]*(r==3))*scale+s*(not a)]
            sq[a]=(sq[a],cor[a])[cor[a]>sq[a]]
            sq[a+2]=(sq[a+2],cor[a]+le[a])[sq[a+2]>cor[a]+le[a]]
            f(sq[0],sq[1],(sq[2]-sq[0])**(not a),(sq[3]-sq[1])**a,col)

coo_r=lambda coo, r: [(1-r+r%2)*coo[r%2]-(r//2),(1-2*(((-r)%4)//2))*coo[(r+1)%2]-(((-r)%4)//2)]

coos_r= lambda coos, r,cntr:[[(1-r+r%2)*(coo[r%2]-cntr[r%2])-(r//2) + cntr[0],(1-2*(((-r)%4)//2))*(coo[(r+1)%2]-cntr[(r+1)%2])-(((-r)%4)//2) + cntr[1]] for coo in coos]

def cross(size,thick,pos,col):
  for x in range(size-thick+1):
    f(x+pos[0],pos[1]+x,thick,thick,col)
    f(x+pos[0],pos[1]+size-x,thick,-thick,col)

def circle(r,pos,col):
  for x in range(r):
    h=int(((r**2)-(x**2))**0.5)
    f(pos[0]-x,pos[1]-h,1,h*2,col)
    f(pos[0]+x,pos[1]-h,1,h*2,col)

def square(a,coo,w,col):
  f(coo[0],coo[1],a,w,col)
  f(coo[0],coo[1]+a-w,a,w,col)
  f(coo[0],coo[1],w,a,col)
  f(coo[0]+a-w,coo[1],w,a,col)

#draws a number
NS=737837656508197734647
def number(num,x,y,size,col,sp=1):
  a=[1+size[1]//12,1+size[0]//(len(str(num))*12)]
  a=a[a[0]>a[1]]
  b=[(size[1]-3*a)//2,(size[0]+sp)//len(str(num))-sp-2*a]
  b=b[b[0]>b[1]]
  if b>1 and num>=0:
    num=[int(a) for a in str(num)]
    coo=((x,y+a,a,b),(x+a,y,b,a),(x+a+b,y+a,a,b),(x+a,y+a+b,b,a),(x,y+2*a+b,a,b),(x+a,y+2*a+2*b,b,a),(x+a+b,y+2*a+b,a,b))
    for n,k in enumerate(num):
      for x in range(7):
        if ((k*7 + x) >> NS) & 1:
          f(coo[x][0]+n*(b+2*a+sp),coo[x][1],coo[x][2],coo[x][3],col)


#take number keyboard input
num=0
NUMS={KEY_ZERO:"0",KEY_ONE:"1",KEY_TWO:"2",KEY_THREE:"3",KEY_FOUR:"4",KEY_FIVE:"5",KEY_SIX:"6",KEY_SEVEN:"7",KEY_EIGHT:"8",KEY_NINE:"9"}
def min_max(min_n=0,max_n=0):
  global num
  if max_n>0 and int(num)>max_n:
    num=str(max_n)
  if int(num)<min_n:
    num=str(min_n)
def update(input):
  global num
  if input==KEY_BACKSPACE:
    if len(num)==1:
      num="0"
    else:
      num=num[:-1]
  elif not (int(num)==0 and input==KEY_ZERO):
    if len(num)==1 and int(num)==0:
      num=""
    num+=NUMS[input]

#check wich keys were pressed between calls
#put this at the beginning of your code with the keys you want to check in listening
"""""
key_pressing={KEY_OK:0,KEY_DOWN:0,KEY_UP:0,KEY_LEFT:0,KEY_RIGHT:0}
key_pressed={k:0 for k in key_pressing}
"""""
#use this to check for presses
"""""
for k in key_pressing:
  key_pressed[k]=0
  if keydown(k):
    if not key_pressing[k]:
      key_pressed[k]=1
    key_pressing[k]=1
  else:
    key_pressing[k]=0
"""""
#condition to see if a key was just pressed, replace KEY_NAME
# key_pressed[KEY_NAME]

#creates gradient
GRADIENT_COLORS=((255,150,0),(0,149,255),(50,50,50))
N=1/(len(GRADIENT_COLORS)-1)
def rgb(v):
  return tuple([GRADIENT_COLORS[int(v/N)][x]+(GRADIENT_COLORS[int(v/N)+1][x]-GRADIENT_COLORS[int(v/N)][x])*(v%N)/N for x in range(3)])

#draw a line between two points
def line(x0,y0,x1,y1,color):
  h=((x1-x0)**2+(y1-y0)**2)**0.5
  cos=(x1-x0)/h
  sin=(y1-y0)/h
  for p in range(int(h)+1):
    s_p(x0+int(cos*p),y0+int(sin*p),color)

#three points bezier curve
def bezier3(x0,y0,x1,y1,x2,y2):
  d=int(((x0-x1)**2+(y0-y1)**2)**0.5+((x1-x2)**2+(y1-y2)**2)**0.5)
  for a in range(d):
    t=a/d
    s_p(int(x0*(1-2*t+t**2)+2*x1*(t-t**2)+x2*(t**2)),int(y0*(1-2*t+t**2)+2*y1*(t-t**2)+y2*(t**2)),(0,0,0))

#set nth bit of an integer i to 1
def set_1_at(i,n):
  n |= 1<< i

#set nth bit of an integer i to 0
def set_0_at(i,n):
  n &= ~(1 << i)

#set nth bit of an integer i to 0 or 1
def set_at(n,bit,i):
  i = (i & (~(1 << n))) | (bit << n)

#return the value of the nth bit of an integer i
def get_at(n, i):
  return (i >> n) & 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.