-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.asm
1159 lines (1147 loc) · 25.6 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 2021 Assembly Final Project
# University of Toronto, Scarborough
#
# Student: Eric Zhou
#
# 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: 256 (update this as needed)
# - Display height in pixels: 256 (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 1/2/3/4 (choose the one the applies)
# Milestone (reached): 4
#
# Which approved features have been implemented for milestone 4?
# (See the assignment handout for the list of additional features)
# 1. Smooth graphics (only erasing & updating blocks that need to be erased)
# 2. Add “pick-ups” (adding snacks which freezes the obstacles (The World) and hp snack which increases hp by 10%=3 bars)
# 3. Scoring system (Score = # of snacks ate MAX=99)
#
# Link to video demonstration for final submission:
# https://www.youtube.com/watch?v=2RK1F8jGhJ8
#
#Are you OK with us sharing the video with people outside course staff?
# No.
#
# Any additional information that the TA needs to know:
# - Mentioned ALL in video.
# - Pick-ups effects will not stack. (Freeze-time will not stack when its already frozen or will hp "increase" to above 100% when you gather another green hp pickup at 100% hp.
# - Spaceship movement is 2 units!
#
#####################################################################
.eqv BASE_ADDRESS 0x10008000
.eqv WAIT_TIME 40
.eqv OFFSET 128
#COLORS
.eqv BLACK 0x000000
.eqv BLUE 0x8CD3FF
.eqv DGRAY 0x607D8B
.eqv GRAY 0xB0BEC5
.eqv RED 0xDD2C00
.eqv ORANGE 0xFF8F00
.eqv YELLOW 0xFFFF00
.eqv WHITE 0xFFFFFF
.eqv GREEN 0x76FF03
.eqv FREEZE_TIME 50
#NUM OF OBSTACLES
#.eqv NUM_OBSTACLES 3
#DATA
.data
#POS = position (top-leftest position of ship)
SHIP_POS: .word 14, 0, 1792
#ROW, COL, BASE ADDRESS OFFSET
OBST_POS: .word 0, 0, 0, 0, 0, 0, 0, 0, 0 #At least 3 meteors, stores top-left most position
#row, col, offset of address
HP: .word 10 #ingame hp
SCREEN_HP: .word 10 #hp bar on screen
OBSTACLES_DESTROYED: .word 0
TIME: .word 0
GAME_STATE: .word 1 #1 = alive, 0 = game over, 2 = frozen obstacles
SNACK: .word 0, 0, 0, 0, 0
SNACK_COUNTER: .word 0
FREEZE_COUNTER: .word 0
#SNACK_GEN: .word 0 #1 = snack has been generated/spawned
#SNACK_TYPE: .word 0 #type of snack, freeze obstacles or extra hp (0 or 1)
#SNACK_POS: .word 0,0,0 pos, row col
#DESIGNS
#SHIP DESIGN
#FIXED SPAWN POSITION: TOP-LEFTMOST @32x4x(32/2 - 2) = 1792
#G=Gray, Y=Yellow, B=black, O = orange, R=red, D= DGRAY = dark gray
#offset--> B D D B
# Y O D R
# B D D B
#METEOR DESIGN
#B G G G B
#G D G O R
#G G D Y O R
#G D G O R
#B G G G B
.text
.globl main
#==================================================================
#basic set-up in main for game then CALLS game_loop
main:
#resetting values
la $s7, HP #DONT CHANGE s7 s6 s5!!!
la $s6, SCREEN_HP
la $s5, TIME
li $t0, 10
#reset
sw $t0, 0($s7)
sw $t0, 0($s6)
la $s4, GAME_STATE
li $t0, 1
sw $t0, 0($s4)
sw $zero, 0($s5)
la $s0, SNACK_COUNTER
sw $zero, 0($s0)
la $s0, FREEZE_COUNTER
sw $zero, 0($s0)
jal clear_screen
jal draw_hp_bar
jal draw_ship
la $a0, OBST_POS
jal gen_randomized_obstacles
jal draw_obstacles
j game_loop
#==================================================================
#DIMENSIONS:
#1 pixel offset: 4
#1 row offset: 128
# 4096 to last row
# ---------------------------
# Basic Features in the game:
# ---------------------------
#1. Keyboard Input/Spaceship update location
# wasd - w=77, a=61, s=73, d=64 (key in ascii)
#2. Update obstacle location
# updates in game_loop (takes in with account the WAIT_TIME, same with ship)
#3. Collision checking (also border)
#Checks every game_loop
#4. Update other game state & end of game
#Updates state at the end
#5. Erase objects from old position on screen
#Erase at the end
#6. Redraw objects in new position on screen
#Erase then redraws at new position
#==================================================================
game_loop:
#GET_INPUT keys
lw $s2, 0($s5) #time
jal get_input
jal gen_snack
la $s1, GAME_STATE
lw $s0, 0($s1) #game_state
beq $s0, 2, skip_update_obstacles
#move obstacles
jal update_obstacles
jal check_collision
j continue_game
skip_update_obstacles: #keeps updating obstacles when frozen buffed
la $s1, FREEZE_COUNTER
lw $s0, 0($s1)
bgt $s0, FREEZE_TIME, remove_freeze
add $s0, $s0, 1
sw $s0, 0($s1)
j continue_game
remove_freeze: #will remove the freeze snack buff
sw $zero, 0($s1)
la $s1, GAME_STATE
li $s0, 1
sw $s0, 0($s1)
continue_game:
lw $s4, 0($s7) #HP val
lw $s3, 0($s6) #SCREEN HP val
bne $s4, $s3, update_hp
jal check_snack_eaten
jal draw_snack
j loop_game
update_hp: #updates the screen hp bar
lw $s4, 0($s7) #HP val
lw $s3, 0($s6) #SCREEN HP val
blt $s4, $s3, draw_red_ship #if HP < SCREEN_HP=> collision
jal draw_healed_ship
j update_hp_2
draw_red_ship:
jal erase_obstacles
jal gen_randomized_obstacles
jal draw_obstacles
jal draw_destroyed_ship
update_hp_2:
jal update_hp_bar
li $v0, 32
li $a0, 300
syscall
loop_game:
#WAIT_TIME
li $v0, 32
li $a0, WAIT_TIME
syscall
jal draw_ship
add $s2, $s2, 40
sw $s2, 0($s5)
j game_loop
#==================================================================
clear_screen: #clears entire screen
la $t0, BASE_ADDRESS
li $t1, BLACK
li $t5, 0
loop_clear:
bge $t5, 1024, exit_clear #to clear all pixels on screen (32x32=1024)
sw $t1, 0($t0)
addi $t5, $t5, 1
add $t0, $t0, 4
j loop_clear
exit_clear:
jr $ra
#Note: ship would not be allowed to move ONTOP of HP bar (designer choice)
draw_hp_bar: #Writes "HP" in pixels
li $t0, BASE_ADDRESS
li $t1, GREEN
li $t3, DGRAY
li $t2, 0
#offset 128x(32-4) = 3584
addi $t0, $t0, 3584
#last 3rd row offset
move $a0, $ra
jal loop_draw_hp_bar_outline #draws gray hp bar outline (top and bottom horizontal line)
li $t2, 0
jal loop_draw_hp_bar #draws green health bar
li $t2, 0
jal loop_draw_hp_bar_outline
li $t0, BASE_ADDRESS
addi $t0, $t0, 3712 #the row with the hp bar
sw $t3, 0($t0)
sw $t3, 124($t0) #last col = 124 offset, drawing bars that connect the hp_bar_outline top and bottom hr line
addi $t0, $t0, 128 #next row
sw $t3, 0($t0)
sw $t3, 124($t0)
jr $a0
loop_draw_hp_bar_outline:
beq $t2, 32, exit_draw_hp_bar #loops an entire row to draw the gray row
sw $t3, 0($t0)
addi $t0, $t0, 4
addi $t2, $t2, 1
j loop_draw_hp_bar_outline
loop_draw_hp_bar:
beq $t2, 64, exit_draw_hp_bar
sw $t1, 0($t0)
addi $t2, $t2, 1
addi $t0, $t0, 4
j loop_draw_hp_bar
exit_draw_hp_bar:
jr $ra
#draw_ship & draw_obstacles Called at the start of the program
#draw_ship fixed position at the start
#draws based on "updated" ship_pos
#ARGS: NONE
draw_ship:
#vars
#offset--> B D D B
# Y O D R
# B D D B
li $t0, BASE_ADDRESS
li $t1, DGRAY
li $t2, GRAY
li $t3, RED
li $t4, ORANGE
li $t5, YELLOW
li $t6, BLACK
la $t9, SHIP_POS
lw $t8, 8($t9)
#drawing
#first row
add $t0, $t0, $t8
sw $t6, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
#second row
addi $t0, $t0, OFFSET
sw $t5, 0($t0)
sw $t4, 4($t0)
sw $t1, 8($t0)
sw $t3, 12($t0)
#third row
addi $t0, $t0, OFFSET
sw $t6, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
jr $ra
#==================================================================
#Random generated obstacles within the border but at the right-most generated
#Cannot be at the same position or within the meteor.
gen_randomized_obstacles:
#32-4 (hpbar) - 5 (obst height) = 23 max height total
la $t9, OBST_POS
addi $t8, $zero, 26 #obst 32-6= col start
#generate randomized vals
li $v0, 42
li $a0, 0 #stored in a0
li $a1, 3 #obstacles take up 5 blocks
syscall
move $t1, $a0 #store randomized row of first obst
addi $t1, $t1, 1
li $v0, 42
li $a0, 0
li $a1, 2
syscall
add $t2, $t1, $a0
addi $t2, $t2, 8 #store randomized row of second obst
li $v0, 42
li $a0, 0
li $a1, 2
syscall
add $t3, $t2, $a0
addi $t3, $t3, 8 #store randomized row of third obst
#storing randomized vals
#1st obst
sw $t1, 0($t9)
sw $t8, 4($t9)
sll $t1, $t1, 7 #*128
sll $t8, $t8, 2 #*4
add $t1, $t1, $t8
sw $t1, 8($t9) #offset stored calculated by row * 128 + col * 4 (col default: 26, obst size: 6)
#2nd obst
addi $t8, $zero, 26
sw $t2, 12($t9)
sw $t8, 16($t9)
sll $t2, $t2, 7 #*128
sll $t8, $t8, 2 #*4
add $t2, $t2, $t8
sw $t2, 20($t9)
#3rd obst
addi $t8, $zero, 26
sw $t3, 24($t9)
sw $t8, 28($t9)
sll $t3, $t3, 7 #*128
sll $t8, $t8, 2 #*4
add $t3, $t3, $t8
sw $t3, 32($t9)
#ret
jr $ra
draw_obstacles:
#METEOR DESIGN
#B G G G B
#G D G O R
#G G D Y O R
#G D G O R
#B G G G B
li $t1, DGRAY
li $t2, GRAY
li $t3, RED
li $t4, ORANGE
li $t5, YELLOW
la $t9, OBST_POS
li $t6, 0
loop_draw_obst:
beq $t6, 3, ret_draw_obst
li $t0, BASE_ADDRESS
lw $t8, 8($t9)
add $t0, $t0, $t8 #offset
#draw
#1st row of obstacle
sw $t2, 4($t0)
sw $t2, 8($t0)
sw $t2, 12($t0)
#2nd row
addi $t0, $t0, OFFSET
sw $t2, 0($t0)
sw $t1, 4($t0)
sw $t2, 8($t0)
sw $t4, 12($t0)
sw $t3, 16($t0)
#3rd row
addi $t0, $t0, OFFSET
sw $t2, 0($t0)
sw $t2, 4($t0)
sw $t1, 8($t0)
sw $t5, 12($t0)
sw $t4, 16($t0)
sw $t3, 20($t0)
#4th row
addi $t0, $t0, OFFSET
sw $t2, 0($t0)
sw $t1, 4($t0)
sw $t2, 8($t0)
sw $t4, 12($t0)
sw $t3, 16($t0)
#5th row
addi $t0, $t0, OFFSET
sw $t2, 4($t0)
sw $t2, 8($t0)
sw $t2, 12($t0)
#increment
addi $t9, $t9, 12
addi $t6, $t6, 1
j loop_draw_obst
ret_draw_obst:
jr $ra
#==================================================================
#At the beginning of every game_loop:
#Gets user keyboard input and calls erase_ship afterwards then return back to game_loop
#W = move ship forward by 2 unit
#A = move ship upwards by 2 unit
#S = move ship backwards by 2 unit
#D = move ship downwards by 2 unit
#DEFAULT: 2 for now.
get_input:
li $t0, 0xffff0000 #this checks for input
lw $t1, 0($t0)
move $s0, $ra #storing PC back to caller $a0
beq $t1, 1, key_pressed #==1 if key pressed
jr $s0
key_pressed:
lw $t1, 4($t0) #get key-input
li $t0, BASE_ADDRESS
la $t9, SHIP_POS
lw $t6, 0($t9)
lw $t7, 4($t9)
lw $t8, 8($t9) #offset
beq $t1, 0x70,respond_to_restart
la $t5, GAME_STATE #will not respond to any wasd if game_state=0 => game_over
lw $t4, 0($t5)
beq $t4, 0, skip_key_pressed
beq $t1, 0x77, respond_to_w
beq $t1, 0x61, respond_to_a
beq $t1, 0x73, respond_to_s
beq $t1, 0x64, respond_to_d
skip_key_pressed:
jr $s0
respond_to_w:
#top-left block of ship (take in account -3 col b/c of tip and -2 for the 2 blocks added..) 32 - 5 = 27
blt $t6, 2, skip_update #first row
addi $t6, $t6, -2
sw $t6, 0($t9)
li $a1, -256
j update_ship
respond_to_a:
blt $t7, 2, skip_update
addi $t7, $t7, -2
sw $t7, 4($t9)
li $a1, -8
j update_ship
respond_to_s:
bgt $t6, 23, skip_update
addi $t6, $t6, 2
sw $t6, 0($t9)
li $a1, 256
j update_ship
respond_to_d:
bgt $t7, 27, skip_update
addi $t7, $t7, 2
sw $t7, 4($t9)
li $a1, 8
j update_ship
respond_to_restart:
j main
skip_update: #will not update when at border of the screen.
jr $s0
#==================================================================
#Called at the end of every game_loop:
#Completely erases the ship off the screen and calls update_ship then returns back to caller (get_input)
#=> Set all pixels of ship to black
#no args
draw_destroyed_ship:
li $t1, RED
j draw_colored_ship #draws red ship
draw_healed_ship:
li $t1, GREEN #draws green ship
j draw_colored_ship
erase_ship:
li $t1, BLACK
draw_colored_ship:
li $t0, BASE_ADDRESS
la $t9, SHIP_POS
lw $t8, 8($t9)
#first row
add $t0, $t0, $t8
sw $t1, 4($t0)
sw $t1, 8($t0)
#second row
addi $t0, $t0, OFFSET
sw $t1, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
sw $t1, 12($t0)
#third row
addi $t0, $t0, OFFSET
sw $t1, 4($t0)
sw $t1, 8($t0)
jr $ra
#==================================================================
#Called when either collision happens or obstacle reached end of the screen (leftmost)
erase_obstacles:
li $t1, BLACK
la $t9, OBST_POS
li $t6, 0
loop_erase_obst:
beq $t6, 3, ret_erase_obst
li $t0, BASE_ADDRESS
lw $t8, 8($t9)
add $t0, $t0, $t8 #offset
#draw
#1st row
sw $t1, 4($t0)
sw $t1, 8($t0)
sw $t1, 12($t0)
#2nd row
addi $t0, $t0, OFFSET
sw $t1, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
sw $t1, 12($t0)
sw $t1, 16($t0)
#3rd row
addi $t0, $t0, OFFSET
sw $t1, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
sw $t1, 12($t0)
sw $t1, 16($t0)
sw $t1, 20($t0)
#4th row
addi $t0, $t0, OFFSET
sw $t1, 0($t0)
sw $t1, 4($t0)
sw $t1, 8($t0)
sw $t1, 12($t0)
sw $t1, 16($t0)
#5th row
addi $t0, $t0, OFFSET
sw $t1, 4($t0)
sw $t1, 8($t0)
sw $t1, 12($t0)
#increment
addi $t9, $t9, 12
addi $t6, $t6, 1
j loop_erase_obst
ret_erase_obst:
jr $ra
#==================================================================
#Special case: reached end of screen
#Redraws the ship based on the updated position
#a0 = $ra, a1 = how much to add to the SHIP_POS + BASE_ADDRESS
#Returns back to caller(erase_ship)
update_ship:
jal erase_ship
#uses erase temp vars to save time
add $t8, $t8, $a1
sw $t8, 8($t9)
jal draw_ship
jr $s0
#==================================================================
#Moves the obstacle leftwards by 6 unit each game_loop
update_obstacles:
move $s0, $ra
jal erase_obstacles
li $t6, 0
la $t9, OBST_POS
loop_update_obst: #moves each obstacle by 1 units to the left.
beq $t6, 3, draw_updated_obst
#row stays the same
lw $t7, 4($t9) #only changes col # by 1 for now.. (speed)
lw $t8, 8($t9) #offset
blt $t7, 1, draw_new_obst #when obstacles reaches the end <1 rows => remake
addi $t7, $t7, -1
sw $t7, 4($t9)
addi $t8, $t8, -4
sw $t8, 8($t9)
#increment
addi $t6, $t6, 1
addi $t9, $t9, 12
j loop_update_obst
draw_new_obst:
jal gen_randomized_obstacles
jal draw_ship
draw_updated_obst:
jal draw_obstacles
jr $s0
#==================================================================
#Called after updating obstacle's position and ship position (if gotten user input)
check_collision:
move $s0, $ra
la $t0, SHIP_POS
la $t1, OBST_POS
lw $t2, 0($t0) #read ship row & col
lw $t3, 4($t0)
addi $t2, $t2, 1 #+1 row
addi $t3, $t3, 3 #middle ship, +3 col
li $t7, 0
loop_check_collision:
beq $t7, 3, no_collision #if i < 3 => not done checking all obstacles collision with spaceship
lw $t4, 0($t1) #read obst row & col
lw $t5, 4($t1)
addi $t4, $t4, 2 #middle of obst row and already at front of obst
addi $t5, $t5, 2
jal check_obst_collision
addi $t7, $t7, 1
addi $t1, $t1, 12
j loop_check_collision
check_obst_collision: #checking for collision
move $s1, $ra
sub $t6, $t2, $t4 #check row
jal abs_value #absolute of t6
#this checks the row and if it is < 4 then it checks the column after.
blt $t6, 4, check_obst_collision_col
jr $s1
check_obst_collision_col: #checks to see if column is within the spaceship
sub $t6, $t3, $t5
jal abs_value #absolute of t6
blt $t6, 3, minus_hp
jr $s1
minus_hp: #collision occured => minus hp
#max hp = 10, dead=0
la $t9, HP
lw $t8, 0($t9)
addi $t8, $t8, -1
sw $t8, 0($t9) #new HP val
beq $t8, 0, GAMEOVER
jr $s1
no_collision:
jr $s0
abs_value: #abs fcn
bgtz $t6, positive
addi $t8, $zero, -1
mult $t6, $t8
mflo $t6
positive: #Positive=just return
jr $ra
update_hp_bar: #updates the hp bar depending on collision or snack eaten.
# X X X
# X X X
li $t0, BASE_ADDRESS
li $t1, BLACK
#Offset 128 x (32-3) = 3712
la $t2, HP
la $t5, SCREEN_HP
lw $t6, 0($t5)
lw $t3, 0($t2) #0-10, hp values
li $t9, 0
bgt $t3, $t6, increased_hp
j change_bar
increased_hp: #increases hp when the green snack is eaten.
li $t9, 1
li $t1, GREEN
change_bar:
sw $t3, 0($t5) #update screen hp val
beq $t9, 1, increase_bar
j other_bar
increase_bar: #increase hp bar!
move $t3, $t6
other_bar: #normal collision bar
li $t4, 12
mult $t3, $t4
mflo $t3 #at the edge of bar
addi $t3, $t3, 4 #add 1 blocks to get to last 3 hp blocks (green component)
addi $t3, $t3, 3712
add $t0, $t0, $t3
#changing the bar color here!
sw $t1, 0($t0)
sw $t1, 128($t0)
sw $t1, 4($t0)
sw $t1, 132($t0)
sw $t1, 8($t0)
sw $t1, 136($t0)
jr $ra
#==================================================================
#a0 is time
#gets the # of snacks eaten!
get_score:
li $t0, 10
div $a0, $t0 #time/10
mflo $v0 #result
mfhi $v1 #remainder
jr $ra
#draw_"num" will draw the number of value $a0, at position $a3
draw_zero:
#a0 is the val to draw
#a3 pos to draw
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
jr $ra
draw_one:
#a0 is the val to draw
#a2 = $ra
#a3 pos to draw
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
sw $t1, 12($a3)
jr $ra
draw_two:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
sw $t1, 12($a3)
jr $ra
draw_three:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
jr $ra
draw_four:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
jr $ra
draw_five:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 4($a3)
sw $t1, 8($a3)
jr $ra
draw_six:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
jr $ra
draw_seven:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 4($a3)
sw $t1, 8($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
jr $ra
draw_eight:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
jr $ra
draw_nine:
li $t0, BASE_ADDRESS
li $t1, WHITE
add $a3, $t0, $a3
sw $t1, 4($a3)
sw $t1, 8($a3)
addi $a3, $a3, 128
sw $t1, 0($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 4($a3)
sw $t1, 8($a3)
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
addi $a3, $a3, 128
sw $t1, 12($a3)
jr $ra
#END OF DRAWING SCORES FUNCTION
#=========================================================
#a0 = first digit
#a1 = second digit
#max score: 99
#DRAWS THE FIRST DIGIT (TENS)
draw_score:
addi $a3, $zero, 2092 #16th row to draw + offset 44
beq $a0, 0, draw_zero
beq $a0, 1, draw_one
beq $a0, 2, draw_two
beq $a0, 3, draw_three
beq $a0, 4, draw_four
beq $a0, 5, draw_five
beq $a0, 6, draw_six
beq $a0, 7, draw_seven
beq $a0, 8, draw_eight
beq $a0, 9, draw_nine
jr $ra #undefined
#DRAWS THE SECOND DIGIT (ONES)
draw_second_score:
move $a0, $a1
addi $a3, $zero, 2116 #16th row to draw + offset 44 + 24
beq $a0, 0, draw_zero
beq $a0, 1, draw_one
beq $a0, 2, draw_two
beq $a0, 3, draw_three
beq $a0, 4, draw_four
beq $a0, 5, draw_five
beq $a0, 6, draw_six
beq $a0, 7, draw_seven
beq $a0, 8, draw_eight
beq $a0, 9, draw_nine
jr $ra #undefined
#===============================================================================
#Will generate a snack to spawn
gen_snack:
la $t0, SNACK
lw $t1, 0($t0) #1st index to check if generated yet
beq $t1, 1, check_snack_eaten #already generated== 1 => check if its eaten
li $t9, BASE_ADDRESS
#gen randomized num to determine which snack to spawn
li $v0, 42
li $a0, 0
li $a1, 2
syscall
blt $a0, 1, gen_freeze
#otherwise, gen_hp snack
li $t2, GREEN
li $t3, 1
sw $t3, 4($t0)
j spawn_snack
#freeze snack
gen_freeze:
li $t2, BLUE
sw $zero,4($t0)
#draws snack on screen based on randomized row and column
spawn_snack:
#t3 = row, t4 = col
#get randomized row
li $v0, 42
li $a0, 0
li $a1, 16
syscall
move $t3, $a0 #row
addi $t3, $t3, 1
#get randomized column
li $v0, 42
li $a0, 0
li $a1, 16 #col
syscall
move $t4, $a0
addi $t4, $t4, 1
#store in snack pos (at the end of array)
sw $t3, 12($t0) #storing row and col
sw $t4, 16($t0)
sll $t3, $t3, 7 #mul by 128
sll $t4, $t4, 2 #mul by 4
add $t3, $t3, $t4 #total offset
sw $t3, 8($t0)#store position
add $t3, $t3, $t9 #offsetted address..
li $t1, 1 #change the "generated" value to spawned = 1 = true
sw $t1, 0($t0) #indicate that it has been spawned
j snack_ret
#checks to see if the snack has been eaten .
check_snack_eaten:
la $t0, SHIP_POS
la $t1, SNACK
lw $t2, 0($t0) #spaceship rows & cols
lw $t3, 4($t0)
addi $t2, $t2, 1 #getting it centered!
addi $t3, $t3, 1
lw $t4, 12($t1) #snack rows & cols
lw $t5, 16($t1)
sub $t6, $t2, $t4 #getting the abs row difference b/w them
abs $t6, $t6
sub $t7, $t3, $t5 #getting the abs col difference b/w them
abs $t7, $t7
blt $t6, 2, check_col #checks if row is within range
j snack_ret
#checks if col is within range
check_col:
blt $t7, 3, get_eaten
j snack_ret
get_eaten: #note: will not refreeze if already in freeze => cheating!!!
sw $zero, 0($t1)
lw $t3, 12($t1) #storing row and col
lw $t4, 16($t1)
sll $t3, $t3, 7 #mul by 128
sll $t4, $t4, 2 #mul by 4
add $t3, $t3, $t4
add $t3, $t3, BASE_ADDRESS #offsetted address...
li $t5, BLACK
sw $t5, 0($t3) #erasing snack
li $t9, 0
sw $t9, 0($t1)
li $t9, 1
sw $t9, 8($t1)
lw $t2, 4($t1) #check type
la $t0, SNACK_COUNTER #increment snack counter
lw $t1, 0($t0)
add $t1, $t1, 1 #increase snack counter by 1
sw $t1, 0($t0) #update counter
beq $t2, 1, gain_hp #HP SNACK
la $t0, GAME_STATE #FREEZE SNACK
li $t9, 2
sw $t9, 0($t0) #change the game state to 2 => frozen state
j gen_snack #generates a new snack as old one was eaten\
#hp snack eaten
gain_hp:
la $t0, HP
lw $t9, 0($t0)
beq $t9, 10, max_hp
add $t9, $t9, 1 #increase hp by 1 => 10%
sw $t9, 0($t0)
#checks to see if ship already max hp => does nothing (snack)
max_hp:
j snack_ret