i2E9o

Python Zero
-----------

IDE'd saab aadressil http://thonny.org/ (Aivar Annamaa, University of Tartu)

  a) import turtle
     # siis kõike, mida kilpkonn oskab, saab nii:
     turtle.xxx

  b) from turtle import (forward, left)
     # nüüd kilpkonn oskab ainult minna otse või vasakule,
     # kuid pöörduda tema poole pole enam vaja.
     for i in range(3):
         forward(222)
         left(120)

  c) from turtle import *
     # paha mõte. kui kilpkonn oskab midagi samanimelist teise
     # imporditud mooduliga, tekib arusaamatus (kes seda peaks tegema)

Kilpkonna graafika kõigepealt oli loodud tutvumiseks programmeerimiskeelega (>>> help(turtle), .. developed by Wally Feurzig and Seymour Papert in 1966).

 I. Lihtne:

    forward(n)
    backward(n)
    right(a)
    left(a)         # a kraadides
    up()
    down()          # pliiats
    width(w)
    color(c).

    from turtle import ( \
        forward, backward, right, left, up, down, width, color, \
        begin_fill, end_fill)

II. Keskmine:

    clear()         # aken
    reset()

    pencolor( pc)
    fillcolor( fc)
    filling()       # staatus
    begin_fill()
     ..
    end_fill()

    screen=turtle.Screen()    # kuhu joonistada
    screen.setup(800, 800)    # akna mõõdud
    screen.bgpic('mybg.png')  # peab olema kaustas 'Save as'.

    turtle.shape('turtle')    # kes joonistab?
                              # 'arrow', 'turtle', 'circle', 'square',
                              # 'triangle', 'classic'

... vaata lisaks: help( turtle) .

Pythoni kilpkonn on pime. Kuidas talle anda silmi, loe siit:

  {1}. https://stackoverflow.com/questions/23579631
         /how-to-read-pixel-colours-using-python-turtle-py
  {2}. https://stackoverflow.com/questions/28014347
         /get-pixel-colors-of-tkinter-canvas
  {3}. https://stackoverflow.com/questions/138250
         /how-can-i-read-the-rgb-value-of-a-given-pixel-in-python

--------------------------------

Lisa A: Leitud Internetis:

Fill demo 1

  from turtle import (forward, backward, \
                      right, left, up, down, \
                      width, color, \
                      shape, begin_fill, end_fill)

  shape('turtle')
  width(10)
  color('blue','green')
  begin_fill()
  forward(200)
  left(120)
  forward(200)
  left(120)
  forward(200)
  #left(120)
  end_fill()

Python random Bezier

  https://sites.google.com/site/algorithms2013/home
  /python-turtle-graphics/python-random-walk-with-bezier-curve

  #!python3
  # 2013-Jan-21 Mon 07:15
  import turtle, random, time

  def turtle_cubic_bezier(p0,p1,p2,p3, n=20):
      x0, y0 = p0[0], p0[1]
      x1, y1 = p1[0], p1[1]
      x2, y2 = p2[0], p2[1]
      x3, y3 = p3[0], p3[1]
      turtle.penup()
      turtle.goto(x0,y0)
      turtle.pendown()
      for i in range(n+1):
          t = i / n
          a = (1. - t)**3
          b = 3. * t * (1. - t)**2
          c = 3.0 * t**2 * (1.0 - t)
          d = t**3

          x = int(a * x0 + b * x1 + c * x2 + d * x3)
          y = int(a * y0 + b * y1 + c * y2 + d * y3)
          turtle.goto(x,y)
      turtle.goto(x3,y3)

  def r01(): return random.random()
  def r(): return int(r01()*200-100)
  def rr(): return(r(),r())
  def rmid(x1,x2): return int(x1+x2+r()/2)/2

  def next_points(points):
    p0, p3 = points[3], rr()
    p1 = rmid(p0[0],p3[0]), rmid(p0[1], p3[1])
    p2 = rmid(p0[0],p3[0]), rmid(p0[1], p3[1])
    return [p0,p1,p2,p3]

  def random_color():
    def t(i): return time.clock() * i % 1
    rbg =  [t(1), t(3), t(5)]
    print("rbg=(%3.2f,%3.2f,%3.2f)" % (rbg[0],rbg[1],rbg[2]))
    turtle.color(*rbg)

  def main():
      turtle.speed(0)
      turtle.colormode(1.)

      points = [rr(),rr(),rr(),rr()]
      i = 0
      while True:
        i=i+1
        print(i,*points)
        turtle_cubic_bezier(*points)
        points = next_points(points)
        random_color()
      turtle.Screen().exitonclick()

  if __name__ == "__main__":
      main()

Python. Heart

  from turtle import (right, left, forward, \
                      color, begin_fill, end_fill)

  def curvemove():
      for i in range(200):
          right(1)
          forward(1)

  color('red','pink')
  begin_fill()
  left(140)
  forward(111.65)
  curvemove()
  left(120)
  curvemove()
  forward(111.65)
  end_fill()

Modifications
.............

# Hearts ------------------------
import turtle
from turtle import (right, left, forward, \
                    up, down, goto, shape, \
                    color, begin_fill, end_fill)

def curvemove():
    for i in range(200):
      right(1)
      forward(1)

def heart_at(x, y, l, f):
    up(); goto( x, y); down(); color(l, f)
    begin_fill()
    left(140)
    forward(111.65)
    curvemove()
    left(120)
    curvemove()
    forward(111.65)
    left(140)
    end_fill()

shape('turtle')
turtle.tracer(0, 0)
heart_at(-50, 20, 'blue','aqua')
heart_at(50, -20, 'red', 'pink')
up(); goto(0,0); left(90); color('black')
turtle.update()

# Olympic -----------------------
import turtle

# https://stackoverflow.com/questions/9441331/how-to-create-a-
#   subclass-in-python-that-is-inherited-from-turtle-module

class myTurtle( turtle.Turtle):
    def __init__(self, *args, **kwargs):
        super(myTurtle, self).__init__(*args, **kwargs)
        self.width(10)

    def flyto( self, x, y):
        self.up()
        self.goto(x, y)
        self.down()

    def fooo( self):
        self.left(5)
        self.forward(5)

t = []
# chain: blue yellow black green red
c = ['blue','black','red','yellow','green']
dx = 130; dy = 70

for i in range(0,5):
    t.append( myTurtle())
    t[i].color(c[i])
    t[i].flyto( i*dx - (i//3)*(3*dx-dx//2), -(i//3)*dy )

for s in range (1, 1 + 360 // 5):
    for i in range(0,5):
        t[i].fooo()

# Flower

from turtle import \
    (begin_fill, end_fill, shape, forward, backward, \
     right, left, up, down, width, color)

c = ['black','violet','blue','green','yellow','orange','red']

up()
right(90)
forward(200)
right(180)
down()
color('brown')
width(10)
forward(200)

for j in range(0,7):
    color('pink',c[j])
    begin_fill()
    for i in range(0,72):
        forward(5)
        left(5)
    forward(5)
    end_fill()
    left(60)

Viited:

  https://stackoverflow.com/questions/44165603
  /filling-triangles-in-python-turtle