-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.asm
2454 lines (2192 loc) · 57.3 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
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
#####################################################################
#
# CSCB58 Winter 2022 Assembly Final Project
# University of Toronto, Scarborough
#
# Student: Like Wang, 1006743062, wanglike, [email protected] #
# Bitmap Display Configuration:
# - Unit width in pixels: 8 (update this as needed)
# - Unit height in pixels: 8 (update this as needed)
# - Display width in pixels: 512 (update this as needed)
# - Display height in pixels: 512 (update this as needed)
# - Base Address for Display: 0x10008000 ($gp)
#
# Which milestones have been reached in this submission?
# (See the assignment handout for descriptions of the milestones)
# - Milestone 3 (choose the one the applies)
# All done!
#
# Which approved features have been implemented for milestone 3?
# (See the assignment handout for the list of additional features)
# 1. Time score: a clock on the bottom right corner will track players’ playing time.
# P.S. it will show the best record of the player after player win. (the shorter, the better)
# 2. Fail condition: hit on the ground or contact with bats or snakes.
# 3. Win condition: after the King find and get the key lost in the Jungle.
# 4. Moving objects: There are bats that fly around the screen.
# 5. Double jump: allow the player to jump when in mid-air, but only once!
# 6. Pick_up: If a player picks up a mushroom, his/hers total time will be cut by 5 seconds.
# 7. Different levels: there are in total of 3 levels for this game. Try to break the record!
#
# Link to video demonstration for final submission:
# https://www.bilibili.com/video/BV13S4y127p9?spm_id_from=333.999.0.0
#
# Are you OK with us sharing the video with people outside course staff?
# - yes, and please share this project github link as well!
#
# Any additional information that the TA needs to know:
# - Time taken : 7 days
# - Control : press "d" to move right.
# : press "a" to move left.
# : press "space" to jump.
# - Background of the game
# The background of the game is about the_lost_King (main_character). He used to rule a kingdom.
# One day, there were enemies from far away attack his kingdom and occupied his castle.
# The exiled King lost in a jungle, he is eager to find the golden key to open the door
# of the_lost_treasure that contain a invincible_sword that only exist in legends.
# Run! little King, Run! Find the unknown sword and fight back for your kingdom!
# P.S. This is only the chapter 1 of the game < RUN >, the only mission is to find the golden key.
# Please let me know if you like this game. I may continue to write it.
# That all! Enjoy the game.
# #####################################################################
# define CONSTANTS
# width = 64, height = 64
.eqv DISPLAY_FIRST_ADDRESS 0x10008000
.eqv DISPLAY_LAST_ADDRESS 0x10003FFF # update this given the values below shift +(64*62)*4
.eqv DISPLAY_MIDLFT_ADDRESS 0x10009C10 # mid left spot for the king (but jump 2 aligned) +(64*28+4)*4
.eqv DISPLAY_GROUND_ADDRESS 0x1000BDE8
.eqv DISPLAY_TIME 0x1000B8D8 # bottom right corner +(64*57-10)*4
.eqv DISPLAY_LEVEL 0x10008218 # top right corner +(64*2+6)*4
.eqv DISPLAY_L 0x10008208 # top right corner +(64*2+2)*4
.eqv DISPLAY_DEAD 0x10009C54 # top right corner +(64*28+21)*4
.eqv DISPLAY_REC 0x1000A640 # top right corner +(64*38+16)*4
.eqv DISPLAY_NUM 0x1000AE7C # top right corner +(64*46+31)*4
.eqv DISPLAY_S 0x1000AE90 # top right corner +(64*46+36)*4
.eqv DISPLAY_SPLASH 0x10009C44 # top right corner +(64*28+17)*4
# last address shifts
.eqv SHIFT_NEXT_ROW 256 # next row shift = width*4 = 128
# number of pixels
.eqv SIZE 4095 # number of pixels - 1 so can use index
.eqv PLF_WIDTH 24 # width of platform (6*4)
.eqv HEIGHT 63 # height of pixels - 1 so can use index
.eqv PLF_HEIGHT 1 # height of platform
# colour
.eqv COLOUR_BLACK 0x00000000
.eqv COLOUR_RED 0x00ff0000
.eqv COLOUR_YELLOW 0x00ffff00
.eqv COLOUR_ORANGE 0x00ff8000
.eqv COLOUR_DARK_BLUE 0x000000a5
.eqv COLOUR_BLUE 0x000000ff
.eqv COLOUR_BROWN 0x00472400
.eqv COLOUR_FLESH 0x00ffcc99
.eqv COLOUR_GREY 0x00555555
.eqv COLOUR_THORN 0x00C6C6C6
.eqv COLOUR_LIGHT_GREEN 0x0091ff85
.eqv COLOUR_GREEN 0x0000ff00
.eqv COLOUR_JUNGLE 0x00014700
.eqv COLOUR_WHITE 0x00ffffff
.eqv COLOUR_PLATFORM 0x00A35C00
.eqv COLOUR_DARK_PURPLE 0x00460076
.eqv COLOUR_PURPLE 0x00BF55BF
# objects information
.eqv MAX_H 6 # the max height the_king can jump
.eqv NUM_PLFS 6 # number of platforms in level 1/2/3
.eqv NUM_S 1 # number of snakes in level 1
.eqv NUM_B 1 # number of bats in level 1
.eqv NUM_S2 2 # number of snakes in level 2/3
.eqv NUM_B2 2 # number of bats in level 2/3
.eqv NUM_M 1 # number of mushrooms
.eqv NUM_T 15 # number of thorns
.eqv NUM_P 1 # number of portals / keys
.eqv NUM_C 1 # number of clocks
.eqv K_W 6 # width of the King
.eqv K_H 8 # height of the King
.eqv L_W 6 # width of the Level
.eqv L_H 1 # height of the Level
.eqv M_W 3 # width of the Mushroom
.eqv M_H 3 # height of the Mushroom
.eqv S_W 3 # width of the Snake
.eqv S_H 7 # height of the Snake
.eqv B_W 9 # width of the Bat
.eqv B_H 5 # height of the Bat
.eqv P_W 5 # width of the Portal
.eqv P_H 5 # height of the Portal
.eqv C_W 5 # width of the Clock
.eqv C_H 5 # height of the Clock
.data
# variables
King_rows: .word 40
King_columns: .word 4
King_address: .word 0
King_is_stand: .word 1
King_is_coll: .word 0
King_num_jump: .word 2
Thorn_rows: .word 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62
Thorn_columns: .word 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58
#------------Level_1------------
Level_rows: .word 48, 47, 26, 56, 22, 33
Level_columns: .word 2, 36, 18, 20, 40, 33
Mushrm_rows: .word 23 #53 for testing; 23 for game
Mushrm_columns: .word 20
is_mushrm: .word 1
Snake_rows: .word 40
Snake_columns: .word 39
Snake_num1: .word 1
Snake_num2: .word 2
Bat_rows: .word 8
Bat_columns: .word 20
Bat_num1: .word 1
Bat_num2: .word 2
Portal_rows: .word 5
Portal_columns: .word 55
#-------------------------------
#------------Level_2------------
Level_rows2: .word 48, 37, 37, 36, 47, 24
Level_columns2: .word 6, 6, 16, 51, 44, 41
Snake_rows2: .word 31, 29
Snake_columns2: .word 10, 46
Bat_rows2: .word 20
Bat_columns2: .word 20
Portal_rows2: .word 14
Portal_columns2:.word 55
#-------------------------------
#------------Level_3------------
Level_rows3: .word 48, 47, 26, 56, 22, 33
Level_columns3: .word 2, 38, 18, 20, 40, 33
Snake_rows3: .word 40, 49
Snake_columns3: .word 40, 17
Bat_rows3: .word 8, 32
Bat_columns3: .word 20, 52
Key_rows: .word 5
Key_columns: .word 10
#-------------------------------
Clock_rows: .word 56
Clock_columns: .word 58
Record: .word 1000000
Clock_count: .word 0
Clock_wait: .word 9
.text
.globl main
# RUN
main:
# ------clear the screen-------
li $a0, DISPLAY_FIRST_ADDRESS
li $a1, 64
li $a2, 64
jal clear
# ------show the openning------
# 1.show "R" in white
# wait for 300 milliseconds
# 2.add "U", show "RU" in white
# wait for 300 milliseconds
# 3.add "N", show "RUN" in white
jal draw_openning
# wait for 1000 milliseconds
li $v0, 32
li $a0, 2000
syscall
# 4.show "RUN" in fading into the background
li $a1, 0x00E9FFE7
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, 0x00BDFFB6
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, COLOUR_LIGHT_GREEN
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, 0x0063FF51
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, 0x002EFF00
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, COLOUR_GREEN
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, 0x0000D300
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, 0x0000A400
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, 0x00007500
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
li $a1, COLOUR_JUNGLE
jal draw_end_openning
# wait for 100 milliseconds
li $v0, 32
li $a0, 200
syscall
# -----end of the openning-----
main_start:
#j main_start2
#j main_start3
# reset time clock
li $t7, 0
sw $t7, Clock_count
# reset existance of mushroom
li $t7, 1
sw $t7, is_mushrm
# ------clear the screen-------
li $a0, DISPLAY_FIRST_ADDRESS
li $a1, 64
li $a2, 64
jal clear # jump to clear and save position to $ra
# ----initialize registers-----
# variables
# $s0: object_rows
# $s1: object_columns
# $s2: 1 if collision happened, 0 if not
# $s3: increment number
# $s4: number of this object
# $s5: height left to be risen
# $s6: time increment
# $s7: wait time
li $s0, 40 # load the value stored in King_rows
sw $s0, King_rows
li $s1, 4 # load the value stored in King_columns
sw $s1, King_columns
sll $s0, $s0, 6
add $s0, $s0, $s1 # stored the address in $gp form in $s2
sll $s0, $s0, 2 # address = address * 4
add $a0, $s0, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
sw $a0, King_address # update King address and store it in the data
main_loop:
lw $a0, King_address # take out the previous address from data
li $a1, K_H # load the value stored in King_height
li $a2, K_W # load the value stored in King_width
jal clear
li $a0, 0xffff0000
lw $t9, 0($a0)
bne $t9, 1, main_update
jal keypress
# Update obstacle location.
main_update:
# show the current level of the game
li $a0, DISPLAY_L
li $a1, COLOUR_WHITE
jal draw_L
li $a0, DISPLAY_LEVEL
li $a1, COLOUR_WHITE
li $a2, 1
li $a3, COLOUR_JUNGLE
jal draw_number
# decrese Clock_wait by 1 each time
lw $s7, Clock_wait
beq $s7, 0, increse_clock
addi $s7, $s7, -1
sw $s7, Clock_wait
j update_wait_done
increse_clock:
# increment clock
lw $s6, Clock_count
addi $s6, $s6, 1
sw $s6, Clock_count
# update time
li $a0, DISPLAY_TIME
li $a1, COLOUR_WHITE
move $a2, $s6
li $a3, COLOUR_JUNGLE
jal draw_number
li $s7, 9
sw $s7, Clock_wait
update_wait_done:
# jump to keypress and save position to $ra
lw $s0, King_rows # load the value stored in King_rows
lw $s1, King_columns # load the value stored in King_columns
sll $s0, $s0, 6
add $s0, $s0, $s1 # stored the address in $gp form in $s2
sll $s0, $s0, 2 # address = address * 4
add $a0, $s0, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
sw $a0, King_address # update King address and store it in the data
jal draw_king
# check if fall on the ground
bge $a0, DISPLAY_GROUND_ADDRESS, fail # if (King_address > ground_address) then game over
# check if collide on a snake
la $a0, Snake_rows
la $a1, Snake_columns
li $a2, S_W
li $a3, S_H
li $t3, NUM_S
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, fail # if (King_is_hurt == 1) then fail
# check if collide on a bat
la $a0, Bat_rows
la $a1, Bat_columns
li $a2, B_W
li $a3, B_H
li $t3, NUM_B
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, fail # if (King_is_hurt == 1) then fail
# check if collide on a portal
la $a0, Portal_rows
la $a1, Portal_columns
li $a2, P_W
li $a3, P_H
li $t3, NUM_P
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, main_start2 # if (King_win == 1) then level_2
# draw mushrooms
lw $s6, is_mushrm
beq $s6, 1, draw_Mushroom
j bouns_over
draw_Mushroom:
la $s0, Mushrm_rows # load the value stored in Mushrm_rows[0]
la $s1, Mushrm_columns # load the value stored in Mushrm_columns[0]
li $s3, 0 # increment number
li $s4, NUM_M # maxium number of Mushrooms
draw_M_loop:
bge $s3, $s4, draw_M_done
lw $s6, 0($s0) # load valve of Mushrm_rows[i] in $s6
lw $s7, 0($s1) # load valve of Mushrm_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Mushrm_rows[]
addi $s1, $s1, 4 # keep track of the next address of Mushrm_columns[]
jal draw_mushroom
# check if collide on a mushroom
la $a0, Mushrm_rows
la $a1, Mushrm_columns
li $a2, M_W
li $a3, M_H
li $t3, NUM_M
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, bouns # if (King_win == 1) then clock_count = clock_count - 5
j bouns_over
j draw_M_loop
draw_M_done:
bouns:
li $s6, 0
sw $s6, is_mushrm
lw $s6, Clock_count
addi $s6, $s6, -5
sw $s6, Clock_count
lw $a0, King_address
addi $a0, $a0, -SHIFT_NEXT_ROW
addi $a0, $a0, -SHIFT_NEXT_ROW
addi $a0, $a0, -SHIFT_NEXT_ROW
addi $a0, $a0, -SHIFT_NEXT_ROW
addi $a0, $a0, -SHIFT_NEXT_ROW
addi $a0, $a0, -SHIFT_NEXT_ROW
jal draw_bouns
li $v0, 32
li $a0, 1000
syscall
# clear the screen to clear the mushroom and prevent time mass
li $a0, DISPLAY_FIRST_ADDRESS
li $a1, 64
li $a2, 64
jal clear
bouns_over:
# draw levels
la $s0, Level_rows # load the value stored in Level_rows[0]
la $s1, Level_columns # load the value stored in Level_columns[0]
li $s3, 0 # increment number
li $s4, NUM_PLFS # maxium number of levels
draw_level_loop:
bge $s3, $s4, draw_level_done
lw $s6, 0($s0) # load valve of Level_rows[i] in $s6
lw $s7, 0($s1) # load valve of Level_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Level_rows[]
addi $s1, $s1, 4 # keep track of the next address of Level_columns[]
jal draw_level
j draw_level_loop
draw_level_done:
# draw thorns
la $s0, Thorn_rows # load the value stored in Thorns_rows[0]
la $s1, Thorn_columns # load the value stored in Thorns_columns[0]
li $s3, 0 # increment number
li $s4, NUM_T # maxium number of Thorns
draw_thorns_loop:
bge $s3, $s4, draw_thorns_done
lw $s6, 0($s0) # load valve of Thorns_rows[i] in $s6
lw $s7, 0($s1) # load valve of Thorns_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Thorns_rows[]
addi $s1, $s1, 4 # keep track of the next address of Thorns_columns[]
jal draw_thorn
j draw_thorns_loop
draw_thorns_done:
# draw clock
la $s0, Clock_rows # load the value stored in Clock_rows[0]
la $s1, Clock_columns # load the value stored in Clock_columns[0]
li $s3, 0 # increment number
li $s4, NUM_C # maxium number of Clocks
draw_clock_loop:
bge $s3, $s4, draw_clock_done
lw $s6, 0($s0) # load valve of Clock_rows[i] in $s6
lw $s7, 0($s1) # load valve of Clock_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Clock_rows[]
addi $s1, $s1, 4 # keep track of the next address of Clock_columns[]
jal draw_clock
j draw_clock_loop
draw_clock_done:
# draw portals
la $s0, Portal_rows # load the value stored in Portal_rows[0]
la $s1, Portal_columns # load the value stored in Portal_columns[0]
li $s3, 0 # increment number
li $s4, NUM_P # maxium number of Portals
draw_portals_loop:
bge $s3, $s4, draw_portals_done
lw $s6, 0($s0) # load valve of Portal_rows[i] in $s6
lw $s7, 0($s1) # load valve of Portal_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Thorns_rows[]
addi $s1, $s1, 4 # keep track of the next address of Thorns_columns[]
jal draw_portal
j draw_portals_loop
draw_portals_done:
# draw snakes
la $s0, Snake_rows # load the value stored in Snake_rows[0]
la $s1, Snake_columns # load the value stored in Snake_columns[0]
li $s3, 0 # increment number
li $s4, NUM_S # maxium number of snakes
draw_S_loop:
bge $s3, $s4, draw_S_done
lw $s6, 0($s0) # load valve of Snake_rows[i] in $s6
lw $s7, 0($s1) # load valve of Snake_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Snake_rows[]
addi $s1, $s1, 4 # keep track of the next address of Snake_columns[]
jal draw_snake
j draw_S_loop
draw_S_done:
# draw bats
la $s0, Bat_rows # load the value stored in Bat_rows[0]
la $s1, Bat_columns # load the value stored in Bat_columns[0]
li $s3, 0 # increment number
li $s4, NUM_B # maxium number of bats
draw_B_loop:
bge $s3, $s4, draw_B_done
lw $s6, 0($s0) # load valve of Bat_rows[i] in $s6
lw $s7, 0($s1) # load valve of Bat_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
li $a1, B_H # load the value stored in Bat_height
li $a2, B_W # load the value stored in Bat_width
jal clear
addi $s7, $s7, 54
li $s6, 55
div $s7, $s6
mfhi $s7
#andi $s7, $s7, 54 # update the address of the Bat
sw $s7, 0($s1) # store the updated valve back to Bat_columns[i]
lw $s6, 0($s0) # load valve of Bat_rows[i] in $s6
lw $s7, 0($s1) # load valve of Bat_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
add $a0, $s2, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
jal draw_bat
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Bat_rows[]
addi $s1, $s1, 4 # keep track of the next address of Bat_columns[]
j draw_B_loop
draw_B_done:
bne $s5, 0, rise
j rise_done
rise:
lw $s0, King_rows #load the value stored in
ble $s0, 0, fall_pre # upper bounder of the screen
addi $s0, $s0, -2
addi $s5, $s5, -1
sw $s0, King_rows
rise_done:
la $s0, Level_rows # load the value stored in Level_rows[0]
la $s1, Level_columns # load the value stored in Level_columns[0]
jal check_stand
lw $s2, King_is_stand # load the value stored in King_is_stand
beq $s2, 0, fall
j main_sleep
fall_pre:
li $s5, 0
j fall
fall:
lw $s0, King_rows
addi $s0, $s0, 1
sw $s0, King_rows
main_sleep:
li $v0, 32
li $a0, 100
syscall
j main_loop
#----------------------------------------------------------------------------------------------------------------------
main_start2:
# ------clear the screen-------
li $a0, DISPLAY_FIRST_ADDRESS
li $a1, 64
li $a2, 64
jal clear # jump to clear and save position to $ra
li $s0, 0
sw $s0, King_is_coll
li $s0, 40 # load the value stored in King_rows
sw $s0, King_rows
li $s1, 4 # load the value stored in King_columns
sw $s1, King_columns
sll $s0, $s0, 6
add $s0, $s0, $s1 # stored the address in $gp form in $s2
sll $s0, $s0, 2 # address = address * 4
add $a0, $s0, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
sw $a0, King_address # update King address and store it in the data
main_loop2:
lw $a0, King_address # take out the previous address from data
li $a1, K_H # load the value stored in King_height
li $a2, K_W # load the value stored in King_width
jal clear
li $a0, 0xffff0000
lw $t9, 0($a0)
bne $t9, 1, main_update2
jal keypress
# Update obstacle location.
main_update2:
# show the current level of the game
li $a0, DISPLAY_L
li $a1, COLOUR_WHITE
jal draw_L
li $a0, DISPLAY_LEVEL
li $a1, COLOUR_WHITE
li $a2, 2
li $a3, COLOUR_JUNGLE
jal draw_number
# decrese Clock_wait by 1 each time
lw $s7, Clock_wait
beq $s7, 0, increse_clock2
addi $s7, $s7, -1
sw $s7, Clock_wait
j update_wait_done2
increse_clock2:
# increment clock
lw $s6, Clock_count
addi $s6, $s6, 1
sw $s6, Clock_count
# update time
li $a0, DISPLAY_TIME
li $a1, COLOUR_WHITE
move $a2, $s6
li $a3, COLOUR_JUNGLE
jal draw_number
li $s7, 9
sw $s7, Clock_wait
update_wait_done2:
# jump to keypress and save position to $ra
lw $s0, King_rows # load the value stored in King_rows
lw $s1, King_columns # load the value stored in King_columns
sll $s0, $s0, 6
add $s0, $s0, $s1 # stored the address in $gp form in $s2
sll $s0, $s0, 2 # address = address * 4
add $a0, $s0, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
sw $a0, King_address # update King address and store it in the data
jal draw_king
# check if fall on the ground
bge $a0, DISPLAY_GROUND_ADDRESS, fail # if (King_address > ground_address) then game over
# check if collide on a snake
la $a0, Snake_rows2
la $a1, Snake_columns2
li $a2, S_W
li $a3, S_H
li $t3, NUM_S2
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, fail # if (King_is_hurt == 1) then fail
# check if collide on a bat
la $a0, Bat_rows2
la $a1, Bat_columns2
li $a2, B_W
li $a3, B_H
li $t3, NUM_B
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, fail # if (King_is_hurt == 1) then fail
# check if collide on a portal
la $a0, Portal_rows2
la $a1, Portal_columns2
li $a2, P_W
li $a3, P_H
li $t3, NUM_P
jal check_collide
lw $s2, King_is_coll
beq $s2, 1, main_start3 # if (King_win == 1) then level_3
# draw levels
la $s0, Level_rows2 # load the value stored in Level_rows[0]
la $s1, Level_columns2 # load the value stored in Level_columns[0]
li $s3, 0 # increment number
li $s4, NUM_PLFS # maxium number of levels
draw_level_loop2:
bge $s3, $s4, draw_level_done2
lw $s6, 0($s0) # load valve of Level_rows[i] in $s6
lw $s7, 0($s1) # load valve of Level_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Level_rows[]
addi $s1, $s1, 4 # keep track of the next address of Level_columns[]
jal draw_level
j draw_level_loop2
draw_level_done2:
# draw thorns
la $s0, Thorn_rows # load the value stored in Thorns_rows[0]
la $s1, Thorn_columns # load the value stored in Thorns_columns[0]
li $s3, 0 # increment number
li $s4, NUM_T # maxium number of Thorns
draw_thorns_loop2:
bge $s3, $s4, draw_thorns_done2
lw $s6, 0($s0) # load valve of Thorns_rows[i] in $s6
lw $s7, 0($s1) # load valve of Thorns_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Thorns_rows[]
addi $s1, $s1, 4 # keep track of the next address of Thorns_columns[]
jal draw_thorn
j draw_thorns_loop2
draw_thorns_done2:
# draw clock
la $s0, Clock_rows # load the value stored in Clock_rows[0]
la $s1, Clock_columns # load the value stored in Clock_columns[0]
li $s3, 0 # increment number
li $s4, NUM_C # maxium number of Clocks
draw_clock_loop2:
bge $s3, $s4, draw_clock_done2
lw $s6, 0($s0) # load valve of Clock_rows[i] in $s6
lw $s7, 0($s1) # load valve of Clock_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Clock_rows[]
addi $s1, $s1, 4 # keep track of the next address of Clock_columns[]
jal draw_clock
j draw_clock_loop2
draw_clock_done2:
# draw portals
la $s0, Portal_rows2 # load the value stored in Portal_rows[0]
la $s1, Portal_columns2 # load the value stored in Portal_columns[0]
li $s3, 0 # increment number
li $s4, NUM_P # maxium number of Portals
draw_portals_loop2:
bge $s3, $s4, draw_portals_done2
lw $s6, 0($s0) # load valve of Portal_rows[i] in $s6
lw $s7, 0($s1) # load valve of Portal_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Thorns_rows[]
addi $s1, $s1, 4 # keep track of the next address of Thorns_columns[]
jal draw_portal
j draw_portals_loop2
draw_portals_done2:
# draw snakes
la $s0, Snake_rows2 # load the value stored in Snake_rows[0]
la $s1, Snake_columns2 # load the value stored in Snake_columns[0]
li $s3, 0 # increment number
li $s4, NUM_S2 # maxium number of snakes
draw_S_loop2:
bge $s3, $s4, draw_S_done2
lw $s6, 0($s0) # load valve of Snake_rows[i] in $s6
lw $s7, 0($s1) # load valve of Snake_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Snake_rows[]
addi $s1, $s1, 4 # keep track of the next address of Snake_columns[]
jal draw_snake
j draw_S_loop2
draw_S_done2:
# draw bats
la $s0, Bat_rows2 # load the value stored in Bat_rows[0]
la $s1, Bat_columns2 # load the value stored in Bat_columns[0]
li $s3, 0 # increment number
li $s4, NUM_B # maxium number of bats
draw_B_loop2:
bge $s3, $s4, draw_B_done2
lw $s6, 0($s0) # load valve of Bat_rows[i] in $s6
lw $s7, 0($s1) # load valve of Bat_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
addi $a0, $s2, DISPLAY_FIRST_ADDRESS
li $a1, B_H # load the value stored in Bat_height
li $a2, B_W # load the value stored in Bat_width
jal clear
addi $s7, $s7, 54
li $s6, 55
div $s7, $s6
mfhi $s7
#andi $s7, $s7, 54 # update the address of the Bat
sw $s7, 0($s1) # store the updated valve back to Bat_columns[i]
lw $s6, 0($s0) # load valve of Bat_rows[i] in $s6
lw $s7, 0($s1) # load valve of Bat_columns[i] in $s7
sll $s2, $s6, 6
add $s2, $s2, $s7 # stored the address in $gp form in $s2
sll $s2, $s2, 2 # address = address * 4
add $a0, $s2, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
jal draw_bat
addi $s3, $s3, 1 # $s3 ++
addi $s0, $s0, 4 # keep track of the next address of Bat_rows[]
addi $s1, $s1, 4 # keep track of the next address of Bat_columns[]
j draw_B_loop2
draw_B_done2:
bne $s5, 0, rise2
j rise_done2
rise2:
lw $s0, King_rows #load the value stored in
ble $s0, 0, fall_pre2 # upper bounder of the screen
addi $s0, $s0, -2
addi $s5, $s5, -1
sw $s0, King_rows
rise_done2:
la $s0, Level_rows2 # load the value stored in Level_rows[0]
la $s1, Level_columns2 # load the value stored in Level_columns[0]
jal check_stand
lw $s2, King_is_stand # load the value stored in King_is_stand
beq $s2, 0, fall2
j main_sleep2
fall_pre2:
li $s5, 0
j fall2
fall2:
lw $s0, King_rows
addi $s0, $s0, 1
sw $s0, King_rows
main_sleep2:
li $v0, 32
li $a0, 100
syscall
j main_loop2
#----------------------------------------------------------------------------------------------------------------------
main_start3:
# ------clear the screen-------
li $a0, DISPLAY_FIRST_ADDRESS
li $a1, 64
li $a2, 64
jal clear # jump to clear and save position to $ra
li $s0, 0
sw $s0, King_is_coll
li $s0, 40 # load the value stored in King_rows
sw $s0, King_rows
li $s1, 4 # load the value stored in King_columns
sw $s1, King_columns
sll $s0, $s0, 6
add $s0, $s0, $s1 # stored the address in $gp form in $s2
sll $s0, $s0, 2 # address = address * 4
add $a0, $s0, $zero
addi $a0, $a0, DISPLAY_FIRST_ADDRESS
sw $a0, King_address # update King address and store it in the data
main_loop3:
lw $a0, King_address # take out the previous address from data
li $a1, K_H # load the value stored in King_height
li $a2, K_W # load the value stored in King_width
jal clear
li $a0, 0xffff0000
lw $t9, 0($a0)
bne $t9, 1, main_update3
jal keypress
# Update obstacle location.
main_update3:
# show the current level of the game
li $a0, DISPLAY_L
li $a1, COLOUR_WHITE
jal draw_L
li $a0, DISPLAY_LEVEL
li $a1, COLOUR_WHITE
li $a2, 3
li $a3, COLOUR_JUNGLE
jal draw_number
# decrese Clock_wait by 1 each time
lw $s7, Clock_wait
beq $s7, 0, increse_clock3
addi $s7, $s7, -1
sw $s7, Clock_wait
j update_wait_done3
increse_clock3:
# increment clock
lw $s6, Clock_count
addi $s6, $s6, 1
sw $s6, Clock_count
# update time
li $a0, DISPLAY_TIME
li $a1, COLOUR_WHITE
move $a2, $s6
li $a3, COLOUR_JUNGLE
jal draw_number
li $s7, 9
sw $s7, Clock_wait