-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
369 lines (336 loc) · 9.75 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <SDL2/SDL.h>
#include "game.h"
#include "display.h"
#include "sound.h"
const char * crash="./Sound/crash.mp3";
const char * klaxon="./Sound/klaxon.mp3";
const char * acceleration="./Sound/acceleration.mp3";
const char * deceleration="./Sound/deceleration.mp3";
const char * ambulance="./Sound/ambulance.mp3";
const char * cow="./Sound/cow.mp3";
const char * monkey="./Sound/singe.mp3";
const char * goat="./Sound/chevre.mp3";
const char * eagle="./Sound/aigle.mp3";
const char * gameOver="./Sound/GameOver.mp3";
const int VIT_MAX_PLAYER = 150;
const int VIT_MIN_PLAYER = 50;
const char * sound0 ="./Sound/LoadingLoop.wav repeat 9999";
const char * sound1 ="./Sound/Gamey1.wav repeat 9999";
const char * sound2 ="./Sound/Beparwa.wav repeat 9999";
const char * sound3 ="./Sound/SongwriterBallad.wav repeat 9999";
const char * sound4 ="./Sound/Musala.wav repeat 9999";
int NB_MAX_CAR;
int radiofrq = 1;
int nb_tour = 0;
char key_pressed() {
struct termios oldterm, newterm;
int oldfd;
char c, result = 0;
tcgetattr(STDIN_FILENO, &oldterm);
newterm = oldterm;
newterm.c_lflag &= ~ (ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newterm);
oldfd = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldfd | O_NONBLOCK);
c = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldterm);
fcntl(STDIN_FILENO, F_SETFL, oldfd);
if(c!=EOF){
ungetc(c, stdin);
result = getchar();
}
return result;
}
vehicule **alloc_road(int nb_c, int nb_l){
vehicule **road =malloc(nb_l*sizeof(vehicule));
int i,j;
vehicule ghost = generGhost();
for(i=0; i<nb_l; i++){
road[i] = malloc(nb_c*sizeof(vehicule));
}
for(i=0;i<nb_l;i++){
for(j=0;j<nb_c;j++){
road[i][j]=ghost;
}
}
return road;
}
void free_road(vehicule ** road, int nb_c, int nb_l){
int i;
for(i=0; i<=nb_l; i++){
free(*(road+i));
}
free(road);
}
int more_cars( vehicule* carList, int nb_cars, vehicule ** road){
int random = (rand()%(NB_VOIE_DEFAULT-1)), car_added = 0, i;
vehicule v;
for(i=0; i<random; i++){
if(nb_cars+car_added<NB_MAX_CAR){
v = generVehicule(NB_VOIE_DEFAULT);
//doit vérifier que la case de la voiture est bien libre
//sinon soit en génère une autre tant que sa case n'est pas libre
//soit change le posx pour une case libre
//if(road[v.posy][0].ghost || road[v.posy][1].ghost || road[v.posy][2].ghost){
//while(!road[v.posy][v.posx].ghost){
v = generVehicule(NB_VOIE_DEFAULT);
//}
addCar(&v, carList, nb_cars);
car_added += 1;
draw_car(&v);
road[v.posy][v.posx] = v;
//}
}
}
return car_added;
}
int scoring(int car_passed, int vitesse){
int points = 0;
points = car_passed*vitesse/30;
return points;
}
int collision(int nbCars, vehicule* carList, vehicule * player){
int i;
for(i=0;i<nbCars;i++){
if(carList[i].posx == player->posx && carList[i].posy == player->posy){
playSound(crash);
printf("\033[%d;%dH\e[100m\e[31m💥 \e[39m\e[49m",player->posy,((player->posx*LARGEUR_ROUTE)+TAB_SIZE));
return 0;
}
}
return 1;
}
int radio(int radiofrq){
if(radiofrq == 0){
killSound(sound4);
playSound(sound1);
radiofrq++;
}else if(radiofrq == 1){
killSound(sound1);
playSound(sound2);
radiofrq++;
}else if(radiofrq == 2){
killSound(sound2);
playSound(sound3);
radiofrq++;
}else if(radiofrq == 3){
killSound(sound3);
playSound(sound4);
radiofrq =0;
}
return radiofrq;
}
void killRadio(int radiofrq){
if(radiofrq == 0){
killSound(sound4);
}else if(radiofrq == 1){
killSound(sound1);
}else if(radiofrq == 2){
killSound(sound2);
}else if(radiofrq == 3){
killSound(sound3);
}
}
int player_actions(char c, vehicule * player){
int b = 1;
if((c=='d'||c=='C') && player->posx<NB_VOIE_DEFAULT-1){
move_player(player->posx+1, player);
}
if((c=='q'||c=='D') && player->posx>0){
move_player(player->posx-1, player);
}
if(c=='l'){
b = 0;
killRadio(radiofrq);
}
if(c=='A' || c=='z'){
//test si vitesse inf a VIT_MAX_PLAYER
if (player->vitesse<VIT_MAX_PLAYER){
player->vitesse +=10;
update_vitesse(player);
if(player->vitesse > 120){
playSound(acceleration);
}
}
}
if(c=='B' || c=='s'){
//test si vitesse sup a VIT_MIN_PLAYER
if (player->vitesse>VIT_MIN_PLAYER){
player->vitesse -=10;
update_vitesse(player);
if(player->vitesse < 80){
playSound(deceleration);
}
}
}
if(c=='k'){
playSound(klaxon);
}
if(c == 'w'){
playSound(monkey);
}
if(c == 'x'){
playSound(goat);
}
if(c == 'c'){
playSound(eagle);
}
if(c == 'v'){
playSound(ambulance);
}
if(c == 'm'){
playSound(cow);
}
if(c == 'r'){
radiofrq = radio(radiofrq);
update_radio();
}
return b;
}
int IA_actions(char c){
int b = 1;
if(c=='l'){
b = 0;
killRadio(radiofrq);
}
if(c=='k'){
playSound(klaxon);
}
if(c == 'm'){
playSound(cow);
}
if(c == 'w'){
playSound(monkey);
}
if(c == 'x'){
playSound(goat);
}
if(c == 'c'){
playSound(eagle);
}
if(c == 'v'){
playSound(ambulance);
}
if(c == 'r'){
radiofrq = radio(radiofrq);
update_restricted_radio();
}
return b;
}
int player_mode(int best, int diff){
srand(time(NULL));
system("play -q -v 0.99 ./Sound/Gamey1.wav repeat 9999 &");
vehicule** road = alloc_road(NB_VOIE_DEFAULT,HAUTEUR_ROUTE+1);
radiofrq =1;
NB_MAX_CAR = difficulty (diff);
vehicule* carList = malloc(NB_MAX_CAR*sizeof(vehicule));
int nb_cars = 0, score=0, size_score = 1;
vehicule player = generPlayer();
road[player.posx][player.posy] = player;
player.custom="\e[31m🚘 \e[39m";
print_road(best, 1);
move_player(player.posx, &player);
int b = 1;
unsigned int lastTime = 0, currentTime;
while(b){
char c = key_pressed();
b = player_actions(c, &player);
clean_cursor();
currentTime = SDL_GetTicks();
if (currentTime > lastTime+ (10000/player.vitesse)) {
nb_tour++;
if(nb_tour%500 == 0){playSound(ambulance);}
move_road();
int car_removed = move_cars(carList, nb_cars, &player, road);
int car_added = more_cars(carList, nb_cars, road);
c=collision(nb_cars,carList,&player);
nb_cars += car_added;
score += scoring(car_removed, player.vitesse);
nb_cars -= car_removed;
size_score = get_size_int(score);
update_panel(&player, score, size_score, best);
lastTime = currentTime;
clean_cursor();
if (c==0){
unsigned int beginTime2= SDL_GetTicks();
unsigned int currentTime2= beginTime2;
while(currentTime2 < beginTime2 + 2500){ currentTime2 = SDL_GetTicks();}
killRadio(radiofrq);
system("play -q -v 0.99 ./Sound/GameOver.mp3 &");
game_over();
b=0;
}
}
}
free(carList);
free_road(road, NB_VOIE_DEFAULT,HAUTEUR_ROUTE+1);
return score;
}
int IA_mode(int best){
srand(time(NULL));
system("./killSound.sh ./Sound/LoadingLoop.wav repeat 9999");
system("play -q -v 0.99 ./Sound/Gamey1.wav repeat 9999 &");
radiofrq =1;
vehicule** road = alloc_road(NB_VOIE_DEFAULT, HAUTEUR_ROUTE+1);
NB_MAX_CAR = 50;
vehicule* carList = malloc(NB_MAX_CAR*sizeof(vehicule));
vehicule * ghost = malloc(sizeof(vehicule));
*ghost = generGhost();
int nb_cars = 0, score=0, size_score = 1;
vehicule IA;
IA.posx = 1;
IA.posy = HAUTEUR_ROUTE;
IA.type = 'v';
IA.couleur = BLUE;
IA.custom="\e[31m🚘 \e[39m";
IA.vitesse = 150;
IA.ghost=12;
road[IA.posx][IA.posy] = IA;
print_road(best, 0);
move_player(IA.posx, &IA);
int b = 1;
unsigned int lastTime = 0, currentTime;
while(b){
char c = key_pressed();
b = IA_actions(c);
clean_cursor();
currentTime = SDL_GetTicks();
if (currentTime > lastTime+ (12000/IA.vitesse)) {
nb_tour++;
if(nb_tour%500 == 0){playSound(ambulance);}
move_road();
move_IA(carList, nb_cars, &IA, road);
int car_removed = move_cars(carList, nb_cars, &IA, road);
int car_added = more_cars(carList, nb_cars, road);
c=collision(nb_cars,carList,&IA);
score += scoring(car_removed, IA.vitesse);
nb_cars += car_added;
nb_cars -= car_removed;
size_score = get_size_int(score);
update_panel(&IA, score, size_score, best);
lastTime = currentTime;
update_vitesse(&IA);
clean_cursor();
if (c==0){
unsigned int beginTime2= SDL_GetTicks();
unsigned int currentTime2= beginTime2;
while(currentTime2 < beginTime2 + 2500){ currentTime2 = SDL_GetTicks();}
killRadio(radiofrq);
system("play -q -v 0.99 ./Sound/GameOver.mp3 &");
game_over();
b=0;
}
}
}
free(carList);
free_road(road, NB_VOIE_DEFAULT,HAUTEUR_ROUTE+1);
return score;
}