draw_v3.py

Created by polettielio

Created on October 07, 2023

7.7 KB

draw_v3.1 - a script to precisely draw on the screen with the turtle module

Usage

Basic

Execute the main() function and it opens the interface. Every action is sent to the program thanks to key presses.

  • Movements
    • Move on the cardinal axes with the great arrow key.
    • Move relative to your position with alpha, ln for/backwards and exp, log left/right.
    • Change the scale of relative movements with keys 7, 4, 1 (length) and 8, 5, 2 (angle).
    • Enter absolute coordinates and angles with pi and square-root.
  • Pen management
    • Toggle pen with sin.
    • Change the line’s color with cos (presets) and tan (rgb).
    • Change the size with left parenthesis, plus and multiply.
    • Clear the screen and with backspace.
  • Miscellanous
    • Change ticking speed with right parenthesis, minus and divide.
    • Toggle turtle with square (x²).
    • Switch prompt with var (interface).

Note that turtle doesn’t use the ion coordinate system: the origin (0,0) is in the very center of the screen. When dealing with angles, turtle uses degrees and describes 0° as “looking to the right”.

Interface

The drawing area starts blank and fills up 90% of the screen (ion coordinates 0,0 to 320,202). There is a status bar, called the prompt, at the bottom which shows all the user values. There are 3 prompt modes ; switch the shown mode with var.

  • The position prompt logs the absolute coordinates (with turtle origin) and the heading/orientation.
  • The settings prompt logs the relative gaps (pixgap and deggap) alongside the tick speed.
  • The pen prompt logs the current color, the pen state (up/down) and its size/weight.

When you need to enter a value, a specific input-prompt gets showed. You can use the digits, minus and backspace to indicate the value and exe or ok to send. Escape it without making changes to any program-value with divide.


from turtle import *
from ion import *
from time import *
from kandinsky import draw_string as ds, fill_rect as fr
from math import fabs
from random import randint as rdi

def pp(s,rs="",rx=310):
  fr(0,202,320,20,(192,)*3)
  ds(s,0,202,(0,)*3,(192,)*3)
  ds(rs,rx,202,(0,)*3,(192,)*3)

def rr(s=""):
  do=True
  t=""
  m=""
  pp(s,rs="escape: ÷",rx=220)
  cdt=True
  while do:
    if keydown(KEY_ZERO):
      t+="0"
    elif keydown(KEY_ONE):
      t+="1"
    elif keydown(KEY_TWO):
      t+="2"
    elif keydown(KEY_THREE):
      t+="3"
    elif keydown(KEY_FOUR):
      t+="4"
    elif keydown(KEY_FIVE):
      t+="5"
    elif keydown(KEY_SIX):
      t+="6"
    elif keydown(KEY_SEVEN):
      t+="7"
    elif keydown(KEY_EIGHT):
      t+="8"
    elif keydown(KEY_NINE):
      t+="9"
    elif keydown(KEY_OK) or keydown(KEY_EXE):
      do=False
    elif keydown(KEY_MINUS):
      if m=="-":
        m=""
      else:
        m="-"
    elif keydown(KEY_BACKSPACE):
      t=t[:-1]
    elif keydown(KEY_DIVISION):
      sleep(0.17)
      return 9999
    else:
      cdt=False
    
    if cdt:
      sleep(0.17)
      pp(s+m+t,rs="escape: ÷",rx=220)
    else:
      sleep(0.02)
      cdt=True

  try:
    return int(m+t)
  except:
    return 9999

