Answered step by step
Verified Expert Solution
Link Copied!

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 1(0 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.
python3 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 3 walls are missing.
There is no snake and no apple.
The game enters an infinite loop waiting for user input.
Press Control-C to interrupt the game.
You may press Control-C at any time to interrupt the game while you are testing and developing the game.
Task 2(2 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 3(2 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 4(2 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 0. 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 5(3 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 0 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 -1.
The head then moves right by adding 1 to the x coordinate of the head.
Task 6(5 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 change_direction method to the Snake class that takes a string direction as an argument. The argument can be either RIGHT,LEFT,UP or DOWN.
When the user presses an arrow key, call change_direction from the main loop. The change_direction 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 1 to the x-coordinate of the head
If the direction is LEFT, subtract 1 from the x-coordinate of the head
If the direction is UP, subtract 1 from the y-coordinate of the head
If the direction is DOWN, add 1 to the y-coordinate of the head
Edit the Snake.draw() method to draw the head correctly, depending on the value of direction.
Task 7(3 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 change_direction() to make sure that the snake cant double back on itself.
Task 8(3 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.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

=+a. Does it flow? (Can anyone read it out loud without stumbling?)

Answered: 1 week ago

Question

=+e. Does it use simple language, not technical jargon?

Answered: 1 week ago