-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnoid.c
executable file
·1063 lines (991 loc) · 33 KB
/
pnoid.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************/
/* PINGUINoid! */
/* ----------- */
/* File : pnoid.c */
/* Dimensioni : 33801 bytes */
/* Ultima modifica : Mercoledi' 7 Giugno 2000 - 16:43 */
/* */
/* N.B. 1) Compilare con : gcc -lvgagl -lvga -opnoid pnoid.c */
/* 2) L'eseguibile dovra' essere settato da un utente root */
/* con permessi 7777, affinche' possa essere usato da */
/* utenti con permessi non-root. In caso contrario */
/* le SVGAlib genererebbero il messagio di errore */
/* "Cannot get I/O permission" causando l'uscita dal */
/* programma. */
/* */
/* Realizzato da Gianluca Esposito (Num.Matr.056/100858) */
/* Prova di Laboratorio di Informatica I */
/* Prof. M.Tucci */
/* Universita' degli Studi di Salerno */
/* Facolta' di Scienze matematiche fisiche e naturali */
/* Corso di Laurea in Informatica */
/* Anno Accademico 1999/2000 */
/*******************************************************************/
#define SOLID 1
#define OMBRA 0
#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0
#define NORMAL 0
#include <stdio.h>
#include <stdlib.h>
#include <vga.h>
#include <vgagl.h>
#include <vgakeyboard.h>
#include "256color.c"
#include "bonus.c"
#include "ball.c"
#include "brick.c"
#include "bar.c"
#include "score.c"
#include "logo.c"
#include "lives.c"
#include "stage.c"
#include "text.c"
#define DEFAULT_USLEEP 1000
#define LEFT_LIMIT 10
#define UPPER_LIMIT 10
#define RIGHT_LIMIT 262
#define TILT_LIMIT 50
#define LOCK_TIME 100
/* Variabili Globali */
int speedcounter;
int leftpress;
int rightpress;
int current_stage;
int numlives;
int tilt_counter;
int lock_counter;
void ProgramInfo(void);
void Partita(void);
void DrawBorder(void);
void DrawSfondo(void);
void DrawMap(void);
void Key_Control(void);
void Key_Tilt(void);
void Key_Laser(void);
void BrickContact(int ballid);
void BarContact(int ballid);
void BorderContact(int ballid);
void LoseALife(void);
void BonusContact(int counter);
void MultiBall(void);
void ActiveBall(void);
void BallLock(void);
void Extend(void);
void Reduce(void);
void UpdateCounter(void);
void InitLife(void);
void InitGame(void);
void InitStage(void);
void AssignBonus(void);
void BackupWinCounter(void);
void RestoreWinCounter(void);
void BackupMaps(void);
void RestoreMaps(void);
void KillTouchedBonus(void);
void KillLaser(void);
void KillMultiBall(void);
void ExitProgram(void);
int main(void)
{
system("clear");
ProgramInfo();
vga_disabledriverreport();
vga_init();
vga_setmode(G320x240x256);
gl_setcontextvga(G320x240x256);
LoadPalette();
keyboard_init();
Partita();
return 0;
}
void ProgramInfo(void)
/* Scrive a video una piccola introduzione al programma. */
{
printf("\n\n\n PINGUINoid\n");
printf(" ----------\n");
printf(" Progetto realizzato da\n");
printf(" ESPOSITO GIANLUCA (Matr.Num. 056/100858)\n");
printf(" per la Prova di Laboratorio di Informatica I\n");
printf(" relativa al Corso tenuto dal Prof. M. Tucci\n\n");
printf(" Universita' degli Studi di Salerno\n");
printf(" Facolta' di Scienze matematiche fisiche e naturali\n");
printf(" Corso di Laurea in Informatica\n");
printf(" Anno Accademico 1999/2000\n\n");
printf("\n\n\n Premi INVIO per continuare\n");
fflush(stdin);
getchar();
}
void Partita(void)
/* Gestisce l'intera partita aggiornando costantemente i vari flag */
/* ed i vari valori di posizione e gestendo le varie immagini da */
/* disegnare a video. */
{
int i;
InitGame();
while(TRUE) {
UpdateCounter();
for(i=0;i<MAX_BALLS;i++) {
keyboard_update();
if(keyboard_keypressed(SCANCODE_ESCAPE)==KEY_PRESSED)
ExitProgram();
Key_Control();
Key_Tilt();
Key_Laser();
if(keyboard_keypressed(SCANCODE_SPACE)==KEY_PRESSED &&
ball_lock == TRUE)
ball_lock = FALSE;
if(ball_lock == FALSE && xball[i]>=0 && xball[i]<=RIGHT_LIMIT &&
yball[i]>=0 && yball[i]<=240) {
BrickContact(i);
BorderContact(i);
BarContact(i);
if(xball[i]!=KILLED && yball[i]!=KILLED) {
DrawObject(xball[i], yball[i], BALL_X, BALL_Y, Ball, OMBRA);
xball[i] += Direzione[velocita][direction[i]].passox;
yball[i] += Direzione[velocita][direction[i]].passoy;
DrawObject(xball[i], yball[i], BALL_X, BALL_Y, Ball, SOLID);
}
if(laserfired == ON && laserflag == ON) {
if(ylaser <= 14.0) {
DrawObject(xlaser, (int)ylaser, LASER_X, LASER_Y, laser, OMBRA);
laserfired = OFF;
xlaser = KILLEDLASER;
ylaser = (float)KILLEDLASER;
}
}
if(yball[i] >= 240) {
xball[i] = KILLED;
yball[i] = KILLED;
ballingame-=1;
}
if(ballingame == 1) KillMultiBall();
if(ballingame==0) LoseALife();
}
}
usleep(DEFAULT_USLEEP);
}
}
void DrawBorder(void)
/* Disegna a video i bordi dell'area giocabile */
{
int i;
for(i=3;i<11;i++) {
vga_setcolor(i);
vga_drawline(i-3,0,i-3,240);
vga_drawline(275-i,0,275-i,240);
vga_drawline(0,i-3,272,i-3);
}
}
void DrawSfondo(void)
/* Disegna a video le componenti informative del gioco quali */
/* il logo, il titolo, l'autore, i tasti, etc. */
{
DrawBorder();
DrawObject(LOGO);
DrawObject(TITLE);
DrawObject(AUTHOR);
DrawObject(KEYS);
DrawObject(LIVES_TXT);
DrawObject(SCORE);
DrawScore();
DrawLives(numlives);
}
void DrawMap(void)
/* Disegna a video la mappa relativa allo stage corrente (current_stage). */
{
int i;
for(i=0;i<MAP_OBJECT;i++) {
DrawBrick(STD_MAPX+MapXY[i].begx,
STD_MAPY+MapXY[i].begy,
MapObj[current_stage][i]);
}
}
void Key_Control(void)
/* Controlla lo stato dei tasti di controllo (left e right) */
/* tenendo in considerazione l'avvenuta o meno attivazione */
/* del flag "reverseflag" che abilita l'inversione dei tasti. */
{
if(reverseflag == OFF) {
if(keyboard_keypressed(SCANCODE_CURSORBLOCKLEFT)==KEY_PRESSED) {
leftpress = TRUE;
if(xbar >= LEFT_LIMIT+3) {
xbar -= 2;
DrawBar(xbar+2, ybar, OMBRA, bartype);
DrawBar(xbar, ybar, SOLID, bartype);
if(ball_lock == TRUE) {
xball[0] -= 2;
DrawObject(xball[0]+2, yball[0], BALL_X, BALL_Y, Ball, OMBRA);
DrawObject(xball[0], yball[0], BALL_X, BALL_Y, Ball, SOLID);
}
}
}
else leftpress = FALSE;
if(keyboard_keypressed(SCANCODE_CURSORBLOCKRIGHT)==KEY_PRESSED) {
rightpress = TRUE;
if(xbar+2+barwidth<RIGHT_LIMIT) {
xbar+=2;
DrawBar(xbar-2, ybar, OMBRA, bartype);
DrawBar(xbar, ybar, SOLID, bartype);
if(ball_lock == TRUE) {
xball[0]+=2;
DrawObject(xball[0]-2, yball[0], BALL_X, BALL_Y, Ball, OMBRA);
DrawObject(xball[0], yball[0], BALL_X, BALL_Y, Ball, SOLID);
}
}
}
else rightpress = FALSE;
}
else if(reverseflag == ON) {
if(keyboard_keypressed(SCANCODE_CURSORBLOCKLEFT)==KEY_PRESSED) {
rightpress = TRUE;
if(xbar+2+barwidth<RIGHT_LIMIT) {
xbar+=2;
DrawBar(xbar-2, ybar, OMBRA, bartype);
DrawBar(xbar, ybar, SOLID, bartype);
if(ball_lock == TRUE) {
xball[0]+=2;
DrawObject(xball[0]-2, yball[0], BALL_X, BALL_Y, Ball, OMBRA);
DrawObject(xball[0], yball[0], BALL_X, BALL_Y, Ball, SOLID);
}
}
}
else rightpress = FALSE;
if(keyboard_keypressed(SCANCODE_CURSORBLOCKRIGHT)==KEY_PRESSED) {
leftpress = TRUE;
if(xbar>=LEFT_LIMIT+3) {
xbar-=2;
DrawBar(xbar+2, ybar, OMBRA, bartype);
DrawBar(xbar, ybar, SOLID, bartype);
if(ball_lock == TRUE) {
xball[0]-=2;
DrawObject(xball[0]+2, yball[0], BALL_X, BALL_Y, Ball, OMBRA);
DrawObject(xball[0], yball[0], BALL_X, BALL_Y, Ball, SOLID);
}
}
}
else leftpress = FALSE;
}
}
void Key_Tilt(void)
/* Controlla i tasti-tilt: se premuti questi generano un effetto */
/* "flipper" ovvero simulano una spinta al monitor, con relativo */
/* spostamento anomalo della pallina. */
/* Controlla il tilt-counter: se premuti troppe volte, generano */
/* il "tilt" del gioco, causando cosi' la perdita di un credit. */
{
if(keyboard_keypressed(SCANCODE_LEFTSHIFT)==KEY_PRESSED) {
tilt_counter += 1;
if(xball[0] != KILLED &&
xball[0]>=LEFT_LIMIT+3 && xball[0]<=RIGHT_LIMIT-7){
DrawObject(xball[0], yball[0], BALL_X, BALL_Y, Ball, OMBRA);
xball[0]+=1;
}
if(xball[1] != KILLED &&
xball[1]>=LEFT_LIMIT+3 && xball[1]<=RIGHT_LIMIT-7) {
DrawObject(xball[1], yball[1], BALL_X, BALL_Y, Ball, OMBRA);
xball[1]+=1;
}
if(xball[2] != KILLED &&
xball[2]>=LEFT_LIMIT+3 && xball[2]<=RIGHT_LIMIT-7) {
DrawObject(xball[2], yball[2], BALL_X, BALL_Y, Ball, OMBRA);
xball[2]+=1;
}
}
if(keyboard_keypressed(SCANCODE_RIGHTSHIFT)==KEY_PRESSED) {
tilt_counter += 1;
if(xball[0] != KILLED &&
xball[0]>=LEFT_LIMIT+3 && xball[0]<=RIGHT_LIMIT-7) {
DrawObject(xball[0], yball[0], BALL_X, BALL_Y, Ball, OMBRA);
xball[0] -= 1;
}
if(xball[1] != KILLED &&
xball[1]>=LEFT_LIMIT+3 && xball[1]<=RIGHT_LIMIT-7) {
DrawObject(xball[1], yball[1], BALL_X, BALL_Y, Ball, OMBRA);
xball[1] -= 1;
}
if(xball[2] != KILLED &&
xball[2]>=LEFT_LIMIT+3 && xball[2]<=RIGHT_LIMIT-7) {
DrawObject(xball[2], yball[2], BALL_X, BALL_Y, Ball, OMBRA);
xball[2] -= 1;
}
}
if(tilt_counter>=TILT_LIMIT) {
DrawObject(TILT_ON);
LoseALife();
sleep(2);
DrawBar( xbar, ybar, OMBRA, bartype);
InitLife();
}
}
void Key_Laser(void)
/* Controlla i tasti relativi allo sparo di un laser (se questo e' */
/* abilitato. */
{
if(keyboard_keypressed(SCANCODE_SPACE)==KEY_PRESSED &&
laserflag == ON && laserfired != ON) {
laserfired = ON;
if(xlaser == KILLEDLASER) xlaser = xbar + 13;
if((int)ylaser == KILLEDLASER) ylaser = (float)(ybar - 6);
}
if(laserflag == ON && laserfired == ON) {
DrawObject(xlaser, (int)ylaser, LASER_X, LASER_Y, laser, OMBRA);
ylaser -= 2.0;
if(ylaser != (float)KILLEDLASER)
DrawObject(xlaser, (int)ylaser, LASER_X, LASER_Y, laser, SOLID);
}
}
void BrickContact(int ballid)
/* Controlla l'eventuale contatto tra la palla ballid e uno dei */
/* mattoni dello stage. In caso di contatto elimina il mattone */
/* e, se presente, lascia cadere un bonus, oltra al cambio */
/* direzionale della pallina. In caso contrario la pallina */
/* continua il suo tragitto. */
/* Controlla anche un eventuale contatto tra laser e mattone. */
{
int i;
for(i=0;i<MAP_OBJECT;i++) {
if(MapObj[current_stage][i]!= FALSE) {
/* Contatto con la parte inferiore del mattone (Laser) */
if(laserfired == ON && laserflag == ON &&
xlaser >= STD_MAPX+MapXY[i].begx-4 &&
xlaser <= STD_MAPX+MapXY[i].begx+20 &&
(int)ylaser >= STD_MAPY+MapXY[i].begy+6 &&
(int)ylaser <= STD_MAPY+MapXY[i].begy+11) {
DrawObject(xlaser, (int)ylaser, LASER_X, LASER_Y, laser, OMBRA);
laserfired = OFF;
xlaser = KILLEDLASER;
ylaser = (float)KILLEDLASER;
score+=5;
DrawScore();
if(MapObj[current_stage][i] == GRAY1)
DrawBrick(STD_MAPX+MapXY[i].begx, STD_MAPY+MapXY[i].begy, GRAY1);
else {
MapObj[current_stage][i] = OMBRA;
DrawBrick(STD_MAPX+MapXY[i].begx, STD_MAPY+MapXY[i].begy,OMBRA);
wincounter[current_stage] -= 1;
Bonus[i].touched = TRUE;
}
}
/* Contatto con la parte superiore del mattone */
if(xball[ballid] >= STD_MAPX+MapXY[i].begx-5 &&
xball[ballid]+2 <= STD_MAPX+MapXY[i].begx+15 &&
yball[ballid]+5 >= STD_MAPY+MapXY[i].begy-3 &&
yball[ballid]+5 <= STD_MAPY+MapXY[i].begy+3) {
score += 5;
DrawScore();
if(MapObj[current_stage][i]==GRAY1)
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy, GRAY1);
else {
MapObj[current_stage][i] = OMBRA;
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy,OMBRA);
wincounter[current_stage] -= 1;
Bonus[i].touched = TRUE;
}
switch(direction[ballid]) {
case 0 : direction[ballid] = 11; break;
case 1 : direction[ballid] = 0; break;
case 2 : direction[ballid] = 1; break;
case 3 : direction[ballid] = 2; break;
case 4 : direction[ballid] = 1; break;
case 5 : direction[ballid] = 0; break;
case 6 : direction[ballid] = 11; break;
case 7 : direction[ballid] = 10; break;
case 8 : direction[ballid] = 9; break;
case 9 : direction[ballid] = 10; break;
case 11 : direction[ballid] = 0; break;
}
}
/* Contatto con la parte inferiore del mattone */
else if(xball[ballid]+3 >= STD_MAPX+MapXY[i].begx-4 &&
xball[ballid]+2 <= STD_MAPX+MapXY[i].begx+15 &&
yball[ballid] >= STD_MAPY+MapXY[i].begy+4 &&
yball[ballid] <= STD_MAPY+MapXY[i].begy+10) {
score+=5;
DrawScore();
if(MapObj[current_stage][i]==GRAY1)
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy, GRAY1);
else {
MapObj[current_stage][i] =OMBRA;
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy,OMBRA);
wincounter[current_stage] -= 1;
Bonus[i].touched = TRUE;
}
switch(direction[ballid]) {
case 0 : direction[ballid] = 5; break;
case 1 : direction[ballid] = 4; break;
case 2 : direction[ballid] = 3; break;
case 3 : direction[ballid] = 4; break;
case 4 : direction[ballid] = 5; break;
case 5 : direction[ballid] = 6; break;
case 6 : direction[ballid] = 7; break;
case 7 : direction[ballid] = 4; break;
case 8 : direction[ballid] = 6; break;
case 9 : direction[ballid] = 8; break;
case 10 : direction[ballid] = 7; break;
case 11 : direction[ballid] = 6; break;
}
}
/* Contatto con la parte sinistra del mattone */
else if(xball[ballid]+5>=STD_MAPX+MapXY[i].begx-3 &&
xball[ballid]+5<=STD_MAPX+MapXY[i].begx+7 &&
yball[ballid]+3>=STD_MAPY+MapXY[i].begy &&
yball[ballid]+2<=STD_MAPY+MapXY[i].begy+8) {
score+=5;
DrawScore();
if(MapObj[current_stage][i]==GRAY1)
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy, GRAY1);
else {
MapObj[current_stage][i] = OMBRA;
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy,OMBRA);
wincounter[current_stage] -= 1;
Bonus[i].touched = TRUE;
}
switch(direction[ballid]) {
case 0 : direction[ballid] = 11; break;
case 1 : direction[ballid] = 10; break;
case 2 : direction[ballid] = 9; break;
case 3 : direction[ballid] = 8; break;
case 4 : direction[ballid] = 7; break;
case 5 : direction[ballid] = 6; break;
case 6 : direction[ballid] = 7; break;
case 7 : direction[ballid] = 8; break;
case 8 : direction[ballid] = 9; break;
case 9 : direction[ballid] = 8; break;
case 10 : direction[ballid] = 9; break;
case 11 : direction[ballid] = 10; break;
}
}
/* Contatto con la parte destra del mattone */
else if(xball[ballid] >= STD_MAPX+MapXY[i].begx+8 &&
xball[ballid] <= STD_MAPX+MapXY[i].begx+18 &&
yball[ballid]+3 >= STD_MAPY+MapXY[i].begy &&
yball[ballid]+2 <= STD_MAPY+MapXY[i].begy+8) {
score += 5;
DrawScore();
if(MapObj[current_stage][i]==GRAY1)
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy, GRAY1);
else {
MapObj[current_stage][i] = OMBRA;
DrawBrick(STD_MAPX+MapXY[i].begx,STD_MAPY+MapXY[i].begy, OMBRA);
wincounter[current_stage] -= 1;
Bonus[i].touched = TRUE;
}
switch(direction[ballid]) {
case 0 : direction[ballid] = 1; break;
case 1 : direction[ballid] = 2; break;
case 2 : direction[ballid] = 3; break;
case 3 : direction[ballid] = 0; break;
case 4 : direction[ballid] = 2; break;
case 5 : direction[ballid] = 1; break;
case 6 : direction[ballid] = 5; break;
case 7 : direction[ballid] = 4; break;
case 8 : direction[ballid] = 3; break;
case 9 : direction[ballid] = 2; break;
case 10 : direction[ballid] = 1; break;
case 11 : direction[ballid] = 0; break;
}
}
}
BonusContact(i);
}
}
void BarContact(int ballid)
/* Controlla l'eventuale contatto tra la pallina (ballid) e */
/* la barra usata dall'utente per ribatterla. In caso di contatto */
/* la palla viene respinta, cambiando quindi direzione. */
/* Il cambio direzionale e' influenzato diversamente se la barra */
/* e' immobile oppure in movimento, rendendo cosi' piu' vario */
/* il tipo di risposta della pallina al contatto con la barra. */
{
if(xball[ballid]>=xbar &&
xball[ballid]<=xbar+barwidth &&
yball[ballid]>=ybar-7 &&
yball[ballid]<=240) {
DrawBar(xbar,ybar,SOLID, bartype);
if(leftpress == TRUE) {
switch(direction[ballid]) {
case 3 : direction[ballid] = 1; break;
case 4 : direction[ballid] = 2; break;
case 5 : direction[ballid] = 2; break;
case 6 : direction[ballid] = 10; break;
case 7 : direction[ballid] = 9; break;
case 8 : direction[ballid] = 11; break;
}
}
if(rightpress == TRUE) {
switch(direction[ballid]) {
case 3 : direction[ballid] = 0; break;
case 4 : direction[ballid] = 2; break;
case 5 : direction[ballid] = 1; break;
case 6 : direction[ballid] = 10; break;
case 7 : direction[ballid] = 9; break;
case 8 : direction[ballid] = 9; break;
}
}
if(leftpress == FALSE && rightpress == FALSE) {
switch(direction[ballid]) {
case 3 : direction[ballid] = 2; break;
case 4 : direction[ballid] = 1; break;
case 5 : direction[ballid] = 0; break;
case 6 : direction[ballid] = 11; break;
case 7 : direction[ballid] = 10; break;
case 8 : direction[ballid] = 9; break;
}
}
}
}
void BorderContact(int ballid)
/* Controlla il contatto della pallina (ballid) con i bordi */
/* gestendo il cambio direzionale. */
{
/* Contatto con il bordo sinistro */
if(xball[ballid]<=LEFT_LIMIT+3) {
switch(direction[ballid]) {
case 6 : direction[ballid] = 5; break;
case 7 : direction[ballid] = 4; break;
case 8 : direction[ballid] = 3; break;
case 9 : direction[ballid] = 2; break;
case 10 : direction[ballid] = 1; break;
case 11 : direction[ballid] = 0; break;
}
}
/* Contatto con il bordo destro */
if(xball[ballid]>=RIGHT_LIMIT-7) {
switch(direction[ballid]) {
case 0 : direction[ballid] = 11; break;
case 1 : direction[ballid] = 10; break;
case 2 : direction[ballid] = 9; break;
case 3 : direction[ballid] = 8; break;
case 4 : direction[ballid] = 7; break;
case 5 : direction[ballid] = 6; break;
}
}
/* Contatto con la parte superiore */
if(yball[ballid]<=UPPER_LIMIT+3) {
switch(direction[ballid]) {
case 0 : direction[ballid] = 5; break;
case 1 : direction[ballid] = 4; break;
case 2 : direction[ballid] = 3; break;
case 9 : direction[ballid] = 8; break;
case 10 : direction[ballid] = 7; break;
case 11 : direction[ballid] = 6; break;
}
}
}
void LoseALife(void)
/* Gestisce la perdita di un credit controllando anche il numero di */
/* credit restanti e generando cosi' soluzioni diverse per ogni caso. */
{
if(numlives>=1) {
numlives -= 1;
DrawLives(numlives);
sleep(2);
DrawBar(xbar, ybar, OMBRA, bartype);
InitLife();
}
if(numlives == 0) {
DrawObject(GAME_OVER_ON);
sleep(2);
keyboard_waitforupdate();
RestoreMaps();
RestoreWinCounter();
InitGame();
}
}
void BonusContact(int counter)
/* Gestisce la caduta dei bonus. */
/* Controlla eventuali conflitti tra bonus in caduta e mattoni */
/* non ancora toccati dalla pallina, ridisegnando il mattone */
/* in conflitto che altrimenti sarebbe sovrascritto dal bonus, */
/* ma rimarebbe attivo e non visto dall'utente. */
/* Controlla se il bonus in caduta viene "catturato" dalla */
/* barra dell'utente, generando cosi' le conseguenze relative */
/* a quel tipo di bonus. */
{
int i;
/* Gestione dei conflitti tra mattoni e bonus in caduta */
for(i=counter;i<MAP_OBJECT;i+=16) {
if(Bonus[counter].touched==TRUE && Bonus[counter].type!=NB &&
((int)(Bonus[counter].y+8.0) >= MapXY[i].begy+STD_MAPY ||
(int)Bonus[counter].y <= MapXY[i].begy+STD_MAPY+8) &&
(int)Bonus[counter].y <= MapXY[MAP_OBJECT-1].begy+8 &&
MapObj[current_stage][i] != OMBRA)
DrawBrick(MapXY[i].begx+STD_MAPX,
MapXY[i].begy+STD_MAPY,
MapObj[current_stage][i]);
}
/* Gestione dello scrolling dei bonus */
if(Bonus[counter].touched==TRUE && Bonus[counter].type!=NB &&
(int)Bonus[counter].y<=240 && Bonus[counter].permission==TRUE) {
switch(ballingame) {
case 1 : Bonus[counter].y += 1.0; break;
case 2 : Bonus[counter].y += 0.5; break;
case 3 : Bonus[counter].y += 0.34; break;
}
DrawBonus(Bonus[counter].x,(int)Bonus[counter].y-1,
Bonus[counter].type,OMBRA);
DrawBonus(Bonus[counter].x,(int)Bonus[counter].y,
Bonus[counter].type,SOLID);
}
/* Contatto Barra/Bonus e relative conseguenze */
if((int)Bonus[counter].y+8 >= ybar && (int)Bonus[counter].y<=ybar+8 &&
Bonus[counter].x+16 >= xbar && Bonus[counter].x<=xbar+barwidth &&
Bonus[counter].permission == TRUE) {
Bonus[counter].permission = FALSE;
DrawBonus(Bonus[counter].x, (int)Bonus[counter].y,
Bonus[counter].type, OMBRA);
DrawBar(xbar, ybar, SOLID, bartype);
switch(Bonus[counter].type) {
case EXTEND : if(laserflag == ON) laserflag = OFF;
Extend();
score += 25;
break;
case REDUCE : if(laserflag == ON) laserflag = OFF;
Reduce();
score += 25;
break;
case LIVES : if(numlives < 5) numlives+=1;
DrawLives(numlives);
score += 100;
break;
case BALL_LOCK : BallLock();
score += 20;
break;
case MULTI_BALL : if(multiball==OFF) {
ballingame = 3;
multiball = ON;
MultiBall();
};
score += 20;
break;
case SPEED_DOWN : if(velocita > 0 ) velocita -= 1;
score += 15;
break;
case LASER : DrawBar(xbar, ybar, OMBRA, bartype);
bartype = NORMAL;
DrawBar(xbar, ybar, SOLID, bartype);
lasercounter = LASER_TIME;
laserflag = ON;
score += 25;
break;
case REVERSE : reversecounter = REVERSE_TIME;
reverseflag = ON;
score += 10;
break;
}
DrawScore();
}
}
void MultiBall(void)
{
xball[1] = xball[0];
yball[1] = yball[0];
xball[2] = xball[0];
yball[2] = yball[0];
switch(direction[0]) {
case 0 : direction[1] = 11;
direction[2] = 1;
break;
case 1 : direction[1] = 0;
direction[2] = 2;
break;
case 2 : direction[1] = 1;
direction[2] = 3;
break;
case 3 : direction[1] = 2;
direction[2] = 4;
break;
case 4 : direction[1] = 3;
direction[2] = 5;
break;
case 5 : direction[1] = 4;
direction[2] = 6;
break;
case 6 : direction[1] = 5;
direction[2] = 7;
break;
case 7 : direction[1] = 6;
direction[2] = 8;
break;
case 8 : direction[1] = 7;
direction[2] = 9;
break;
case 9 : direction[1] = 8;
direction[2] = 10;
break;
case 10 : direction[1] = 9;
direction[2] = 11;
break;
case 11 : direction[1] = 10;
direction[2] = 0;
break;
}
}
void ActiveBall(void)
{
int i;
for(i=0;i<MAX_BALLS;i++) {
if(xball[i]!=KILLED && yball[i]!=KILLED) {
xball[0] = xball[i];
yball[0] = yball[i];
direction[0] = direction[i];
}
}
}
void BallLock(void)
/* Gestisce le conseguenze del bonus BallLock (Blocco della Palla). */
{
int i;
DrawBorder();
KillLaser();
KillTouchedBonus();
for(i=0;i<MAX_BALLS;i++)
DrawObject(xball[i],yball[i],BALL_X,BALL_Y,Ball,OMBRA);
DrawBar( xbar, ybar, OMBRA, bartype);
DrawMap();
InitLife();
}
void Extend(void)
/* Estende la barra-utente controllando che non sia troppo vicina */
/* al bordo destro. In questo caso la sposta un po' piu' a sinistra. */
/* Se non fosse spostata accadrebbe che l'estensione della barra */
/* sovrascriverebbe il bordo destro. */
{
KillLaser();
DrawBar(xbar, ybar, OMBRA, bartype);
if(xbar >= 232) xbar = 232-5;
bartype = EXTEND;
DrawBar(xbar, ybar, SOLID, bartype);
}
void Reduce(void)
/* Riduce la barra-utente. Non ha bisogno di controlli di posizione */
/* come nel caso dell'estensione. */
{
KillLaser();
DrawBar(xbar, ybar, OMBRA, bartype);
bartype = REDUCE;
DrawBar(xbar, ybar, SOLID, bartype);
}
void UpdateCounter(void)
/* Aggiorna i contatori di validita' dei bonus ed il contatore */
/* di fine-stage, controllando il raggiungimento dei rispettivi limiti */
/* imposti e generandone le conseguenze. */
{
if(ball_lock == TRUE) lock_counter -= 1;
if(lock_counter == 0) ball_lock = FALSE;
if(laserflag == ON) lasercounter -= 1;
if(lasercounter == 0) {
KillLaser();
}
if(reverseflag == ON) reversecounter -= 1;
if(reversecounter == 0) reverseflag = OFF;
speedcounter += 1;
if(speedcounter == INT_LIMIT - 1) speedcounter = 0;
if(speedcounter >= SPEED_CHANGE && velocita < VELOCITA) {
speedcounter = 0;
velocita += 1;
}
if(wincounter[current_stage] == 0) {
sleep(2);
if(current_stage < STAGE_ID) current_stage += 1;
if(current_stage == STAGE_ID) {
system("clear");
printf("\n\n COMPLIMENTI! Hai portato a termine la tua missione\n");
printf(" totalizzando %d punti\n", score);
exit(0);
}
DrawBar(xbar, ybar, SOLID, bartype);
InitStage();
}
}
void InitGame(void)
{
current_stage = 0;
numlives = 3;
score = 0;
BackupMaps();
BackupWinCounter();
DrawObject(GAME_OVER_OFF);
InitLife();
InitStage();
}
void InitLife(void)
{
xbar = 100;
ybar = 230;
xball[0] = 118;
yball[0] = 224;
xball[1] = KILLED;
yball[1] = KILLED;
xball[2] = KILLED;
yball[2] = KILLED;
direction[0] = 0;
direction[1] = 0;
direction[2] = 0;
ballingame = 1;
multiball = OFF;
ball_lock = TRUE;
leftpress = FALSE;
rightpress = FALSE;
velocita = 0;
bartype = NORMAL;
laserflag = OFF;
lasercounter = 0;
laserfired = OFF;
reverseflag = OFF;
reversecounter = 0;
speedcounter = 0;
tilt_counter = 0;
lock_counter = LOCK_TIME;
KillTouchedBonus();
DrawBorder();
DrawObject(TILT_OFF);
DrawBar(xbar, ybar, SOLID, NORMAL);
DrawObject(xball[0],yball[0],BALL_X, BALL_Y, Ball, SOLID);
}
void InitStage(void)
{
int i;
KillTouchedBonus();
InitBonus();
AssignBonus();
for(i=0;i<MAX_BALLS;i++)
DrawObject(xball[i], yball[i], BALL_X, BALL_Y, Ball, OMBRA);
DrawObject(xlaser, (int)ylaser, LASER_X, LASER_Y, laser, OMBRA);
DrawBar(xbar, ybar, OMBRA, bartype);
DrawMap();
DrawSfondo();
InitLife();
}
void AssignBonus(void)
/* Assegna a ciascun mattone, che non sia indistruttibile (cond. GRAY1) */
/* o inconsistente (cond. OMBRA), un bonus di tipo diverso a seconda */
/* del risultato generato dalla funzione rand(), collegata alla funzione */
/* srand(). */
{
int i, val, stime;
long ltime;
ltime = time(NULL);
stime = (unsigned)ltime/2;
srand(stime); /* Uso di srand() per variare continuamente */
/* i risultati delle chiamate a rand(). */
for(i=0;i<MAP_OBJECT;i++) {
if(MapObj[current_stage][i]!=OMBRA ||
MapObj[current_stage][i]!=GRAY1) {
val = 1+(int)(100.0*rand()/(RAND_MAX+1.0));
if(val>=99 && val <=100) Bonus[i].type = LIVES;
if(val>=97 && val < 99) Bonus[i].type = REVERSE;
if(val>=94 && val < 97) Bonus[i].type = LASER;
if(val>=91 && val < 94) Bonus[i].type = BALL_LOCK;
if(val>=88 && val < 91) Bonus[i].type = MULTI_BALL;
if(val>=85 && val < 88) Bonus[i].type = EXTEND;
if(val>=82 && val < 85) Bonus[i].type = REDUCE;
if(val>=79 && val < 82) Bonus[i].type = SPEED_DOWN;
if(val>=0 && val < 79) Bonus[i].type = NB;
Bonus[i].touched = FALSE;
Bonus[i].permission = TRUE;
if(Bonus[i].type!=NB) {
Bonus[i].x = MapXY[i].begx + STD_MAPX;
Bonus[i].y = (float)(MapXY[i].begy + STD_MAPY);
}
}
}
}
/* ----------------------------------------------------------------------- */
/* Le seguenti funzioni di BackUp e Restore sono utilizzate per */
/* sdoppiare in memoria i dati relativi alle mappe degli stage */
/* ed ai win-counter (contatori-vittoria) in modo che, i valori modificati */
/* durante una partita, possano essere ripristinati per eseguire una nuova */
/* partita. */
/* ----------------------------------------------------------------------- */
void BackupWinCounter(void)
{
int i;
for(i=0;i<STAGE_ID;i++)
wincountbackup[i] = wincounter[i];
}
void RestoreWinCounter(void)
{
int i;
for(i=0;i<STAGE_ID;i++)
wincounter[i] = wincountbackup[i];
}
void BackupMaps(void)
{
int i,j;
for(j=0;j<STAGE_ID;j++) {
for(i=0;i<MAP_OBJECT;i++) {
MapBackup[j][i] = MapObj[j][i];
}
}
}
void RestoreMaps(void)
{
int i,j;
for(j=0;j<STAGE_ID;j++) {
for(i=0;i<MAP_OBJECT;i++) {
MapObj[j][i] = MapBackup[j][i];
}
}
}
void KillTouchedBonus(void)
/* Elimina eventuali bonus in caduta che vanno persi in alcuni casi, */