tortue.py

Created by gilles-vrn

Created on April 24, 2018

829 Bytes

Un essai de tortue, avec la fonction ‘seg’ de telpe51. Fonctions : av(n) : avancer de n pixels, re(n) : reculer, td(a) : tourner à droite de a degres, tg(a) : tourner à gauche, go(x,y) : aller au point de coordonnées (x;y)


from math import *
from kandinsky import *

def seg(xa,ya,xb,yb,col):
  clr=col
  if xa==xb :
    if ya<yb:
      y=ya
      bs=yb
    else :
      y=yb
      bs=ya
    while y<=bs:
      set_pixel(floor(xa),floor(y),clr)
      y=y+0.2
  else:
    m=(yb-ya)/(xb-xa)
    p=ya-m*xa
    if xa<xb:
      x=xa
      bs=xb
    else :
      x=xb
      bs=xa
    while x<=bs:
      y=m*x+p
      set_pixel(floor(x),floor(y),clr)
      x=x+0.2

def init():
  global x,y,ang,col
  x=160
  y=80
  ang=0
  col=color(0,0,255)

def av(dist):
  global x,y,ang,col
  xa=floor(x+dist*cos(radians(ang)))
  ya=floor(y+dist*sin(radians(ang)))
  seg(x,y,xa,ya,col)
  x=xa
  y=ya

def td(angle):
  global ang
  ang+=angle

def tg(angle):
  global ang
  ang-=angle

def re(dist):
  av(-dist)

def go(xd,yd):
  global x,y
  x=xd
  y=yd