def main():
  go=True
  s=0.2 # tick-clock delay (sec)
  pen="z" # pen up/down
  l=0 # color, see also colors
  co=[10,22.5] # pix/deg gap
  pt=0 # prompt mode
  hidden=False # hide turtle
  size=1 # pen size (pix)
  
  cdt=2 # button pushed: 0 none, 1 quick-tick, 2 delay-tick
  
  colors=(("K","black"),
        ("W","white"),
        ("A","grey"),
        ("R","red"),
        ("O","orange"),
        ("Y","yellow"),
        ("B","blue"),
        ("P","purple"),
        ("I","pink"),
        ("N","brown"),
        ("+",)) # custom rgb
  color(colors[l][1])
  crgb=(0,0,0)

  ds(" draw_v3.1.py ",90,93)
  sleep(1)
  ds(" by lapingenieur ",75,111,(102,77,158))
  sleep(1)
  fr(70,90,180,40,(255,)*3)

  penup()
  speed(9)
  goto(0,0)
  setheading(0)

  while go:
    ## MOVEMENTS
    # cardinal moves
    if keydown(KEY_UP):
      cdt=1
      goto(position()[0],position()[1]+co[0])
    elif keydown(KEY_DOWN):
      cdt=1
      goto(position()[0],position()[1]-co[0])
    elif keydown(KEY_LEFT):
      cdt=1
      goto(position()[0]-co[0],position()[1])
    elif keydown(KEY_RIGHT):
      cdt=1
      goto(position()[0]+co[0],position()[1])
    # relative moves
    elif keydown(KEY_EXP):
      cdt=1
      left(co[1])
    elif keydown(KEY_LOG):
      cdt=1
      right(co[1])
    elif keydown(KEY_ALPHA):
      cdt=1
      forward(co[0])
    elif keydown(KEY_LN):
      cdt=1
      backward(co[0])
    # relative gaps
    elif keydown(KEY_SEVEN):
      cdt=1
      co[0]+=2
    elif keydown(KEY_ONE):
      cdt=1
      if co[0]>2:
        co[0]-=2
    elif keydown(KEY_FOUR):
      cdt=1
      sleep(0.2)
      pixs=rr("pix gap: ")
      if pixs<999:
        co[0]=pixs
    elif keydown(KEY_EIGHT):
      cdt=1
      co[1]*=2
    elif keydown(KEY_TWO):
      cdt=1
      if co[1]>22.5:
        co[1]/=2
    elif keydown(KEY_FIVE):
      cdt=1
      sleep(0.2)
      degs=rr("deg gap: ")
      if degs<361:
        co[1]=degs
    # absolute moves
    elif keydown(KEY_PI):
      cdt=1
      xy=rr("new x: "),rr("new y: ")
      if fabs(xy[0])<200 and fabs(xy[1])<200:
        goto(xy[0],xy[1])
    elif keydown(KEY_SQRT):
      cdt=1
      degs=rr("set deg: ")
      if degs<361:
        setheading(degs)
    
    ## DRAWING
    elif keydown(KEY_SINE):
      cdt=2
      if isdown():
        penup()
        pen="U"
      else:
        pendown()
        pen="D"
    elif keydown(KEY_COSINE):
      cdt=2
      if l<0:
        l=0
      else:
        l+=1
        l%=len(colors)-1 # skip custom rgb
      color(colors[l][1])
    elif keydown(KEY_TANGENT):
      cdt=1
      tcr=rr("red/255: ")
      sleep(0.2)
      tcg=rr("green/255: ")
      sleep(0.2)
      tcb=rr("blue/255: ")
      sleep(0.2)
      if (0<=tcr and tcr<=255) and (0<=tcg and tcg<=255) and (0<=tcb and tcb<=255):
        color(tcr, tcg, tcb)
        crgb=[tcr, tcg, tcb]
        l=-1
    elif keydown(KEY_LEFTPARENTHESIS):
      cdt=1
      size+=1
      pensize(size)
    elif keydown(KEY_PLUS):
      cdt=1
      if size>=0:
        size-=1
        pensize(size)
    elif keydown(KEY_MULTIPLICATION):
      cdt=1
      tsz=rr("size (pix): ")
      if tsz>0:
        size=tsz
        pensize(tsz)
    elif keydown(KEY_BACKSPACE):
      cdt=1
      num=rdi(100,999)
      if rr(str(num) + "=clear? ")==num:
        pen="U"
        penup()

        reset()
    
    # MISC
    elif keydown(KEY_RIGHTPARENTHESIS):
      cdt=2
      if s<1:
        s*=2
    elif keydown(KEY_MINUS):
      cdt=2
      if s>0.001:
        s/=2
    elif keydown(KEY_DIVISION):
      cdt=1
      tsp=rr("delay /s: ")
      if (0.001<=tsp and tsp<=1):
        s=tsp
    elif keydown(KEY_SQUARE):
      cdt=2
      if hidden:
        showturtle()
      else:
        hideturtle()
      hidden=not hidden
    elif keydown(KEY_VAR):
      cdt=2
      pt+=1
      pt%=3
      sleep(0.2)
    else:
      cdt=0

    # bottom bar => prompt
    if pt==0:
      # prompt 0: position
      tx="1>"
      tx+=" xy "+str(round(position()[0])) +":"+ str(round(position()[1]))+"pix"
      tx+="; "+str(round(heading()))+"deg"
    elif pt==1:
      # prompt 1: relative+tick
      tx="2>"
      tx+=" gaps "
      tx+=str(co[0])+"pix "
      tx+=str(co[1])+"deg"
      tx+="; "+str(s)+"s"
    elif pt==2:
      # prompt 2: pen mode
      tx="3> "
      if l==-1:
        tx+="rgb({},{},{})".format(crgb[0], crgb[1], crgb[2])
      else:
        tx+=colors[l][1]
      tx+=" "+ ("dn" if isdown() else "up")
      tx+=" "+str(size)+"pix"
    pp(tx)
    
    if cdt==1:
      sleep(s)
    if cdt==2:
      sleep(max(s, 0.2))
    else:
      sleep(0.02)
      cdt=True

