My turtle

Onscreenclick choose the object

 

Task:Create game to choose of object on the screen


Code:

from turtle import *
from random import *
import turtle

screen = turtle.Screen()
screen.setup(width=800, height=4000, startx=-400, starty=500)

bgcolor('black')
hideturtle()
speed(0)

def move(x,y):
    penup()
    goto(x,y)
    pendown()

def ghost1(x,y,size,colir,dir): 
    move(x,y)
    color(colir)
    pensize(4)
    pencolor('white')
    setheading(dir)
    begin_fill()
    for i in range(4):      
        forward(size)
        left(90)
    end_fill()

def clear():
    color('black')
    begin_fill()
    setheading(0)
    forward(600)
    right(90)
    forward(100)
    right(90)
    forward(600)
    right(90)
    forward(100)
    end_fill() 

def table():
    x1 = randint(-400,250)
    y1 = randint(-400,250)
    ghost1(x1,y1,150,'red',0)
    setheading(45)
    penup()
    forward(105)
    pendown()
    color('blue')
    dot(100)
    global xc1
    xc1 = x1
    global yc1
    yc1 = y1

    while True:
        x2 = randint(-400,250)
        y2 = randint(-400,250)
        if x1+160>x2>x1 or y1+160>y2>y1:
            x2 = randint(-400,250)
            y2 = randint(-400,250)      
        else:
            ghost1(x2,y2,150,'green',0)
            setheading(45)
            penup()
            forward(105)
            pendown()
            color('white')
            shapesize(4)
            shape('square')
            stamp()
            break
    global xc2
    xc2 = x2
    global yc2
    yc2 = y2

    while True:
        x3 = randint(-400,250)
        y3 = randint(-400,250)
        if x1+160>x3>x1 or y1+160>y3>y1 or x2+160>x3>x2 or y2+160>y3>y2:
            x3 = randint(-400,250)
            y3 = randint(-400,250)  
        else:
            ghost1(x3,y3,150,'blue',0)
            setheading(45)
            penup()
            forward(105)
            pendown()
            color('orange')
            shapesize(4)
            shape('triangle')
            stamp()
            break
    global xc3
    xc3 = x3
    global yc3
    yc3 = y3

    while True:
        x4 = randint(-400,250)
        y4 = randint(-400,250)
        if x1+160>x4>x1 or y1+160>y4>y1 or x2+160>x4>x2 or y2+160>y4>y2 or x3+160>x4>x3 or y3+160>y4>y3:
            x4 = randint(-400,250)
            y4 = randint(-400,250)       
        else:
            ghost1(x4,y4,150,'yellow',0)
            setheading(45)
            penup()
            forward(105)
            pendown()
            color('green')
            shapesize(4)
            shape('turtle')
            stamp()
            break
    global xc4
    xc4 = x4
    global yc4
    yc4 = y4
    

def f(x,y):
        
    move(-150, -400)
    clear()
    move(-150, -450)
    color('yellow')
    if (xc3+150>x>xc3 and yc3+150>y>yc3):
        write(' Congratulations', font=("Verdana", 24, "bold"))
    elif (xc1+150>x>xc1 and yc1+150>y>yc1) or (xc2+150>x>xc2 and yc2+150>y>yc2) or (xc3+150>x>xc3 and yc3+150>y>yc3) or (xc4+150>x>xc4 and yc4+150>y>yc4):
        write('Mmm... Try again', font=("Verdana", 24, "bold"))
    else:
        write("You haven't chosen", font=("Verdana", 24, "bold"))

move(-150, 450)
color('yellow')
write('Choose a triangle', font=("Verdana", 24, "bold"))

table()
x = xcor
y = ycor
onscreenclick(f)
done()

2026-07-21 12:36:34

Comments

No comments yet. Be the first to leave one!

Leave a Comment