-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruit.c
74 lines (69 loc) · 2.02 KB
/
fruit.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL_image.h>
#include "app.h"
#include "fruit.h"
#include "game.h"
#include "snake.h"
Fruit *new_fruit(Fruit *fruits[], int nr_of_fruits, Snake *snake)
{
Fruit *fruit = malloc(sizeof(Fruit));
// generate random position
fruit->pos.x = (rand() % (WINDOW_WIDTH / CELL_SIZE)) * CELL_SIZE;
fruit->pos.y = (rand() % (WINDOW_HEIGHT / CELL_SIZE)) * CELL_SIZE;
// generate random fruit type
fruit->type = rand() % 4;
// different points based on fruit type
switch(fruit->type) {
case Cherry:
fruit->points = 10;
break;
case Apple:
fruit->points = 20;
break;
case Pear:
fruit->points = 30;
break;
case Mango:
fruit->points = 40;
break;
}
// check if position overlaps with snake position
// head
if(fruit->pos.x == snake->head.pos.x && fruit->pos.y == snake->head.pos.y) {
free(fruit);
return NULL;
}
// body
for(int i = 0; i < snake->body_length; i++) {
if(fruit->pos.x == snake->body[i].pos.x && fruit->pos.y == snake->body[i].pos.y) {
free(fruit);
return NULL;
}
}
// tail
if(fruit->pos.x == snake->tail.pos.x && fruit->pos.y == snake->tail.pos.y) {
free(fruit);
return NULL;
}
// check if position overlaps with other fruit positions
for(int i = 0; i < nr_of_fruits; i++) {
if(fruit->pos.x == fruits[i]->pos.x && fruit->pos.y == fruits[i]->pos.y) {
free(fruit);
return NULL;
}
}
return fruit;
}
bool fruit_collision(Snake *snake, Fruit *fruits[], int nr_of_fruits)
{
//check if snake head is at fruit position
for(int i = 0; i < nr_of_fruits; i++) {
if(fruits[i]->pos.x == snake->head.pos.x && fruits[i]->pos.y == snake->head.pos.y) {
snake->head.mouth_eating = true;
return true;
}
}
return false;
}