import turtle
import random
# Создаем игровое окно
window = turtle.Screen()
window.title("Ловец объектов")
window.bgcolor("black")
window.setup(width=600, height=600)
window.tracer(0)
# Переменные для игры
score = 0
game_is_on = True
# Создаем игрока (синий квадрат)
player = turtle.Turtle()
player.speed(0)
player.shape("square")
player.color("blue")
player.shapesize(stretch_wid=1, stretch_len=3)
player.penup()
player.goto(0, -250)
# Создаем падающий предмет (красный круг)
item = turtle.Turtle()
item.speed(0)
item.shape("circle")
item.color("red")
item.penup()
item.goto(random.randint(-270, 270), 250)
# Экранный счетчик очков
score_display = turtle.Turtle()
score_display.speed(0)
score_display.color("white")
score_display.penup()
score_display.hideturtle()
score_display.goto(0, 250)
score_display.write("Очки: 0", align="center", font=("Arial", 20, "bold"))
# Надпись конца игры
over_display = turtle.Turtle()
over_display.speed(0)
over_display.color("red")
over_display.penup()
over_display.hideturtle()
# Движение влево с ограничением экрана
def move_left():
if game_is_on and player.xcor() > -260:
player.setx(player.xcor() - 25)
# Движение вправо с ограничением экрана
def move_right():
if game_is_on and player.xcor() < 260:
player.setx(player.xcor() + 25)
# Перезапуск игры
def restart_game():
global score, game_is_on
if not game_is_on:
score = 0
game_is_on = True
over_display.clear()
score_display.clear()
score_display.write("Очки: 0", align="center", font=("Arial", 20, "bold"))
player.goto(0, -250)
item.goto(random.randint(-270, 270), 250)
game_loop()
# Выход из игры
def exit_game():
window.bye()
# Нажатие на стрелки клавиатуры
window.listen()
window.onkeypress(move_left, "Left")
window.onkeypress(move_right, "Right")
window.onkeypress(restart_game, "r")
window.onkeypress(exit_game, "q")
# Главный цикл игры
def game_loop():
global score, game_is_on
if game_is_on:
# Предмет падает вниз
item.sety(item.ycor() - 5)
# Проверяем, поймал ли игрок предмет
if item.distance(player) < 35 and item.ycor() < -230:
score += 1
score_display.clear()
score_display.write(f"Очки: {score}", align="center", font=("Arial", 20, "bold"))
item.goto(random.randint(-270, 270), 250)
# Проверяем, упал ли предмет мимо игрока
if item.ycor() < -290:
game_is_on = False
over_display.goto(0, 0)
over_display.write("Game Over\nНажми 'R' для рестарта или 'Q' для выхода", align="center", font=("Arial", 16, "bold"))
window.update()
window.ontimer(game_loop, 20)
# Запуск игры
game_loop()
window.mainloop()