summaryrefslogtreecommitdiff
path: root/catch.c
diff options
context:
space:
mode:
authorVictor Gamper <victor@wenzeslaus.de>2020-01-26 19:45:20 +0100
committerVictor Gamper <victor@wenzeslaus.de>2020-01-26 19:45:20 +0100
commit4abb5e4664654591ba8beecf7774b69365c46d14 (patch)
treed43f81a540360cdc6b6fae8edbc1973038919012 /catch.c
parentdac6b72f8f5fc9ecf564412927dcff7a04d37dc0 (diff)
downloadc-catch-4abb5e4664654591ba8beecf7774b69365c46d14.tar.gz
c-catch-4abb5e4664654591ba8beecf7774b69365c46d14.zip
Added a collision detection
Diffstat (limited to 'catch.c')
-rw-r--r--catch.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/catch.c b/catch.c
index e2168ff..4c560a4 100644
--- a/catch.c
+++ b/catch.c
@@ -235,12 +235,22 @@ void gameRoutine() {
if(state == STATE_GAME) {
for(int i = 0; i<BIRD_COUNT; i++) {
- printf("%d: %d, %d, %d\n", i, bird[i].x, bird[i].y, bird[i].type);
+
+ // if the selected bird exists, move it
if(bird[i].type != BIRD_TYPE_NONE) {
- bird[i].x++;
+ bird[i].x+=0.5;
- if(bird[i].x > screen->w)
+ // if the bird left the screen, reset it
+ if(bird[i].x > screen->w) {
bird[i].type = BIRD_TYPE_NONE;
+ }
+ else {
+
+ // check if the bird collided with the cat
+ if(checkCollision(cat.x, cat.y, image_cat->w, image_cat->h, bird[i].x, bird[i].y, image_cat->w, image_cat->h)) {
+ bird[i].type = BIRD_TYPE_NONE;
+ }
+ }
} else {
// there should be a 1 in 10 chance for a bird to spawn
if(getRandomInt(10) == 1) {
@@ -253,6 +263,16 @@ void gameRoutine() {
}
}
+bool checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
+ if(y1 > y2 + h2) return false;
+ if(y2 > y1 + h1) return false;
+ if(x1 > x2 + w2) return false;
+ if(x2 > x1 + w1) return false;
+
+ return true;
+}
+
+
int getRandomInt(int limit) {
return rand() % (limit+1);
}