-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgold.c
38 lines (34 loc) · 1.08 KB
/
gold.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "graphics.h"
#include "gold.h"
SDL_Rect **init_gold(SDL_Texture *gold_tex)
{
SDL_Rect **gold_array = malloc(NUM_GOLD * sizeof(SDL_Rect *));
if (!gold_array) {
printf("Ran out of memory\n");
exit(-1);
}
for (int i = 0; i < NUM_GOLD; i++) {
gold_array[i] = malloc(sizeof(SDL_Rect));
if (!gold_array[i]) {
printf("Ran out of memory\n");
exit(-1);
}
SDL_QueryTexture(gold_tex, NULL, NULL, &gold_array[i]->w, &gold_array[i]->h);
gold_array[i]->w /= 10;
gold_array[i]->h /= 10;
gold_array[i]->x = rand() % (WINDOW_WIDTH-30+1)+30;
gold_array[i]->y = rand() % (WINDOW_HEIGHT-30+1)+30;
}
return gold_array;
}
int check_gold_collisions(SDL_Rect player, SDL_Rect **gold_array, int score)
{
for (int i = 0; i < NUM_GOLD; i++) {
if (SDL_HasIntersection(&player, gold_array[i])) {
score += 10;
gold_array[i]->x = rand() % (WINDOW_WIDTH-30+1)+30;
gold_array[i]->y = rand() % (WINDOW_HEIGHT-30+1)+30;
}
}
return score;
}