Below, one sees clearly the difference between the class and the base class. Shape
is called from the base class Turtle - super(shape = 'anything')- whereas color is from
RawTurtle.
from turtle import TurtleScreen, RawTurtle
from tkinter import*
import random
root = Tk()
c1 = Canvas(root, width= 300, height=400)
c1.pack()
screen = TurtleScreen(c1)
screen.bgcolor('sky blue')
def go():
t1.forward(130)
class turtle(RawTurtle):
def _init_(self, color):
super().__init__(shape='shape')
self.TurtleScreen = 'c1'
self.color = color
my_list1 = ['Hello!', 'Bonjour!', 'Guten Tag!', 'Hola!']
message1 = random.choice(my_list1)
my_list2 = ['Good-Bye!','Au revoir!', 'Auf Wiedersehen!', 'Chao!']
message2 = random.choice(my_list2)
t1 =turtle(screen, 'turtle')
t1.color('blue', 'red')
t1.pu()
t1.goto(-20, 30)
t1.pd()
t1.write(message2, font=('Ink_Free', 20, 'bold'))
t1.pu()
t1.backward(50)
screen.onkey(fun=go, key="Up")
screen.listen()
root.mainloop()
screen.exitonclick()
Next, I need to define write(message) as a class method ie on the class and not
on any particular instance of it...
* * *
rom turtle import TurtleScreen, RawTurtle
from tkinter import*
import random
root = Tk()
c1 = Canvas(root, width= 300, height=450)
c1.pack()
screen = TurtleScreen(c1)
screen.bgcolor('sky blue')
def go():
t1.forward(130)
class turtle(RawTurtle):
def _init_(self, color,message ):
super().__init__(shape='shape')
self.TurtleScreen = 'c1'
self.color = color
self.message = 'message'
self.f = write(message)
my_list1 = ['Hello!', 'Bonjour!', 'Guten Tag!', 'Hola!']
message1 = random.choice(my_list1)
my_list2 = ['Good-Bye!','Au revoir!', 'Auf Wiedersehen!', 'Chao!']
message2 = random.choice(my_list2)
@classmethod
def write(turtle, message):
turtle.write(message)
turtle.style = ('Ink_Free', 20, 'bold')
t1 =turtle(screen,'turtle')
t1.color('blue', 'red')
t1.pu()
t1.goto(-20, 30)
t1.pd()
t1.write(message1,font='style')
t1.pu()
t1.goto(40,-10)
t1.pd()
t1.write(message2,font ='style')
t1.pu()
t1.backward(150)
screen.onkey(fun=go, key="Up")
screen.listen()
root.mainloop()
screen.exitonclick()
No comments:
Post a Comment