write_v3.py

Created by polettielio

Created on September 07, 2022

8.28 KB

Writes text given to main() on the screen (graphical output). Ecrit le texte donné en argument à main() sur l’écran (résultat graphique)


### write
# write a text string on the screen
# written by lapingenieur
# version 3

### USAGE - PLEASE READ
#
# import the whole file
#  then execute main() with the
#  string to print as the argument
# for ex. main("hello world")
# (note: accepts all case letters,
#  but converts them all to upper
#  case/capital ones)
#
# note that this script can only
#  print capital letters and spaces
# this is due to memory errors I
#  corrected by compressing the
#  script as much as I could, this
#  is why it is so unreadable...
#
# please look at the USER SETTINGS
#  part of this script to customize
#  the output a bit
# note that the setings section has been
#  moved to the end of this source code
#  file to be more acessible
#
# note that this script outputs a
#  bunch of logs that can be useful
#  debugging it or just understanding
#  what is does. Just press back
#  once the program is done

# IMPORTS
import turtle
from math import sqrt

# USER SETTINGS
#
# NOTE! please use setings staying at the
# end of this source code file
#
#times=5   # letters per *1/2* line
#lines=4   # nb of text lines
#btw=10    # nb px between letters ; = gap
#ratio=20  # letter width in px

def s_r(mr,sr): # set°/ratio
  turtle.setheading(sr)
  turtle.forward(mr)

def r_r(mr):    # right/ratio
  turtle.right(90)
  turtle.forward(mr)

def l_r(mr):    # right/ratio
  turtle.left(90)
  turtle.forward(mr)

def w_ah(a):
  turtle.pendown()
  l_r(ratio*2)
  turtle.right(90)
  if (a != 1):
    turtle.penup()
  turtle.forward(ratio)
  turtle.pendown()
  r_r(ratio)
  r_r(ratio)
  turtle.backward(ratio)
  l_r(ratio)
  turtle.penup()
  s_r(btw,0)

def w_cg(g):
  turtle.pendown()
  turtle.forward(ratio)
  if (g == 1):
    l_r((ratio/5)*4)
    l_r(ratio/3)
    turtle.backward(ratio/3)
    r_r(-(ratio/5)*4)
    turtle.right(90)
  turtle.backward(ratio)
  l_r(ratio*2)
  r_r(ratio)
  turtle.penup()
  r_r(ratio*2)
  l_r(btw)

def w_dob(d,q):
  turtle.pendown()
  l_r(ratio*2)
  if (d != 0):
    r_r((ratio/4)*3)
    if (d == 1):
      s_r(sqrt((ratio**2)+((ratio/4)**2)),-75.9)
    else:
      r_r(ratio)
      r_r((ratio/4)*3)
      turtle.backward(ratio)
  else:
    r_r(ratio)
    r_r(ratio)
  s_r(ratio,-90)
  if (q == 1):
    s_r((ratio/2),125)
    turtle.backward((ratio/6)*4)
    turtle.forward(ratio/6)
  s_r(ratio,180)
  turtle.penup()
  s_r((ratio+btw),0)

def w_ef(f):
  turtle.pendown()
  l_r(ratio*2)
  r_r(ratio)
  turtle.right(90)
  r_r(ratio)
  l_r(ratio)
  l_r((ratio/4)*3)
  turtle.backward((ratio/4)*3)
  r_r(ratio)
  if (f == 1):
    turtle.penup()
  l_r(ratio)
  turtle.penup()
  turtle.forward(btw)

def w_ijt(t):
  turtle.forward(ratio/2)
  turtle.pendown()
  l_r(ratio*2)
  if (t != 0):
    r_r(ratio/2)
    turtle.backward(ratio)
    turtle.forward(ratio/2)
  s_r((ratio*2),270)
  if (t == 2):
    r_r(ratio/2)
    r_r(ratio/4)
    turtle.backward(ratio/4)
    r_r(ratio/2)
  turtle.penup()
  s_r(((ratio/2)+btw),0)

def w_k():
  turtle.pendown()
  l_r(ratio*2)
  turtle.backward(ratio)
  go=sqrt((ratio**2)+(ratio**2))
  s_r(go,45)
  turtle.backward(go)
  r_r(go)
  turtle.penup()
  s_r(btw,0)

def w_l():
  turtle.pendown()
  l_r(ratio*2)
  turtle.backward(ratio*2)
  r_r(ratio)
  turtle.penup()
  turtle.forward(btw)

def w_mn(n):
  turtle.pendown()
  l_r(ratio*2)
  go=sqrt((ratio**2)+((ratio/2)**2))
  s_r(go,-63)
  if (n == 1):
    turtle.forward(go)
    s_r((ratio*2),90)
  else:
    s_r(go,63)
  s_r((ratio*2),-90)
  turtle.penup()
  l_r(btw) #^line's bugged (+1px ?)

def w_pr(r):
  turtle.pendown()
  l_r(ratio)
  turtle.left(90)
  for loop in range(4):
    r_r(ratio)
  turtle.right(90)
  if (r == 0):
    turtle.penup()
  s_r(sqrt((ratio**2)*2),-45)
  turtle.penup()
  s_r(btw,0)

def w_s():
  turtle.pendown()
  turtle.forward(ratio)
  for loop in range(2):
    l_r(ratio)
  for loop in range(2):
    r_r(ratio)
  turtle.penup()
  r_r(ratio*2)
  l_r(btw)

