|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Project: TicTacToe with Turtle (Section 1)\n", |
| 8 | + "\n", |
| 9 | + "The main idea: Create a two player tic tac toe game using turtle graphics.\n", |
| 10 | + "\n", |
| 11 | + "Today's goals - Developing the UI functionality:\n", |
| 12 | + "* Be able to draw the board (using a `drawLine()` function)\n", |
| 13 | + "* Draw either 'X' or 'O' at a specific location on the board" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "# Setup / Constants\n", |
| 23 | + "import turtle\n", |
| 24 | + "\n", |
| 25 | + "CELL = 100\n", |
| 26 | + "RADIUS = 35\n", |
| 27 | + "X_LENGTH = 40\n", |
| 28 | + "\n", |
| 29 | + "LOC = [[(-CELL, +CELL), (0, +CELL), (+CELL, +CELL)],\n", |
| 30 | + " [(-CELL, 0), (0, 0), (+CELL, 0)],\n", |
| 31 | + " [(-CELL, -CELL), (0, -CELL), (+CELL, -CELL)]]" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "# Basic UI Function\n", |
| 41 | + "\n", |
| 42 | + "def goto(x, y):\n", |
| 43 | + " \"\"\" Move turtle to position (x, y) \"\"\"\n", |
| 44 | + " turtle.penup()\n", |
| 45 | + " turtle.goto(x, y)\n", |
| 46 | + " turtle.pendown()\n", |
| 47 | + "\n", |
| 48 | + "def line(x, y, length, angle):\n", |
| 49 | + " \"\"\" Draw a line of given length and angle \"\"\"\n", |
| 50 | + " goto(x, y)\n", |
| 51 | + " turtle.setheading(angle)\n", |
| 52 | + " turtle.forward(length)" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "# Player Move - X or O\n", |
| 62 | + "\n", |
| 63 | + "def X(x, y):\n", |
| 64 | + " \"\"\" Draw an X at position (x, y) \"\"\"\n", |
| 65 | + " for i in range(4):\n", |
| 66 | + " line(x, y, X_LENGTH, i * 90 + 45)\n", |
| 67 | + "\n", |
| 68 | + "def O(x, y):\n", |
| 69 | + " \"\"\" Draw an O at position (x, y) \"\"\"\n", |
| 70 | + " turtle.setheading(0)\n", |
| 71 | + " goto(x, y - RADIUS)\n", |
| 72 | + " turtle.circle(RADIUS)" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "# Draw Grid\n", |
| 82 | + "def draw_grid():\n", |
| 83 | + " \"\"\" Draw the grid \"\"\"\n", |
| 84 | + " line(-3 * CELL / 2, +CELL / 2, 3 * CELL, 0) # line(-150, +50, 300, 0)\n", |
| 85 | + " line(-3 * CELL / 2, -CELL / 2, 3 * CELL, 0) # line(-150, -50, 300, 0)\n", |
| 86 | + " line(+CELL / 2, 3 * CELL / 2, 3 * CELL, 270) # line(+50, 150, 300, 270)\n", |
| 87 | + " line(-CELL / 2, 3 * CELL / 2, 3 * CELL, 270) # line(-50, 150, 300, 270)" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "# Testing\n", |
| 97 | + "draw_grid()\n", |
| 98 | + "X(*LOC[0][0])\n", |
| 99 | + "O(*LOC[1][1])\n", |
| 100 | + "X(*LOC[0][1])\n", |
| 101 | + "O(*LOC[2][0])\n", |
| 102 | + "X(*LOC[0][2])\n", |
| 103 | + "turtle.hideturtle()\n", |
| 104 | + "turtle.exitonclick()" |
| 105 | + ] |
| 106 | + } |
| 107 | + ], |
| 108 | + "metadata": { |
| 109 | + "kernelspec": { |
| 110 | + "display_name": "Python 3.10.6 64-bit", |
| 111 | + "language": "python", |
| 112 | + "name": "python3" |
| 113 | + }, |
| 114 | + "language_info": { |
| 115 | + "codemirror_mode": { |
| 116 | + "name": "ipython", |
| 117 | + "version": 3 |
| 118 | + }, |
| 119 | + "file_extension": ".py", |
| 120 | + "mimetype": "text/x-python", |
| 121 | + "name": "python", |
| 122 | + "nbconvert_exporter": "python", |
| 123 | + "pygments_lexer": "ipython3", |
| 124 | + "version": "3.10.6" |
| 125 | + }, |
| 126 | + "orig_nbformat": 4, |
| 127 | + "vscode": { |
| 128 | + "interpreter": { |
| 129 | + "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" |
| 130 | + } |
| 131 | + } |
| 132 | + }, |
| 133 | + "nbformat": 4, |
| 134 | + "nbformat_minor": 2 |
| 135 | +} |
0 commit comments