aboutsummaryrefslogtreecommitdiff
path: root/graphics.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 /graphics.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 'graphics.adb')
-rw-r--r--graphics.adb38
1 files changed, 31 insertions, 7 deletions
diff --git a/graphics.adb b/graphics.adb
index e9e4b71..dba73b9 100644
--- a/graphics.adb
+++ b/graphics.adb
@@ -6,17 +6,16 @@ with SDL.Timers;
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;
with SDL.Video.Renderers.Makers;
use SDL.Video;
-with Graphics, Game;
-use Graphics, Game;
-
with Interfaces.C;
-use Interfaces.C;
+use Interfaces.C;
package body Graphics is
@@ -40,10 +39,20 @@ package body Graphics is
end Quit;
procedure Run(state: in out T_State) is
+ use SDL.Timers;
+
+ last_time: SDL.Timers.Milliseconds := SDL.Timers.Ticks;
+ current_time: SDL.Timers.Milliseconds;
begin
+ Update(state);
while state.running loop
+ current_time := SDL.Timers.Ticks;
Event_Handler(state);
- Update(state);
+ if current_time >= last_time then
+ last_time := current_time + TIME_STEP;
+ state.running := Next(state.game);
+ Update(state);
+ end if;
SDL.Timers.Wait_Delay(3);
end loop;
end Run;
@@ -54,6 +63,19 @@ package body Graphics is
while SDL.Events.Events.Poll(event) loop
case event.common.event_type is
when SDL.Events.Quit => state.running := false;
+ when SDL.Events.Keyboards.Key_Down =>
+ case event.keyboard.key_sym.scan_code is
+ when Scan_Code_Escape => state.running := false;
+ when Scan_Code_Up =>
+ Change_Direction(state.game, DIRECTION_UP);
+ when Scan_Code_Down =>
+ Change_Direction(state.game, DIRECTION_DOWN);
+ when Scan_Code_Left =>
+ Change_Direction(state.game, DIRECTION_LEFT);
+ when Scan_Code_Right =>
+ Change_Direction(state.game, DIRECTION_RIGHT);
+ when others => null;
+ end case;
when others => null;
end case;
end loop;
@@ -66,11 +88,13 @@ package body Graphics is
Renderers.Set_Draw_Colour(state.renderer, COLOR_BLACK);
Renderers.Clear(state.renderer);
- Renderers.Set_Draw_Colour(state.renderer, COLOR_WHITE);
+ Renderers.Set_Draw_Colour(state.renderer, COLOR_GREEN);
while cursor /= null loop
Draw_Square(state, cursor.all.data);
cursor := cursor.all.next;
end loop;
+ Renderers.Set_Draw_Colour(state.renderer, COLOR_RED);
+ Draw_Square(state, state.game.food);
Renderers.Present(state.renderer);
end Update;
@@ -78,7 +102,7 @@ package body Graphics is
pos: T_Position) is
rect: Rectangles.Rectangle;
begin
- rect := ((C.int(pos.y) - 1) * 20, (C.int(pos.x) - 1) * 20, 20, 20);
+ rect := ((C.int(pos.x) - 1) * 20, (C.int(pos.y) - 1) * 20, 20, 20);
Renderers.Fill(state.renderer, rect);
end Draw_Square;