-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.asm
1121 lines (877 loc) · 16.3 KB
/
snake.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
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
; Snake multiplayer game
; written by quckly.ru
; v. 0.1a
org 100h
jmp Main
; Video const
VideoW = 320
VideoH = 200
VideoSegment = 0A000h
;offset = (y<<8) + (y<<6) + x;
; Constants
CellSize = 6
FieldW = 53
FieldH = 28
FieldOffsetR = (VideoW - (FieldW * CellSize))/2
FieldOffsetU = VideoH - (FieldH * CellSize)
BodySize = 1500
; Structures
struc CSnake _x, _y, _b1, _b2, _b3
{
.dirX db _x
.dirY db _y
.oldDX db _x
.oldDY db _y
.body dw _b1, _b2, _b3
dw BodySize dup(0) ; 0XXYYh
.tail dw 0
.head dw 2
.length dw 3
.size = $ - .dirX
}
; bx + CSnake.field => Snake.field
virtual at bx
Snake CSnake ?, ?, ?, ?, ?
end virtual
; Global vars
Old8:
dw 0, 0
snake1 CSnake 0, 0FFh, 2311h, 2310h, 230Fh
snake2 CSnake 0, 001h, 0F0Fh, 0F10h, 0F11h
Player1Score dw 0
Player2Score dw 0
BonusXY dw 0FFFFh ; X:Y
BonusID dw 0
GameOver dw 0
GameWinner dw 0
GameStateExit = 0
GameStateMenu = 1
GameStateGame = 2
GameStateOver = 3
GameStatePause = 4
GameState dw ?
; Random
R_seed dw 0
; Constants
MenuHelloText db 'Snake multiplayer game!', 10, 13, 'Press any key to start...$'
TextGameOver db 'Game over!'
TextGameOver.sizeof = $ - TextGameOver
TextWinnerSnake db ' snake - the winner!$'
TextRoundDraw db 'Round draw ;($'
TextPlayer1 db 'Player 1: $'
TextPlayer2 db 'Player 2: $'
BMP_EmptyCell db 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h
db 00h, 00h, 00h, 00h, 00h, 00h
; Snake 1
BMP_Snake1Head db 001h, 0E0h, 0E0h, 0E0h, 0E0h, 001h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 0E0h, 0E0h, 020h, 020h, 0E0h, 0E0h
db 0E0h, 0E0h, 020h, 020h, 0E0h, 0E0h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 001h, 0E0h, 0E0h, 0E0h, 0E0h, 001h
BMP_Snake1Tail db 001h, 0E0h, 0E0h, 0E0h, 0E0h, 001h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 0E0h, 0E0h, 0B0h, 0B0h, 0E0h, 0E0h
db 0E0h, 0E0h, 0B0h, 0B0h, 0E0h, 0E0h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 001h, 0E0h, 0E0h, 0E0h, 0E0h, 001h
BMP_Snake1Body db 001h, 0E0h, 0E0h, 0E0h, 0E0h, 001h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 0E0h, 0E0h, 022h, 022h, 0E0h, 0E0h
db 0E0h, 0E0h, 022h, 022h, 0E0h, 0E0h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 001h, 0E0h, 0E0h, 0E0h, 0E0h, 001h
; Snake 2
BMP_Snake2Head db 004h, 0E0h, 0E0h, 0E0h, 0E0h, 004h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 0E0h, 0E0h, 028h, 028h, 0E0h, 0E0h
db 0E0h, 0E0h, 028h, 028h, 0E0h, 0E0h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 004h, 0E0h, 0E0h, 0E0h, 0E0h, 004h
BMP_Snake2Tail db 004h, 0E0h, 0E0h, 0E0h, 0E0h, 004h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 0E0h, 0E0h, 0B8h, 0B8h, 0E0h, 0E0h
db 0E0h, 0E0h, 0B8h, 0B8h, 0E0h, 0E0h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 004h, 0E0h, 0E0h, 0E0h, 0E0h, 004h
BMP_Snake2Body db 004h, 0E0h, 0E0h, 0E0h, 0E0h, 004h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 0E0h, 0E0h, 02Ah, 02Ah, 0E0h, 0E0h
db 0E0h, 0E0h, 02Ah, 02Ah, 0E0h, 0E0h
db 0E0h, 0E0h, 0E0h, 0E0h, 0E0h, 0E0h
db 004h, 0E0h, 0E0h, 0E0h, 0E0h, 004h
BMP_SnakeDead db 04h, 00h, 00h, 00h, 00h, 04h
db 00h, 04h, 00h, 00h, 04h, 00h
db 00h, 00h, 04h, 04h, 00h, 00h
db 00h, 00h, 04h, 04h, 00h, 00h
db 00h, 04h, 00h, 00h, 04h, 00h
db 04h, 00h, 00h, 00h, 00h, 04h
; Apple
BMP_BonusApple db 000h, 06Fh, 06Fh, 000h, 074h, 078h
db 000h, 06Fh, 070h, 06Fh, 0BBh, 000h
db 06Fh, 070h, 070h, 070h, 06Fh, 06Fh
db 06Fh, 070h, 070h, 070h, 070h, 06Fh
db 000h, 06Fh, 070h, 070h, 06Fh, 000h
db 000h, 000h, 06Fh, 06Fh, 000h, 000h
; Banan
BMP_BonusBanan db 000h, 074h, 074h, 000h, 074h, 078h
db 000h, 074h, 02Ch, 074h, 0BBh, 000h
db 074h, 02Ch, 02Ch, 02Ch, 074h, 074h
db 074h, 02Ch, 02Ch, 02Ch, 02Ch, 074h
db 000h, 074h, 02Ch, 02Ch, 074h, 000h
db 000h, 000h, 074h, 074h, 000h, 000h
; Macros
macro push [arg] { push arg }
macro pop [arg] { pop arg }
; Stack: A: CSnake
; Output: Stack: A - Y, X
GetNextPoint:
push bp
mov bp, sp
push ax
push bx
push si
mov bx, [bp+4]
mov si, [Snake.head]
shl si, 1 ; *= sizeof(body[0])
mov ax, [Snake.body + si]
add ah, [Snake.dirX]
add al, [Snake.dirY]
; Check Y:
.CheckY:
cmp al, 0FFh
jz .SetYEnd
cmp al, FieldH
jnz .CheckX
mov al, 0
jmp .CheckX
.SetYEnd:
mov al, FieldH - 1
.CheckX:
cmp ah, 0FFh
jz .SetXEnd
cmp ah, FieldW
jnz .Return
mov ah, 0
jmp .Return
.SetXEnd:
mov ah, FieldW - 1
.Return:
mov [bp+4], ax
pop si
pop bx
pop ax
leave
ret
; Stack: A: CSnake
RenderSnake:
push bp
mov bp, sp
pusha
mov bx, [bp+4] ; Get snake from stack, and set global Snake structure
mov cx, [Snake.length]
sub cx, 2
mov si, [Snake.tail]
shl si, 1 ; *= dw
; Render Tail
mov ax, [Snake.body + si]
cmp bx, snake2
jz @f
push BMP_Snake1Tail
jmp .DrawTail
@@:
push BMP_Snake2Tail
.DrawTail:
call DrawCell
add si, 2
.RS_For:
mov ax, [Snake.body + si]
cmp bx, snake2
jz @f
push BMP_Snake1Body
jmp .DrawBody
@@:
push BMP_Snake2Body
.DrawBody:
call DrawCell
add si, 2 ; Add sizeof(body[0])
loop .RS_For ; for --[Snake.length] != 0
; Render Head
mov ax, [Snake.body + si]
cmp bx, snake2
jz @f
push BMP_Snake1Head
jmp .DrawHead
@@:
push BMP_Snake2Head
.DrawHead:
call DrawCell
popa
leave
ret 2
; Stack: CSnake, const Point dw
; Return: CSnake -> Bool, Point dw
CollidePointSnakeWithoutHead:
push bp
mov bp, sp
pusha
; mov word[bp+4], 1
; jmp .Exit
mov bx, [bp+4]
mov cx, [Snake.length]
dec cx ; Collide without Head of snake
mov si, [Snake.tail]
;shl si, 1 ; *= dw
mov di, [bp+6] ; Point
@@:
; mov ax, si
; xor dx, dx
; mov si, BodySize
; div si
; mov si, dx ; Index %= BodySize
cmp si, BodySize
jnz .AfterMod
mov si, 0
.AfterMod:
shl si, 1 ; *= dw
mov ax, [Snake.body + si]
cmp ax, di
jz .Collide
shr si, 1
inc si
loop @b
jmp .NotCollide
.Collide:
popa
or ax, 1
leave
ret 4
.NotCollide:
.Exit:
popa
leave
ret 4
; Input:
; Stack: CSnake, NextPoint dw, EatBonus dw
; bp+4 bp+6 bp+8
MoveSnake:
push bp
mov bp, sp
pusha
mov bx, [bp+4]
mov ax, [bp+8]
or ax, ax
jnz .NotShiftTail
; ReDraw tail:
mov si, [Snake.tail]
shl si, 1 ; *= dw
mov ax, [Snake.body + si]
push BMP_EmptyCell
call DrawCell ; Remove Tail on screen
shr si, 1 ; Return index
inc si ; To next body cell
mov ax, si
mov dx, 0
mov si, BodySize
div si
mov si, dx ; Index %= BodySize
mov [Snake.tail], si
shl si, 1
; Draw next tail
mov ax, [Snake.body + si]
cmp bx, snake2
jz @f
push BMP_Snake1Tail
jmp .DrawTail
@@:
push BMP_Snake2Tail
.DrawTail:
call DrawCell
.NotShiftTail:
; ReDraw head:
mov si, [Snake.head]
shl si, 1 ; *= dw
mov ax, [Snake.body + si]
cmp bx, snake2
jz @f
push BMP_Snake1Body
jmp .DrawBody
@@:
push BMP_Snake2Body
.DrawBody:
call DrawCell
shr si, 1 ; Return index
inc si ; To next body cell
mov ax, si
mov dx, 0
mov si, BodySize
div si
mov si, dx ; Index %= BodySize
mov [Snake.head], si
shl si, 1
; Draw next head
mov ax, [bp+6]
mov [Snake.body + si], ax ; Set head to next point
cmp bx, snake2
jz @f
push BMP_Snake1Head
jmp .DrawHead
@@:
push BMP_Snake2Head
.DrawHead:
call DrawCell
; Save current directions
xor ax, ax
mov al, [Snake.dirX]
mov [Snake.oldDX], al
mov al, [Snake.dirY]
mov [Snake.oldDY], al
popa
leave
ret 6
TimerTickFlag dw 0
TimerTick:
pusha
push bp
mov bp, sp
;
mov ax, [TimerTickFlag]
or ax, ax
jz .FlagZero
dec [TimerTickFlag]
jmp .Exit
.FlagZero:
mov [TimerTickFlag], 2
; Game Logic Here
; Check state
cmp [GameState], GameStateGame
jnz .Exit
; Show "Game Over", and change state
.CheckGameOver:
cmp [GameOver], 1
jnz .GameLoop
; call SetTxtMode
; mov ax, 0200h ; Set Cursor
; xor bx, bx
; mov dx, 00B0Fh
; int 10h
; mov dx, TextGameOver
; mov ax, 0900h
; int 21h
mov ax, 1300h ; /* Print "Game Over"
mov bx, 0Eh
mov cx, TextGameOver.sizeof
mov dx, 00B0Fh
push es
push bp
push ds
pop es
mov bp, TextGameOver
int 10h
pop bp
pop es ; */
cmp [GameWinner], 0
jz .PrintRoundDraw
mov ax, 0200h ; Set Cursor
xor bx, bx
mov dx, 00D0Bh
int 10h
mov ah, 0eh ; Print winner digit
mov al, byte[GameWinner]
add al, '0'
mov bx, 00030h
int 10h
mov dx, TextWinnerSnake ; Print winner text
mov ax, 0900h
int 21h
jmp .EndGameOver
.PrintRoundDraw:
mov ax, 0200h ; Set Cursor
xor bx, bx
mov dx, 00D0Eh
int 10h
mov dx, TextRoundDraw ; Print round draw text
mov ax, 0900h
int 21h
.EndGameOver:
mov [GameState], GameStateExit
jmp .Exit
; Game Tick
.GameLoop:
; LOCAL VARS:
push snake1 ; bp-2
call GetNextPoint
push snake2 ; bp-4
call GetNextPoint
; Local:
; bp-6 - collide snake1
; bp-8 - collide snake2
; bp-9 - pickup snake1
; bp-10 - pickup snake2
sub sp, 6
.CheckPickUpBonus:
; bp-9 - pickup snake1
; bp-10 - pickup snake2
mov word[bp-10], 0 ; Init zero
mov ax, [BonusXY]
cmp [bp-2], ax ; Snake1
jnz @f
mov byte[bp-9], 1
inc [Player1Score]
@@:
cmp [bp-4], ax ; Snake2
jnz @f
mov byte[bp-10], 1
inc [Player2Score]
@@:
; If bonus is pickup
mov ax, [bp-10]
or ax, ax
jz @f
call UpdateScore
call SpawnBonus
jmp .CheckPickUpBonus_After
@@:
call DrawBonus
.CheckPickUpBonus_After:
.MoveSnakesToNextPoints:
xor ax, ax
mov al, [bp-9]
push ax
push word[bp-2]
push snake1
call MoveSnake
mov al, [bp-10]
push ax
push word[bp-4]
push snake2
call MoveSnake
;jmp .Exit
.Collisions:
.CollisionsHeadByHead:
mov ax, [bp-2]
cmp ax, [bp-4] ; compare next points
jnz .CollideSnakes
; GameOver
mov [GameOver], 1
mov [GameWinner], 0
mov ax, [bp-2]
push BMP_SnakeDead
call DrawCell
mov word[bp-6], 1
mov word[bp-8], 1
jmp .AfterCollisions ; Draw round
.CollideSnakes:
xor ax, ax
push word[bp-2] ; Check Point of snake1
push snake1
call CollidePointSnakeWithoutHead
push word[bp-2]
push snake2
call CollidePointSnakeWithoutHead
or ax, ax
jz .CheckPointOfSnake2 ; if collide = 0
; else
; GameOver
mov [GameOver], 1
mov [GameWinner], 2
mov ax, [bp-2]
push BMP_SnakeDead
call DrawCell
mov word[bp-6], 1
.CheckPointOfSnake2:
xor ax, ax
push word[bp-4] ; Check Point of snake2
push snake1
call CollidePointSnakeWithoutHead
push word[bp-4]
push snake2
call CollidePointSnakeWithoutHead
or ax, ax
jz .AfterCollisions ; if collide = 0
; else
; GameOver
mov [GameOver], 1
cmp [GameWinner], 2 ; To je .Draw
mov [GameWinner], 0 ; SetDraw
je .Draw
mov [GameWinner], 1 ; UnsetDraw
.Draw:
mov ax, [bp-4]
push BMP_SnakeDead
call DrawCell
mov word[bp-8], 1
.AfterCollisions:
.Exit:
mov sp, bp
pop bp
popa
; Return to DOS
pushf
call dword [cs:Old8]
iret
; void
SpawnBonus:
push ax, bx, dx, cx
call Random_Next ; ax - random
xor dx, dx
mov bx, FieldW*FieldH
div bx
mov ax, dx
xor dx, dx
mov bx, FieldW
div bx ; dx - X, ax - Y
; TODO
mov ah, dl
mov [BonusXY], ax
; Sprite
call Random_Next
test ax, 4
jz .B_Banan
mov [BonusID], 0
jmp .Draw
.B_Banan:
mov [BonusID], 1
.Draw:
; Draw bonus
call DrawBonus
.Exit:
pop cx, dx, bx, ax
ret
DrawBonus:
push ax
cmp [BonusID], 1
jz .B_Banan
mov ax, [BonusXY]
push BMP_BonusApple
call DrawCell
jmp .Exit
.B_Banan:
mov ax, [BonusXY]
push BMP_BonusBanan
call DrawCell
jmp .Exit
.Exit:
pop ax
ret
; Input:
; AH - X, AL - Y
; STACK: CSnake
SetSnakeDir:
push bp
mov bp, sp
pusha
mov bx, [bp+4]
xor cx, cx
mov dh, [Snake.oldDX]
mov dl, [Snake.oldDY]
add dh, ah
or cl, dh
add dl, al
or cl, dl
jz .Exit ; if NewDir + OldDir = 0, if snake want reverse direction
mov [Snake.dirX], ah
mov [Snake.dirY], al
.Exit:
popa
leave
ret
StartGameLoop:
pusha
; Init vars
mov [GameOver], 0
mov [GameWinner], 0
; Change int vector to DOS Timer
mov dx, TimerTick
mov ax, 2508h ; 25 - set int vector, 08 - timer
int 21h
; SetVideoMode
call SetVideoMode
; Fill video screen
push snake1
call RenderSnake
push snake2
call RenderSnake
call SpawnBonus
call ShowScoresText
mov ax, FieldOffsetU
dec ax
call DrawLine
; Handle keys
.ReadKey:
xor ax, ax
int 16h
cmp al, 1Bh ; if key == ESC
jz .CloseGame
; For snake1
push snake1
.CheckUp:
cmp ah, 48h
jnz .CheckDn
mov ax, 000FFh
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckDn:
cmp ah, 50h
jnz .CheckL
mov ax, 00001h
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckL:
cmp ah,4Bh
jnz .CheckR
mov ax, 0FF00h
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckR:
cmp ah,4Dh
jnz .CheckSnake2Keys
mov ax, 00100h
call SetSnakeDir
jmp .EndReadSnakeKey
; Snake2 keys
.CheckSnake2Keys:
add sp, 2
push snake2
.CheckU2:
cmp al, 'w'
jnz .CheckD2
mov ax, 000FFh
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckD2:
cmp al, 's'
jnz .CheckL2
mov ax, 00001h
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckL2:
cmp ah, 1Eh
jnz .CheckR2
mov ax, 0FF00h
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckR2:
cmp ah, 20h
jnz .EndReadSnakeKey
mov ax, 00100h
call SetSnakeDir
jmp .EndReadSnakeKey
.CheckElse:
jmp .ReadKey
.EndReadSnakeKey:
add sp, 2
jmp .ReadKey
.CloseGame:
mov [GameState], GameStateExit
popa
ret
ShowGameMenu:
push bp
mov bp, sp
pusha
call SetTxtMode
mov dx, MenuHelloText ; Print text
mov ah, 9
int 21h
xor ax, ax ; Read key
int 16h
; call StartGameLoop
mov [GameState], GameStateGame
popa
leave
ret
SetVideoMode:
push ax
mov ax, 13h
int 10h
pop ax
ret
SetTxtMode:
push ax
mov ax, 3h
int 10h
pop ax
ret
; ah - x
; al - y
; Stack:
; dw PointerOfBitmask
DrawCell:
push bp
mov bp, sp
pusha
push es
mov cx, VideoSegment ; Set Video segment
mov es, cx
mov cx, ax
shr cx, 8 ; CX = x
; Calcuculation of the cell position
xor ah, ah
mov dx, CellSize
mul dx
add ax, FieldOffsetU
mov dx, VideoW
mul dx ; AX = VideoW * (AX(Y) * CellSize + FieldOffsetU)
push ax
mov ax, cx
mov dx, CellSize
mul dx
pop cx
add ax, cx ; AX = AX + X * CellSize
mov di, ax
mov si, [bp+4]
cld ; df = 0
; bx - row counter
mov bx, CellSize
.ForRow:
or bx, bx
jz .Exit
; dx - col counter
mov dx, CellSize
.ForCol:
or dx, dx
jz .ForRowEnd
lodsb
stosb
dec dx
jmp .ForCol
.ForRowEnd:
add di, VideoW - CellSize ; To next row
dec bx
jmp .ForRow
.Exit:
pop es
popa
leave
ret 2
Main:
; Save int vector DOS Timer
mov ax, 3508h
int 21h
mov [cs:Old8], bx
mov [cs:Old8+2], es
; Init random generator
call Random_Init
; First show GameMenu
mov [GameState], GameStateMenu
.MainGameLoop:
cmp [GameState], GameStateExit
jz .Exit
cmp [GameState], GameStateMenu
jnz @f
call ShowGameMenu
@@:
cmp [GameState], GameStateGame
jnz @f
call StartGameLoop
@@:
jmp .MainGameLoop
.Exit:
; Restore int vector
mov ds, [cs:Old8+2]
mov dx, [cs:Old8]
mov ax, 2508h
int 21h
mov ax, 4C00h ; Exit from program
int 21h
ShowScoresText:
pusha
mov ax, 0200h ; Set Cursor
xor bx, bx
mov dx, 00101h ; YX
int 10h