In: Computer Science
The coding for this program to run as described on Python:
Your (turtle) program must include:
Here our task is to make a turtle program which move according to the user input
when no key is pressed turtle move in constant speed
when up is pressed speed increase
when down is pressed speed will reduce
when left /right pressed move left/right
when space is pressed turtle clear the window and move to origin
special functions
when c is pressed turtle draw a circle
when z is pressed turtle make a zigzag path
The complete python code for above task is given below
Please read comments for better understanding of the program
Output
I am also attaching the text version of the code in case you need to copy paste
import turtle #importing turtle library
turtle.setup(1024,768) #setting screen window
window = turtle.Screen() #initializing window
window.title("Turtle Car") #seting name for window
window.bgcolor("lightgreen")
vanila = turtle.Turtle() # turtl created with icecream flavor
name
def h0(): #function for normal speed
vanila.forward(1)
def h1(): # function for fast movement
for i in range(0,50):
vanila.forward(3)
def h2(): #function for turn left
vanila.left(90)
def h3(): #function for turn right
vanila.right(90)
def h4(): #function for slow movement
for i in range(0,50):
vanila.forward(0.5)
def h5(): #function to goto (0,0) when space pressed
vanila.penup()
vanila.goto(0,0)
vanila.clear()
vanila.pendown()
def h6(): #function draw circle
vanila.circle(8)
def h7(): #function for zigzag movement
vanila.left(45)
vanila.forward(10)
vanila.right(90)
vanila.forward(20)
vanila.left(90)
vanila.forward(10)
vanila.right(45)
while(1):
h0() #normal speed will run always when no key is pressed
window.onkey(h1, "Up") #event triggered when Up pressed
window.onkey(h2, "Left") #event triggered when left pressed
window.onkey(h3, "Right") #event triggered when right pressed
window.onkey(h4, "Down") #event triggered when down pressed
window.onkey(h5, " ") #event triggered when space pressed
window.onkey(h6, "c") #event triggered when c pressed
window.onkey(h7, "z") #event triggered when z pressed
window.listen()
window.mainloop()