draw.py

Created by octo

Created on May 29, 2024

2.79 KB

Pixel art editor or simple 32x32 drawing program


from kandinsky import *
from time import *
from ion import *

ScW=320
ScH=224

GrS=32
Stp=ScH//GrS
Pal="ffffff,000000,3e3546,625565,966c6c,9e4539,7a3045,6b3e75,905ea9,a884f3,eaaded,fca790,f68181,f04f78,c32454,831c5d,484a77,4d65b4,4d9be6,8fd3ff,8ff8e2,30e1b9,0b8a8f,0b5e65,1ebc73,91db69,fbff86,fbb954,fb6b1d,e83b3b,cd683d,ab947a,e3c896".split(",")

Px=Py=0
Ps=1
Brd=[]
Cc=1
for i in range(GrS**2):Brd+=[0]

def CH2D(c):
  return color(int("0x"+c[0:2]),int("0x"+c[2:4]),int("0x"+c[4:6]))
def GPal(c):
  return CH2D(Pal[c])

def DrawPal():
  h=-1
  fill_rect(244+19*h,0,19,19,GPal(0))
  for i in range(len(Pal)-1):
    if i%8==0:h+=1
    fill_rect(244+19*h,19*(i%8),19,19,GPal(i+1))

def UpdZone(z=2+Ps):
  v=-1
  for i in range(z**2):
    if i%z==0:v+=1
    p=32*(Py+v)+(Px+(i%z))
    if p<len(Brd):fill_rect((Px+(i%z))*Stp,(Py+v)*Stp,Stp,Stp,GPal(Brd[p]))
  if Px>29:
    fill_rect(ScH,Py*Stp,3*Stp-2,3*Stp,color(255,232,255))
    fill_rect(244-19,0,19,19,GPal(0))

def DrawBase():
  UpdZone()

  fill_rect(0,0,Stp*GrS,2,GPal(1))
  fill_rect(0,0,2,Stp*GrS,GPal(1))
  fill_rect(0,Stp*GrS-2,Stp*GrS,2,GPal(1))
  fill_rect(Stp*GrS-2,0,2,Stp*GrS,GPal(1))

  fill_rect(Stp*(GrS//2),0,2,Stp*GrS,GPal(1))
  fill_rect(0,Stp*(GrS//2),Stp*GrS,2,GPal(1))

def DrawPencil(x,y):
  for i in range(12):
    fill_rect((x+1)*Stp+i,(y+1)*Stp+i+1,2,2,GPal(0))
  for i in range(12):
    fill_rect((x+1)*Stp+i,(y+1)*Stp+i,2,2,GPal(1))
  fill_rect((x+1)*Stp,(y+1)*Stp,12,3,GPal(1))
  fill_rect((x+1)*Stp,(y+1)*Stp,3,12,GPal(1))
  fill_rect((x+1)*Stp+2,(y+1)*Stp+2,10,1,GPal(0))
  fill_rect((x+1)*Stp+2,(y+1)*Stp+2,1,10,GPal(0))

def DrawShine(x,y,d=1):
  fill_rect(x,y,3*d,1*d,GPal(1))
  fill_rect(x,y,1*d,3*d,GPal(1))
  fill_rect(x+1*d,y+1*d,3*d,1*d,GPal(0))
  fill_rect(x+1*d,y+1*d,1*d,3*d,GPal(0))

def DrawPointer(k=None):
  global Px,Py
  DrawBase()
  if k=="L" and Px>0:Px-=1
  elif k=="R" and Px<GrS-1:Px+=1
  elif k=="U" and Py>0:Py-=1
  elif k=="D" and Py<GrS-1:Py+=1
  
  fill_rect(Px*Stp,Py*Stp,Stp,Stp,GPal(Cc))
  DrawShine(Px*Stp,Py*Stp)
  DrawPencil(Px,Py)

def DrawPixel(c):
  DrawBase()
  Brd[32*Py+Px]=c
  fill_rect(Px*Stp,Py*Stp,Stp,Stp,GPal(c))
  DrawPointer()

def DrawCSel(c=Cc):
  global Cc
  Cc=c
  DrawPal()
  if c==0:DrawShine(244-19,0,2)
  else:DrawShine(244+19*((c-1)//8),19*((c-1)%8),2)
  DrawPointer()
  sleep(0.1)

fill_rect(ScH,0,ScW-ScH,ScH,color(255,232,255))
fill_rect(0,0,Stp*GrS,Stp*GrS,GPal(0))
DrawBase()
DrawPointer()
DrawPal()
DrawCSel()

while True:
  sleep(0.05)
  if keydown(KEY_LEFTPARENTHESIS) and Cc>0:DrawCSel(Cc-1)
  elif keydown(KEY_RIGHTPARENTHESIS)and Cc<len(Pal)-1:DrawCSel(Cc+1)
  if keydown(KEY_EXE):DrawPixel(Cc)
  elif keydown(KEY_DOT):DrawPixel(0)
  if keydown(KEY_LEFT):DrawPointer("L")
  elif keydown(KEY_RIGHT):DrawPointer("R")
  if keydown(KEY_UP):DrawPointer("U")
  elif keydown(KEY_DOWN):DrawPointer("D")

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.