/
MyTurtle.py
147 lines (134 loc) · 5.03 KB
/
MyTurtle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'''
Created on Oct 30, 2015
@author: Quinton Dean
MyTurtle.py contains the class MyTurtle with the following methods: circle, rectangle, polygon, chessboard
and writeText. The methods are taken directly from the functions in GraphicsAndPatternLibrary and assigned as
methods to class MyTurtle. The turtle method .setheading(0) was added to the end of each method to correct angle
problems from Assignment 2 so no longer use turtle.done() in main() as a work around.
The functions writeText was added and musicnote was removed.
'''
import turtle
class MyTurtle():
def circle(radius, cx = 0, cy = 0, color = 'black', fill = False, move = True):
turtle.showturtle()
if move == True:
turtle.penup()
turtle.goto(cx,cy)
turtle.pendown()
turtle.color(color)
if fill == True:
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
else:
turtle.circle(radius)
turtle.setheading(0)
turtle.hideturtle()
def rectangle(length, width, x = 0, y = 0, color = 'black', fill = False):
turtle.showturtle()
turtle.penup()
turtle.goto(x,y)
turtle.color(color)
turtle.pendown()
if fill == True:
turtle.begin_fill()
turtle.forward(length)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(length)
turtle.left(90)
turtle.forward(width)
turtle.end_fill()
else:
turtle.forward(length)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(length)
turtle.left(90)
turtle.forward(width)
turtle.setheading(0)
turtle.hideturtle()
def polygon(side, numberSides, angle = 0, xstart = 0, ystart = 0, color = 'black', fill = False):
totDegree = (numberSides - 2) * 180
new = totDegree / numberSides
degree = 180-new
turtle.showturtle()
turtle.penup()
turtle.goto(xstart, ystart)
turtle.color(color)
turtle.pendown()
turtle.right(angle)
turtle.forward(side)
if fill == True:
turtle.begin_fill()
for i in range(1,numberSides):
turtle.left(degree)
turtle.forward(side)
turtle.end_fill()
else:
for i in range(1,numberSides):
turtle.left(degree)
turtle.forward(side)
turtle.setheading(0)
turtle.hideturtle()
def chessboard(side, xstart = 0, ystart = 0, color = 'black', background = 'white'):
turtle.speed(50)
turtle.showturtle()
turtle.penup()
turtle.goto(xstart, ystart)
turtle.right(45)
squareSize = side/8
for i in range(1,9):
oddoreven = i % 2
if oddoreven == 1:
for k in range(0,4):
turtle.color(color)
turtle.begin_fill()
turtle.circle(squareSize, steps = 4)
turtle.end_fill()
turtle.left(45)
turtle.forward(squareSize*1.45)
turtle.color(background)
turtle.begin_fill()
turtle.right(45)
turtle.circle(squareSize, steps = 4)
turtle.end_fill()
turtle.left(45)
turtle.forward(squareSize*1.45)
turtle.right(45)
else:
for k in range(0,4):
turtle.color(background)
turtle.begin_fill()
turtle.circle(squareSize, steps = 4)
turtle.end_fill()
turtle.left(45)
turtle.forward(squareSize*1.45)
turtle.color(color)
turtle.begin_fill()
turtle.right(45)
turtle.circle(squareSize, steps = 4)
turtle.end_fill()
turtle.left(45)
turtle.forward(squareSize*1.45)
turtle.right(45)
turtle.penup()
turtle.goto(xstart, ystart+squareSize*1.45*i)
turtle.pendown()
turtle.penup()
turtle.goto(xstart,ystart)
turtle.color('black')
turtle.pensize(5)
turtle.pendown()
turtle.circle(side*1.01, steps = 4)
turtle.setheading(0)
def writeText(x, y, text, color ='black'):
turtle.showturtle()
turtle.color(color)
turtle.penup()
turtle.goto(x + .05*abs(x),y + .05*abs(y))
turtle.pendown()
turtle.write(text)
turtle.setheading(0)