import turtle
h = 0
import random

rand = random.Random()
rr = rand
if rr == 0.0:
    f1 = 'blue'
elif rr == 0.1:
    f1 = 'violet'
elif rr == 0.2:
    f1 = 'green'
elif rr == 0.3:
    f1 = 'orange'
elif rr == 0.4:
    f1 = 'yellow'
elif rr == 0.5:
    f1 = 'black'
elif rr == 0.6:
    f1 = 'blue'
elif rr == 0.7:
    f1 = 'red'
elif rr == 0.8:
    f1 = 'brown'
elif rr == 0.9:
    f1 = 'grey'
elif rr == 1.0:
    f1 = 'pink'
else:
    f1 = 'grey'
f = f1


def prepare_fig(fig, x, y):
    fig.hideturtle()
    fig.penup()
    fig.setposition(x, y)
    fig.speed(20)



def draw_square(fig, color, side_length):
    fig.pendown()
    fig.fillcolor(color)
    fig.begin_fill()
    for i in range(4):
        fig.fd(side_length)
        fig.rt(90)
    fig.end_fill()

def message(text, color):
    circ.hideturtle()
    circ.goto(0, 0)
    circ.color(color)
    sq.clear()
    sq2.clear()
    print(moves)
    circ.write(text, font=("Arial", 12, "bold"))

def win_or_die(moves):
    if -20 < circ.xcor() < 40 and 10 < circ.ycor() < 70:
        message(GAME_OVER_MSG + str(moves), 'red')
    if -60 < circ.xcor() < -20 and 50 < circ.ycor() < 90:
        message(WIN_MSG + str(moves), 'green')

def movey(deltay):
    global moves
    y = circ.ycor() + deltay
    circ.sety(y)
    moves += 1
    win_or_die(moves)

def movex(deltax):
    global moves
    x = circ.xcor() + deltax
    circ.setx(x)
    moves += 1
    win_or_die(moves)

wndow = turtle.Screen()
wndow.title("Игра с черепашкой")
wndow.setup(1600, 900)

circ = turtle.Turtle()
circ.penup()
circ.shape("turtle")
circ.color('green')

sq = turtle.Turtle()
prepare_fig(sq, 1, 100)
draw_square(sq, 'red', 60)
cr = turtle.Turtle()
prepare_fig(cr, -250, -100)
draw_square(cr, 'violet', 50)
c = turtle.Turtle()
prepare_fig(c, -200, -1)
draw_square(c, f, random.random() * 150)
c1 = turtle.Turtle()
prepare_fig(c1, -670, random.random() * 150)
draw_square(c1, 'brown', random.random() * 150)
sq2 = turtle.Turtle()
prepare_fig(sq2, 100, 1)
draw_square(sq2, 'yellow', 40)
s = turtle.Turtle()
prepare_fig(s, 400, 50)
draw_square(s, 'blue', 40)


moves = 0
#GAME_OVER_MSG = 'Game over!\nСделано шагов: '
#WIN_MSG = 'Победа!\nСделано шагов: '
STEP = 10

turtle.listen()
turtle.onkeypress(lambda: movey(STEP), 'Up')
turtle.onkeypress(lambda: movey(-STEP), 'Down')
turtle.onkeypress(lambda: movex(STEP), 'Right')
turtle.onkeypress(lambda: movex(-STEP), 'Left')
turtle.done()