{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c4f72d4",
   "metadata": {},
   "source": [
    "# 19-07 · Проверка столкновений\n",
    "\n",
    "Практика к разделу [«Проверка столкновений»](../../site/chapters/glava-19/19-07-stolknoveniya.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e895b29b",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Определить столкновение со стеной и с собственным телом."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93e02c39",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c966b089",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:19.653418Z",
     "iopub.status.busy": "2026-08-14T20:59:19.653297Z",
     "iopub.status.idle": "2026-08-14T20:59:19.800537Z",
     "shell.execute_reply": "2026-08-14T20:59:19.799981Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Экран готов.\n"
     ]
    }
   ],
   "source": [
    "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": "d4e7c237",
   "metadata": {},
   "source": [
    "## Рабочий пример — стена"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "01bb2865",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:19.801550Z",
     "iopub.status.busy": "2026-08-14T20:59:19.801429Z",
     "iopub.status.idle": "2026-08-14T20:59:19.804892Z",
     "shell.execute_reply": "2026-08-14T20:59:19.804454Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Игра окончена (стена): True\n"
     ]
    }
   ],
   "source": [
    "golova = turtle.Turtle()\n",
    "golova.speed(0)\n",
    "golova.penup()\n",
    "\n",
    "segmenty = []\n",
    "igra_okonchena = False\n",
    "\n",
    "def proverit_stolknoveniya():\n",
    "    global igra_okonchena\n",
    "    if abs(golova.xcor()) > GRANICA or abs(golova.ycor()) > GRANICA:\n",
    "        igra_okonchena = True\n",
    "    for segment in segmenty:\n",
    "        if segment.distance(golova) < RAZMER_SHAGA / 2:\n",
    "            igra_okonchena = True\n",
    "\n",
    "golova.goto(GRANICA + 10, 0)  # выехали за границу\n",
    "proverit_stolknoveniya()\n",
    "print(\"Игра окончена (стена):\", igra_okonchena)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e97695b1",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — столкновение с собой"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "277a318b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:19.805838Z",
     "iopub.status.busy": "2026-08-14T20:59:19.805727Z",
     "iopub.status.idle": "2026-08-14T20:59:19.808639Z",
     "shell.execute_reply": "2026-08-14T20:59:19.808232Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Игра окончена (самостолкновение): True\n"
     ]
    }
   ],
   "source": [
    "igra_okonchena = False\n",
    "golova.goto(40, 40)\n",
    "\n",
    "segment = turtle.Turtle()\n",
    "segment.speed(0)\n",
    "segment.penup()\n",
    "segment.goto(40, 40)  # сегмент точно там же, где голова\n",
    "segmenty.append(segment)\n",
    "\n",
    "proverit_stolknoveniya()\n",
    "print(\"Игра окончена (самостолкновение):\", igra_okonchena)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f37f4c3d",
   "metadata": {},
   "source": [
    "## Проверка результата"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aa313832",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:19.809726Z",
     "iopub.status.busy": "2026-08-14T20:59:19.809618Z",
     "iopub.status.idle": "2026-08-14T20:59:19.811725Z",
     "shell.execute_reply": "2026-08-14T20:59:19.811294Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Верно: столкновение обнаружено.\n"
     ]
    }
   ],
   "source": [
    "assert igra_okonchena is True\n",
    "print(\"Верно: столкновение обнаружено.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5170538",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача — без ложных срабатываний"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "af2a99ab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:19.812724Z",
     "iopub.status.busy": "2026-08-14T20:59:19.812569Z",
     "iopub.status.idle": "2026-08-14T20:59:19.815084Z",
     "shell.execute_reply": "2026-08-14T20:59:19.814701Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Игра окончена (должно быть False): False\n",
      "Верно: далёкий сегмент не считается столкновением.\n"
     ]
    }
   ],
   "source": [
    "igra_okonchena = False\n",
    "golova.goto(0, 0)\n",
    "segmenty[0].goto(-200, -200)  # сегмент далеко\n",
    "\n",
    "proverit_stolknoveniya()\n",
    "print(\"Игра окончена (должно быть False):\", igra_okonchena)\n",
    "assert igra_okonchena is False\n",
    "print(\"Верно: далёкий сегмент не считается столкновением.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6163f2de",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7927c54c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:19.816009Z",
     "iopub.status.busy": "2026-08-14T20:59:19.815888Z",
     "iopub.status.idle": "2026-08-14T20:59:19.819052Z",
     "shell.execute_reply": "2026-08-14T20:59:19.818427Z"
    }
   },
   "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
}