print("draw_v3.1 - by lapingenieur")
print("main()")


##### help

# === Usage ===
#
#   a) Basic
#
# Execute the main() function and
# it opens the interface. Every
# action is sent to the program
# thanks to key presses.
#
# - Movements
#  * Move on the cardinal axes
#    with the great arrow key.
#  * Move relative to your position
#    with alpha, ln for/backwards
#    and exp, log left/right.
#  * Change the scale of relative
#    movements with keys 7, 4, 1
#    (length) and 8, 5, 2 (angle).
#  * Enter absolute coordinates and
#    angles with pi and square-root.
# - Pen management
#  * Toggle pen with sin.
#  * Change the line’s color with
#    cos (presets) and tan (rgb).
#  * Change the size with left
#    parenthesis, plus and multiply.
#  * Clear the screen and with
#    backspace.
#
# - Miscellanous
#  * Change ticking speed with right
#    parenthesis, minus and divide.
#  * Toggle turtle with square (x2).
#  * Switch prompt with var
#    (see interface).
#
# Note that turtle doesn’t use the
# ion coordinate system: the origin
# (0,0) is in the very center of the
# screen. When dealing with angles,
# turtle uses degrees and describes
# 0° as “looking to the right”.
#
#   b) Interface
#
# The drawing area starts blank and
# fills up 90% of the screen (ion
# coordinates 0,0 to 320,202). There
# is a status bar, called the prompt,
# at the bottom which shows all the
# user values. There are 3 prompt
# modes ; switch the shown mode
# with var.
#
# - The position prompt logs the
#   absolute coordinates (with turtle
#   origin) and the heading/orientation.
# - The settings prompt logs the
#   relative gaps (pixgap and deggap)
#   alongside the tick speed.
# - The pen prompt logs the current
#   color, the pen state (up/down)
#   and its size/weight.
#
# When you need to enter a value, a
# specific input-prompt gets showed.
# You can use the digits, minus and
# backspace to indicate the value and
# exe or ok to send. Escape it without
# making changes to any program-value
# with divide.