{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7ccfa235",
   "metadata": {},
   "source": [
    "# 12-06 · Гонка Turtle\n",
    "\n",
    "Практика к разделу [«Проект 12-6»](../../site/chapters/glava-12/12-06-gonka-turtle-itogi.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da62b9d4",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Несколько черепашек на одном экране одновременно."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b37bac30",
   "metadata": {},
   "source": [
    "## О случайности в этом ноутбуке\n",
    "\n",
    "Гонка использует `random.randint()` для шага каждой черепашки — результат может отличаться\n",
    "при каждом запуске. Чтобы ноутбук выполнялся предсказуемо, здесь дополнительно закрепляем\n",
    "случайность через `random.seed()`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e8d29e8",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0174c05",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:30.974917Z",
     "iopub.status.busy": "2026-08-14T20:58:30.974803Z",
     "iopub.status.idle": "2026-08-14T20:58:31.156413Z",
     "shell.execute_reply": "2026-08-14T20:58:31.155869Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle готово.\n"
     ]
    }
   ],
   "source": [
    "import turtle\n",
    "\n",
    "screen = turtle.Screen()\n",
    "artist = turtle.Turtle()\n",
    "artist.speed(0)\n",
    "print(\"Окно Turtle готово.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d005aec6",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f0c9e451",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:31.157405Z",
     "iopub.status.busy": "2026-08-14T20:58:31.157295Z",
     "iopub.status.idle": "2026-08-14T20:58:34.534210Z",
     "shell.execute_reply": "2026-08-14T20:58:34.533683Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Победила черепашка цвета blue!\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "random.seed(3)\n",
    "screen.setup(500, 400)\n",
    "\n",
    "cveta = [\"red\", \"blue\", \"green\", \"orange\"]\n",
    "uchastniki = []\n",
    "\n",
    "for i, cvet in enumerate(cveta):\n",
    "    t = turtle.Turtle()\n",
    "    t.shape(\"turtle\")\n",
    "    t.color(cvet)\n",
    "    t.penup()\n",
    "    t.goto(-200, i * 40 - 60)\n",
    "    uchastniki.append(t)\n",
    "\n",
    "finish_line = 200\n",
    "pobeditel = None\n",
    "\n",
    "while pobeditel is None:\n",
    "    for t in uchastniki:\n",
    "        t.forward(random.randint(1, 10))\n",
    "        if t.xcor() >= finish_line:\n",
    "            pobeditel = t.pencolor()\n",
    "            break\n",
    "\n",
    "print(f\"Победила черепашка цвета {pobeditel}!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ab33a12",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "70f70f13",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:34.535382Z",
     "iopub.status.busy": "2026-08-14T20:58:34.535153Z",
     "iopub.status.idle": "2026-08-14T20:58:34.539067Z",
     "shell.execute_reply": "2026-08-14T20:58:34.538561Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle закрыто.\n"
     ]
    }
   ],
   "source": [
    "screen.bye()\n",
    "print(\"Окно Turtle закрыто.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