def w_uv(v):
  l_r(ratio*2)
  turtle.pendown()
  turtle.backward(ratio)
  if (v == 1):
    s_r(sqrt((ratio**2)+((ratio/2)**2)),-63)
    s_r(sqrt((ratio**2)+((ratio/2)**2)),63)
  else:
    turtle.backward(ratio)
    r_r(ratio)
    l_r(ratio)
  s_r(ratio,90)
  turtle.penup()
  turtle.backward(ratio*2)
  r_r(btw)

def w_w():
  l_r(ratio*2)
  turtle.pendown()
  turtle.backward(ratio)
  for loop in range(2):
    s_r(sqrt((ratio**2)+((ratio/2)**2)),-76)
    s_r(sqrt((ratio**2)+((ratio/2)**2)),76)
    turtle.setheading(90)
  turtle.forward(ratio)
  turtle.penup()
  turtle.backward(ratio*2)
  r_r(btw)

def w_x():
  turtle.pendown()
  s_r(sqrt((ratio**2)+((ratio*2)**2)),63)
  turtle.backward(sqrt((ratio**2)+((ratio*2)**2)))
  turtle.penup()
  s_r(ratio,0)
  turtle.pendown()
  s_r(sqrt((ratio**2)+((ratio*2)**2)),117)
  turtle.backward(sqrt((ratio**2)+((ratio*2)**2)))
  turtle.penup()
  s_r(btw,0)

def w_y():
  turtle.forward(ratio/2)
  turtle.pendown()
  l_r(ratio)
  go=sqrt((ratio**2)+((ratio/4)**2))
  s_r(go,117)
  turtle.backward(go)
  s_r(go,63)
  turtle.penup()
  turtle.backward(go)
  s_r(-(ratio),90)
  r_r((ratio/2)+btw)

def w_z():
  l_r(ratio*2)
  turtle.pendown()
  r_r(ratio)
  s_r(sqrt((ratio**2)+((ratio*2)**2)),243)
  s_r(ratio,0)
  turtle.penup()
  turtle.forward(btw)

def w_space():
  turtle.forward(ratio+btw)

def main(text):
  if (type(text) == str):
    print("WRITE.PY - logs :")
    turtle.penup()
    turtle.goto(-(ratio+btw)*(times),((lines/2)*((ratio*2)+btw)-((ratio*2)+btw)))
    print("main/setup : set turtle plugin\n===")
    
    donetext = [5]*len(text)
    donetimes = 0
    donelines = 0
    print("main/setup : done")
    print("main : writing")
    for i in range(len(text)):
      donetimes+=1
      donetext[i]=1
      if (text[i].lower() == 'a'):
        w_ah(1)
      elif (text[i].lower() == 'b'):
        w_dob(2,0)
      elif (text[i].lower() == 'c'):
        w_cg(0)
      elif (text[i].lower() == 'd'):
        w_dob(1,0)
      elif (text[i].lower() == 'e'):
        w_ef(0)
      elif (text[i].lower() == 'f'):
        w_ef(1)
      elif (text[i].lower() == 'g'):
        w_cg(1)
      elif (text[i].lower() == 'h'):
        w_ah(0)
        # ijt : i=0 t=1 j=2
      elif (text[i].lower() == 'i'):
        w_ijt(0)
      elif (text[i].lower() == 'j'):
        w_ijt(2)
      elif (text[i].lower() == 'k'):
        w_k()
      elif (text[i].lower() == 'l'):
        w_l()
      elif (text[i].lower() == 'm'):
        w_mn(0)
      elif (text[i].lower() == 'n'):
        w_mn(1)
      elif (text[i].lower() == 'o'):
        w_dob(0,0)
      elif (text[i].lower() == 'p'):
        w_pr(0)
      elif (text[i].lower() == 'q'):
        w_dob(0,1)
      elif (text[i].lower() == 'r'):
        w_pr(1)
      elif (text[i].lower() == 's'):
        w_s()
      elif (text[i].lower() == 'u'):
        w_uv(0)
      elif (text[i].lower() == 'v'):
        w_uv(1)
      elif (text[i].lower() == 'w'):
        w_w()
      elif (text[i].lower() == 't'):
        w_ijt(1)
      elif (text[i].lower() == 'x'):
        w_x()
      elif (text[i].lower() == 'y'):
        w_y()
      elif (text[i].lower() == 'z'):
        w_z()
      elif (text[i] == ' '):
        if donetimes == 1:
          print("main/log: o space at index 0")
          donetimes=0
          donetext[i]=5
        else:
          w_space()
      else:
        print("main/ERR: unknown char",i,":",text[i])
        donetext[i]=0
        donetimes-=1
      if (donetimes == (times*2)):
        donelines+=1
        if (donelines == lines):
          print("main/ERR: there is to much to print\n > no more free lines")
          break
        print("nreturn : finished line",donelines)
        turtle.setheading(0)
        turtle.backward((ratio+btw)*(times*2))
        turtle.right(90)
        turtle.forward(btw+(ratio*2))
        turtle.setheading(0)
        donetimes=0
    print("main : finished writing")
    print("main/recap :\n  (.=ok;/=skipped;?=unknown;!=other)\n", end='')
    for i in range(len(text)):
      if (donetext[i] == 1):
        print(".",text[i],sep='',end='')
      elif (donetext[i] == 0):
        print("?",text[i],sep='',end='')
      elif (donetext[i] == 5):
        print("/",text[i],sep='',end='')
      else:
        print("!",text[i],sep='',end='')
  else:
    print("Your argument seems to be\nnot a string...")

# ==> USER SETTINGS <==

# var, defaults, description
times=10 # 5,10,19,  letters per *1/2* line
lines=8  # 4,8,16,  nb of text lines
btw=5    # 10,5,3, nb px between letters ; = gap
ratio=10 # 20,10,5, letter width in px