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. # Main game loop while True: win.update() 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 the main game loop which is always set to true. #Snake Head head = turtle.Turtle() head.speed(0) head.shape("square") lor("black") head.penup() head.goto(0, 100) head.direction = "stop" 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. We set the head speed to 0 as we’re just initializing in this section and the head does not need to move. Create a turtle with the function turtle.Turtle() and assign it the name head. 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. #set up the screen win = turtle.Screen() win.title("Kalgi's snake game") win.bgcolor("blue") win.setup(width=600,height=600) win.tracer(0) 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.
It basically lets you create a drawing board and command a turtle to draw for you. Isn’t that cool? Python’s turtle module lets you do that. Now, imagine, instead of manually drawing on the board, you could command the system to draw for you. I’m sure everybody has used a drawing board as a kid. Wouldn’t it be cool to build it by yourself? Hell Yeah! In this article, I am going to use Python’s Turtle Module to build it from scratch. As a matter of fact, it was one of the first mobile games that came into the market. As a kid, I am sure everybody has played the famous snake game.