From 245325815ee121ae3ecca309da29f98adcfc42af Mon Sep 17 00:00:00 2001 From: Nicolas Sudres Date: Sun, 31 Jul 2022 22:54:39 +0200 Subject: [PATCH] Add collision detection and error generation --- Snake/src/snake.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Snake/src/snake.py b/Snake/src/snake.py index d7991b8..e8e3166 100644 --- a/Snake/src/snake.py +++ b/Snake/src/snake.py @@ -33,12 +33,19 @@ def Snake_move(M, L, dir): Fruit = False List_save = L Save_Value = L.val - L.val = (DIRECTION[dir][0] + L.val[0], DIRECTION[dir][1] + L.val[1]) + Next_pos = (DIRECTION[dir][0] + L.val[0], DIRECTION[dir][1] + L.val[1]) - if M[L.val[0]][L.val[1]] == 2: + if M[Next_pos[0]][Next_pos[1]] == 3: Fruit = True - M[L.val[0]][L.val[1]] == 0 + M[Next_pos[0]][Next_pos[1]] == 0 + elif M[Next_pos[0]][Next_pos[1]] == 2: + raise ValueError(1) # Error : You take a wall ! + quit + elif M[Next_pos[0]][Next_pos[1]] == 1: + raise ValueError(2) # Error : You ate your tale ! + quit + L.val = Next_pos L = L.next while L is not None: