{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8e64ed2e",
   "metadata": {},
   "source": [
    "# 19-04 · Клавиши и движение\n",
    "\n",
    "Практика к разделу [«Клавиши и движение головы»](../../site/chapters/glava-19/19-04-klavishi-dvizhenie.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea82d34e",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить смену направления с защитой от разворота на 180°."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff97f5e",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc7a8dec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:17.584657Z",
     "iopub.status.busy": "2026-08-14T20:59:17.584498Z",
     "iopub.status.idle": "2026-08-14T20:59:17.772056Z",
     "shell.execute_reply": "2026-08-14T20:59:17.771553Z"
    }
   },
   "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": "f38bdd54",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5ed66760",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:17.773253Z",
     "iopub.status.busy": "2026-08-14T20:59:17.773105Z",
     "iopub.status.idle": "2026-08-14T20:59:17.777556Z",
     "shell.execute_reply": "2026-08-14T20:59:17.776941Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "После шага вправо: (20.00,0.00)\n"
     ]
    }
   ],
   "source": [
    "golova = turtle.Turtle()\n",
    "golova.speed(0)\n",
    "golova.shape(\"square\")\n",
    "golova.penup()\n",
    "golova.goto(0, 0)\n",
    "\n",
    "napravlenie = \"stop\"\n",
    "\n",
    "def idti_vverh():\n",
    "    global napravlenie\n",
    "    if napravlenie != \"down\":\n",
    "        napravlenie = \"up\"\n",
    "\n",
    "def idti_vniz():\n",
    "    global napravlenie\n",
    "    if napravlenie != \"up\":\n",
    "        napravlenie = \"down\"\n",
    "\n",
    "def idti_vlevo():\n",
    "    global napravlenie\n",
    "    if napravlenie != \"right\":\n",
    "        napravlenie = \"left\"\n",
    "\n",
    "def idti_vpravo():\n",
    "    global napravlenie\n",
    "    if napravlenie != \"left\":\n",
    "        napravlenie = \"right\"\n",
    "\n",
    "def dvigat_golovu():\n",
    "    if napravlenie == \"up\":\n",
    "        golova.sety(golova.ycor() + RAZMER_SHAGA)\n",
    "    elif napravlenie == \"down\":\n",
    "        golova.sety(golova.ycor() - RAZMER_SHAGA)\n",
    "    elif napravlenie == \"left\":\n",
    "        golova.setx(golova.xcor() - RAZMER_SHAGA)\n",
    "    elif napravlenie == \"right\":\n",
    "        golova.setx(golova.xcor() + RAZMER_SHAGA)\n",
    "\n",
    "idti_vpravo()\n",
    "dvigat_golovu()\n",
    "print(\"После шага вправо:\", golova.position())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00ae280d",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — защита от разворота на 180°"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19669927",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:17.779248Z",
     "iopub.status.busy": "2026-08-14T20:59:17.779061Z",
     "iopub.status.idle": "2026-08-14T20:59:17.782125Z",
     "shell.execute_reply": "2026-08-14T20:59:17.781639Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Направление после idti_vpravo(): right\n",
      "Направление после idti_vlevo() (должно остаться right): right\n",
      "Направление после idti_vverh(): up\n"
     ]
    }
   ],
   "source": [
    "idti_vpravo()\n",
    "print(\"Направление после idti_vpravo():\", napravlenie)\n",
    "\n",
    "idti_vlevo()  # НЕ должно сработать — змейка едет вправо\n",
    "print(\"Направление после idti_vlevo() (должно остаться right):\", napravlenie)\n",
    "\n",
    "idti_vverh()  # а вот поворот на 90° должен сработать\n",
    "print(\"Направление после idti_vverh():\", napravlenie)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56091076",
   "metadata": {},
   "source": [
    "## Проверка результата"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c7fbf151",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:17.783112Z",
     "iopub.status.busy": "2026-08-14T20:59:17.782993Z",
     "iopub.status.idle": "2026-08-14T20:59:17.785169Z",
     "shell.execute_reply": "2026-08-14T20:59:17.784731Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Верно: разворот на 180° заблокирован, поворот на 90° разрешён.\n"
     ]
    }
   ],
   "source": [
    "assert napravlenie == \"up\"\n",
    "print(\"Верно: разворот на 180° заблокирован, поворот на 90° разрешён.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "357350aa",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0c57daf3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:59:17.786132Z",
     "iopub.status.busy": "2026-08-14T20:59:17.785973Z",
     "iopub.status.idle": "2026-08-14T20:59:17.789204Z",
     "shell.execute_reply": "2026-08-14T20:59:17.788719Z"
    }
   },
   "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
}
