From the course: Building the Classic Snake Game with Python

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Game reset

Game reset

- At the moment, when the snake collides with either itself or a wall, the game ends abruptly due to a court at turtle.bye, which closes the turtle graphics window and ends the program. In order to play again, we would have to run the program again, which is not ideal. To fix this, we can create a reset function, and put all of the code, defining the initial state of the game into it. So that when we call it, we basically start over. So let's write that reset function now. So we need some global variables, for score, snake, snake direction, and also food plus. And then within the reset, we set the score to zero. We create the snake, and this is exactly the same as what we were doing out here, so we can, cut that and put it in here. So we created the snake, We set the snake direction to up as well. And while I'm here, I'll get rid of, those ones I don't need. So snake direction is up. So for the food positive, again, we get the code that was outside of the reset function and we just…

Contents