{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "da0cfafa",
   "metadata": {},
   "source": [
    "# 19-29 · Render: model → Turtle\n",
    "\n",
    "Praktyka do rozdziału [„Render: model → Turtle”](/pl/chapters/rozdzial-19/19-29-render-model.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c4898c9",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "render() ponownie wykorzysta istniejące Turtle- segmenty zamiast tworzyć nowe."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee0fdee",
   "metadata": {},
   "source": [
    "## Ustawienie (wykonać raz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ef47d6a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:30.046313Z",
     "iopub.status.busy": "2026-09-03T00:54:30.046179Z",
     "iopub.status.idle": "2026-09-03T00:54:30.182531Z",
     "shell.execute_reply": "2026-09-03T00:54:30.181915Z"
    }
   },
   "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": "a401a476",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d9bfc960",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:30.183809Z",
     "iopub.status.busy": "2026-09-03T00:54:30.183635Z",
     "iopub.status.idle": "2026-09-03T00:54:30.189938Z",
     "shell.execute_reply": "2026-09-03T00:54:30.189073Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Сегментов в пуле: 2\n"
     ]
    }
   ],
   "source": [
    "segment_pool = []\n",
    "\n",
    "def ensure_pool(needed):\n",
    "    while len(segment_pool) < needed:\n",
    "        seg = turtle.Turtle()\n",
    "        seg.speed(0)\n",
    "        seg.shape(\"square\")\n",
    "        seg.color(\"grey\")\n",
    "        seg.penup()\n",
    "        segment_pool.append(seg)\n",
    "\n",
    "def render(body):\n",
    "    ensure_pool(len(body))\n",
    "    for i, seg in enumerate(segment_pool):\n",
    "        if i < len(body):\n",
    "            seg.showturtle()\n",
    "            seg.goto(*body[i])\n",
    "        else:\n",
    "            seg.hideturtle()\n",
    "    screen.update()\n",
    "\n",
    "render([(-20, 0), (-40, 0)])\n",
    "print(\"Сегментов в пуле:\", len(segment_pool))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ef41893",
   "metadata": {},
   "source": [
    "## Eksperyment 1 — wąż skrócił się, zbędny segment się ukrywa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d45b269b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:30.191179Z",
     "iopub.status.busy": "2026-09-03T00:54:30.190964Z",
     "iopub.status.idle": "2026-09-03T00:54:30.194260Z",
     "shell.execute_reply": "2026-09-03T00:54:30.193720Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Лишний сегмент спрятан, а не удалён — пул готов вырасти снова без пересоздания.\n"
     ]
    }
   ],
   "source": [
    "render([(-20, 0)])\n",
    "assert segment_pool[0].isvisible() is True\n",
    "assert segment_pool[1].isvisible() is False\n",
    "print(\"Лишний сегмент спрятан, а не удалён — пул готов вырасти снова без пересоздания.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e130a9d",
   "metadata": {},
   "source": [
    "## Ukończenie (wykonaj raz, na samym końcu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "623828ff",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:30.195363Z",
     "iopub.status.busy": "2026-09-03T00:54:30.195230Z",
     "iopub.status.idle": "2026-09-03T00:54:30.199007Z",
     "shell.execute_reply": "2026-09-03T00:54:30.198383Z"
    }
   },
   "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
}
