- #1
chaoseverlasting
- 1,050
- 3
Hi,
I'm trying to take keyboard inputs to make changes to a turtle from the 'Turtle' graphics in Python.
I'm not able to register keyboard inputs for the code I've written. I've tested out the examples that had been provided online, and they seem to work fine. I'm not quite sure where I'm going wrong.
Please have a look at the code and let me know your thoughts:
I'm trying to take keyboard inputs to make changes to a turtle from the 'Turtle' graphics in Python.
I'm not able to register keyboard inputs for the code I've written. I've tested out the examples that had been provided online, and they seem to work fine. I'm not quite sure where I'm going wrong.
Please have a look at the code and let me know your thoughts:
Code:
import turtle
t = turtle.Turtle()
turtle.setup(1200,720)
t.pensize(3)
t.color("red")
t.speed(3)
t.shape()
colour = ["red","green","blue"] #turtle can be of three colours
wn = turtle.Screen()
wn.title("Turtle Lives!")def change_colour(t,k): #change colour of turtle according to array
t.color(colour[k])
pen_size = 3
def change_size(t,k): #increase / decrease turtle pen_size
global pen_size #modifying global pen_size variable
if pen_size > 0:
if k==1:
pen_size +=1
elif k==0:
pen_size -=1
t.pensize(pen_size)wn.onkey(change_colour(t,0),"r") #Change colour
wn.onkey(change_colour(t,1),"g")
wn.onkey(change_colour(t,2),"b")
wn.onkey(change_size(t,1),"+") #Increase / Decrease size
wn.onkey(change_size(t,0),"-")
wn.listen()
wn.mainloop()