Search This Blog

Doodle it: Imagine, Draw, Erase and Repeat - Python Project for Beginners


 Doodle it: Imagine, Draw, Erase and Repeat

In order to create this game first step is to know a way of being able to listen to things the user does, like when a user taps a specific key on the keyboard. And the code that allows us to do this are called  Event Listeners.

Go to python documentation > listen method()


It has a whole bunch of section-on-screen events, including listening to key presses or listening to a click or other things that you can listen to. So the important thing is this listen method.


This allows turtle screen to start listening and waiting for the events that the user might trigger, like tapping on a key.

So for this first thing, we'll be going to do is we'll import the turtle class and the screen class from the turtle module. And then, we'll use them to create our Ted the turtle, and also create a screen object.


from turtle import Turtle,Screen

ted=Turtle()
screen=Screen()


This is basically going to control the window when we run our code.
Now in order to start listening for events, we have to get hold of the screen object and then tell it to start listening.


from turtle import Turtle,Screen

ted=Turtle()
screen=Screen()

screen.listen()


And once it starts listening, we have to bind a function that will be triggered when a particular key is pressed on the keyboard. Now, in order to bind a keystroke to an event in our code, we have to use an event listener.

So the one that we're going to be using is this onkey method. And you can see it expects a function with no arguments and also a key, like the 'a' key on your keyboard or a key symbol, so the space or up or down.



So I'm going to say screen.onkey and then I have to bind some sort of function to this method.

So let's create a function named ted_move_right and all that this function is going to do is it's going to get Ted to go forward by 10 paces. 


from turtle import Turtle,Screen

ted=Turtle()
screen=Screen()

def ted_move_right():
    ted.forward(10)

screen.listen()
screen.onkey() 




Now, coming back to our onkey method, we are going to say when the 'w', is pressed, then trigger the function move_forward.


def move_forward():
    ted.forward(10)

screen.listen()
screen.onkey(key='w',fun=move_forward)


Next, added the concerning triggered function move_backward to move backward when the 's' key is pressed.



from turtle import Turtle,Screen

ted=Turtle()
screen=Screen()

def move_forward():
    ted.forward(10)

def move_backward():
    ted.backward(10)
    
screen.listen()
screen.onkey(key='w',fun=move_forward)
screen.onkey(key='s',fun=move_backward)

screen.exitonclick()


Such that, functions to move counter-clockwise(left), clockwise(right) and to clear screen(reset screen) are written and binded them with the following keys 'a','d', & 'c' respectively.


from turtle import Turtle,Screen

ted=Turtle()
screen=Screen()

def move_forward():
    ted.forward(10)

def move_backward():
    ted.backward(10)
    

def move_left():
    new_headingted.heading()+10
    ted.setheading(new_heading)

def move_right():
    new_headingted.heading()-10
    ted.setheading(new_heading)


def clear_screen():
    ted.clear()
    ted.penup()
    ted.home()
    ted.pendown()

screen.listen()

screen.onkey(key='w',fun=move_forward)
screen.onkey(key='s',fun=move_backward)

screen.onkey(key='a',fun=move_left)
screen.onkey(key='d',fun=move_right)
screen.onkey(key='c',fun=clear_screen)

screen.exitonclick()


And now what I'm going to try is when I hit the 'w' key on the keyboard, you should see my turtle move forwards by 10 paces each time. And, when 's' key is pressed it moves backward. So we're now controlling our turtle using a keystroke. That's pretty much in Doodle it: just imagine,draw & repeat.


No comments:

Post a Comment