Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 1 ( 0 points ) Your first task is to run the starter code. If you are doing this on Windows, see the section
Task points
Your first task is to run the starter code. If you are doing this on Windows, see the section above. You need to install an extra library before starting it To run the starter code, execute this in a terminal or command shell.
python play.py
Notice that it doesnt do very much.
The room is drawn with the top wall on the top edge of the screen.
The other walls are missing.
There is no snake and no apple.
The game enters an infinite loop waiting for user input.
Press ControlC to interrupt the game.
You may press ControlC at any time to interrupt the game while you are testing and developing the game.
Task points
Your next task is to detect when the user presses a q When the user presses a q the game should quit gracefully.
Hint:
Start by reading the main program in play.py
The program is heavily commented.
The main loop has a section that reads a user keypress.
Add code to that section to break out of the main loop when the user presses q
Remember to test your changes after each step!
Task points
Your next task is to fully implement the Room. Modify the Room class so that the four walls of the room are drawn when Room.draw is called.
Hint:
You can see how the top wall of the room is drawn in the code for Room.draw
Follow that example and draw the left, bottom and right walls of the room.
Task points
Your next task is to implement a basic Snake. The Snakes head denoted by should start out at the center of the screen, and it should initially be three characters long, like so:
The Snake doesnt have to move in this step. It just has to show up on the screen when the Snake.draw method is called.
Hint:
The Snake object needs to know where the center of the screen is Modify the constructor for Snake so that the center of the screen is passed to it when the Snake object is constructed.
In the constructor:
The Snake is represented as a list of positions.
The heads position is at index Create a position for the head, and add it to the list.
The tails position is represented by the rest of the list. Create two other positions that represent the tail, and add it to the list
The Snake.draw method should loop over all positions.
Draw the head with a and tail section with using the Gui object.
Task points
The Snakes default direction to move is to the right, since it starts as Next, you should make the snake move right one step at a time. At each step, the snake moves one step to the right.
When you complete this step, the snake moves right one step at each iteration of the main loop, until it eventually runs off the edge of the screen on the right hand side. Thats ok for now.
Hint:
Add a move method to the Snake class that takes no arguments.
Call snake.move in the main loop.
The Snake is represented as a list, with the head at index of the list.
Starting from the end of the tail, each element of the snake steps into the place of the element before it That is each element at index i should get the value at index i
The head then moves right by adding to the x coordinate of the head.
Task points
Make the Snake change directions when the user presses the up down, left, or right arrow keys.
When the Snake goes right, the head is
When it goes down, the head is V
When it goes up the head is
When it goes left, the head is
Hint:
Add a member variable called direction to the Snake class. The variable tells the snake which direction the head should move.
Add a changedirection method to the Snake class that takes a string direction as an argument. The argument can be either RIGHTLEFTUP or DOWN
When the user presses an arrow key, call changedirection from the main loop. The changedirection should set the direction member variable to the new direction of movement.
The head moves in the direction of motion. Edit the Snake.move method so that:
If the direction is RIGHT add to the xcoordinate of the head
If the direction is LEFT subtract from the xcoordinate of the head
If the direction is UP subtract from the ycoordinate of the head
If the direction is DOWN add to the ycoordinate of the head
Edit the Snake.draw method to draw the head correctly, depending on the value of direction.
Task points
The Snake shouldnt be able to double back on itself. For example, when its going right, the only valid direction change is up or down. Left is not valid. Add to the code in changedirection to make sure that the snake cant double back on itself.
Task points
Your next task is to implement Apple. The Apple should be red and green in color, drawn with the asterisk character, and show up in a random position on the screen.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started