aboutsummaryrefslogtreecommitdiff
path: root/game.adb
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-12-08 11:06:26 +0100
committerCharles <sircharlesaze@gmail.com>2019-12-08 11:06:26 +0100
commit4b367c25b0365be8fe38381136353a659d5ce545 (patch)
tree8e92a5948c67a58f1fb4ce27f2a73286ed57ed1c /game.adb
parent22e73779f55b051d34fc0490fbb55045f3f321d9 (diff)
downloadsnake-4b367c25b0365be8fe38381136353a659d5ce545.tar.gz
snake-4b367c25b0365be8fe38381136353a659d5ce545.tar.bz2
snake-4b367c25b0365be8fe38381136353a659d5ce545.zip
Added direction change with arrow key, food spawn
Diffstat (limited to 'game.adb')
-rw-r--r--game.adb41
1 files changed, 28 insertions, 13 deletions
diff --git a/game.adb b/game.adb
index 2c1934d..cd403e2 100644
--- a/game.adb
+++ b/game.adb
@@ -13,6 +13,9 @@ package body Game is
Enqueue(game.snake, (2, 2));
Enqueue(game.snake, (2, 3));
game.food := (1, 1);
+ Reset(game.width_generator);
+ Reset(game.height_generator);
+ Spawn_Food(game);
end Init;
function Next(game: in out T_Game) return Boolean is
@@ -24,22 +27,35 @@ package body Game is
Enqueue(game.snake, new_head);
if new_head /= game.food then
Dequeue(game.snake);
+ else
+ Spawn_Food(game);
end if;
-
return true;
end Next;
+ procedure Change_Direction(game: in out T_Game;
+ direction: T_Direction) is
+ begin
+ case direction is
+ when DIRECTION_UP =>
+ if game.direction /= DIRECTION_DOWN then
+ game.direction := direction;
+ end if;
+ when DIRECTION_DOWN =>
+ if game.direction /= DIRECTION_UP then
+ game.direction := direction;
+ end if;
+ when DIRECTION_LEFT =>
+ if game.direction /= DIRECTION_RIGHT then
+ game.direction := direction;
+ end if;
+ when DIRECTION_RIGHT =>
+ if game.direction /= DIRECTION_LEFT then
+ game.direction := direction;
+ end if;
+ end case;
+ end Change_Direction;
procedure Spawn_Food(game: in out T_Game) is
- subtype T_Width_Random_Range is Positive range 1..game.width;
- subtype T_Height_Random_Range is Positive range 1..game.height;
- package P_Width_Random is new Ada.Numerics.Discrete_Random(T_Width_Random_Range);
- package P_Height_Random is new Ada.Numerics.Discrete_Random(T_Height_Random_Range);
- use P_Width_Random;
- use P_Height_Random;
-
- width_random_generator: P_Width_Random.Generator;
- height_random_generator: P_Height_Random.Generator;
-
function Valid_Food(game: T_Game; food: T_Position) return Boolean is
cursor: T_List := game.snake.front;
begin
@@ -54,9 +70,8 @@ package body Game is
food: T_Position;
begin
- -- reset in main
loop
- food := (random(height_random_generator), random(width_random_generator));
+ food := (random(game.height_generator), random(game.width_generator));
exit when Valid_Food(game, food);
end loop;
game.food := food;