-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
141 lines (128 loc) · 3.56 KB
/
game.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdlib.h>
#include <time.h>
#include "game.h"
#include "filepaths.h"
int gameOver;
int score;
int snakeLength;
int *snakeX, *snakeY;
int fruitX, fruitY;
int direction;
int gameMode;
int specialFruitX, specialFruitY;
int specialFruitActive;
int **obstacles;
int delay;
void allocateMemory() {
snakeX = (int *)malloc(100 * sizeof(int));
snakeY = (int *)malloc(100 * sizeof(int));
obstacles = (int **)malloc(WIDTH * sizeof(int *));
for (int i = 0; i < WIDTH; i++) {
obstacles[i] = (int *)malloc(HEIGHT * sizeof(int));
}
}
void freeMemory() {
free(snakeX);
free(snakeY);
for (int i = 0; i < WIDTH; i++) {
free(obstacles[i]);
}
free(obstacles);
}
void setup() {
gameOver = 0;
score = 0;
snakeLength = 1;
snakeX[0] = WIDTH / 2;
snakeY[0] = HEIGHT / 2;
srand(time(NULL));
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
specialFruitActive = 0;
direction = RIGHT;
delay = INITIAL_DELAY;
if (gameMode == 1) {
for (int i = 0; i < WIDTH * HEIGHT / 10; i++) {
int obsX = rand() % WIDTH;
int obsY = rand() % HEIGHT;
if ((obsX != snakeX[0] || obsY != snakeY[0]) && (obsX != fruitX || obsY != fruitY)) {
obstacles[obsX][obsY] = 1;
}
}
}
}
void handleInput(SDL_Event *e) {
while (SDL_PollEvent(e) != 0) {
if (e->type == SDL_QUIT) {
gameOver = 1;
}
if (e->type == SDL_KEYDOWN) {
switch (e->key.keysym.sym) {
case SDLK_w:
direction = UP;
break;
case SDLK_s:
direction = DOWN;
break;
case SDLK_a:
direction = LEFT;
break;
case SDLK_d:
direction = RIGHT;
break;
}
}
}
}
void move() {
int prevX = snakeX[0];
int prevY = snakeY[0];
int prev2X, prev2Y;
snakeX[0] = snakeX[0] + (direction == RIGHT ? 1 : (direction == LEFT ? -1 : 0));
snakeY[0] = snakeY[0] + (direction == DOWN ? 1 : (direction == UP ? -1 : 0));
for (int i = 1; i < snakeLength; i++) {
prev2X = snakeX[i];
prev2Y = snakeY[i];
snakeX[i] = prevX;
snakeY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
if (gameMode == 0) {
if (snakeX[0] >= WIDTH) snakeX[0] = 0;
else if (snakeX[0] < 0) snakeX[0] = WIDTH - 1;
if (snakeY[0] >= HEIGHT) snakeY[0] = 0;
else if (snakeY[0] < 0) snakeY[0] = HEIGHT - 1;
} else if (gameMode == 1) {
if (snakeX[0] >= WIDTH || snakeX[0] < 0 || snakeY[0] >= HEIGHT || snakeY[0] < 0) {
gameOver = 1;
}
}
if (snakeX[0] == fruitX && snakeY[0] == fruitY) {
score += 10;
snakeLength++;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
delay = INITIAL_DELAY - score / 10;
}
if (specialFruitActive && snakeX[0] == specialFruitX && snakeY[0] == specialFruitY) {
score += 20;
snakeLength += 2;
specialFruitActive = 0;
}
for (int i = 1; i < snakeLength; i++) {
if (snakeX[i] == snakeX[0] && snakeY[i] == snakeY[0]) {
gameOver = 1;
}
}
if (obstacles[snakeX[0]][snakeY[0]] == 1) {
gameOver = 1;
}
}
void generateSpecialFruit() {
if (!specialFruitActive && rand() % 20 == 0) {
specialFruitX = rand() % WIDTH;
specialFruitY = rand() % HEIGHT;
specialFruitActive = 1;
}
}