christmastree.py

Created by schraf

Created on December 13, 2022

2.55 KB

4 dessins d’arbres de Noël (sur le simulateur il n’y a qu’un arbre qui s’affiche mais vous en aurez 4 différents en mettant le programme sur votre machine)

Découvrez ma chaine YouTube (Python, JavaScript, calculatrices…)


from turtle import *
from math import *
from time import sleep
from random import randint, random
from kandinsky import fill_rect

def T(u,a1,a2,b1,b2):
    return int((b2-b1)/(a2-a1)*(u-a1)+b1)

def f1(x): return -100
def f2(x): return 80 + 9 * x / 4

def trait(f,p):
 xd = -80
 while xd < 80:
  ps = randint(4,7)
  penup()
  x,y = xd, f(xd)
  goto(x, y)
  pendown()
  pensize(ps)
  while y < 80 - 9 * x / 4:
   fd(2)
   (x, y) = position()
  xd += ps + p

NB = 10
svg = ['M',166,38,'C',170,38,178,40,174,42,169,44,160,43,163,46,166,48,171,47,180,49,178,52,160,53,159,56,159,59,175,58,189,59,176,63,159,62,151,69,156,74,182,73,208,71,199,77,131,81,139,83,147,85,202,87,222,86,210,89,116,106,116,106,116,106,226,100,233,102,222,107,109,117,99,119,124,120,231,115,244,115,226,123,111,134,100,140,128,138,237,136,250,137,242,149,82,153,65,167,103,166,277,151,288,160,261,170,75,183,60,193,83,198,128,190,145,204,'M',156,38,'C',156,38,164,16,165,12,170,16,176,24,177,34,169,28,159,28,150,23,162,21,169,20,183,18,172,24,156,38,156,38,'Z']

def move(x,y): 
  goto(x-160,110-y)

def coord(a,b,c,d,t):
  return a*(1-t)**3+3*b*t*(1-t)**2+3*c*t*t*(1-t)+d*t**3

def bezier(l):
  pendown()
  t = 0
  for i in range(NB+1):
    x = coord(l[0],l[2],l[4],l[6],t)
    y = coord(l[1],l[3],l[5],l[7],t)
    move(x,y)
    t += 1/NB

def tree0():
    i = 0
    color(30,140,0)
    tt = len(svg)
    while i<tt:
        pensize(1 + int(12 * i / tt) if i < 120 else 2)
        if svg[i] == "M":
            l = [svg[i+1],svg[i+2]]
            penup()
            move(l[0], l[1])
            i += 3
        elif svg[i] == "Z" or svg[i] == "C": i += 1
        else:  
            for j in range(6): l.append(svg[i+j])
            bezier(l)
            l = [svg[i+4],svg[i+5]]
            i += 6
    hideturtle()

def tree1():
    setheading(30)
    trait(f1,12)
    trait(f2,1)
    penup()
    goto(0,92)

def tree2():
    xd = -75
    while xd < 75:
     h = 80-9*abs(xd)/4
     penup()
     x,y = xd,-100
     goto(x,y)
     pendown()
     a = T(abs(x),0,75,90,75)
     if x > 0: a = 180-a
     setheading(a)
     while y < h:
      pensize(T(y,-100,h,7,1))
      fd(1)
      (x,y) = position()
     xd += 5
     penup()
     goto(0,90)
    fill_rect(150,210,20,10,(30,140,0))

def tree3():
    k=.4
    for a in range(173):
        x=k*a*cos(21.6*a)
        pendown() if a else penup()
        goto(x,75-a)
        if a>158: k=.1
    penup()
    goto(0,85)


while True:
 for i in sorted(range(4), key=lambda x: random()):  
    reset()    
    pencolor(30,140,0)   
    eval("tree"+str(i)+"()")
    sleep(1)

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.