Repository navigation
Expand file tree
/
Copy pathSI.cpp
More file actions
2737 lines (2357 loc) · 85.3 KB
/
Copy pathSI.cpp
File metadata and controls
2737 lines (2357 loc) · 85.3 KB
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
// ===========================================================================
// Project: Space Invaders
// File: SI.cpp
// Author: Mirza Kolakovic
// Date: April 6, 2001.
// ===========================================================================
// ===========================================================================
//
// File: SI.CPP
//
// Description: This file is the main source file for Space InvaderZ.
//
//
// ===========================================================================
#include "SI.h"
#include "debug.h"
// ---------------------------------------------------------------------------
//
// Name: cdx_Init
//
// Function: handles initialization of the CDX objects
//
//
// Parameters: None.
//
// Return value: BOOL.
//
// ---------------------------------------------------------------------------
BOOL cdx_Init( void )
{
int i,j;
/***
Create the CDXSreen object
***/
Screen = new CDXScreen();
if (Screen==NULL)
CDXError( NULL , "Could not create CDXScreen object" );
/***
Enable triple buffering ?
***/
Screen->SetTripleBuffering(TripleBuffer);
/***
Create Back buffer in Video RAM ? (too slow)
***/
Screen->CreateBackBufferInVRAM( BackBufferInVRAM );
/***
Start app
***/
StartVideoMode();
/***
Open Debug file?
***/
if (DebugFlag )
fdebug = fopen("debug.txt","a+");
/***
Create Sound Object
***/
Sound = new CDXSound;
Sound->Create(g_hWnd);
/***
Create Direct Input Object
***/
Input = new CDXInput();
Input->Create(g_hInst, g_hWnd );
/***
Load the tiles
***/
TitleT = new CDXTile();
if( TitleT->Create( Screen , TITLE_BMP , 350 , 150 , 1 , CDXMEM_SYSTEMONLY ) == FALSE)
{
CDXError( Screen , "Could not load tiles from file TITLE.BMP" );
}
UfoT = new CDXTile();
if( UfoT->Create( Screen , UFO_BMP , 32 , 32 , 2 , CDXMEM_SYSTEMONLY ) == FALSE)
{
CDXError( Screen , "Could not load tiles from file UFO_BMP" );
}
ShipT = new CDXTile();
if( ShipT->Create( Screen , SHIP_BMP , 32 , 32 , 2 , CDXMEM_SYSTEMONLY ) == FALSE)// ??? ADD EXPLOSION
{
CDXError( Screen , "Could not load tiles from file SHIP_BMP" );
}
SiT = new CDXTile();
if( SiT->Create( Screen , SI_BMP , 32 , 32, 12 , CDXMEM_SYSTEMONLY ) == FALSE)
{
CDXError( Screen , "Could not load tiles from file SI_BMP" );
}
BulletT= new CDXTile();
if( BulletT->Create( Screen , BULLET_BMP , 16 , 16, 3 , CDXMEM_SYSTEMONLY ) == FALSE) // ??? ADD BOMB AND TORPEDO AND EXPLOSION
{
CDXError( Screen , "Could not load tiles from file BULLET_BMP" );
}
/***
Create Sprites
***/
Title = new CDXSprite();
Title->Create(TitleT);
Title->SetColorKey(0);
UFO = new CDXSprite();
UFO->Create(UfoT);
UFO->SetColorKey(0);
Ship = new CDXSprite();
Ship->Create(ShipT);
SI = new CDXSprite();
SI->Create(SiT);
SI->SetColorKey(0);
LivesSSprite = new CDXSprite();
LivesSSprite->Create(ShipT);
LivesSSprite->SetScale ( 0.5 ); // SCALE IT
LivesSSprite->SetColorKey(0);
LivesSSprite->SetFrame(1);
/***
Create bullets
***/
for( i=0; i < MAX_BULLETS; i++)
{
Bullets[i] = 0;
Bullets[i] = new CDXSprite();
Bullets[i]->Create(BulletT);
}
/***
Create bombs
***/
for( i=0; i < MAX_BOMBS; i++)
{
Bombs[i] = 0;
Bombs[i] = new CDXSprite();
Bombs[i]->Create(BulletT);
}
/***
Create Invaders
***/
for( j=0; j < (HEAD_INVADERS+ANTENA_INVADERS+CONE_INVADERS); j++)
{
SInvaders[j] = 0;
SInvaders[j] = new CDXSprite();
SInvaders[j]->Create( SiT );
}
/***
Create torpedos
***/
for( i=0; i < MAX_TORPEDOS; i++)
{
Torpedos[i] = 0;
Torpedos[i] = new CDXSprite();
Torpedos[i]->Create(BulletT);
}
/***
Load Sounds
***/
BulletSound = new CDXSoundBuffer();
BulletSound->Load( Sound, BULLET_WAV, 5 );
Step1 = new CDXSoundBuffer();
Step1->Load( Sound, STEP1_WAV, 5 );
Step2 = new CDXSoundBuffer();
Step2->Load( Sound, STEP2_WAV, 5 );
Step3 = new CDXSoundBuffer();
Step3->Load( Sound, STEP3_WAV, 5 );
Step4 = new CDXSoundBuffer();
Step4->Load( Sound, STEP4_WAV, 5 );
UFOSound = new CDXSoundBuffer();
UFOSound->Load( Sound, UFO_WAV, 5 );
ExplodeSI = new CDXSoundBuffer();
ExplodeSI->Load( Sound, SI_EXPLODE_WAV, 5 );
ExplodeUFO = new CDXSoundBuffer();
ExplodeUFO->Load( Sound, UFO_EXPLODE_WAV, 2 );
ExplodeShip = new CDXSoundBuffer();
ExplodeShip->Load( Sound, SHIP_EXPLODE_WAV,2 ); // ?????????????????
/***
Create Stars
***/
Stars = new CDXStarField(Screen);
Stars->m_Towards = TRUE;
Stars->m_Flashing = FALSE;
Stars->m_Colored = FALSE;
Stars->Create();
/***
Create Menus
***/
CreateMenus();
// Set active menu
CurrentMenu = MainMenu;
/***
Create Credits Scroll Text
***/
CreditsFont = new CDXBitmapFont( Screen , "Westminster" , 22 , YELLOW );
CreditsScroll = new CDXVertTextScroller( Screen , &CreditsClipRect ,ScrollText, CDX_CENTERJUSTIFY , 4 , CreditsFont );
/***
Get default Screen Palette
***/
Screen->GetPalette(0, 256, pe); //????????????????????
/***
Create Timers
***/
PCScrollTimer = new CDXHPC();
PCFrameDuration = new CDXHPC();
PCShip = new CDXHPC();
PCFireBullet = new CDXHPC();
PCBullet = new CDXHPC();
PCSIMove = new CDXHPC();
PCSIExplosion = new CDXHPC();
PCUFOExplosion = new CDXHPC();
PCBulletExplode = new CDXHPC();
PCShipExplode = new CDXHPC();
PCStars = new CDXHPC();
PCGameOver = new CDXHPC(); //?????????????????????
PCNextLevel = new CDXHPC();
/***
Start counters
***/
PCShip->Start();
PCStars->Start();
PCBullet->Start();
PCSIMove->Start();
PCNextLevel->Start();
PCGameOver->Start();
PCFireBullet->Start();
PCSIExplosion->Start();
PCShipExplode->Start();
PCScrollTimer->Start();
PCUFOExplosion->Start();
PCFrameDuration->Start();
PCBulletExplode->Start();
/***
Load HiScore from a file.
***/
HiScore = ReadHighScore();
bPlayThunder = true; // Play Thunder ???????????????
bRestorePalette = true; // Show stars and restore palette
// Seed the random-number generator with current time
srand( (unsigned)time( NULL ) );
/***
Set GameState & GO !!!!!!!!!!!
***/
GameState = SPLASH;
return TRUE;
}/* ==== End of: void cdx_Init( void ) =================================== */
// ---------------------------------------------------------------------------
//
// Name: cdx_DeInit
//
// Function: handles cleanup of CDX objects
//
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void cdx_DeInit( void )
{
int i;
if( BulletSound )
{
BulletSound->Stop();
delete BulletSound;
}
else
if(Step1)
{
Step1->Stop();
delete Step1;
}
else
if(Step2)
{
Step2->Stop();
delete Step2;
}
else
if(Step3)
{
Step3->Stop();
delete Step3;
}
else
if(Step4)
{
Step4->Stop();
delete Step4;
}
else
if(ExplodeSI)
{
ExplodeSI->Stop();
delete ExplodeSI;
}
else
if(ExplodeUFO)
{
ExplodeUFO->Stop();
delete ExplodeUFO;
}
else
if(ExplodeShip)
{
ExplodeShip->Stop();
delete ExplodeShip;
}
else
if(UFOSound)
{
UFOSound->Stop();
delete UFOSound;
}
for( i=0; i < MAX_BULLETS; i++)
delete Bullets[i];
for( i=0; i < MAX_BOMBS; i++)
delete Bombs[i];
for( i=0; i < MAX_TORPEDOS; i++)
delete Torpedos[i];
for( i=0; i < (HEAD_INVADERS+ANTENA_INVADERS+CONE_INVADERS); i++ )
delete SInvaders[i];
SAFEDELETE( PCScrollTimer );
SAFEDELETE( LivesSSprite );
SAFEDELETE( BulletT );
SAFEDELETE( Ship );
SAFEDELETE( SI );
SAFEDELETE( Title );
SAFEDELETE( TitleT );
SAFEDELETE( SiT );
SAFEDELETE( ShipT );
SAFEDELETE( UfoT );
SAFEDELETE( PCBulletExplode);
SAFEDELETE( PCUFOExplosion );
SAFEDELETE( PCSIExplosion );
SAFEDELETE( PCSIMove );
SAFEDELETE( PCBullet );
SAFEDELETE( PCFireBullet );
SAFEDELETE( PCAppTiming );
SAFEDELETE( PCFrameDuration);
SAFEDELETE( PCShip );
SAFEDELETE( PCShipExplode );
SAFEDELETE( PCGameOver );
SAFEDELETE( PCStars );
SAFEDELETE( Sound );
SAFEDELETE( Stars );
SAFEDELETE( Input );
SAFEDELETE( CreditsFont );
SAFEDELETE( CreditsScroll );
SAFEDELETE( MainMenu );
SAFEDELETE( OptionsMenu );
SAFEDELETE( Screen );
// Close the Debug file
if (DebugFlag)
fclose(fdebug);
// Save Options
WriteOptions( (((unsigned)g_bFullScreen&0xff)<<8) | ((unsigned)SoundFlag&0xff) );
// Write Exir status 1 = "Success"
WriteExitStatus( 0x1 );
}/* ==== End of: void cdx_DeInit( void ) ================================= */
// ---------------------------------------------------------------------------
//
// Name: cdx_DoFrame
//
// Function: Main game loop! Here we decide what part of the game we want
// to show, depending on the GameState variable. (State Machine)
//
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void cdx_DoFrame()
{
// Get background surface
Back = Screen->GetBack();
switch( GameState )
{
case SPLASH:
DisplaySplash();
break;
case INTRO:
GameIntro();
break;
case SETUP:
DisplaySetup();
break;
case RESET:
ResetGame();
break;
case PLAY_GAME:
PlayGame();
break;
case PAUSE:
PauseGame();
break;
case NEXT_LEVEL:
NextLevel();
break;
case GAME_OVER:
GameOver();
break;
default:
break;
}
}/* ==== End of: void cdx_DoFrame( void ) ================================ */
// ---------------------------------------------------------------------------
//
// Name: DisplaySplash
//
// Function: Displays Logo and Plays Thunder Sound. This is a
// GameState function. GameState = SPLASH
//
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void DisplaySplash( void )
{
if ( !g_bFullScreen )
GameState = INTRO;
else
GameState = INTRO; //should be SPLASH ????????????
}/* ==== End of: void DisplaySplash( void ) ============================== */
// ---------------------------------------------------------------------------
//
// Name: GameIntro
//
// Function: Displays Intro screen with points and Title. This is a
// GameState function. GameState = INTRO
//
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void GameIntro( void )
{
HDC dc;
UINT DefaultMode;
Back->Fill(0); // Clear background
/***
Put some Stars
***/
if( bRestorePalette ) // Only the first time afer the Logo scene !
{
bRestorePalette = false;
Screen->Flip();
// Screen->SetPalette(0, 256, pe); // Restore our palette.
}
if( PCStars->GetValue() >= STAR_DELAY )
{
Stars->Update(); // Update stars
PCStars->Reset();
}
else
{
Stars->DrawStars();
}
/***
Display Text
***/
dc = Back->GetDC();
DefaultMode = GetTextAlign(dc);
SetTextAlign(dc, TA_CENTER | VTA_CENTER);
// Back->SetFont(); //??????????????????????????????????? what font
Back->TextXY(ScreenWidth/2, ScreenHeight-20, WHITE, MirzaKingText); // TextXY(x, y, color, string)
int startatY = ScreenHeight/2-100;
for( int i=0; i<4; i++ )
{
Back->TextXY(ScreenWidth/2, startatY, WHITE, PointsText[i]);
startatY += 50;
}
/***
Draw menu
***/
CurrentMenu->Draw(ScreenWidth/2, ScreenHeight-140); //X,Y
SetTextAlign(dc, DefaultMode);
Back->ReleaseDC();
/***
Draw Title
***/
Title->SetPosX(ScreenWidth/2-175);
Title->SetPosY(0);
Title->Draw(Back , 0 , 0 , CDXBLT_TRANS );
/***
Draw Invaders & ufo
***/
SI->SetFrame( 8 ); // Green Invader
SI->SetPos(ScreenWidth/2-85,ScreenHeight/2+45);
SI->Draw(Back , 0 , 0 , CDXBLT_TRANS );
SI->SetFrame( 4 );
SI->SetPos(ScreenWidth/2-85,ScreenHeight/2-5);
SI->Draw(Back , 0 , 0 , CDXBLT_TRANS );
SI->SetFrame( 9 ); //Red Invader
SI->SetPos(ScreenWidth/2-85,ScreenHeight/2-55);
SI->Draw(Back , 0 , 0 , CDXBLT_TRANS );
SI->SetFrame( 10 );//UFO
SI->SetPos(ScreenWidth/2-85,ScreenHeight/2-105);
SI->Draw(Back , 0 , 0 , CDXBLT_TRANS );
Screen->Flip();
}/* ==== End of: void GameIntro( void ) ================================== */
// ---------------------------------------------------------------------------
//
// Name: DisplaySetup
//
// Function: Video mode setup menu. This is a GameState function.
// GameState = SETUP
//
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void DisplaySetup( void )
{
Back->Fill(0);
CurrentMenu->DrawCentered();
Screen->Flip();
}/* ==== End of: void DisplaySetup( void ) =============================== */
// ---------------------------------------------------------------------------
//
// Name: ResetShip
//
// Function: Rest ship's state and position.
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void ResetShip(void)
{
Ship->SetPosY( SHIP_Y ); //Position the Ship on Y-axis (constant)
Ship->SetPosX( 0 );
Ship->SetFrame(1);
Ship->SetColorKey(0);
Ship->SetState( SHIP_ACTIVE );
Ship->SetDelay(0);
}/* ==== End of: void ResetShip( void ) =============================== */
// ---------------------------------------------------------------------------
//
// Name: ResetGame
//
// Function: Sets all the variables and objects in their starting position.
// This is a GameState function. GameState = RESET
//
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void ResetGame( void )
{
/***
Reset all game variables
***/
if(GameLevel > 20)
GameLevel=0;
BulletsToFire = 1;
SIMoveDelay = 1000 - GameLevel*25;
NumberOfLiveSI = HEAD_INVADERS + ANTENA_INVADERS + CONE_INVADERS;
BulletsSinceLastUFO = NoActiveBombs = 0;
ShipdX = UFOdX = BombdY = 0.0;
SIPlayStep = 0;
SINewFrame = 1;
MoveDown = false;
/***
Set colors for collision detection !
Well, this didn't work when dispay was set for 32-bit colors
so we'll have to change startegy! We'll draw sprites and call GetPixel(x,y)
to determine color hex number.
CDX_LPDIRECTDRAWSURFACE surface = Screen->GetBack()->GetDDS();
WhitePixel = DDColorMatch( surface, WHITE );
RedPixel = DDColorMatch( surface, RED );
GreenPixel = DDColorMatch( surface, GREEN );
YellowPixel = DDColorMatch( surface, YELLOW );
***/
// Ufo
UFOSound->Stop();
UFO->SetFrame( 1 );
UFO->SetPos( 0, 0 );
UFO->Draw(Back, 0,0, CDXBLT_TRANS );
Back->Lock();
RedPixel=Back->GetPixel(10,10);//xy
Back->UnLock();
// Invader
SI->SetFrame( 1 );
SI->SetPos(0,0);
SI->Draw(Back, 0,0, CDXBLT_TRANS );
Back->Lock();
WhitePixel=Back->GetPixel(10,10);//xy
Back->UnLock();
// Shield
SI->SetFrame( 11 );//shield block
SI->SetPos(0,0);
SI->Draw(Back , 0 , 0 , CDXBLT_TRANS );
Back->Lock();
ShieldPixel=Back->GetPixel(10,10);//xy
Back->UnLock();
// Ship
Ship->SetFrame(1);
Ship->SetPos(0,0);
Ship->Draw(Back , 0 , 0 , CDXBLT_TRANS );
Back->Lock();
ShipPixel=Back->GetPixel(15,15);//xy
Back->UnLock();
/***
Reset Ship
***/
ResetShip();
/***
Reset UFO
***/
UFO->SetState( UFO_NOT_ACTIVE );
UFO->SetFrame( 1 );
UFO->SetPos( UFO_X, UFO_Y );
/***
Reset Bullets
***/
for( int i=0; i < MAX_BULLETS; i++)
{
Bullets[i]->SetFrame(1);
Bullets[i]->SetState( BULLET_NOT_ACTIVE );
Bullets[i]->SetColorKey(0);
}
/***
Reset Bombs
***/
for( i=0; i < MAX_BOMBS; i++)
{
Bombs[i]->SetState( BOMB_NOT_ACTIVE );
Bombs[i]->SetColorKey(0);
}
/***
Reset Invaders
***/
SIStartMoveOnX = 5;
SIStopMoveOnX = ScreenWidth-37; // limits when moving when to stop and change direction
SIStopMoveOnY = SHIP_Y;
SIStartDrawOnX = 75; // for initial drawing and line up
SIStartDrawOnY = 205;
SIDrawStep = 30;
int posX = 0;
for( int j=0; j < (HEAD_INVADERS+ANTENA_INVADERS+CONE_INVADERS); j++, posX++)
{
if( posX == CONE_INVADERS ) posX = 0; //CONE_INVADERS is max number per row
SInvaders[j]->SetState( SI_ACTIVE );
SInvaders[j]->SetVelX( SIVelocityRight );
SInvaders[j]->SetVelY( SIVelocityDown );
if( j < HEAD_INVADERS/2 )
{
SInvaders[j]->SetPos( SIStartDrawOnX + SIDrawStep*posX, SIStartDrawOnY ); //x,y Head Invaders 1st row
SInvaders[j]->SetFrame( 1 );
SInvaders[j]->SetType( SI_HEAD );
}
else
if( j >= HEAD_INVADERS/2 && j < HEAD_INVADERS )
{
SInvaders[j]->SetPos( SIStartDrawOnX + SIDrawStep*posX, SIStartDrawOnY-35 ); //x,y Head Invaders 2nd row (from the bottom )
SInvaders[j]->SetFrame( 1 );
SInvaders[j]->SetType( SI_HEAD );
}
else
if( j >= HEAD_INVADERS && j < HEAD_INVADERS+ANTENA_INVADERS/2)
{
SInvaders[j]->SetPos(SIStartDrawOnX + SIDrawStep*posX, SIStartDrawOnY - 70 ); //x,y Antenas, 3rd row
SInvaders[j]->SetFrame( 3 );
SInvaders[j]->SetType( SI_ANTENA );
}
else
if( j >= HEAD_INVADERS+ANTENA_INVADERS/2 && j < HEAD_INVADERS+ANTENA_INVADERS )
{
SInvaders[j]->SetPos(SIStartDrawOnX + SIDrawStep*posX, SIStartDrawOnY - 105 ); //x,y Antenas, 4th row
SInvaders[j]->SetFrame( 3 );
SInvaders[j]->SetType( SI_ANTENA );
}
else
if( j >= HEAD_INVADERS+ANTENA_INVADERS )
{
SInvaders[j]->SetPos(SIStartDrawOnX + SIDrawStep*posX, SIStartDrawOnY - 140 ); //x,y Cones, 5th row
SInvaders[j]->SetFrame( 5 );
SInvaders[j]->SetType( SI_CONE );
}
SInvaders[j]->SetColorKey( 0 );
}
/***
Reset Shields
***/
for(i=0; i<SHIELD_HEIGHT; i++)
{
for(j=0; j<SHIELD_WIDTH; j++)
{
Shield1[i][j]=Shield2[i][j]=Shield3[i][j]=Shield4[i][j]=ShieldBitMap[i][j];
}
}
/***
Reset Timers
***/
PCFrameDuration->Reset();
PCNextLevel->Reset();
GameState = NEXT_LEVEL;
}/* ==== End of: void ResetGame( void ) ================================== */
// ---------------------------------------------------------------------------
//
// Name: PlayGame
//
// Function: Function that moves the game. It will first check for the inputs,
// then move objects based on time it took to render previous frame.
// After "collision detection", background buffer will be cleared
// and updated game will be drawn.
// This is a GameState function. GameState = PLAY_GAME
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void PlayGame( void )
{
CheckInput(); // Check to see if ship is moving or bullets are fired ?
MoveObjects(); // Move still active objects to the new positions !
Back->Fill(0); // Clear background before drawing new scene !
DrawObjects(); // Draw new scene !
DisplayStatistics( ); // Display score, hi-score and number of lives !
if( DebugFlag )
Screen->Flip(TRUE,TRUE,TRUE);
else
Screen->Flip();
CollisionDetection(); // Detect collisions in the previous frame and update game !
}/* ==== End of: void PlayGame( void ) =================================== */
// ---------------------------------------------------------------------------
//
// Name: CheckInput
//
// Function: Check if keyboard pressed: update ship position, fire bullet
// or quit.
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void CheckInput( void )
{
/***
Get the time it took to render last frame
***/
DurationOfTheFrame = PCFrameDuration->GetValue();
PCFrameDuration->Reset();
/***
Update keyboard input
***/
Input->Update();
/***
Quit ?
***/
if (Input->GetKeyState(CDXKEY_ESCAPE)==CDXKEY_PRESS) // Quit playing
{
UFOSound->Stop();
// Save HiScore
if( Score > HiScore )
{
WriteHighScore( Score );
}
GameState = INTRO;
GameLevel = 0;
Score = 0;
Lives = 3;
// return out as the code below is invalid now
return;
}
/***
Move Ship if arrow key pressed and ship is still active and we are in the game
***/
if( (Ship->GetState() == SHIP_ACTIVE) && (GameState == PLAY_GAME) )
{
double MoveShipNoPixelsThisFrame = ShipVelocityX * (double)DurationOfTheFrame;
// error correction
ShipdX += MoveShipNoPixelsThisFrame - (int)MoveShipNoPixelsThisFrame;
if( ShipdX >= 1.0 )
{
MoveShipNoPixelsThisFrame++;
ShipdX -= 1.0;
}
if(Input->GetKeyState(CDXKEY_RIGHTARROW) ) // go right
{
Ship->SetPosX( Ship->GetPosX() + (int)MoveShipNoPixelsThisFrame);
if(Ship->GetPosX() > (ScreenWidth-40) )
Ship->SetPosX(ScreenWidth-40);
}
if(Input->GetKeyState(CDXKEY_LEFTARROW) ) // go left
{
Ship->SetPosX( Ship->GetPosX() - (int)MoveShipNoPixelsThisFrame );
if(Ship->GetPosX() < 0)
Ship->SetPosX(0);
}
}// if( Ship->GetState() == SHIP_ACTIVE )
/***
Fire bullets is 'space' key pressed and ship is still active
***/
int BulletsFired = 0;
if (Input->GetKeyState(CDXKEY_SPACE) && ( Ship->GetState() == SHIP_ACTIVE ) && (GameState == PLAY_GAME)) // Fire the bulet
{
if( PCFireBullet->GetValue() >= BULLET_DELAY )
{
for( int i=0; (i < MAX_BULLETS) ; i++)
{
if( Bullets[i]->GetState() == BULLET_NOT_ACTIVE ) //find a bullet that is currently inactive
{
Bullets[i]->SetState( BULLET_ACTIVE );
Bullets[i]->SetFrame(1);
BulletsFired++;
BulletsSinceLastUFO++;
if( BulletsFired == 1 )
{
Bullets[i]->SetPosX(Ship->GetPosX() + 8 );
Bullets[i]->SetPosY(Ship->GetPosY() - 8 );
Bullets[i]->SetFrame(1);
}
else
if( BulletsFired == 2 )
{
Bullets[i]->SetPosX(Ship->GetPosX() - 6 );
Bullets[i]->SetPosY(Ship->GetPosY() + 6 );
Bullets[i]->SetFrame(1);
}
else
if( BulletsFired == 3 )
{
Bullets[i]->SetPosX(Ship->GetPosX() + 22 );
Bullets[i]->SetPosY(Ship->GetPosY() + 6 );
Bullets[i]->SetFrame(1);
}
if( SoundFlag && (BulletsFired == 1))
{
BulletSound->Stop();
BulletSound->Play();
}
if( BulletsFired == BulletsToFire ) break;
}
}//for( int i=0; (i < MAX_BULLETS) ....
PCFireBullet->Reset();
// Move UFO if time to...
if( (BulletsToFire == 1 && BulletsSinceLastUFO >= ACTIVATE_UFO_ON) ||
(BulletsToFire == 3 && BulletsSinceLastUFO >= 2*ACTIVATE_UFO_ON))
{
BulletsSinceLastUFO = 0;
UFO->SetPos( UFO_X, UFO_Y );
UFO->SetState( UFO_ACTIVE );
if( SoundFlag )
{ UFOSound->Stop();
UFOSound->Play();
}
}
}//if( PCFireBullet->GetValue() >= BULLET_DELAY )
}//if (Input->GetKeyState(CDXKEY_SPACE) ) // Fire the bulet
/***
Flash Keyboard buffer
***/
Input->FlushKeyboardData();
}/* ==== End of: void CheckInput( void ) ================================= */
// ---------------------------------------------------------------------------
//
// Name: MoveObjects
//
// Function: Move all the sprites (except Ship).
//
// Parameters: None.
//
// Return value: None.
//
// ---------------------------------------------------------------------------
void MoveObjects( void )
{
int i,j;
bool bFireBombs = false;
/***