My turtle

Onscreenclick with random numbers

 

Task: Build onscreenclick objects with random number


Code:

from turtle import *
from random import *
setup(800,600)
bgcolor('light blue')
title("Onclick rundom")

tracer(0, 0)
color("brown")
left(90)
k=3          

def flower(fx, fy):
    move(fx,fy)
    n = randint(1, 9)
    #flower stem with leaves
    pencolor("green")
    pensize(10/k)
    setheading(60)
    circle(250/k,20)
    setheading(60)
    fillcolor("green")
    begin_fill()
    circle(190/k,90)
    lt(90)
    circle(190/k,90)
    end_fill()
    setheading(60)
    circle(250/k,20)
    setheading(340)
    fillcolor("green")
    begin_fill()
    circle(190/k,90)
    lt(90)
    circle(190/k,90)
    end_fill()
    setheading(60)
    circle(190/k,90)
    #flower head
    col=("orangered", "magenta", "red", "crimson", "blue")    
    colir=choice(col)
    for i in range(12):
        pendown()
        pencolor("white")
        pensize(4/k)
        fillcolor(colir)
        begin_fill()
        circle(190/k-i/2,90)
        lt(90)
        circle(190/k-i/3,90)
        end_fill()
        lt(60)
    dot(200/k,'yellow')
    pencolor('black')
    penup()
    setheading(250)
    forward(28)
    pendown()
    write(n, font=("Arial", 32, "bold"))

def move(x,y):
    penup()
    goto(x,y)
    pendown()
    
def ground(wid, high):
    fillcolor('light green')
    goto(-wid, -high/2)
    begin_fill()
    setheading(0)
    forward(wid*2)
    setheading(90)
    forward(high/2)
    setheading(180)
    forward(wid*2)
    setheading(270)
    forward(high/2)
    end_fill()

def sun (sx, sy):
    move(sx, sy)
    color("yellow")
    rd =50
    ang = 0
    for i in range(50):
            dis = randint(10, 100)
            ang = ang + 15
            pensize(2)
            setheading(ang)
            forward(dis)
            backward(dis)
    dot(rd)
    setheading(90)


penup()
goto(-400, -300)
ground(800, 600)
setheading(90)

onscreenclick(flower,1)
onscreenclick(sun,3)

done()

2026-07-10 09:27:15

Comments

No comments yet. Be the first to leave one!

Leave a Comment