forked from takluyver/mobilechelonian
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of files from https://github.com/macewanCMPT395/aspidites
- Loading branch information
0 parents
commit 8897e9d
Showing
5 changed files
with
12,832 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import math | ||
|
||
from IPython.core.displaypub import publish_display_data | ||
|
||
class Turtle: | ||
SIZE = 400 | ||
OFFSET = 20; | ||
TURTLE_BUFF = ""; | ||
REGISTERED = 0 | ||
def __init__(self): | ||
'''Create a Turtle. | ||
Turtle() | ||
Example: t = Turtle()''' | ||
self.pen = 1 | ||
self.speedVar = 1 | ||
self.color = "black" | ||
self.home() | ||
if(not Turtle.REGISTERED): | ||
get_ipython().events.register('post_run_cell', self.postExecuteTurtle) | ||
|
||
def postExecuteTurtle(self): | ||
publish_display_data(source="NewTurtle", | ||
data={'application/javascript' : 'IPython.do_turtle("%s", element);' % Turtle.TURTLE_BUFF}) | ||
Turtle.TURTLE_BUFF = "" | ||
Turtle.REGISTERED = 1 | ||
|
||
|
||
def pendown(self): | ||
'''Put down the pen. This is the default. | ||
pendown() | ||
Example: t.pendown()''' | ||
self.pen = 1 | ||
|
||
def penup(self): | ||
'''Lift up the pen. | ||
penup() | ||
Example: t.penup()''' | ||
self.pen = 0 | ||
|
||
def speed(self, speed): | ||
'''Change the speed of the turtle | ||
speed(speed) | ||
Example: t.speed(speed)''' | ||
self.speedVar=speed%11 | ||
|
||
def right(self, num): | ||
'''Move the Turtle num degrees to the right. | ||
right(num) | ||
Example: t.right(90)''' | ||
self.bearing -= num | ||
self.bearing = self.bearing%360 | ||
self.b_change = num | ||
self.printTurtle() | ||
|
||
def left(self, num): | ||
'''Move the Turtle num degrees to the left. | ||
left(num) | ||
Example: t.left(90)''' | ||
self.bearing += num | ||
self.bearing = self.bearing%360 | ||
self.b_change = (-1)*num | ||
self.printTurtle() | ||
|
||
def forward(self, num): | ||
'''Move the Turtle forward by num units. | ||
forward(num) | ||
Example: t.forward(100)''' | ||
'[1, "simple", "list"]' | ||
|
||
self.posX += round(num * math.cos(math.radians(self.bearing)), 1) | ||
self.posY -= round(num * math.sin(math.radians(self.bearing)), 1) | ||
|
||
|
||
if self.posX < Turtle.OFFSET: | ||
self.posX = Turtle.OFFSET | ||
if self.posY < Turtle.OFFSET: | ||
self.posY = Turtle.OFFSET | ||
|
||
if self.posX > Turtle.SIZE - Turtle.OFFSET: | ||
self.posX = Turtle.SIZE - Turtle.OFFSET | ||
if self.posY > Turtle.SIZE - Turtle.OFFSET: | ||
self.posY = Turtle.SIZE - Turtle.OFFSET | ||
|
||
self.b_change = 0 | ||
self.printTurtle() | ||
|
||
def backward(self, num): | ||
'''Move the Turtle backward by num units. | ||
backward(num) | ||
Example: t.backward(100)''' | ||
self.posX -= round(num * math.cos(math.radians(self.bearing)), 1) | ||
self.posY += round(num * math.sin(math.radians(self.bearing)), 1) | ||
|
||
if self.posX < Turtle.OFFSET: | ||
self.posX = Turtle.OFFSET | ||
if self.posY < Turtle.OFFSET: | ||
self.posY = Turtle.OFFSET | ||
|
||
if self.posX > Turtle.SIZE - Turtle.OFFSET: | ||
self.posX = Turtle.SIZE - Turtle.OFFSET | ||
if self.posY > Turtle.SIZE - Turtle.OFFSET: | ||
self.posY = Turtle.SIZE - Turtle.OFFSET | ||
|
||
self.b_change = 0 | ||
self.printTurtle() | ||
|
||
def pencolor(self, color): | ||
'''Change the color of the pen to color. Default is black. | ||
pencolor(color) | ||
Example: t.pencolor("red")''' | ||
self.color = color | ||
|
||
def printTurtle(self): | ||
Turtle.TURTLE_BUFF += str(self.pen) + "," + str(self.color) + "," + str(self.posX) + "," + str(self.posY) + "," + str(self.b_change) + "," + str(self.speedVar) + ","; | ||
|
||
def circle(self, radius, extent=360): | ||
temp = self.bearing | ||
self.b_change = 0; | ||
tempSpeed = self.speedVar | ||
self.speedVar = 1 | ||
|
||
for i in range(0, (extent/2)): | ||
n = math.fabs(math.radians(self.b_change) * radius) | ||
if(radius >= 0): | ||
self.forward(n); | ||
self.left(2); | ||
else: | ||
self.forward(n); | ||
self.right(2); | ||
if(radius >= 0): | ||
self.bearing = (temp + extent) | ||
else: | ||
self.bearing = (temp - extent) | ||
self.speedVar = tempSpeed | ||
|
||
def home(self): | ||
'''Move the Turtle to its home position. | ||
home() | ||
Example: t.home()''' | ||
self.posX = 200 | ||
self.posY = 200 | ||
self.bearing = 0 | ||
self.b_change = 0 | ||
self.printTurtle() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"metadata": { | ||
"name": "", | ||
"signature": "sha256:952e35f33d2801b38da4feb7477ecb0a767d5e8e31730838ee5e7156a9e8adc0" | ||
}, | ||
"nbformat": 3, | ||
"nbformat_minor": 0, | ||
"worksheets": [ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [ | ||
"from NewTurtle import Turtle\n", | ||
"t = Turtle();\n", | ||
"t.speed(10)\n", | ||
"colours=[\"red\",\"blue\",\"yellow\",\"brown\",\"black\",\"purple\",\"green\"]\n", | ||
"t.penup(); t.left(90); t.forward(200);t.right(90);t.pendown();\n", | ||
"for i in range (0,18):\n", | ||
" t.pencolor(colours[i%7]);\n", | ||
" t.right(20);\n", | ||
" t.forward(50);" | ||
], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"javascript": [ | ||
"IPython.do_turtle(\"1,black,200,200,0,1,0,black,200,200,-90,10,0,black,200.0,20,0,10,0,black,200.0,20,90,10,1,red,200.0,20,20,10,1,red,247.0,37.1,0,10,1,blue,247.0,37.1,20,10,1,blue,285.3,69.2,0,10,1,yellow,285.3,69.2,20,10,1,yellow,310.3,112.5,0,10,1,brown,310.3,112.5,20,10,1,brown,319.0,161.7,0,10,1,black,319.0,161.7,20,10,1,black,310.3,210.89999999999998,0,10,1,purple,310.3,210.89999999999998,20,10,1,purple,285.3,254.2,0,10,1,green,285.3,254.2,20,10,1,green,247.0,286.3,0,10,1,red,247.0,286.3,20,10,1,red,200.0,303.40000000000003,0,10,1,blue,200.0,303.40000000000003,20,10,1,blue,150.0,303.40000000000003,0,10,1,yellow,150.0,303.40000000000003,20,10,1,yellow,103.0,286.3,0,10,1,brown,103.0,286.3,20,10,1,brown,64.7,254.20000000000002,0,10,1,black,64.7,254.20000000000002,20,10,1,black,39.7,210.90000000000003,0,10,1,purple,39.7,210.90000000000003,20,10,1,purple,31.000000000000004,161.70000000000005,0,10,1,green,31.000000000000004,161.70000000000005,20,10,1,green,39.7,112.50000000000004,0,10,1,red,39.7,112.50000000000004,20,10,1,red,64.7,69.20000000000005,0,10,1,blue,64.7,69.20000000000005,20,10,1,blue,103.0,37.100000000000044,0,10,1,yellow,103.0,37.100000000000044,20,10,1,yellow,150.0,20.000000000000043,0,10,1,brown,150.0,20.000000000000043,20,10,1,brown,200.0,20.000000000000043,0,10,\", element);" | ||
], | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"prompt_number": 19 | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"collapsed": false, | ||
"input": [], | ||
"language": "python", | ||
"metadata": {}, | ||
"outputs": [] | ||
} | ||
], | ||
"metadata": {} | ||
} | ||
] | ||
} |
Oops, something went wrong.