{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9a2cc9bc",
   "metadata": {},
   "source": [
    "# 19-06 · Еда и движение тела\n",
    "\n",
    "Практика к разделу [«Змейка ест! Движение тела»](../../site/chapters/glava-19/19-06-eda-telo.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae6a2787",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить проверку поедания яблока и движение сегментов тела."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8c68ba5",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "32d5a693",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:18.613153Z",
     "iopub.status.busy": "2026-08-14T20:59:18.613028Z",
     "iopub.status.idle": "2026-08-14T20:59:18.765550Z",
     "shell.execute_reply": "2026-08-14T20:59:18.765089Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Экран готов.\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import turtle\n",
    "\n",
    "RAZMER_SHAGA = 20\n",
    "GRANICA = 280\n",
    "\n",
    "screen = turtle.Screen()\n",
    "screen.title(\"Змейка\")\n",
    "screen.bgcolor(\"black\")\n",
    "screen.setup(width=600, height=600)\n",
    "screen.tracer(0)\n",
    "print(\"Экран готов.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e38cc603",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1de7a26d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:18.766619Z",
     "iopub.status.busy": "2026-08-14T20:59:18.766502Z",
     "iopub.status.idle": "2026-08-14T20:59:18.770871Z",
     "shell.execute_reply": "2026-08-14T20:59:18.770442Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Счёт: 10 | Сегментов: 1\n"
     ]
    }
   ],
   "source": [
    "golova = turtle.Turtle()\n",
    "golova.speed(0)\n",
    "golova.shape(\"square\")\n",
    "golova.penup()\n",
    "golova.goto(0, 0)\n",
    "\n",
    "yabloko = turtle.Turtle()\n",
    "yabloko.speed(0)\n",
    "yabloko.shape(\"circle\")\n",
    "yabloko.color(\"red\")\n",
    "yabloko.penup()\n",
    "yabloko.goto(20, 0)  # ставим яблоко рядом для теста\n",
    "\n",
    "schet = 0\n",
    "segmenty = []\n",
    "\n",
    "def novoe_yabloko():\n",
    "    x = random.randrange(-GRANICA, GRANICA, RAZMER_SHAGA)\n",
    "    y = random.randrange(-GRANICA, GRANICA, RAZMER_SHAGA)\n",
    "    yabloko.goto(x, y)\n",
    "\n",
    "def dobavit_segment():\n",
    "    novyj = turtle.Turtle()\n",
    "    novyj.speed(0)\n",
    "    novyj.shape(\"square\")\n",
    "    novyj.color(\"grey\")\n",
    "    novyj.penup()\n",
    "    segmenty.append(novyj)\n",
    "\n",
    "def proverit_edu():\n",
    "    global schet\n",
    "    if golova.distance(yabloko) < RAZMER_SHAGA:\n",
    "        novoe_yabloko()\n",
    "        dobavit_segment()\n",
    "        schet += 10\n",
    "\n",
    "golova.goto(20, 0)  # \"подъезжаем\" к яблоку\n",
    "proverit_edu()\n",
    "print(\"Счёт:\", schet, \"| Сегментов:\", len(segmenty))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b35939dc",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — движение тела вслед за головой\n",
    "\n",
    "Важен порядок: `dvigat_telo()` вызывается, пока голова ещё на *старой* позиции — только потом голова едет дальше."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "181b173d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:18.771859Z",
     "iopub.status.busy": "2026-08-14T20:59:18.771748Z",
     "iopub.status.idle": "2026-08-14T20:59:18.774875Z",
     "shell.execute_reply": "2026-08-14T20:59:18.774464Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Голова сейчас: (20.00,0.00)\n",
      "Сегмент после dvigat_telo(): (20.00,0.00)\n",
      "Голова уехала вперёд: (40.00,0.00)\n",
      "А сегмент остался на прежнем месте головы: (20.00,0.00)\n"
     ]
    }
   ],
   "source": [
    "def dvigat_telo():\n",
    "    for indeks in range(len(segmenty) - 1, 0, -1):\n",
    "        x = segmenty[indeks - 1].xcor()\n",
    "        y = segmenty[indeks - 1].ycor()\n",
    "        segmenty[indeks].goto(x, y)\n",
    "    if segmenty:\n",
    "        segmenty[0].goto(golova.xcor(), golova.ycor())\n",
    "\n",
    "print(\"Голова сейчас:\", golova.position())\n",
    "dvigat_telo()  # сегмент подтягивается к ТЕКУЩЕЙ (пока ещё старой) позиции головы\n",
    "print(\"Сегмент после dvigat_telo():\", segmenty[0].position())\n",
    "\n",
    "golova.setx(golova.xcor() + RAZMER_SHAGA)  # и только теперь голова едет дальше\n",
    "print(\"Голова уехала вперёд:\", golova.position())\n",
    "print(\"А сегмент остался на прежнем месте головы:\", segmenty[0].position())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0d93268",
   "metadata": {},
   "source": [
    "## Проверка результата"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "453964a6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:18.775819Z",
     "iopub.status.busy": "2026-08-14T20:59:18.775713Z",
     "iopub.status.idle": "2026-08-14T20:59:18.778075Z",
     "shell.execute_reply": "2026-08-14T20:59:18.777642Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Верно: сегмент остался там, где раньше была голова, а голова уехала дальше.\n"
     ]
    }
   ],
   "source": [
    "assert segmenty[0].position() == (20.0, 0.0)\n",
    "assert golova.position() == (40.0, 0.0)\n",
    "print(\"Верно: сегмент остался там, где раньше была голова, а голова уехала дальше.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9673345",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dc7bfe82",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:18.779019Z",
     "iopub.status.busy": "2026-08-14T20:59:18.778914Z",
     "iopub.status.idle": "2026-08-14T20:59:18.781851Z",
     "shell.execute_reply": "2026-08-14T20:59:18.781454Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно закрыто.\n"
     ]
    }
   ],
   "source": [
    "screen.bye()\n",
    "print(\"Окно закрыто.\")"
   ]
  }
 ],
 "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
}
