morpher.py

Created by squarepoint

Created on March 30, 2022

2.32 KB

This script draws a linear interpolation between two shapes. First you need to input the number of points you want your shapes to have, then you draw them using arrow keys and OK, and finally you change the value of the interpolation with left and right keys. Have fun!


#Shape Morpher - by Squarepoint
#Credits to Scratch user Dinosu for heavy inspiration
from ion import *
from kandinsky import *
from time import sleep
x1=[]
y1=[]
x2=[]
y2=[]
lintx=[]
linty=[]
x=159
y=100
t=0
res=int(input("Write max number of points(re\ncommended: 50)\n"))
def move(x,y):
  fill_rect(x,y,2,2,color(200,200,200))
  if keydown(KEY_LEFT)and x>0: x-=3
  elif keydown(KEY_RIGHT)and x<318: x+=3
  if keydown(KEY_UP)and y>20: y-=3
  elif keydown(KEY_DOWN)and y<220: y+=3
  return x,y
def drawshape(x,y):
  if len(x)==1:
    fill_rect(x[0],y[0],2,2,color(0,0,0))
  else:
    for i in range(len(x)-1):
      draw_line(x[i],y[i],x[i+1],y[i+1],color(0,0,0))
def linearly_interpolate(a,b,t):
  return int((b-a)*t+a)
def draw_line(x1,y1,x2,y2,c):
  for i in range(10):
    fill_rect(linearly_interpolate(x1,x2,i/10),linearly_interpolate(y1,y2,i/10),2,2,color(0,0,0))
fill_rect(0,18,320,204,color(200,200,200))
while True:
  draw_string("Draw first shape. Space left:"+str(res-len(x1))+"  ",0,0)
  x,y=move(x,y)
  drawshape(x1,y1)
  fill_rect(x,y,2,2,color(255,0,0))
  sleep(0.02)
  if len(x1)==res:
    break
  if keydown(KEY_OK):
      x1.append(x)
      y1.append(y)
      sleep(0.1)
fill_rect(0,18,320,204,color(200,200,200))
while True:
  draw_string("Draw second shape.Space left:"+str(res-len(x2))+"  ",0,0)
  x,y=move(x,y)
  drawshape(x2,y2)
  fill_rect(x,y,2,2,color(255,0,0))
  sleep(0.02)
  if len(x2)==res:
    break
  if keydown(KEY_OK):
      x2.append(x)
      y2.append(y)
      sleep(0.1)
draw_string("Now press left/right for magic!",0,0)
fill_rect(0,18,320,204,color(200,200,200))
lintx=[]
linty=[]
for i in range(res):
  lintx.append(linearly_interpolate(x1[i],x2[i],t))
  linty.append(linearly_interpolate(y1[i],y2[i],t))
drawshape(lintx,linty)
while True:
  if keydown(KEY_LEFT) and t>0:
    fill_rect(0,18,320,204,color(200,200,200))
    t-=0.05
    lintx=[]
    linty=[]
    for i in range(res):
      lintx.append(linearly_interpolate(x1[i],x2[i],t))
      linty.append(linearly_interpolate(y1[i],y2[i],t))
    drawshape(lintx,linty)
  elif keydown(KEY_RIGHT)and t<1:
    fill_rect(0,18,320,204,color(200,200,200))
    t+=0.05
    lintx=[]
    linty=[]
    for i in range(res):
      lintx.append(linearly_interpolate(x1[i],x2[i],t))
      linty.append(linearly_interpolate(y1[i],y2[i],t))
    drawshape(lintx,linty)
  sleep(0.05)

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.