Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Hi there, 

I am making a snake game in python. My game over function thrwos an error though. I have attached a screenshot of the error an my code. the game should be over when the head of the snake collides with the rest of the snake. Can you help fixing it? also where exactly should i call the game_over function, is it it correct in my code? 

thanks you so much in advance

my code:

import snakelib
from snakelib import SnakeUserInterface
import importlib

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

direction = "r"
keep_running = True

#WIDTH = 10
#HEIGHT = 10
#SCALE = 0.75
#ui = SnakeUserInterface(WIDTH,HEIGHT,SCALE)
#ui.set_animation_speed(5)

class Coordinate:
def __init__(self,x,y):
self.x = x
self.y = y

def equals(self, coordinate):
if self.x == coordinate.x and self.y == coordinate.y:
return True

def shift(self,x_shift,y_shift):
self.x += x_shift
self.y += y_shift

def copy(self):
return Coordinate(self.x, self.y)

class CoordinateSnake:
def __init__(self):
self.coordinate_row = [] # (0, 2), (0, 1), (0, 0)

def add(self, coordinate):
self.coordinate_row.append(coordinate)

def addFront(self, coordinate):
self.coordinate_row.insert(0, coordinate)

def removeTail(self):
self.coordinate_row.pop(-1)

def contains(self, coordinate):
for el in self.coordinate_row:
if coordinate.equals(el):
return True
return False

head = Coordinate(1,0)
tail = Coordinate(0,0)
body = CoordinateSnake()
body.addFront(tail)
body.addFront(head)
apple = Coordinate(5,5)

def draw():
global head, tail, body, apple, CoordinateSnake
ui.clear()

for coord in body.coordinate_row:
ui.place(coord.x, coord.y, ui.SNAKE)

ui.place(apple.x,apple.y,ui.FOOD)
ui.show()


def generate_apple():
global apple, CoordinateSnake, Coordinate
nrFreeSpots = (width * height) - len(body.coordinate_row)
apple_spot = ui.random(nrFreeSpots)

i = 0
for y in range(height):
for x in range(width):
if not body.contains(Coordinate(x,y)):
#print(i, x, y)
if i == apple_spot:
apple = Coordinate(x,y)
return
i = i + 1


def process_event(event):
if event.name == 'alarm' and event.data == "refresh":
process_alarm()
draw()
elif event.name == "arrow":
process_arrow(event.data)

def process_arrow(data):
global direction
if data == "l" and direction != "r":
direction = "l"
elif data == "r" and direction != "l":
direction = "r"
elif data == 'u' and direction != "d":
direction = "u"
elif data == 'd' and direction != "u":
direction = "d"

def process_alarm():
global head,body,apple
move()
#game_over()
if head.x == width:
head.x = 0
if head.x < 0:
head.x = width - 1
if head.y == height:
head.y = 0
if head.y < 0:
head.y = height -1
game_over() #might be needed here instead. Depends on the tests

CoordinateSnake.addFront(body,head)
if apple.x == head.x and apple.y == head.y:
generate_apple()
else:
CoordinateSnake.removeTail(body)
game_over()

def move():
global direction, head
head = Coordinate.copy(head)
if direction == "r":
Coordinate.shift(head,1,0)
if direction == "l":
Coordinate.shift(head,-1,0)
if direction == "u":
Coordinate.shift(head,0,-1)
if direction == "d":
Coordinate.shift(head,0,1)
#draw()

def game_over():
global head, tail
#tmp_head = body.coordinate_row[0]
#tmp_body = body.coordinate_row[1:]
tmp_head = body.coordinate_row[-1]
tmp_body = body.coordinate_row[:-1]
for l in tmp_body:
if tmp_head.equals(l):
ui.set_game_over()
ui.show
return

def play_snake(init_ui):
global width, height, ui, keep_running
ui = init_ui
width, height = ui.board_size()
generate_apple()
draw()

while keep_running:
event = ui.get_event()
process_event(event)


# 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)

1. self = <test_snake.TestSnake testMethod=test_game_over>
2.
3.
def test_game_over (self):
4. >
self.run_test_from_file("tests/game_over.txt")
5.
6. test_snake.py:58:
7.
8. test_snake.py:22: in run_test_from_file
self.assertTrue(test_ok,msg)
9.
10. E
AssertionError: False is not true : The game was over, but you
did not did not indicate so.
11. E
12. E
Hint: The game ends when as the head of the snake collides with
expand button
Transcribed Image Text:1. self = <test_snake.TestSnake testMethod=test_game_over> 2. 3. def test_game_over (self): 4. > self.run_test_from_file("tests/game_over.txt") 5. 6. test_snake.py:58: 7. 8. test_snake.py:22: in run_test_from_file self.assertTrue(test_ok,msg) 9. 10. E AssertionError: False is not true : The game was over, but you did not did not indicate so. 11. E 12. E Hint: The game ends when as the head of the snake collides with
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education