![]() # keyboard bindingsįood again is a turtle that remains stationary until it’s been touched(eaten). We use the function win.onkeypress(function, “key”) for the same. Every keypress needs to be bound to a function that carries out an action. We add a function called win.listen() that listens to the key presses. We need the system to listen to our control key presses. Note: The snake cannot go right from left, left from right, top from down and down from the top. But how does the computer know what up, down, left and right is? We need to define a function for each of these directions and set the head.direction to up, down, right and left. We’ve made the functions for moving the turtle up, down, left and right. And then call the function time.sleep(delay) to reduce turtle speed. We initialize a variable called delay to 0.1. Go to the import section of your code and import the time module. To slow this down, we need to use the time module. That’s the default behavior for the move function. You can try executing the code so far and you’ll notice that the snake moves but very fast. Update the main game loop as follows: #Main Game Loop So we need to call the function everytime we update the screen or the window. The function does nothing until it’s called. ![]() X = head.xcor() #y coordinate of the turtle Y = head.ycor() #y coordinate of the turtle If the head goes up, the ‘y’ coordinate is increased, if the head goes down, the ‘y’ coordinate decreases, if the head moves right, the ‘x’ coordinate increases and if the head moves left, the ‘x’ coordinate decreases. Now that we have created a snake lets go ahead and make the snake move. ![]() This function basically updates my screen continuously with the loop. I am going to update my window using the function window.update(). Once the head is created, I need a main game loop which is always set to true. We use the functions turtle_name.goto() and turtle_name.direction() for it. I want my snakehead’s position to be the center of the window and the direction to be “stop”. We use the functions turtle_name.shape() and turtle_lor().ĭo we need to draw the paths taken by the snake? No! The function turtle_name.penup() makes sure that the path taken by the snake is not drawn. Next, we need to initialize the head shape and color. We use the function turtle_name.speed() for this. Create a turtle with the function turtle.Turtle() and assign it the name head. We set the head speed to 0 as we’re just initializing in this section and the head does not need to move. Snake is basically a turtle(in python language) that moves around. Once you’ve created the window, the next thing we need is a snakehead. We do not need any screen updates other than the scoreboard and hence set to 0. The function acer() turns off the screen updates. Set the window height and width with the function tup(width=X, height=Y). Set the background color for the window with the function window.bgcolor(“Color”). Give this window a name with the function window.title(“Kalgi’s Snake Game”). In this case, our window is win for the game. The function turtle.Screen() is used to create a window. To start using the module, you need to import it like any other module in python. On an obvious note, the player needs to win and hence avoid dying. The snake dies if it touches its own body or the boundaries of the window. The player has to move the snake such that it touches(eats) the food and grows in size. There are two elements in this game – snake and food. Let’s understand this game before we start building. I have used P圜harm with Python version 2.7 for this article. Let’s move ahead and start building the game.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |