This round, i was able to add the food and set up the collisions but after i edited some parts, the game just started crashing everytime.

It worked up until I got to test to take away the food and add it.
This commit is contained in:
mbhatti4
2025-12-14 00:51:52 -05:00
parent e273f1a954
commit cf49d4e0e4
5 changed files with 60 additions and 27 deletions

View File

@@ -10,33 +10,47 @@ class Player:
self.position = (start_x, start_y)
self.body = [self.position]
self.direction = (1, 0)
def handle_keystroke(self, keystroke, game):
x, y = self.body[0]
def handle_keystroke(self, keystroke, game):
x, y = self.body[0]
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"):
if keystroke.name == "KEY_LEFT":
self.direction = (-1, 0)
elif keystroke.name == "KEY_RIGHT":
self.direction = (1, 0)
elif keystroke.name == "KEY_UP":
self.direction = (0, -1)
elif keystroke.name == "KEY_DOWN":
self.direction = (0, 1)
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"):
dx, dy = self.direction
new_head = (x + dx, y + dy)
if keystroke.name == "KEY_LEFT":
self.direction = (-1, 0)
elif keystroke.name == "KEY_RIGHT":
self.direction = (1, 0)
elif keystroke.name == "KEY_UP":
self.direction = (0, -1)
elif keystroke.name == "KEY_DOWN":
self.direction = (0, 1)
if not game.on_board(new_head):
game.end()
return
dx, dy = self.direction
new_head = (x + dx, y + dy)
if new_head in self.body:
game.end()
return
if not game.on_board(new_head):
game.end()
return
food = None
for actor in game.actors:
if getattr(actor, "name", None) == "food":
food = actor
break
if new_head in self.body:
game.end()
return
if food is not None and new_head == food.position:
self.body.insert(0, new_head)
if hasattr(food, "respawn"):
food.respawn()
else:
self.body.insert(0, new_head)
self.body.pop()
self.body.insert(0, new_head)
self.body.pop()
self.position = self.body[0]
self.position = self.body[0]