i need to make a snake game with this given code and description attached: import snakelib width = 0  # initialized in play_snake height = 0  # initialized in play_snake ui = None  # initialized in play_snake SPEED = 1 keep_running = True def play_snake(init_ui):     global width, height, ui, keep_running     ui = init_ui     width, height = ui.board_size()     ui.show()     while keep_running:         event = ui.get_event()         # print(event.name + " : " + event.data)         if event.name == "alarm":             ui.show()         # make sure you handle the quit event like below,         # or the test might get stuck in an infinite loop         if event.name == "quit":             keep_running = False if __name__ == "__main__":     # do this if running this module directly     # (not when importing it for the tests)     ui = snakelib.SnakeUserInterface(10, 10)     ui.set_animation_speed(SPEED)     play_snake(ui

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

i need to make a snake game with this given code and description attached:

import snakelib

width = 0  # initialized in play_snake
height = 0  # initialized in play_snake
ui = None  # initialized in play_snake
SPEED = 1
keep_running = True


def play_snake(init_ui):
    global width, height, ui, keep_running
    ui = init_ui
    width, height = ui.board_size()
    ui.show()
    while keep_running:
        event = ui.get_event()
        # print(event.name + " : " + event.data)
        if event.name == "alarm":
            ui.show()
        # make sure you handle the quit event like below,
        # or the test might get stuck in an infinite loop
        if event.name == "quit":
            keep_running = False


