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 increasing 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.
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 increasing 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.
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 increasing 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.
this is the given code and in the picture you can see the task.
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)
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.