-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.c
77 lines (73 loc) · 2.21 KB
/
food.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
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include "gameStructs.h"
#include "food.h"
#include "snake.h"
int checkFoodEaten(game* currentGame)
{
if ((*currentGame)->snake->head->posX == (*currentGame)->food->foodX && (*currentGame)->snake->head->posY == (*currentGame)->food->foodY) //Se il serpente va sul cibo
{
(*currentGame)->score += (*currentGame)->food->value; //Aggiorno il punteggio
stretchSnake(currentGame); //Allungo il serpente
spawnFood(currentGame); //Spawno il cibo
return 0;
}
return 1;
}
void spawnFood(game* currentGame)
{
int x,y;
getmaxyx(stdscr, y, x); //Misuro la grandezza dello schermo
if((*currentGame)->food!=NULL)
{
free((*currentGame)->food); //Libero la memoria del precedente pezzo di cibo
}
struct food* newFood = (struct food*) malloc(sizeof(struct food)); //Alloco il nuovo pezzo di cibo
if(newFood == NULL) {erase(); printw("Errore durante lo spawn del cibo. Memoria insufficiente."); endwin();}
while(1)
{
newFood->foodX = (rand() % (x-3)) +1; //Spawno entro i margini dello schermo
newFood->foodY = (rand() % (y-3)) +2;
if(checkPos(*currentGame, newFood)){break;} //Controllo se il cibo viene posizionato in una posizione valida
}
if ((rand() % 10) > 7) //Cibo speciale dorato
{
newFood->value = 200;
}
else //Cibo non speciale
{
newFood->value = 100;
}
newFood->food = '@';
if (newFood->value == 100)
{
//init_pair(3, COLOR_RED, COLOR_BLACK); //Imposto le proprietà del cibo normale
attron(COLOR_PAIR(3));
mvaddch(newFood->foodY, newFood->foodX, newFood->food);
attroff(COLOR_PAIR(3));
}
else
{
//init_pair(4, COLOR_YELLOW, COLOR_BLACK); //Oppure imposto le proprietà di quello dorato
attron(COLOR_PAIR(4));
mvaddch(newFood->foodY, newFood->foodX, newFood->food);
attroff(COLOR_PAIR(4));
}
(*currentGame)->food = newFood;
}
int checkPos(game currentGame, struct food* food) //Controllo la posizione del cibo
{
int valid=1;
struct body*p;
p=currentGame->snake->head;
while(p!=NULL)
{
if((p->posX==food->foodX) && (p->posY==food->foodY)) //Se il cibo viene posizionato in un posto occupato dal serpente, il piazzamento non è valido
{
valid=0;
}
p=p->next;
}
return valid;
}