From 2f537114acc74a9e09443f69ab2508b292fdfa91 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Wed, 20 Apr 2022 02:56:09 +0200 Subject: Add screen limit collision --- src/main.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 8b14bfa..70b283d 100644 --- a/src/main.c +++ b/src/main.c @@ -99,10 +99,25 @@ main (int argc, controller_update(&ctrl, &event); } - if (controller_is_up_pressed(&ctrl)) rect.y -= 8; - if (controller_is_down_pressed(&ctrl)) rect.y += 8; - if (controller_is_left_pressed(&ctrl)) rect.x -= 8; - if (controller_is_right_pressed(&ctrl)) rect.x += 8; + if (controller_is_up_pressed(&ctrl) && rect.y > 0) { + rect.y -= 8; + rect.y = rect.y < 0 ? 0 : rect.y; + } + + if (controller_is_down_pressed(&ctrl) && rect.y + 8 < SCREEN_HEIGHT) { + rect.y += 8; + rect.y = rect.y + rect.h > SCREEN_HEIGHT ? SCREEN_HEIGHT - rect.h : rect.y; + } + + if (controller_is_left_pressed(&ctrl) && rect.x > 0) { + rect.x -= 8; + rect.x = rect.x < 0 ? 0 : rect.x; + } + + if (controller_is_right_pressed(&ctrl) && rect.x + 8 < SCREEN_WIDTH) { + rect.x += 8; + rect.x = rect.x + rect.h > SCREEN_WIDTH ? SCREEN_WIDTH - rect.h : rect.x; + } SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 1); SDL_RenderFillRect(renderer, &rect); -- cgit v1.2.3