{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8e64ed2e",
   "metadata": {},
   "source": [
    "# 19-04 · Klucze i ruch\n",
    "\n",
    "Praktyka do sekcji [„Klucze i ruch głowy”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea82d34e",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Opanować zmianę kierunku dzięki ochronie przeciwskrętowej o 180°."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff97f5e",
   "metadata": {},
   "source": [
    "## Ustawienie (wykonać raz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc7a8dec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:23.314001Z",
     "iopub.status.busy": "2026-09-03T00:53:23.313842Z",
     "iopub.status.idle": "2026-09-03T00:53:23.493365Z",
     "shell.execute_reply": "2026-09-03T00:53:23.492771Z"
    }
   },
   "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": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5ed66760",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:23.495018Z",
     "iopub.status.busy": "2026-09-03T00:53:23.494870Z",
     "iopub.status.idle": "2026-09-03T00:53:23.500115Z",
     "shell.execute_reply": "2026-09-03T00:53:23.499509Z"
    }
   },
   "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": [
    "## Eksperyment 1 - Ochrona skrętu o 180°"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19669927",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:23.501543Z",
     "iopub.status.busy": "2026-09-03T00:53:23.501354Z",
     "iopub.status.idle": "2026-09-03T00:53:23.505058Z",
     "shell.execute_reply": "2026-09-03T00:53:23.504472Z"
    }
   },
   "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": [
    "## Sprawdzenie wyniku"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c7fbf151",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:23.506650Z",
     "iopub.status.busy": "2026-09-03T00:53:23.506445Z",
     "iopub.status.idle": "2026-09-03T00:53:23.509369Z",
     "shell.execute_reply": "2026-09-03T00:53:23.508853Z"
    }
   },
   "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": [
    "## Ukończenie (wykonaj raz, na samym końcu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0c57daf3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:23.511045Z",
     "iopub.status.busy": "2026-09-03T00:53:23.510836Z",
     "iopub.status.idle": "2026-09-03T00:53:23.516509Z",
     "shell.execute_reply": "2026-09-03T00:53:23.514713Z"
    }
   },
   "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
}
