pixelmaker.py

Created by golem64

Created on April 20, 2021

3.45 KB


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

def dec(hex):
  hexa=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
  deci=0
  for i in range(len(hex)): deci+=(16**i)*hexa.index(hex[-i-1])
  return deci

def load():
  file=input("File Name: ")
  global pxl
  try: pxl=open(file+".pxl")
  except: print("No such file")
  else:
    print("File loaded")
    global colors
    colors=pxl.readline()
    colors=colors.split("\n")
    colors.pop(1)
    colors=colors[0]
    colors=colors.split("/")
    try:
      for i in range(len(colors)): colors[i]=(dec(colors[i][0]+colors[i][1]),dec(colors[i][2]+colors[i][3]),dec(colors[i][4]+colors[i][5]))
    except: print("Error: cannot fetch colors")
    else:
#      colors.insert(0,(255,255,255))
      print("Successfully fetched colors")
      print("Ready to display")

def display(size=1,xstart=0,ystart=0,silent=0):
  if silent==0: print("Loading display...")
  global pxl
  global colors
  clearDraw()
  pxl.seek(0)
  for i in range(ystart+1): pxl.readline()
  a=0
  j=0
  while a!=1:
    line=pxl.readline()
    if line!='':
      line=line.split("\n")
      line.pop(1)
      line=line[0]
      print(len(line))
      assert len(line)&1==0,"incorrect or corrupted line"
      cut=[]
      i=0
      while i+xstart!=len(line) and i*size<642:
        if i+xstart>-1: cut.append(line[xstart+i]+line[xstart+i+1])
        else: cut.append('00')
        i+=2
      for i in range(len(cut)):
        fill_rect(i*size,j*size,size,size,colors[dec(cut[i])])
        if j*size>222: a=1
#      while i!=len(cut):
#        if i*size<321:
#          if j*size<223: fill_rect(i*size-xstart*size,j*size-ystart*size,size,size,colors[dec(cut[i])])
#          else:
#            i=len(cut)-1
#            a=1
#        else: i=len(cut)-1
#        i+=1
    else: a=1
    j+=1
  if silent==0: print("Done")
  
def clearDraw():
  fill_rect(0,0,320,222,(255,255,255))
  
def new():
  print("New pxl")

def nav():
  x=0
  y=0
  s=1
  display(s,x,y)
  while not keydown(KEY_ONOFF):
    if keydown(KEY_RIGHT):
      x+=2
      
      display(s,x,y,1)
    if keydown(KEY_LEFT):
      x-=2
      if x<0: x=0
      display(s,x,y,1)
    if keydown(KEY_DOWN):
      y+=1
      display(s,x,y,1)
    if keydown(KEY_UP):
      y-=1
      if y<0: y=0
      display(s,x,y,1)
    if keydown(KEY_ALPHA):
      s*=2
      if s>64: s=64
      display(s,x,y,1)
    if keydown(KEY_SHIFT):
      s=int(s/2)
      if s<1: s=1
      display(s,x,y,1)
    rgb(x,y,s)

def rgb(x,y,s):
  change=get_pixel(x,y)
  r=change[0]
  g=change[1]
  b=change[2]
  if keydown(KEY_LEFTPARENTHESIS):
    if r-5>0: r-=5
  if keydown(KEY_RIGHTPARENTHESIS):
    print(r)
    if r+5<255: r+=5
    print(r)
  if keydown(KEY_MULTIPLICATION):
    if g-5>0: g-=5
  if keydown(KEY_DIVISION):
    if g+5<255: g+=5
  if keydown(KEY_PLUS):
    if b-5>0: b-=5
  if keydown(KEY_MINUS):
    if b+5<255: b+=5
  print((r,g,b))
  fill_rect(x,y,s,s,(r,g,b))#????????

print("Home=New\nOk=Load\nVar=Import\nOnOff=Exit")
while not (keydown(KEY_OK) or keydown(KEY_HOME) or keydown(KEY_ONOFF) or keydown(KEY_VAR)): sleep(0)
if keydown(KEY_OK): load()
elif keydown(KEY_HOME):
  clearDraw()
  new()
elif keydown(KEY_VAR):
  name=input("Filename: ")
  try: open(name+".py")
  except: print("No such file")
  else:
    rename(name+".py",name+".pxl")
    file=open(name+".pxl")
    adapt=file.read()
    adapt=adapt.split("\x00")
    adapt=adapt[1]
    open(name+".pxl","wt").write(adapt)
    print("Done")