added player movement, gravity, and a way to check collisions

This commit is contained in:
2025-04-05 03:23:10 -04:00
parent 95f99927f4
commit 38d47f4b4a
+180 -26
View File
@@ -39,7 +39,31 @@
.data
# Store the offsets from the ship position. First 8 are pod, second 8 are service module, last 4 are legs. 20 pixels total
S: .word 8 12 516 520 524 528 1032 1036 1540 1544 1548 1552 2052 2056 2060 2064 2048 2068 2560 2580
# Store the screen behind the ship
BH: .word 0:20
sw $t8, 8($a0) # Layer 1
sw $t8, 12($a0)
sw $t8, 516($a0) # Layer 2
sw $t8, 520($a0)
sw $t8, 524($a0)
sw $t8, 528($a0)
sw $t8, 1032($a0) # Layer 3
sw $t8, 1036($a0)
sw $t9, 1540($a0) # Layer 4
sw $t9, 1544($a0)
sw $t9, 1548($a0)
sw $t9, 1552($a0)
sw $t7, 2048($a0) # Layer 5
sw $t9, 2052($a0)
sw $t9, 2056($a0)
sw $t9, 2060($a0)
sw $t9, 2064($a0)
sw $t7, 2068($a0)
sw $t7, 2560($a0) # Layer 6
sw $t7, 2580($a0)
@@ -51,8 +75,60 @@ main:
li $s0, BASE_ADDRESS # $s0 stores the base address for display
jal lev0
li $s2, 0 # initial x velocity is 0
li $s3, 0 # initial y velocity is 0 (-1 is upwards)
li $s4, 0
mainl: # Main Game loop
#
li $t0, 0xffff0000
lw $t1, 0($t0)
beq $t1, 1, inp
main1:
beq $s4, 10, gravity
main2:
jal mship
# Sleep, 5Hz
li $a0, 200
li $v0, 32
syscall
addi $s4, $s4, 1
j mainl
gravity:# Apply gravity
addi $s3, $s3, 1
li $s4, 0
j main2
inp: # Handle input
lw $t0, 4($t0) # t0 = ascii code of input
beq $t0, 114, inpr
beq $t0, 113, inpq
beq $t0, 97, inpa
beq $t0, 119, inpw
beq $t0, 100, inpd
j main1 # return to main loop
inpa: # accelerate left is pushed
addi $s2, $s2, -1
j main1
inpw: # accelerate up is pushed
addi $s3, $s3, -1
j main1
inpd: # accelerate right is pushed
addi $s2, $s2, 1
j main1
inpr:# reset if r if pressed
j main
inpq:# Exit if q if pressed
li $v0, 10 # terminate the program gracefully
syscall
@@ -79,10 +155,8 @@ lev0: # Prepare level 0
jal dmp
addi $s1, $s0, 27160 # s1 stores the players position. 27984 is spawn
la $a0 ($s1)
jal dship
lw $t0, 0($sp)
addi $sp, $sp, 4 # Return to main from the stack
jr $t0
@@ -99,8 +173,6 @@ ssl: add $t1, $t0, $a0
ss0: jr $ra
dsp: # Draw small platform. Topleft address is stored in $a0. $a0 is not modified
li $t8, 0xa4b0c8 # t8 stores blue-grey
li $t9, 0x7e848f # t9 stores grey
@@ -202,34 +274,36 @@ dlp1: jr $ra
dship: # Draw ship. Topleft address is stored in $a0. $a0 is not modified
dship: # Draw ship. Topleft address is stored in $s1. $s1 is not modified
li $t7, 0x828282 # t7 stores dark grey
li $t8, 0xa4a4a4 # t8 stores light grey
li $t9, 0xdf9213 # t9 stores gold-bronze
sw $t8, 8($a0) # Layer 1
sw $t8, 12($a0)
sw $t8, 516($a0) # Layer 2
sw $t8, 520($a0)
sw $t8, 524($a0)
sw $t8, 528($a0)
sw $t8, 1032($a0) # Layer 3
sw $t8, 1036($a0)
sw $t9, 1540($a0) # Layer 4
sw $t9, 1544($a0)
sw $t9, 1548($a0)
sw $t9, 1552($a0)
sw $t7, 2048($a0) # Layer 5
sw $t9, 2052($a0)
sw $t9, 2056($a0)
sw $t9, 2060($a0)
sw $t9, 2064($a0)
sw $t7, 2068($a0)
sw $t7, 2560($a0) # Layer 6
sw $t7, 2580($a0)
# Draw the ship using values from the ship list
li $t3, 0 # counter i
la $t0, S # t0 = &S
# Draw the first 8
dship1: lw $t1, 0($t0) # t1 = S[i]
add $t2, $s1, $t1 # t2 = &write_to
sw $t8, 0($t2) # write the pixel
addi $t0, $t0, 4
addi $t3, $t3, 1
bne $t3, 8, dship1
dship2: lw $t1, 0($t0) # t1 = S[i]
add $t2, $s1, $t1 # t2 = &write_to
sw $t9, 0($t2) # write the pixel
addi $t0, $t0, 4
addi $t3, $t3, 1
bne $t3, 16, dship2
dship3: lw $t1, 0($t0) # t1 = S[i]
add $t2, $s1, $t1 # t2 = &write_to
sw $t7, 0($t2) # write the pixel
addi $t0, $t0, 4
addi $t3, $t3, 1
bne $t3, 20, dship3
jr $ra
dplm: # Draw the ships thrust plume. Topleft is address of ships drawn address. $a0 is not modified
li $t7 0xfd2f2e # t7 stores red
li $t8 0xff8800 # t8 stores orange
@@ -335,6 +409,86 @@ dland0: sw $t9 0($t0) # Draw 6 more horizontal levels
dland1: jr $ra
land: # Return 1 in v0 iff spaceship is landed. Checks $s1 for the ships position. Landed iff neither black or white under both feet
addi $t0, $s1, 3056 # Load the pixel under left leg
beqz $t0, land1 # Not landed if white star or black space under foot
beq $t0, 0xffffff, land1
j landtr
land1: addi $t0, $s1, 3076 # Load the pixel under right leg
beqz $t0, landfa # Not landed if white star or black space under foot
beq $t0, 0xffffff, landfa
j landtr
landfa: li $v0, 0
jr $ra
landtr: li $v0, 1
jr $ra
mship : # move the ship (in $s1) by ($a0, $a1).
# 1. Write the saved pixel over the ship
# 2. save the pixels in the new position
# 3. draw the ship in the new position
# if at any point a strong collision is detected, return 1
# otherwise return 0
addi $sp, $sp, -4
sw $ra, 0($sp)
# 1. Write the saved pixel over the ship
la $t0, S # t0 = &S
la $t4, BH # t4 = $BH
li $t3, 0 # counter i
mship1: lw $t1, 0($t0) # t1 = S[i]
add $t2, $s1, $t1 # t2 = &(S[i] pixel)
lw $t5, 0($t4) # t5 = BH[i]
sw $t5, 0($t2) # write t5 to S[i] pixel
addi $t0, $t0, 4
addi $t4, $t4, 4
addi $t3, $t3, 1
bne $t3, 20, mship1
# Calculate the new position
sll $t0, $s2, 2
add $s1, $s1, $t0
sll $t0, $s3, 9
add $s1, $s1, $t0
# Reset if the player is out of screen
blt $s1, $s0, main
addi $t0, $s0, 29696
bgt $s1, $t0, main
# 2. save the pixels in the new position
li $t3, 0 # counter i
la $t0, S # t0 = &S
la $t4, BH # t4 = $BH
mship2: lw $t1, 0($t0) # t1 = S[i]
add $t2, $s1, $t1 # t2 = &(S[i] pixel)
lw $t2, 0($t2) # t2 = S[i] pixel
sw $t2, 0($t4) # BH[i] = t2
addi $t0, $t0, 4
addi $t4, $t4, 4
addi $t3, $t3, 1
bne $t3,20, mship2
# 3. draw the ship in the new position
jal dship
addi $sp, $sp, 4
lw $ra, -4($sp)
jr $ra