spider.py

Created by schraf

Created on October 03, 2022

1.2 KB


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

# ecran blanc et toile grise
fill_rect(0,0,320,222,(255,)*3)
color((200,)*3)
pensize(2)
# on dessine la toile
for r in range(2, 100, 10):
    penup()
    goto(r, 0)
    pendown()
    for i in range(1, 9):
        # Trace legerement aleatoire
        for j in range(10):
            x = r * cos(random()/10 + pi * 2 * (i - 1 + sin(j * pi / 20)) / 8)
            y = r * sin(pi * 2 * (i - 1 + sin(j * j * pi / 200)) / 8)            
            goto(x, y)
        # les rayons
        setheading(i * 360 / 8)
        forward(10)
        backward(10)
goto(0,0)
penup()
speed(4)
# tortue
color((0,0,0))
while True:    
    (x,y) = position()
    # elle ne doit pas sortir de la toile
    if x * x + y * y > 90*90:
        setheading(heading()+180)
    else : setheading(randint(0,360))
    # elle bouge
    forward(5)
    sleep(.5)
    # un insecte arrive sur la toile
    if random()<.25:
        x = int(70 * (2 * random() - 1))
        y = int(70 * (2 * random() - 1))
        fill_rect(160 + x, 110 + y, 3, 3, (255,0,0))
        # la tortue va le manger
        goto(x, - y)

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.