paint.py

Created by ziii

Created on December 15, 2023

2.79 KB

A monochrome paint with possible but limited input and output because of the calculator limitations.

It was made to create map for grid based games or sprites ( I used it to create chess pieces for my chess game or to create map for my plateformer).

How to use:

The console prompt will ask you the settings you want.

You can change the length in pixels of the side of the painted squares (a zoom, only for comfort purposes won’t change the output).

You can also change the width and height resolution of the canvas.

Arrows move the cursor (red square).

1 to select the pencil tool (black squares, will count as drawn pixel).

0 to select the eraser(white squares, will count as not drawn pixel).

Shift to use the tool.

“x,n,t” key to use your tool on the entire canvas.

Alpha can be used to apply your tool on a rectangle, two presses are needed to define the two corners of the rectangle.

Exe will stop the program, you need to press a key to see the shell back.

Ans will output the final drawing in the command prompt, while the cursor is not red it is still processing your drawing, if it is too big it may not works as well. It is a tuple with three values, the first one is a string which encodes the entirety of your drawing, the two others are the dimensions (first width then height) of the canvas. If you drawing is too large the calculator isn’t able to copy paste the entire string (219 characters copy limit) so you’l have to copy paste each line to your program, just don’t put the line breaks.

I wrote a function in another one of my scripts (“draw_function”) which is able to use this output with the kandinsky module.


from kandinsky import fill_rect as f, draw_string as d
from ion import *
from time import monotonic
def set_at(c:[int],bit:bool):
  global grid
  grid = (grid & (~(1 << (c[0] + c[1]*lw)))) | (bit << (c[0] + c[1]*lw))
def get_at(c:[int]) -> bool:
  return (grid >> (c[0] + c[1]*lw)) & 1
def square(COLOR):
  f(pos[0]*p_s,pos[1]*p_s,cs[0],cs[1],COLOR)
sign=lambda x: 1 if x>=0 else -1
get_cs=lambda ps: (((2*ps)//5)+1,((2*ps)//5)+1)
COLORS=(
(255,255,255),#empty pixel
(0,0,0),#filled pixel
)
CURSOR=(255,0,0)
BACKGROUND=(60,60,60)
TEXT=(255,255,255)
BEGIN=(255,255,204)
DOING=(255,165,0)
p_s=int(input("Enter size of scale/zoom \n(squares of x length \nwill represents pixels): "))
lw=int(input("Enter resolution of the draw\nWidth: "))
lh=int(input("Height: "))
time=monotonic()-0.1
pressed={KEY_SHIFT:0,KEY_ALPHA:0,KEY_XNT:0,KEY_ANS:0,}
pos=[0,0]
tool=1
cs=get_cs(p_s)
rec=[-1,-1]
key_pressing={KEY_SHIFT:0,KEY_ALPHA:0,KEY_XNT:0,KEY_ANS:0}
key_pressed={k:0 for k in key_pressing}
f(0,0,320,222,BACKGROUND)
f(0,0,lw*p_s,lh*p_s,COLORS[0])
square(CURSOR)
grid=0
run=1
while run:
  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
  if keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_RIGHT) or keydown(KEY_DOWN):
    if monotonic()-time>0.1:
        time=monotonic()
        square(COLORS[get_at(pos)])
        pos[0]=(pos[0]-keydown(KEY_LEFT)+keydown(KEY_RIGHT))%lw
        pos[1]=(pos[1]-keydown(KEY_UP)+keydown(KEY_DOWN))%lh
        square(CURSOR)
  elif key_pressed[KEY_SHIFT]:
    set_at(pos,tool)
    f(pos[0]*p_s,pos[1]*p_s,p_s,p_s,COLORS[tool])
    square(CURSOR)
  elif keydown(KEY_ZERO):
    tool=0
  elif keydown(KEY_ONE):
    tool=1
  elif key_pressed[KEY_ALPHA]:
    if rec[0]==-1 and rec[1]==-1:
      f(pos[0]*p_s,pos[1]*p_s,p_s,p_s,CURSOR)
      rec=tuple(pos)
    elif rec==tuple(pos):
      f(pos[0]*p_s,pos[1]*p_s,p_s,p_s,COLORS[get_at(pos)])
    else:
      for x in range(rec[0],pos[0]+sign(pos[0]-rec[0]),sign(pos[0]-rec[0])):
        for y in range(rec[1],pos[1]+sign(pos[1]-rec[1]),sign(pos[1]-rec[1])):
          set_at([x,y],tool)
          f(x*p_s,y*p_s,p_s,p_s,COLORS[tool])
      rec=[-1,-1]
    square(CURSOR)
  elif key_pressed[KEY_XNT]:
    f(0,0,p_s*lw,p_s*lh,COLORS[tool])
    square(CURSOR)
    if tool:
      grid=2**(lw*lh)-1
    else:
      grid=0
  elif key_pressed[KEY_ANS]:
    copy=grid
    res=""
    square(BEGIN)
    while copy!=0:
      res+=chr(copy % 92 +35 )
      copy //=92
    square(DOING)
    print("(\"",end="")
    for i in range(len(res)//200+1):
      if i==(len(res)//200):
        print(res[200*i:200*(1+i)],end="")
      else:
        print(res[200*i:200*(1+i)])
    print("\",{},{})".format(lw,lh))
    square(CURSOR)
  elif keydown(KEY_EXE):
    run=0