-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.c
executable file
·372 lines (357 loc) · 9.3 KB
/
engine.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "engine.h"
#include "constants.h"
void map_init(int map[31][30]) {
int i, j;
FILE * map_file;
/*Open map file*/
if ((map_file = fopen("map.txt", "r")) == NULL) {
fprintf(stderr, "Couldn't open \"map.txt\"\n");
exit(1);
}
/*Convert to the array*/
for (i = 0; i < 31; i++) {
for (j = 0; j < 30; j++) {
char c = getc(map_file);
switch (c) {
case ' ':
map[i][j] = EMPTY;
break;
case '.':
map[i][j] = PILL;
break;
case 'P':
map[i][j] = POWERUP;
break;
case '#':
map[i][j] = WALL;
break;
case 'T':
map[i][j] = TELEPORT;
break;
case 'C':
map[i][j] = CAGE;
break;
default:
printf("Unknown char: %c\n",c);
exit(0);
}
}
getc(map_file);
}
fclose(map_file);
}
void characters_init(pacman_t *pacman, ghost_t *ghosts, int *direction, SDL_Rect *background_dest) {
int i;
/*Pacman*/
pacman->animation_state = 0 * PACMAN_ANIMATION_SPEED;
pacman->direction = LEFT;
pacman->speed = PACMAN_SPEED;
pacman->slow = 0;
*direction = NONE;
/*Ghosts*/
for (i=0;i<4;i++) {
ghosts[i].animation_state = 0;
ghosts[i].direction = LEFT;
ghosts[i].image = i;
ghosts[i].speed = GHOST_SPEED;
ghosts[i].weakness_state = NORMAL;
ghosts[i].time_to_recover = 0;
}
set_all_start_positions(pacman, ghosts, background_dest);
}
void set_start_position(SDL_Rect *name_destination, int n, int m) {
name_destination->x = m * IMAGE_WIDTH;
name_destination->y = n * IMAGE_HEIGHT;
}
void set_all_start_positions(pacman_t *pacman, ghost_t *ghosts, SDL_Rect *background_dest) {
set_start_position(&pacman->position, 23, 15);
set_start_position(&ghosts[BLINKY].position, 11, 15);
set_start_position(&ghosts[INKY].position, 14, 14);
set_start_position(&ghosts[PINKY].position, 14, 15);
set_start_position(&ghosts[CLYDE].position, 14, 16);
set_start_position(background_dest, 0, 1);
}
void bring_ghosts_morale_back(ghost_t *ghosts) {
int i;
for (i = 0; i < 4; i++) {
if (ghosts[i].weakness_state != DEAD) {
ghosts[i].time_to_recover--;
if (ghosts[i].weakness_state == TELEPORTED && ghosts[i].time_to_recover == 0) {
ghosts[i].weakness_state = NORMAL;
}
if (ghosts[i].time_to_recover == (TIME_TO_RECOVER / 3) && ghosts[i].weakness_state == WEAK) {
ghosts[i].weakness_state = FLASHING;
} else if (ghosts[i].time_to_recover <= 0 && ghosts[i].weakness_state == FLASHING) {
ghosts[i].animation_state = 0;
ghosts[i].weakness_state = NORMAL;
ghosts[i].time_to_recover = 0;
}
}
if (ghosts[i].weakness_state == NORMAL || ghosts[i].weakness_state == DEAD) {
ghosts[i].speed = GHOST_SPEED;
}
if (ghosts[i].position.x >= 14 * IMAGE_WIDTH && ghosts[i].position.x <= 15 * IMAGE_WIDTH
&& ghosts[i].position.y <= 14 * IMAGE_HEIGHT && ghosts[i].position.y >= 13 * IMAGE_HEIGHT) {
ghosts[i].weakness_state = NORMAL;
}
}
}
void move_pacman(pacman_t *pacman, int direction, int *score) {
/*Turning backwards*/
if ( direction == UP && pacman->direction == DOWN ||
direction == DOWN && pacman->direction == UP ||
direction == RIGHT && pacman->direction == LEFT ||
direction == LEFT && pacman->direction == RIGHT ) {
pacman->direction = direction;
}
/*Check if pacman is can change direction*/
if (pacman->position.x % IMAGE_WIDTH == 0 && pacman->position.y % IMAGE_HEIGHT == 0) {
int x = pacman->position.x / IMAGE_HEIGHT;
int y = pacman->position.y / IMAGE_WIDTH;
/*Check if user pressed the DIRECTION key*/
if (direction) {
switch (direction)
{
case RIGHT:
x++;
break;
case LEFT:
x--;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
printf("Direction %d\n",direction);
exit(0);
}
if (map[y][x] != WALL && map[y][x] != CAGE) {
pacman->direction = direction;
}
}
/*Check movement with map*/
x = pacman->position.x / IMAGE_HEIGHT;
y = pacman->position.y / IMAGE_WIDTH;
if (map[y][x] == PILL) {
*score += 10;
map[y][x] = EMPTY;
pacman->slow = 5;
} else if (map[y][x] == POWERUP) {
map[y][x] = EMPTY;
have_powerup = 1;
}
switch (pacman->direction) {
case RIGHT:
x++;
break;
case LEFT:
x--;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
printf("Direction %d\n",pacman->direction);
exit(0);
}
if (map[y][x] == TELEPORT) {
if (x == 29) {
pacman->position.x = IMAGE_WIDTH * 1;
} else {
pacman->position.x = IMAGE_WIDTH * 28;
}
return;
} else if (map[y][x] == WALL) {
return;
}
}
/*Move sprite*/
switch (pacman->direction) {
case RIGHT:
pacman->position.x++;
break;
case LEFT:
pacman->position.x--;
break;
case UP:
pacman->position.y--;
break;
case DOWN:
pacman->position.y++;
break;
default:
printf("Direction: %d\n",pacman->direction);
exit(0);
}
}
int allot_direction(ghost_t *ghost) {
int directions[4] = {ghost->direction, ghost->direction, ghost->direction, ghost->direction};
int x = ghost->position.x / IMAGE_WIDTH;
int y = ghost->position.y / IMAGE_HEIGHT;
int i = 0;
if (map[y][x+1] != WALL && ghost->direction != LEFT) {
directions[i++] = RIGHT;
}
if (map[y][x-1] != WALL && ghost->direction != RIGHT) {
directions[i++] = LEFT;
}
if (map[y-1][x] != WALL && ghost->direction != DOWN) {
directions[i++] = UP;
}
if (map[y+1][x] != WALL && map[y+1][x] != CAGE && ghost->direction != UP) {
directions[i++] = DOWN;
}
return directions[(rand() % i)];
}
int get_direction_towards(ghost_t *ghost, int n, int m) {
int directions[4] = {ghost->direction, ghost->direction, ghost->direction, ghost->direction};
int x = ghost->position.x / IMAGE_WIDTH;
int y = ghost->position.y / IMAGE_HEIGHT;
if (ghost->position.x < m * IMAGE_WIDTH && map[y][x+1] != WALL && ghost->direction != LEFT) {
return RIGHT;
} else if (ghost->position.x > m * IMAGE_WIDTH && map[y][x-1] != WALL && ghost->direction != RIGHT) {
return LEFT;
} else if (ghost->position.y < n * IMAGE_HEIGHT && map[y+1][x] != WALL && ghost->direction != UP) {
return DOWN;
} else if (ghost->position.y > n * IMAGE_HEIGHT && map[y-1][x] != WALL && ghost->direction != DOWN) {
return UP;
}
return allot_direction(ghost);
}
void move_ghost(ghost_t *ghost, int ghost_name, pacman_t *pacman) {
/*Check if I can change direction*/
if (ghost->position.x % IMAGE_WIDTH == 0 && ghost->position.y % IMAGE_HEIGHT == 0) {
/*Get my position*/
int x = ghost->position.x / IMAGE_WIDTH;
int y = ghost->position.y / IMAGE_HEIGHT;
/*If I'm in cage, get out of it*/
if (ghost->weakness_state != DEAD && map[y][x] == CAGE) {
if (map[y-1][x] != WALL) {
ghost->direction = UP;
} else if (map[y][x-1] != WALL) {
ghost->direction = LEFT;
} else {
ghost->direction = RIGHT;
}
} else if (ghost->weakness_state == DEAD) {
/*Go to cage*/
ghost->direction = get_direction_towards(ghost, 14, 14);
} else if (ghost->weakness_state == WEAK || ghost->weakness_state == FLASHING) {
/*Run away from pacman*/
ghost->direction = allot_direction(ghost);
} else {
if (ghost_name != BLINKY) {
ghost->direction = allot_direction(ghost);
} else {
/*Blinky's tactic*/
int tactic = rand() % 5;
if (tactic) {
int pacman_x = pacman->position.x / IMAGE_WIDTH;
int pacman_y = pacman->position.y / IMAGE_HEIGHT;
ghost->direction = get_direction_towards(ghost, pacman_y, pacman_x);
} else {
ghost->direction = allot_direction(ghost);
}
}
}
/*Move within chosen direction*/
switch (ghost->direction)
{
case RIGHT:
x++;
break;
case LEFT:
x--;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
printf("Direction %d\n", ghost->direction);
exit(0);
}
if (map[y][x] == TELEPORT) {
if (ghost->weakness_state == NORMAL) {
ghost->time_to_recover = AFTER_TELEPORT;
ghost->weakness_state = TELEPORTED;
}
if (x == 29) {
ghost->position.x = IMAGE_WIDTH *1;
} else {
ghost->position.x = IMAGE_WIDTH * 28;
}
return;
} else if (map[y][x] == WALL) {
return;
}
}
/*Move sprite*/
switch (ghost->direction)
{
case RIGHT:
ghost->position.x++;
break;
case LEFT:
ghost->position.x--;
break;
case UP:
ghost->position.y--;
break;
case DOWN:
ghost->position.y++;
break;
default:
printf("Direction %d\n", ghost->direction);
exit(0);
}
}
int ghosts_collision(pacman_t *pacman, ghost_t *ghosts) {
int i;
/*Check for collision*/
for (i=0;i<4;i++) {
if (abs(ghosts[i].position.x-pacman->position.x) < TOLERANCE && abs(ghosts[i].position.y-pacman->position.y) < TOLERANCE) {
return i;
}
}
return NOT_CAUGHT;
}
int pills_left(void) {
int i, j, pills = 0;
/*Check if there is anyting to eat for pacman*/
for (i = 1; i < 30; i++) {
for (j = 1; j < 28; j++) {
if (map[j][i] == PILL) {
pills++;
}
}
}
return pills;
}
unsigned int high_score (unsigned int score) {
FILE * hisc;
unsigned int hiscore;
/*Open high score file*/
if ((hisc = fopen("hiscore.txt", "r+b")) == NULL) {
fprintf(stderr, "Nie mozna otworzyc pliku\"high_score.txt\"");
exit(1);
}
/*Read current hiscore and check with player score*/
fread(&hiscore, sizeof (unsigned int), 1, hisc);
if (score > hiscore) {
hiscore = score;
fclose(hisc);
hisc = fopen("hiscore.txt", "w");
fwrite(&score, sizeof (unsigned int), 1, hisc);
}
fclose(hisc);
return hiscore;
}