So here we have linguistically gifted Turtle writing a greeting in a random
language. Pressing the up key moves him forward.
The program shows how class and super work together when one is passing
a class as an argument to another. super is the base class, ready to pass
on shape and color to the turtles class. Turtles will use these with self but
add a little something, here a message function, in a random language.
One could make some great cards using these tools.
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, shape, color, message):
super().__init__(shape, color)
self.master = 'screen'
self.shape = 'turtle'
self.color = color
self.message = message
my_list = ['Hello!', 'Bonjour!', 'Guten Tag!', 'Hola!']
message = random.choice(my_list)
t1 = turtle(screen)
t1.shape('turtle')
t1.color('blue', 'red')
t1.pu()
t1.goto(-20, 30)
t1.pd()
t1.write(message, font=('Ink_Free', 20, 'bold'))
t1.pu()
t1.backward(50)
screen.onkey(fun=go, key="Up")
screen.listen()
root.mainloop()
screen.exitonclick()
No comments:
Post a Comment