https://github.com/python-ninja-hebi/ipyturtle-next
Creating Turtle Graphics in IPython/Jupyter with ipycanvas
https://github.com/python-ninja-hebi/ipyturtle-next
ipycanvas jupyter python turtle
Last synced: 4 months ago
JSON representation
Creating Turtle Graphics in IPython/Jupyter with ipycanvas
- Host: GitHub
- URL: https://github.com/python-ninja-hebi/ipyturtle-next
- Owner: Python-Ninja-Hebi
- License: mit
- Created: 2022-01-02T15:39:16.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-02T19:02:21.000Z (about 4 years ago)
- Last Synced: 2025-09-01T03:07:17.384Z (6 months ago)
- Topics: ipycanvas, jupyter, python, turtle
- Language: Python
- Homepage:
- Size: 788 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.ipynb
- License: LICENSE
Awesome Lists containing this project
README
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ipython-turtle-next\n",
"## Creating Turtle Graphics in IPython/Jupyter with ipycanvas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is inspired by https://github.com/gkvoelkl/ipython-turtle-widget. Thanks. \n",
"\n",
"I am using **ipycanvas** (https://github.com/martinRenou/ipycanvas). Thanks to Martin Renou."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you like it, use it. If you have some suggestions, tell me (hebi@python-ninja.com)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### To install use pip:"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"```\n",
"$ pip install ipyturtlenext\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The most examples are inspired by **Harold Abelson, Andrea diSessa: Turtle Geometry, MIT Press 1986** \n",
"\n",
"**ipyturtlenext** should work in every jupyter environment, where ipycanvas works."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Start"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipyturtlenext import Turtle\n",
"\n",
"t = Turtle(width=200, height=200)\n",
"t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With *width* and *height* you can change the extension of the drawing canvas. \n",
"\n",
"In Jupyterlab you can create with *new view for outout* a separeted tab."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### First Steps"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.right(90)\n",
"t.heading()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.forward(150)\n",
"t.left(45)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.back(100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.left(45)\n",
"t.penup()\n",
"t.forward(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Square"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset() #clear canvas and start again"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.back(40)\n",
"t.forward(100)\n",
"t.position()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def square(size):\n",
" for i in range(4):\n",
" t.forward(size)\n",
" t.right(90)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"square(20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Triangel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def triangle(size):\n",
" for i in range(3):\n",
" t.forward(size)\n",
" t.right(120)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triangle(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### House"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def house(size):\n",
" square(size)\n",
" t.forward(size)\n",
" t.right(30)\n",
" triangle(size)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.back(100)\n",
"house(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Circle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()\n",
"\n",
"def circle():\n",
" for i in range(360):\n",
" t.forward(1)\n",
" t.right(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"circle()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Poly"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def poly(side, angle):\n",
" turn = 0\n",
" while turn == 0 or turn % 360 != 0:\n",
" t.forward(side)\n",
" t.right(angle)\n",
" turn += angle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"poly(44,135)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Return the current pen color as RGB tuple or web color name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()\n",
"t.pencolor()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set pen color as web color name "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.pencolor('Green')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set pen color with RGB value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.pencolor(255,0,0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.forward(40)\n",
"t.right(120)\n",
"t.pencolor('Blue')\n",
"t.forward(40)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.pencolor('Red')\n",
"t.pendown()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.forward(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Branch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def lbranch(length, angle, level):\n",
" t.pencolor('Green')\n",
" t.forward(2*length)\n",
" node(length, angle, level)\n",
" t.back(2*length)\n",
" \n",
"def rbranch(length, angle, level):\n",
" t.pencolor('Brown')\n",
" t.forward(length)\n",
" node(length, angle, level)\n",
" t.back(length)\n",
"\n",
"def node(length, angle, level):\n",
" if level==0:\n",
" return\n",
" t.left(angle)\n",
" lbranch(length, angle, level-1)\n",
" t.right(2*angle)\n",
" rbranch(length, angle, level-1)\n",
" t.left(angle)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()\n",
"node(8,24,7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nested Triangle"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def nested_triangle(size):\n",
" if size < 10:\n",
" return\n",
" for i in range(3):\n",
" nested_triangle(size/2)\n",
" t.forward(size)\n",
" t.right(120)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()\n",
"nested_triangle(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Snowflake"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def snowflake(size, level):\n",
" for i in range(3):\n",
" side(size, level)\n",
" t.right(120)\n",
"\n",
"def side(size, level):\n",
" if level == 0:\n",
" t.forward(size)\n",
" return\n",
" side(size/3, level - 1)\n",
" t.left(60)\n",
" side(size/3, level - 1)\n",
" t.right(120)\n",
" side(size/3, level - 1)\n",
" t.left(60)\n",
" side(size/3, level - 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()\n",
"snowflake(100,4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nested squares"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t.reset()\n",
"sideLength = 40\n",
"for square in range(5):\n",
" for side in range(4):\n",
" t.forward(sideLength)\n",
" t.left(90)\n",
" sideLength += 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Changelog"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"0.1.0 First published version "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
},
"widgets": {
"state": {
"9c8dc03f06b6421aac543dbcc7360258": {
"views": [
{
"cell_index": 12
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}