-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubclasses with Turtle.py
34 lines (28 loc) · 1.37 KB
/
Subclasses with Turtle.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
from turtle import *
setworldcoordinates(-50, -50, 50, 50)
# Let's create a standard turtle, a virtual green tortoise, it will not show into screen until we call with another subclass
class tortoise(Turtle):
def __init__(self, color="green"):# Here we input the "changeable" attributes
Turtle.__init__(self, shape="turtle") # Here we initiate the Turtle Object
self.color(color)
self.shapesize(3)
self.penup()
self.ht()
self.goto(-50, 0)
# Now we created an flying tortoise, we can't see it without calling her,
# So, let's create a racing tortoise! It'll be our normal tortoise but with another attributes
class racingtortoise(tortoise):
def __init__(self):
tortoise.__init__(self)
self.st()
self.speed(4) #That's a speedy boi
while self.xcor() <= 45:
self.forward(2)
print(self.position())
# Now we need some finish line right?
fline = Turtle()
fline.speed(0); fline.ht(); fline.penup(); fline.goto(45, 45)
fline.pendown(); fline.pencolor("red"); fline.width(2); fline.setheading(270); fline.forward(90)
x = racingtortoise()
#This only variable is enough to make our first tortoise, then racing tortoise, run to the finish line
exitonclick()