{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 海龟写字\n",
    "恭喜你,学到这就把 Python 的基础语法基本学完了,算是学习告一小段落。现在,来做几个小项目来练练手。先以 Python 的经典名言开始。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle\n",
    "import time\n",
    "\n",
    "tony = turtle.Turtle()\n",
    "pos = [(-150, 30), (80, -10)]\n",
    "poem = ['人生苦短,我用 Python。','—杜甫']\n",
    "tony.speed(1)\n",
    "for i in range(len(pos)):\n",
    "    tony.penup()\n",
    "    tony.goto(pos[i])\n",
    "    tony.pendown()\n",
    "    tony.write(poem[i], font=(\"Kaiti Regular\", 22, \"normal\"))  # 设置字体为楷体 \n",
    "    time.sleep(0.5)\n",
    "tony.hideturtle()\n",
    "turtle.done()\n",
    "\n",
    "try:\n",
    "    turtle.bye()   \n",
    "except turtle.Terminator:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行以上程序,看会发生什么。\n",
    "\n",
    "## 美队之盾\n",
    "接下来画个美国队长的盾牌吧!\n",
    "\n",
    "\n",
    "<img src='images/ch10.1.png' width='211' align='left'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "盾牌由 4 个不同颜色和半径的圆组成,中间是一个五角星。这里的关键在于分好画图步骤,并计算五角星的角度和长度。将问题进行分解,再逐一解决是解决问题的常见思路。以画马为例:\n",
    "\n",
    "<img src='images/ch10.0.jpg' width='400' align='left'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "画盾牌可以分成两大步骤:\n",
    "1. 画出 4 个不同颜色和大小的圆,这里可以用 turtle.dot(size, color) 来实现。\n",
    "2. 画出五角星,五角星由 5 条线段组成,画完五角星之后要用白色填充形状。\n",
    "\n",
    "五角星每条边的长度与外接圆班级满足什么关系?自己计算一下吧!给出画图程序:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle\n",
    "import math\n",
    "\n",
    "tony = turtle.Turtle()\n",
    "radius = [200, 160, 120, 80]\n",
    "colors = ['crimson', 'white', 'crimson', 'royalblue']\n",
    "for i in range(4):\n",
    "    tony.dot(radius[i], colors[i])\n",
    "\n",
    "\n",
    "# 画五角星\n",
    "tony.color('white')  # 设置画笔颜色和填充颜色\n",
    "tony.begin_fill()\n",
    "r = 40\n",
    "tony.goto(0, r)\n",
    "tony.right(108)\n",
    "for i in range(5):\n",
    "    tony.forward(2*r*math.cos(math.radians(18)))  \n",
    "    tony.left(144)\n",
    "tony.end_fill()\n",
    "\n",
    "tony.hideturtle()\n",
    "turtle.done()\n",
    "\n",
    "try:\n",
    "    turtle.bye()   \n",
    "except turtle.Terminator:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 魔法阵\n",
    "现在来画另一个图案——魔法阵,赋予我力量吧:\n",
    "\n",
    "<img src='images/ch10.2.png' width='260' align='left'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请根据上图分解画图的步骤:\n",
    "\n",
    "1.\n",
    "\n",
    "2.\n",
    "\n",
    "3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle\n",
    "import math\n",
    "\n",
    "\n",
    "r1 = 100  # 中间圆的半径\n",
    "l1 = 2 * r1 * math.cos(math.radians(30))  # 三角形边长\n",
    "r2 = l1 * math.cos(math.radians(30)) - r1  # 最小的圆的半径\n",
    "r3 = r1 + 20  # 最外面圆的半径\n",
    "tony = turtle.Turtle()\n",
    "tony.pencolor('purple')\n",
    "tony.pensize(2)\n",
    "\n",
    "def draw_circle(pos, radius):\n",
    "    tony.penup()\n",
    "    tony.goto(pos)\n",
    "    tony.pendown()\n",
    "    tony.circle(radius)\n",
    "\n",
    "# 画三角形\n",
    "def draw_triangle(pos, ang, l):\n",
    "    tony.penup()\n",
    "    tony.goto(pos)\n",
    "    tony.pendown()\n",
    "    tony.right(ang)\n",
    "    for i in range(3):\n",
    "        tony.forward(l)\n",
    "        tony.left(120)\n",
    "    tony.setheading(0)\n",
    "\n",
    "\n",
    "draw_circle((0, -r1), r1)\n",
    "draw_circle((0, -r3), r3)\n",
    "draw_circle((0, -r2), r2)\n",
    "draw_triangle((0, r1), 120, l1)\n",
    "draw_triangle((0, -r1), 300, l1)\n",
    "\n",
    "tony.hideturtle()\n",
    "turtle.done()\n",
    "\n",
    "try:\n",
    "    turtle.bye()   \n",
    "except turtle.Terminator:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 编程挑战\n",
    "1. 为你写诗:\n",
    "\n",
    "   不言\n",
    "\n",
    "   不语\n",
    "\n",
    "   不忘记\n",
    "\n",
    "  用海龟写出三行情诗吧,或者替换成自己喜欢的诗词。\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "\n",
    "2. 请画出以下图形:\n",
    "\n",
    "<img src='images/ch10.3.png' align='left'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=330 height=86 src=\"//music.163.com/outchain/player?type=2&id=524794933&auto=0&height=66\"></iframe>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import HTML\n",
    "\n",
    "# Music\n",
    "HTML('<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=330 height=86 src=\"//music.163.com/outchain/player?type=2&id=524794933&auto=0&height=66\"></iframe>')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}