aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-12-08 12:24:57 +0100
committerCharles <sircharlesaze@gmail.com>2019-12-08 12:24:57 +0100
commit90e41c14a5e7fba3c6ad25afe729ba6bdbd73f88 (patch)
tree1112039ad0594a57f1e06352e0d7335733808256
parent4b367c25b0365be8fe38381136353a659d5ce545 (diff)
downloadsnake-master.tar.gz
snake-master.tar.bz2
snake-master.zip
Game over if move in snake bodyHEADmaster
-rw-r--r--game.adb14
-rw-r--r--game.ads1
-rw-r--r--graphics.adb2
3 files changed, 15 insertions, 2 deletions
diff --git a/game.adb b/game.adb
index cd403e2..b30731c 100644
--- a/game.adb
+++ b/game.adb
@@ -21,7 +21,7 @@ package body Game is
function Next(game: in out T_Game) return Boolean is
new_head: constant T_Position := Next_Head(game);
begin
- if not In_Border(game, new_head) then
+ if not In_Border(game, new_head) or else In_Body(game, new_head) then
return false;
end if;
Enqueue(game.snake, new_head);
@@ -94,4 +94,16 @@ package body Game is
return pos.y >= 1 and pos.y <= game.height and pos.x >= 1 and pos.x <= game.width;
end In_Border;
+ function In_Body(game: T_Game; pos: T_Position) return Boolean is
+ cursor: T_List := game.snake.front;
+ begin
+ while cursor /= null loop
+ if pos = cursor.data then
+ return true;
+ end if;
+ cursor := cursor.next;
+ end loop;
+ return false;
+ end In_Body;
+
end Game;
diff --git a/game.ads b/game.ads
index 558f272..5da26ce 100644
--- a/game.ads
+++ b/game.ads
@@ -49,5 +49,6 @@ private
procedure Spawn_Food(game: in out T_Game);
function Next_Head(game: T_Game) return T_Position;
function In_Border(game: T_Game; pos: T_Position) return Boolean;
+ function In_Body(game: T_Game; pos: T_Position) return Boolean;
end Game;
diff --git a/graphics.adb b/graphics.adb
index dba73b9..1381afc 100644
--- a/graphics.adb
+++ b/graphics.adb
@@ -7,7 +7,6 @@ with SDL.Video.Rectangles;
with SDL.Events;
with SDL.Events.Events;
with SDL.Events.Keyboards;
-use SDL.Events.Keyboards;
with SDL.Video.Windows;
with SDL.Video.Windows.Makers;
with SDL.Video.Renderers;
@@ -58,6 +57,7 @@ package body Graphics is
end Run;
procedure Event_Handler(state: in out T_State) is
+ use SDL.Events.Keyboards;
event: SDL.Events.Events.Events;
begin
while SDL.Events.Events.Poll(event) loop