if __name__ == "__main__":
    # do this if running this module directly
    # (not when importing it for the tests)
    ui = snakelib.SnakeUserInterface(10, 10)
    ui.set_animation_speed(SPEED)
    play_snake(ui

2. Snake
A logical step forward from interactive animated programs is games. The goal of this assignment is to program the classic computer game, Snake.
The goal of Snake is to create a snake as long as possible. This is achieved by guiding the snake to an apple on the game board. The snake cannot stop
moving, and dies whenever it hits something (excluding apples). Because the snake is growing longer and longer as the game progresses, it gets increasingly
difficult to avoid collisions with the snake itself. The player can change the direction of the head of the snake by using the arrow keys. At step in the game,
there is always an apple somewhere on the board. If the snake eats an apple, the snake becomes one cell longer. A new apple is placed on a random
location, excluding all places covered by the snake. When the snake reaches a side of the game board, it re-emerges at the opposite end.
It is probably easiest to understand the game by playing it yourself. You can find a lot of snake implementations on the web, for example here 2. There are
four differences between this implementation and the one you should make. Your implementation should:
make the snake should grow block at a time instead of 4.
make the snake re-emerge at the other end of the screen when reaching the end of the screen instead of dying.
make the snake start at a length of 2, at the top left (instead of a length of 1 at the center).
Take the following steps to implement the game:
1. First make a single cell that keeps moving, and of which you can change the direction via the arrow keys.
2. Change the program such that if the cell goes beyond the end of the screen, it reemerges at the other end. For example, if the cell leaves the screen on
the right side, it should come out on the left side.
3. Make a snake of length 2 that starts in the top left, and then only grows (as if it eats an apple each step). Keep a list of points in the snake and add a new
point to the end after each step.
4. Make a snake that moves and stays of length 2. It moves by adding an extra step to your program: after adding a new point to the end of the list,
remove a point at the start of the list.
5. Place the apple using ui.random as described below (at this point you should be able to pass some tests).
6. Allow the snake to eat apples. Do the following steps (afterward you should pass some more tests):
o If the head of the snake is on the apple:
1. Do not remove a point at the start of the list (this makes the snake grow by 1 cell)
2. Place a new apple as described above
7. Implement game over. If the point where the head of the snake is is also in the body (snake without head), then the game is over. Call
ui.set_game_over() to display the game over screen. Afterward, you should be able to pass all tests.
Transcribed Image Text:2. Snake A logical step forward from interactive animated programs is games. The goal of this assignment is to program the classic computer game, Snake. The goal of Snake is to create a snake as long as possible. This is achieved by guiding the snake to an apple on the game board. The snake cannot stop moving, and dies whenever it hits something (excluding apples). Because the snake is growing longer and longer as the game progresses, it gets increasingly difficult to avoid collisions with the snake itself. The player can change the direction of the head of the snake by using the arrow keys. At step in the game, there is always an apple somewhere on the board. If the snake eats an apple, the snake becomes one cell longer. A new apple is placed on a random location, excluding all places covered by the snake. When the snake reaches a side of the game board, it re-emerges at the opposite end. It is probably easiest to understand the game by playing it yourself. You can find a lot of snake implementations on the web, for example here 2. There are four differences between this implementation and the one you should make. Your implementation should: make the snake should grow block at a time instead of 4. make the snake re-emerge at the other end of the screen when reaching the end of the screen instead of dying. make the snake start at a length of 2, at the top left (instead of a length of 1 at the center). Take the following steps to implement the game: 1. First make a single cell that keeps moving, and of which you can change the direction via the arrow keys. 2. Change the program such that if the cell goes beyond the end of the screen, it reemerges at the other end. For example, if the cell leaves the screen on the right side, it should come out on the left side. 3. Make a snake of length 2 that starts in the top left, and then only grows (as if it eats an apple each step). Keep a list of points in the snake and add a new point to the end after each step. 4. Make a snake that moves and stays of length 2. It moves by adding an extra step to your program: after adding a new point to the end of the list, remove a point at the start of the list. 5. Place the apple using ui.random as described below (at this point you should be able to pass some tests). 6. Allow the snake to eat apples. Do the following steps (afterward you should pass some more tests): o If the head of the snake is on the apple: 1. Do not remove a point at the start of the list (this makes the snake grow by 1 cell) 2. Place a new apple as described above 7. Implement game over. If the point where the head of the snake is is also in the body (snake without head), then the game is over. Call ui.set_game_over() to display the game over screen. Afterward, you should be able to pass all tests.
Apple placement
To ensure reproducible behavior, we specify exactly how a random number generator must be used to place the food. For this, we provide the method
ui.random(max)
This gives a random integer in the range [0 - max ) (exclusive). During testing, the result of this method will not be random, but will be the number specified
as apple_spot in the test files.
You might be tempted to randomly place the apple using the following method, which we DO NOT USE:
1. Pick a random horizontal coordinate.
2. Pick a random vertical coordinate.
3. If the resulting coordinate is not free, try again.
This
hod has the downside that it might take a lot of tries before you find a
spot, especially
is alm
full. Moreover, it requires an
arbitrary amount of numbers for placing a single apple, which complicates testing.
Instead of the above method, we opt for a method that always succeeds at placing an apple, using only a single number. To see how this works, suppose we
have a 5x3 board and the current position of the snake is as follows:
.XXX.
To determine the position of the apple, we want to pick a random free spot on the board (the apple should not be placed on the snake). We number the
free spots on the board from left to right, top to bottom, skipping any spot occupied by the snake. In our example, this means the following numbering:
0 | 1 | 2 1 3 | 4
5 I XI XI X 6
7 | 8 | 9 1 101 11
To pick a random spot, first compute the number of free spots and then call ui.random(nrFreeSpots) (in our case nrFreeSpots=12). Then place the apple at
the position corresponding to the number you got.
Game over
The game ends when the head of the snake collides with the rest of the body. In that event, you should call ui.set_game_over() to display the game over
screen.
Transcribed Image Text:Apple placement To ensure reproducible behavior, we specify exactly how a random number generator must be used to place the food. For this, we provide the method ui.random(max) This gives a random integer in the range [0 - max ) (exclusive). During testing, the result of this method will not be random, but will be the number specified as apple_spot in the test files. You might be tempted to randomly place the apple using the following method, which we DO NOT USE: 1. Pick a random horizontal coordinate. 2. Pick a random vertical coordinate. 3. If the resulting coordinate is not free, try again. This hod has the downside that it might take a lot of tries before you find a spot, especially is alm full. Moreover, it requires an arbitrary amount of numbers for placing a single apple, which complicates testing. Instead of the above method, we opt for a method that always succeeds at placing an apple, using only a single number. To see how this works, suppose we have a 5x3 board and the current position of the snake is as follows: .XXX. To determine the position of the apple, we want to pick a random free spot on the board (the apple should not be placed on the snake). We number the free spots on the board from left to right, top to bottom, skipping any spot occupied by the snake. In our example, this means the following numbering: 0 | 1 | 2 1 3 | 4 5 I XI XI X 6 7 | 8 | 9 1 101 11 To pick a random spot, first compute the number of free spots and then call ui.random(nrFreeSpots) (in our case nrFreeSpots=12). Then place the apple at the position corresponding to the number you got. Game over The game ends when the head of the snake collides with the rest of the body. In that event, you should call ui.set_game_over() to display the game over screen.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY