In this Python programming challenge, we are going to revisit the classic game called Snake. In this game, the player controls a snake using the arrow keys of the keyboard. The snake can go in all four directions (up, down, left, and right). The player aims is to direct the snake to eat an apple: a coloured dot randomly positioned on the screen. Each time the player reaches the apple, they score a point and the tail of their snake grows further. The games ends when the player either redirects the snake over the edges of the screen or when the snakes eats/crosses over its own tail.
Problem Decomposition, using a Structure Diagram
Let’s first use a structure diagram to recap the key components of this game:
Object Oriented Programming approach, using a Class Diagram
To implement this game we will use Object Oriented Programming. We will use the Python Turtle library to create the Graphical User Interface of this game. We will create two classes for the main two sprites of the game: the snake and the apple. Both of these classes will inherit the main properties and methods from the Turtle class. Here is the class diagram for our project:
User Interface
The player will control the snake using the arrow keys, to move the snake in all four directions: up and down, left and right.
The snake will evolve on a 20×20 grid represented on a screen of 400 pixels by 400 pixels.
Python Code
Here is the Python code for our game.
Your Task
Your task is to review and adapt the above code to create a two-player game. You will need to ensure that:
-
The first player (purple) should control the purple snake using the arrow keys as it is currently the case with the code provided above. The snake starts from the bottom left side of the screen, moving towards the right.
A second snake (blue) will be added to the game and start from the top right corner of the grid, moving towards the left.
The second player will use the WASD keys to control the blue snake.
The scoring system should allocate points to the first player to reach the apple.
The game ends as soon as one of the two snakes goes off the screen.
The game ends as soon as one of the two snakes bites their own tail.
The game ends as soon as the two snakes collide (head to head) or one snake bites the tail of the other snake.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area