synthwave.py

Created by schraf

Created on November 12, 2022

1.73 KB

Suite au tweet de @nsi.xyz proposant à ces élèves le thème “Ecrire un script python qui produit une image”, avec différents sujets possibles et imposant l’usage de la tortue, voici ci-dessous ma proposition. Attention, il faut plusieurs minutes avant de voir le résultat final…

Je suis resté dans l’esprit Synthwave


import turtle

from math import *
from random import *
from kandinsky import fill_rect

t = turtle
t.speed(0) 
t.hideturtle()

coul1 = (0, 0, 0)
coul2 = (255, 60, 255)
blanc = (255,) * 3
w = 160

fill_rect(0,0,320,222,coul1)

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

def fond():
    dep = coul1
    arr = coul2
    rvb = [0,0,0]
    t.pensize(4)
    for l in range(39):
        for i in range(3):
            rvb[i] = int(T(l,0,39,dep[i],arr[i]))
        t.pencolor((rvb[0], rvb[1], rvb[2]))
        t.penup()
        t.goto(-w, w - 8 * l)
        t.pendown()
        t.goto(-w + 8 * l, w)
        t.penup()
        t.goto(w, -w + 8 * l)
        t.pendown()
        t.goto(w - 8 * l, -w)

def trait(x, y):
    t.pensize(1)
    t.penup()
    t.goto(0,0)
    t.pendown()
    t.goto(x,y)

def rayons():
    for c in range(10):
        t.pencolor(blanc if c else coul2)
        trait(-w + 32 * c, -w)
        trait(-w + 32 * c, w)
        trait(-w, w - 32 * c)
        trait(w, w - 32 * c)
        
def etoiles():
 for _ in range(200):
    t.pencolor(blanc)
    x, y = randint(-w, w), randint(-w, w)
    if random() < .5:
      t.pencolor(coul2)
      y = x
    e = randint(1,4)
    t.penup()
    t.pensize(e)
    t.goto(x, y)       
    t.pendown()
    t.goto(x, y) 

def triangle(x,y,coul, e = 1):
    t.penup()
    t.goto(x-160+55 * y * sin(radians(3*y)), -125+35 * y * cos(radians(5*y)))
    t.setheading(-65-5*y)
    t.pendown()
    t.pencolor(coul)
    t.pensize(int(y * e))
    for _ in range(3):
        t.fd(20 * y)
        t.rt(120)        
        
def formes():
    for c in range(13):
        triangle(0, c, blanc, 1.6)
        triangle(0, c, coul1)
        triangle(2, c, coul2, .3)       

fond()
etoiles()
rayons()
formes()

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.