sprite_to_bin.py

Created by loic-azavant

Created on December 07, 2022

1.55 KB

Ce script prend la taille du Canvas, et permet de dessiner un sprite en noir et blanc, et ensuite de recevoir ce sprite sous la forme d’un grand nombre. Ce dernier peut être reconverti en sprite grâce au script bin_to_sprite.py


from kandinsky import *
from keyinput import *
from time import *

size=10

sprite_size = eval(input("sprite size: "))
while sprite_size[0]*size>200 or sprite_size[1]*size>222:
  size/=2
  size=int(size)

lines = [[0 for j in range(sprite_size[0])] for i in range(sprite_size[1])]

y = 111-int(sprite_size[1]/2)*size
x = 100-int(sprite_size[1]/2)*size


cursorX = 0
cursorY = 0

fill_rect(x,y-size,-size,sprite_size[1]*size+size*2,(0,)*3)
fill_rect(x,y,sprite_size[0]*size,-size,(0,)*3)
fill_rect(x+sprite_size[0]*size,y-size,size,sprite_size[1]*size+size*2,(0,)*3)
fill_rect(x,y+sprite_size[1]*size,sprite_size[0]*size,size,(0,)*3)
draw_string("Use the\narrow key to \nmove the \ncursor\n\nOK to change \nthe color\n\nON/OFF to \nget the \nimage in \nbinary",200,5)



while 1:
  oldCX = cursorX
  oldCY = cursorY
  cursorX = max(0,min(cursorX+keyinput(3)-keyinput(0),sprite_size[0]-1))
  cursorY = max(0,min(cursorY+keyinput(2)-keyinput(1),sprite_size[1]-1))
  okPressed = keyinput(4)
  if okPressed:
    lines[cursorY][cursorX] += 1
    lines[cursorY][cursorX] %= 2
  if cursorX!=oldCX or cursorY!=oldCY or okPressed:
    fill_rect(x+oldCX*size,y+oldCY*size,size,size,(255*(-1*lines[oldCY][oldCX]+1),)*3)
    fill_rect(x+cursorX*size,y+cursorY*size,size,size,(100+100*(-1*lines[cursorY][cursorX]+1),)*3)
  sleep(0.05)
  if keyinput(KEY_ONOFF):
    break

for i in range(len(lines)):
  for j in range(len(lines[i])):
    lines[i][j] = str(lines[i][j])
  lines[i] = "".join(reversed(lines[i]))

lines.reverse()

complete = "0b"+"".join(lines)
print(int(complete))

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.