-
Notifications
You must be signed in to change notification settings - Fork 3
/
Game.asm
executable file
·886 lines (661 loc) · 15.5 KB
/
Game.asm
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
DATASEG
include "Strings.asm"
DebugBool db 0
;Files:
RandomFileName db 'Assets/Random.txt', 0
RandomFileHandle dw ?
ScoresFileName db 'Assets/Scores.txt', 0
ScoresFileHandle dw ?
ScoreTableFileName db 'Assets/ScoreTab.bmp', 0
ScoreTableFileHandle dw ?
AskSaveFileName db 'Assets/AskSave.bmp', 0
AskSaveFileHandle dw ?
MainMenuFileName db 'Assets/MainMenu.bmp',0
MainMenuFileHandle dw ?
InstructionsFileName db 'Assets/Instruct.bmp',0
InstructionsFileHandle dw ?
InvaderFileName db 'Assets/Invader.bmp',0
InvaderFileHandle dw ?
InvaderLength equ 32
InvaderHeight equ 32
ShooterFileName db 'Assets/Shooter.bmp', 0
ShooterFileHandle dw ?
ShooterLength equ 16
ShooterHeight equ 16
HeartFileName db 'Assets/Heart.bmp', 0
HeartFileHandle dw ?
HeartLength equ 16
HeartHeight equ 16
;Enemies move & status info:
InvadersMoveRightBool db ?
InvadersMovesToSideDone db ?
InvadersPrintStartLine dw ?
InvadersPrintStartRow dw ?
InvadersLeftAmount db ?
InvadersStatusArray db 24 dup (?)
InvadersLoopMoveCounter db ? ;Invaders move every 4 repeats of the game loop
ShooterLineLocation equ 149
ShooterRowLocation dw ?
ShootingLength equ 2
ShootingHeight equ 4
PlayerShootingExists db ?
PlayerShootingLineLocation dw ?
PlayerShootingRowLocation dw ?
InvadersShootingMaxAmount db ?
InvadersShootingCurrentAmount db ?
InvadersShootingLineLocations dw 10 dup (?)
InvadersShootingRowLocations dw 10 dup (?)
Score db ?
LivesRemaining db ?
Level db ?
DidNotDieInLevelBool db ?
HeartsPrintStartLine equ 182
HeartsPrintStartRow equ 125
StatsAreaBorderLine equ 175
FileReadBuffer db 320 dup (?)
;Colors:
BlackColor equ 0
GreenColor equ 30h
RedColor equ 40
BlueColor equ 54
WhiteColor equ 255
CODESEG
include "Invader.asm"
include "Procs.asm"
; -----------------------------------------------------------
; Prints the lower game area with score, lives, level, etc...
; Ben Raz
; -----------------------------------------------------------
proc PrintStatsArea
; Print border:
push 320
push 2
push StatsAreaBorderLine
push 0
push 100
call PrintColor
;Print labels:
;Level label:
xor bh, bh
mov dh, 23
mov dl, 1
mov ah, 2
int 10h
mov ah, 9
mov dx, offset LevelString
int 21h
;Score label:
xor bh, bh
mov dh, 23
mov dl, 29
mov ah, 2
int 10h
mov ah, 9
mov dx, offset ScoreString
int 21h
ret
endp PrintStatsArea
;----------------------------------
; Updates the lives shown on screen
; Ben Raz
;----------------------------------
proc UpdateLives
;Clear previous hearts:
push 64
push 14
push HeartsPrintStartLine
push HeartsPrintStartRow
push BlackColor
call PrintColor
push offset HeartFileName
push offset HeartFileHandle
call OpenFile
;Print amount of lifes remaining:
xor ch, ch
mov cl, [LivesRemaining]
mov bx, HeartsPrintStartRow
@@printHeart:
push bx
push cx
push [HeartFileHandle]
push HeartLength
push HeartHeight
push HeartsPrintStartLine
push bx
push offset FileReadBuffer
call PrintBMP
pop cx
pop bx
add bx, 20
loop @@printHeart
push [HeartFileHandle]
call CloseFile
ret
endp UpdateLives
;----------------------------------
; Updates the score shown on screen
; Ben Raz
;----------------------------------
proc UpdateScoreStat
xor bh, bh
mov dh, 23
mov dl, 36
mov ah, 2
int 10h
xor ah, ah
mov al, [Score]
push ax
call HexToDecimal
push ax
mov ah, 2
int 21h
pop dx
xchg dl, dh
int 21h
xchg dl, dh
int 21h
ret
endp UpdateScoreStat
; ---------------------------------------
; Updates the level # and the score count
; Ben Raz
; ---------------------------------------
proc UpdateStats
;Update level:
xor bh, bh
mov dh, 23
mov dl, 8
mov ah, 2
int 10h
mov ah, 2
mov dl, [byte ptr Level]
add dl, 30h
int 21h
;Update score:
call UpdateScoreStat
ret
endp UpdateStats
; ------------------------------------------------------------
; Moving invaders + player to initial location, removing shots
; Not getting back dead invaders
; Ben Raz
; ------------------------------------------------------------
proc MoveToStart
mov [byte ptr InvadersMoveRightBool], 1
mov [byte ptr InvadersMovesToSideDone], 0
mov [byte ptr InvadersLoopMoveCounter], 0
mov [byte ptr InvadersPrintStartLine], 10
mov [byte ptr InvadersPrintStartRow], 8
mov [word ptr ShooterRowLocation], 152
mov [byte ptr PlayerShootingExists], 0
mov [byte ptr InvadersShootingCurrentAmount], 0
cld
push ds
pop es
;Zero invaders shots locations:
xor ax, ax
mov di, offset InvadersShootingLineLocations
mov cx, 10
rep stosw
mov di, offset InvadersShootingRowLocations
mov cx, 10
rep stosw
ret
endp MoveToStart
; ------------------------------------------------------------
; Resetting invaders locations, shootings, etc for a new level
; Ben Raz
; ------------------------------------------------------------
proc InitializeLevel
mov [InvadersLeftAmount], 24
cmp [byte ptr Level], 1
jne @@checkLevelTwo
mov [byte ptr InvadersShootingMaxAmount], 3
jmp @@resetDidNotDieBool
@@checkLevelTwo:
cmp [byte ptr Level], 2
jne @@setLevelThree
mov [byte ptr InvadersShootingMaxAmount], 5
jmp @@resetDidNotDieBool
@@setLevelThree:
mov [byte ptr InvadersShootingMaxAmount], 7
@@resetDidNotDieBool:
mov [byte ptr DidNotDieInLevelBool], 1 ;true
call MoveToStart
cld
push ds
pop es
;Set all invaders as 'active':
mov di, offset InvadersStatusArray
mov cx, 24
mov al, 1
rep stosb
ret
endp InitializeLevel
; -----------------------------------------------
; Resetting every stat to its initial game value,
; and setting the first level
; Ben Raz
; -----------------------------------------------
proc InitializeGame
mov [byte ptr Score], 0
mov [byte ptr LivesRemaining], 3
mov [byte ptr Level], 1
call InitializeLevel
ret
endp InitializeGame
; ------------------------------------------------
; Checking if player had died from invaders' shots
; If no, returned ax = 0, if yes, ax = 1
; Ben Raz
; ------------------------------------------------
proc CheckIfPlayerDied
xor ch, ch
mov cl, [InvadersShootingCurrentAmount]
cmp cx, 0
je @@returnZero
xor si, si
@@checkShot:
;check from above:
mov ax, ShooterLineLocation
sub ax, 3
cmp ax, [InvadersShootingLineLocations + si]
ja @@checkNextShot
;check from below:
add ax, 3
add ax, 16 ;height
cmp ax, [InvadersShootingLineLocations + si]
jb @@checkNextShot
;check from left
mov ax, [ShooterRowLocation]
dec ax
cmp ax, [InvadersShootingRowLocations + si]
ja @@checkNextShot
;check from right:
add ax, 16 ;length
cmp ax, [InvadersShootingRowLocations + si]
jb @@checkNextShot
;Player killed:
mov ax, 1
ret
@@checkNextShot:
inc si
loop @@checkShot
@@returnZero:
;Player not killed:
xor ax, ax
ret
endp CheckIfPlayerDied
; ---------------------------------------------------------------
; Checks if the currently lowest line of invaders reached too low
; If true, ax = 1. If not, ax = 0.
; Ben Raz
; ---------------------------------------------------------------
proc CheckIfInvadersReachedBottom
mov cx, 8
mov bx, 16
@@checkLineTwo:
cmp [InvadersStatusArray + bx], 0
jne @@lineTwoNotEmpty
inc bx
loop @@checkLineTwo
mov cx, 8
mov bx, 8
@@checkLineOne:
cmp [InvadersStatusArray + bx], 0
jne @@lineOneNotEmpty
inc bx
loop @@checkLineOne
mov cx, 8
xor bx, bx
@@checkLineZero:
cmp [InvadersStatusArray + bx], 0
jne @@lineZeroNotEmpty
inc bx
loop @@checkLineZero
jmp @@invadersDidNotReachBottom
@@lineTwoNotEmpty:
cmp [word ptr InvadersPrintStartLine], ShooterLineLocation - 59
ja @@invadersReachedBottom
jmp @@invadersDidNotReachBottom
@@lineOneNotEmpty:
cmp [word ptr InvadersPrintStartLine], ShooterLineLocation - 39
ja @@invadersReachedBottom
jmp @@invadersDidNotReachBottom
@@lineZeroNotEmpty:
cmp [word ptr InvadersPrintStartLine], ShooterLineLocation - 19
ja @@invadersReachedBottom
@@invadersDidNotReachBottom:
xor ax, ax
ret
@@invadersReachedBottom:
mov ax, 1
ret
endp CheckIfInvadersReachedBottom
; -----------------------------------------------------------
; Initiating the game, combining the game parts together
; Handles shooter + Invaders hits and deaths, movements, etc.
; Ben Raz
; -----------------------------------------------------------
proc PlayGame
push offset InvaderFileName
push offset InvaderFileHandle
call OpenFile
push offset ShooterFileName
push offset ShooterFileHandle
call OpenFile
call InitializeGame
call ClearScreen
@@firstLevelPrint:
call PrintStatsArea
call UpdateStats
call UpdateLives
call CheckAndMoveInvaders
push [ShooterFileHandle]
push ShooterLength
push ShooterHeight
push ShooterLineLocation
push [word ptr ShooterRowLocation]
push offset FileReadBuffer
call PrintBMP
call PrintInvaders
;Print countdown to start:
mov cx, 3
mov dx, 33h
@@printCountdownNum:
push cx
push dx
mov ah, 2
xor bh, bh
mov dh, 12
mov dl, 19
int 10h
pop dx
push dx
mov ah, 2
int 21h
push 18
call Delay
pop dx
dec dx
pop cx
loop @@printCountdownNum
;clear number:
mov ah, 2
xor bh, bh
mov dh, 12
mov dl, 19
int 10h
xor dl, dl
mov ah, 2
int 21h
@@readKey:
mov ah, 1
int 16h
jz @@checkShotStatus
;Clean buffer:
push ax
xor al, al
mov ah, 0ch
int 21h
pop ax
;Check which key was pressed:
cmp ah, 1 ;Esc
je @@procEnd
cmp ah, 39h ;Space
je @@shootPressed
cmp ah, 4Bh ;Left
jne @@checkRight
cmp [word ptr ShooterRowLocation], 21
jb @@clearShot
;Clear current shooter print:
push ShooterLength
push ShooterHeight
push ShooterLineLocation
push [word ptr ShooterRowLocation]
push BlackColor
call PrintColor
sub [word ptr ShooterRowLocation], 10
jmp @@printAgain
@@checkRight:
cmp ah, 4Dh
jne @@readKey
cmp [word ptr ShooterRowLocation], 290
ja @@clearShot
;Clear current shooter print:
push ShooterLength
push ShooterHeight
push ShooterLineLocation
push [word ptr ShooterRowLocation]
push BlackColor
call PrintColor
add [word ptr ShooterRowLocation], 10
@@printAgain:
push [ShooterFileHandle]
push ShooterLength
push ShooterHeight
push ShooterLineLocation
push [word ptr ShooterRowLocation]
push offset FileReadBuffer
call PrintBMP
@@checkShotStatus:
;Check if shooting already exists in screen:
cmp [byte ptr PlayerShootingExists], 0
jne @@moveShootingUp
jmp @@clearShot
@@shootPressed:
;Check if shooting already exists in screen:
cmp [byte ptr PlayerShootingExists], 0
jne @@moveShootingUp
@@initiateShot:
;Set initial shot location:
mov ax, ShooterLineLocation
sub ax, 6
mov [word ptr PlayerShootingLineLocation], ax
mov ax, [ShooterRowLocation]
add ax, 7
mov [word ptr PlayerShootingRowLocation], ax
mov [byte ptr PlayerShootingExists], 1
jmp @@printShooting
@@moveShootingUp:
cmp [word ptr PlayerShootingLineLocation], 10
jb @@removeShot
sub [word ptr PlayerShootingLineLocation], 10
@@printShooting:
push ShootingLength
push ShootingHeight
push [word ptr PlayerShootingLineLocation]
push [word ptr PlayerShootingRowLocation]
push RedColor
call PrintColor
jmp @@clearShot
@@removeShot:
mov [byte ptr PlayerShootingExists], 0
mov [word ptr PlayerShootingLineLocation], 0
mov [word ptr PlayerShootingRowLocation], 0
@@clearShot:
push 2
call Delay
;Clear shot:
push ShootingLength
push ShootingHeight
push [word ptr PlayerShootingLineLocation]
push [word ptr PlayerShootingRowLocation]
push BlackColor
call PrintColor
cmp [byte ptr InvadersLeftAmount], 0
je @@setNewLevel
;Check if invader killed:
call CheckAndKillInvader
@@moveInvaders:
call ClearInvadersShots
call CheckAndMoveInvaders
call CheckIfInvadersReachedBottom
cmp ax, 1
je @@playerDied
call UpdateInvadersShots
call InvadersRandomShot
call printInvadersShots
;Check if player was killed:
call CheckIfPlayerDied
cmp ax, 0
je @@readKey
@@playerDied:
;Player died:
push 18
call Delay
;decrease amount of lives left, check if 0 left:
dec [byte ptr LivesRemaining]
cmp [byte ptr LivesRemaining], 0
je @@printDied
;Clear screan without stats area:
push 320
push StatsAreaBorderLine
push 0 ;line
push 0 ;row
push BlackColor
call PrintColor
mov ah, 2
xor bh, bh
mov dh, 12
mov dl, 8
int 10h
;tell user he was hit, -5 score...
mov ah, 9
mov dx, offset HitString
int 21h
; Nice blink animation for death:
mov cx, 3
@@blinkShooter:
push cx
push ShooterLength
push ShooterHeight
push ShooterLineLocation
push [word ptr ShooterRowLocation]
push BlackColor
call PrintColor
push 6
call Delay
push [word ptr ShooterFileHandle]
push ShooterLength
push ShooterHeight
push ShooterLineLocation
push [word ptr ShooterRowLocation]
push offset FileReadBuffer
call PrintBMP
push 6
call Delay
pop cx
loop @@blinkShooter
;sub 5 score if possible, if he doesn't have 5 yet, just reset to 0:
cmp [byte ptr Score], 5
jb @@resetScoreAfterDeath
sub [byte ptr Score], 5
jmp @@resetBeforeContinueAfterDeath
@@resetScoreAfterDeath:
mov [byte ptr Score], 0
@@resetBeforeContinueAfterDeath:
call MoveToStart
mov [byte ptr DidNotDieInLevelBool], 0 ;false
push 24
call Delay
call ClearScreen
jmp @@firstLevelPrint
jmp @@readKey
@@printDied:
call ClearScreen
; Print a message when game is over:
mov ah, 2
xor bh, bh
mov dh, 12
mov dl, 15
int 10h
mov ah, 9
mov dx, offset GameOverString
int 21h
;print actual score #:
mov ah, 2
xor bh, bh
mov dh, 13
mov dl, 10
int 10h
mov ah, 9
mov dx, offset YouEarnedXString
int 21h
xor ah, ah
mov al, [Score]
push ax
call HexToDecimal
push ax
mov ah, 2
int 21h
pop dx
xchg dl, dh
int 21h
xchg dl, dh
int 21h
mov ah, 9
mov dx, offset ScoreWordString
int 21h
push 54
call Delay
jmp @@procEnd
@@setNewLevel:
cmp [byte ptr DidNotDieInLevelBool], 1
jne @@SkipPerfectLevelBonus
add [byte ptr Score], 5 ;special bonus for perfect level (no death in level)
;print bonus message:
mov ah, 2
xor bh, bh
mov dh, 12
mov dl, 8
int 10h
mov ah, 9
mov dx, offset PerfectLevelString
int 21h
push 24
call Delay
call ClearScreen
@@SkipPerfectLevelBonus:
cmp [byte ptr Level], 3
je @@printWin
inc [byte ptr Level]
call InitializeLevel
call ClearScreen
jmp @@firstLevelPrint
@@printWin:
; Print win message to user (finished 3 levels):
mov ah, 9
mov dx, offset WinString
int 21h
;print actual score #:
mov ah, 2
xor bh, bh
mov dh, 13
mov dl, 15
int 10h
mov ah, 9
mov dx, offset YouEarnedXString
int 21h
xor ah, ah
mov al, [Score]
push ax
call HexToDecimal
push ax
mov ah, 2
int 21h
pop dx
xchg dl, dh
int 21h
xchg dl, dh
int 21h
mov ah, 9
mov dx, offset ScoreWordString
int 21h
@@procEnd:
push [ShooterFileHandle]
call CloseFile
push [InvaderFileHandle]
call CloseFile
ret
endp PlayGame