My turtle

Find the ghost with onscreenclick

 

Task: Create game to find the ghost


Code:

from turtle import *
from random import *
from math import sqrt

bgcolor('dark blue')
speed(0)

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

def eri_gnome (size, x, y):
    #1 ghost
    penup()
    goto(x,y)
    pendown()
    #body
    pencolor("black")
    pensize(size)
    fillcolor("white")
    begin_fill()
    setheading(240)
    forward(size*200)
    setheading(0)
    forward(size*200)
    setheading(120)
    forward(size*200)
    end_fill()
    begin_fill()
    setheading(255)
    forward(size*210)
    setheading(0)
    forward(size*110)
    goto(x,y)
    end_fill()
    #head
    color("white")
    setheading(270)
    forward(size*50)
    begin_fill()
    dot(size*120)
    end_fill()
    setheading(90)
    forward(size*10)
    color("pink")
    begin_fill()
    dot(size*100)
    end_fill()
    penup()
    setheading(180)
    forward(size*20)
    pendown()
    color("black")
    dot(size*20)
    color("white")
    dot(size*5)
    penup()
    setheading(0)
    forward(size*40)
    color("black")
    dot(size*20)
    color("white")
    dot(size*5)
    penup()
    forward(size*20)
    pencolor("black")
    pensize(size*4)
    setheading(90)
    circle(size*35, -60)
    pendown()
    circle(size*40, -70)
    penup()
    setheading(20)
    forward(size*25)
    color("red")
    dot(size*20)
    penup()
    hideturtle()
    setheading(0)
    forward(50)
    color('white')

def clear():
    color('dark blue')
    begin_fill()
    setheading(0)
    forward(300)
    right(90)
    forward(30)
    right(90)
    forward(300)
    right(90)
    forward(30)
    end_fill() 

def ghost1(x,y,size,colir): #x,y center of circle, size radius of circle, colir
    move(x,y)
    dot(2*size,colir)
    
def f(x,y):
    global click
    click = click + 1
    color('black')
    if click <= 10:
        if sqrt((xghost-x)*(xghost-x) + (yghost-y)*(yghost-y)) < size:
            move(-10, -320)
            clear()
            ghost1(xghost,yghost,size,'red')
            eri_gnome (3/4, xghost, yghost+70)
            move(0,-350)
            color('yellow')
            write('Congratulations', font=("Verdana", 18, "bold"))
        else:
            move(x, y)
            color('light blue')
            dot(20)
            move(0,-350)
            color('white')
            write('Try again', font=("Verdana", 14, "bold"))
            hideturtle()
    else:
        move(-10, -320)
        clear()
        move(x+2*size,y)
        ghost1(xghost,yghost,size,'dark blue')
        eri_gnome (3/4, xghost, yghost+60)
        move(0,-350)
        color('orange')
        write('Game over', font=("Verdana", 14, "bold"))

xghost = randint(-400,400)
yghost = randint(-300,300)
size = randint(80,120)#ghost radius

click = 0
onscreenclick(f)
done()

2026-07-21 12:48:26

Comments

No comments yet. Be the first to leave one!

Leave a Comment