COMP 271 Sdsu

I’m going to post 2 of my mips codes that i’m trying to fix . I only need one. my game is about where arthe player is ‘@’, obstacles are ‘#’, and the finish line is ‘ #include
#include
#include
#include
#include
#define ROWS 10
#define COLS 10
#define TIME_LIMIT_SECONDS 60 // Set your desired time limit in seconds
int posX, posY; // Character position
char gameMap[ROWS][COLS];
int score = 0; // Game score
struct termios originalTermios; // Store the original terminal settings
time_t startTime; // Store the start time
void clearScreen() {
printf(“\033[H\033[J”); // ANSI escape code to clear screen
}
void disableRawMode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &originalTermios);
}
void enableRawMode() {
tcgetattr(STDIN_FILENO, &originalTermios);
atexit(disableRawMode);
struct termios raw = originalTermios;
raw.c_lflag &= ~(ECHO | ICANON); // Disable echo and canonical mode
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void initializeGame() {
// Initialize the game map with empty spaces
for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { gameMap[i][j] = ' '; } } // Place the character at the center posX = ROWS / 2; posY = COLS / 2; gameMap[posX][posY] = '@'; // Place obstacles in random positions for (int k = 0; k < 3; ++k) { int obsX, obsY; do { obsX = rand() % ROWS; obsY = rand() % COLS; } while (gameMap[obsX][obsY] != ' '); // Find a new empty position gameMap[obsX][obsY] = '#'; } // Place the finish line at a random position int finishX, finishY; do { finishX = rand() % ROWS; finishY = rand() % COLS; } while (gameMap[finishX][finishY] != ' ' && (finishX != posX || finishY != posY)); // Avoid player's position and obstacles gameMap[finishX][finishY] = '$'; // Record the start time startTime = time(NULL); } void printGame() { clearScreen(); // Clear console screen // Print the game map for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { printf("%c ", gameMap[i][j]); } printf("\n"); } // Print the current score and time remaining time_t currentTime = time(NULL); int timeRemaining = TIME_LIMIT_SECONDS - (int)(currentTime startTime); printf("Score: %d Time: %d seconds remaining\n", score, timeRemaining); } void moveCharacter(char direction) { // Move the character based on the direction gameMap[posX][posY] = ' '; // Clear current position switch (direction) { case 'w': posX--; break; case 's': posX++; break; case 'a': posY--; break; case 'd': posY++; break; } // Check for collisions with obstacles if (gameMap[posX][posY] == '#') { printf("Game Over! You collided with an obstacle.\n"); exit(0); } // Check for reaching the finish line if (gameMap[posX][posY] == '$') { printf("Congratulations! You reached the finish line.\n"); score++; // Increment the score initializeGame(); // Reset the game for the next round } gameMap[posX][posY] = '@'; // Set new position } int main() { char move; srand(time(NULL)); initializeGame(); enableRawMode(); // Enable raw mode for non-blocking input while (1) { printGame(); // Non-blocking input without conio.h if (read(STDIN_FILENO, &move, 1) == 1) { // Check for quitting if (move == 'q' || move == 'Q') { break; } // Move the character moveCharacter(move); // Check for time limit time_t currentTime = time(NULL); if (currentTime - startTime >= TIME_LIMIT_SECONDS) {
printf(“Time’s up! Game Over.\n”);
break;
}
}
}
return 0;
}
.data
ROWS: .word 10
COLS: .word 10
TIME_LIMIT: .word 60
numObstacles: .word 3
score: .word 0
gameMap: .space 100 # 10×10 game map
posX: .word 5
# X position of the character
posY: .word 5
# Y position of the character
intro_msg: .asciiz “Welcome to the game!\n”
instructions: .asciiz “You are ‘@’, obstacles are ‘#’, and the finish line is ‘$’.\nUse ‘w’ to move up,
‘s’ to move down, ‘a’ to move left, and ‘d’ to move right.\nAvoid the obstacles and reach the
finish line to score points.\nPress ‘q’ or ‘Q’ to quit the game.\nYou have 60 seconds. Good
luck!\n”
gameOver_msg: .asciiz “Game Over! You collided with an obstacle.\n”
finish_msg: .asciiz “Congratulations! You reached the finish line.\n”
timeUp_msg: .asciiz “Time’s up! Game Over.\n”
score_msg: .asciiz “Score: ”
time_msg: .asciiz ” Time: %d seconds remaining\n”
TIME_LIMIT_SECONDS: .word 60
.text
.globl main
main:
# Seed random number generator
li $v0, 30
# Syscall 30 is for getting the current time in MARS
syscall
move $a0, $v0 # Use the current time as the seed
li $v0, 41
# Syscall 41 is for seeding the random number generator in MARS
syscall
# Call initializeGame to set up the map
jal initializeGame
# Print game introduction
la $a0, intro_msg # Load intro message address
li $v0, 4
# syscall for printing string
syscall
# Print instructions
la $a0, instructions # Load instructions address
li $v0, 4
# syscall for printing string
syscall
# Wait for the first key press
li $v0, 12
# syscall code for reading character
li $a1, 1
# Read a single character with no echo
syscall
# Call printGame to display the game state
jal printGame
loop:
# Read user input
li $v0, 12
# syscall code for reading character
li $a1, 1
# Read a single character with no echo
syscall
move $t0, $v0 # Store user input in $t0
# Check for quitting
beq $t0, ‘q’, exit # Branch if ‘q’ entered
beq $t0, ‘Q’, exit # Branch if ‘Q’ entered
# Check for movement keys (optional, depending on your game’s logic)
beq $t0, ‘w’, move_up
# If input is ‘w’, jump to move_up
beq $t0, ‘a’, move_left # If input is ‘a’, jump to move_left
beq $t0, ‘s’, move_down # If input is ‘s’, jump to move_down
beq $t0, ‘d’, move_right # If input is ‘d’, jump to move_right
# If input is not ‘q’, ‘Q’, ‘w’, ‘a’, ‘s’, or ‘d’, continue looping
j loop
initializeGame:
# Fill gameMap with spaces
li $t1, 0
# Loop counter for rows
loop_row:
li $t2, 0
# Loop counter for columns
loop_col:
li $t0, 32
# Character code for space (ASCII ‘ ‘)
mul $t3, $t1, 10
# Calculate row offset
add $t3, $t3, $t2
# Add column offset
sb $t0, gameMap($t3)
# Store space in gameMap
addi $t2, $t2, 1
# Increment column counter
blt $t2, 10, loop_col # Loop until all columns filled
addi $t1, $t1, 1
# Increment row counter
blt $t1, 10, loop_row # Loop until all rows filled
# Place character at center
li $t1, 5
# Calculate center row
li $t2, 5
# Calculate center column
li $t0, 64
# Character code for ‘@’ (player)
mul $t3, $t1, 10 # Calculate row offset
add $t3, $t3, $t2 # Add column offset
sb $t0, gameMap($t3) # Store ‘@’ at new position
# Place obstacles (replace with a loop for multiple obstacles)
li $t0, 35
# Character code for ‘#’ (obstacle)
li $t1, 0
# Loop counter for obstacles
li $t2, 3
# Number of obstacles
place_obstacles:
# Generate a random position for the obstacle
li $v0, 42 # Syscall 42 is for generating a random number in MARS
syscall
remu $v0, $v0, 100 # Ensure the random position is within the bounds of the 10×10 game
map
# Store the obstacle at the random position
sb $t0, gameMap($v0)
addi $t1, $t1, 1
blt $t1, $t2, place_obstacles
# Place finish line (random position)
li $t0, 36
# Character code for ‘$’ (finish line)
# Generate a random position for the finish line
li $v0, 42 # Syscall 42 is for generating a random number in MARS
syscall
remu $v0, $v0, 100 # Ensure the random position is within the bounds of the 10×10 game
map
# Store the finish line at the random position
sb $t0, gameMap($v0)
# Return from the function
jr $ra
printGame:
# Clear console screen
li $v0, 12
# syscall code for clear screen
syscall
# Print each row of the game map
li $t1, 0
# Loop counter for rows
loop_row_print:
# Print a newline character
li $v0, 10
# syscall for printing newline
syscall
# Print each character in the current row
li $t2, 0
# Loop counter for columns
loop_col_print:
mul $t3, $t1, 10
# Calculate row offset
add $t3, $t3, $t2 # Add column offset
lb $t0, gameMap($t3) # Load character at current position
move $a0, $t0
# Set character code to print
li $v0, 11
# syscall for printing character
syscall
addi $t2, $t2, 1
# Increment column counter
blt $t2, 10, loop_col_print # Loop until all columns in the row printed
addi $t1, $t1, 1
# Increment row counter
blt $t1, 10, loop_row_print # Loop until all rows printed
# Print current score and remaining time
la $a0, score_msg
# Load address of score message string
li $v0, 4
# syscall for printing string
syscall
jal get_time
# Hypothetical function to get current time
sub $t2, $t2, $v0
# Calculate time difference (current – start)
lw $t3, TIME_LIMIT_SECONDS
# Load time limit
sub $t4, $t3, $t2
# Calculate remaining time
la $a0, time_msg
# Load address of time message string
li $v0, 4
# syscall for printing string
syscall
# Print the remaining time value
move $a0, $t4
# Set remaining time as argument for printing
li $v0, 1
# syscall for printing integer (may differ on platforms)
syscall
# Return from the function
jr $ra
moveCharacter:
# Move the character based on input
beq $t0, ‘w’, move_up
# If input is ‘w’, jump to move_up
beq $t0, ‘a’, move_left # If input is ‘a’, jump to move_left
beq $t0, ‘s’, move_down # If input is ‘s’, jump to move_down
beq $t0, ‘d’, move_right # If input is ‘d’, jump to move_right
j loop
# If input is not one of ‘w’, ‘a’, ‘s’, ‘d’, continue looping
move_up:
# Move the character up
addi $t1, $t1, -1
# Decrement row counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
move_left:
# Move the character left
addi $t2, $t2, -1
# Decrement column counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
move_down:
# Move the character down
addi $t1, $t1, 1
# Increment row counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
move_right:
# Move the character right
addi $t2, $t2, 1
# Increment column counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
check_and_move:
# Check boundaries and update position if valid
blt $t1, 0, stay
# If row is before top boundary, branch to stay
bge $t1, 10, stay
# If row is after bottom boundary, branch to stay
blt $t2, 0, stay
# If column is before left boundary, branch to stay
bge $t2, 10, stay
# If column is after right boundary, branch to stay
# Check for collision or reaching the finish line
# Calculate the effective address
mul $t3, $t1, 10
# Multiply the row index by the number of columns (10)
add $t3, $t3, $t2 # Add the column index
# Load the byte at the calculated memory address
lb $t3, gameMap($t3) # Load character at new position
beq $t3, ‘#’, collision
# If character is ‘#’, branch to collision
beq $t3, ‘$’, finish_line # If character is ‘$’, branch to finish_line
# Update character position in gameMap
li $t4, 64
# Character code for ‘@’ (player)
# Calculate the offset
mul $t3, $t1, 10
# Multiply the row index by the number of columns (10)
add $t3, $t3, $t2 # Add the column index
# Store the value in $t4 at the calculated memory address
sb $t4, gameMap($t3) # Store ‘@’ at new position
# Clear previous position (overwrite with space)
li $t5, 32
# Character code for space
# Calculate the offset
mul $t3, $t1, 10
# Multiply the row index by the number of columns (10)
add $t3, $t3, $t2 # Add the column index
# Store the value in $t5 at the calculated memory address
sb $t5, gameMap($t3) # Store space at old position
j stay
# Jump to stay
stay:
# Update character’s position back to the original position
move $t1, $t4 # Restore the original row from $t4
move $t2, $t5 # Restore the original column from $t5
jr $ra
# Return from the function
collision:
# Print collision message
la $a0, gameOver_msg
# Load address of game over message string
li $v0, 4
# syscall for printing string
syscall
j loop
# Jump back to loop
finish_line:
# Print finish line message
la $a0, finish_msg
# Load address of finish message string
li $v0, 4
# syscall for printing string
syscall
# Increment score
lw $t6, score
addi $t6, $t6, 1
sw $t6, score
# Load the score from memory
# Increment the score
# Store the updated score back to memory
jal initializeGame
j loop
# Reset the game for the next round
# Jump back to loop
checkTimeLimit:
jal get_time
sub $t2, $t2, $v0
lw $t3, TIME_LIMIT
sub $t4, $t3, $t2
# Check if time’s up
bltz $t4, time_up
# Get current time
# Calculate time difference (current – start)
# Load time limit
# Calculate remaining time
# If time is less than zero, branch to time_up
# If time is not up, return 1
li $v0, 1
# Set return value to 1 (not time up)
jr $ra
# Return from the function
time_up:
# Print time’s up message
la $a0, timeUp_msg
# Load address of time’s up message string
li $v0, 4
# syscall for printing string
syscall
# Exit the loop
li $v0, 0
jr $ra
# Set return value to 0 (time up)
# Return from the function
get_time:
# Hypothetical function to get current time
# Placeholder for actual implementation
li $v0, 42
# syscall code for get current time (not available on all MIPS simulators)
syscall
jr $ra
# Return from the function
exit:
# Print score
la $a0, score_msg
# Load address of score message string
li $v0, 4
# syscall for printing string
syscall
lw $a0, score
# Load score
li $v0, 1
syscall
# syscall for printing integer
# Print time remaining
jal get_time
# Get current time
sub $t2, $t2, $v0
# Calculate time difference (current – start)
lw $t3, TIME_LIMIT_SECONDS
# Load time limit
sub $t4, $t3, $t2
# Calculate remaining time
la $a0, time_msg
# Load address of time message string
li $v0, 4
# syscall for printing string
syscall
move $a0, $t4
# Set remaining time as argument for printing
li $v0, 1
# syscall for printing integer
syscall
# Exit the program
li $v0, 10
# syscall code for exit
syscall
.data
ROWS: .word 10
COLS: .word 10
numObstacles: .word 3
score: .word 0
gameMap: .space 100 # 10×10 game map
frameBuffer: .space 0x80000 # 512 wide x 256 high pixels
xVel: .word 0 # x velocity start 0
yVel: .word 0 # y velocity start 0
xPos: .word 50 # x position
yPos: .word 27 # y position
shadow: .word 7624 # location of rail on bitmap display
FinishX: .word 32 # Finish x position
FinishY: .word 16 # Finish y position
PlayerColor: .word 0x00ff0000 # red pixel for player ‘@’
ObstacleColor: .word 0x0000ff00 # green pixel for obstacles ‘#’
FinishColor: .word 0x000000ff # blue pixel for finish line ‘$’
xConversion: .word 64 # x value for converting xPos to bitmap display
yConversion: .word 4 # y value for converting (x, y) to bitmap display
KEY_INPUT: .word 0xFFFF0004 # Address of MMIO simulator for keyboard input
numRows: .word 10
numCols: .word 10
intro_msg: .asciiz “Welcome to the game!\n”
instructions: .asciiz “You are ‘@’, obstacles are ‘#’, and the finish line is ‘$’.\nUse ‘w’ to move up,
‘s’ to move down, ‘a’ to move left, and ‘d’ to move right.\nAvoid the obstacles and reach the
finish line to score points.\nPress ‘q’ or ‘Q’ to quit the game.\nGood luck!\n”
gameOver_msg: .asciiz “Game Over! You collided with an obstacle.\n”
finish_msg: .asciiz “Congratulations! You reached the finish line.\n”
timeUp_msg: .asciiz “Time’s up! Game Over.\n”
score_msg: .asciiz “Score: ”
time_msg: .asciiz ” Time: %d seconds remaining\n”
.text
.globl main
main:
# Initialize frame buffer
jal initFrameBuffer
# Seed random number generator
li $v0, 30
# Syscall 30 is for getting the current time in MARS
syscall
move $a0, $v0 # Use the current time as the seed
li $v0, 41
syscall
# Syscall 41 is for seeding the random number generator in MARS
# Print game introduction
jal printIntro
# Print instructions
jal printInstructions
# Initialize game state
jal initGame
# Start game loop
jal gameLoop
# Exit game
j exit
initFrameBuffer:
# Initialize frame buffer without filling background color
jr $ra
printIntro:
# Print game introduction message
li $v0, 4
# syscall for printing string
la $a0, intro_msg # Load intro message address
syscall
jr $ra
printInstructions:
# Print game instructions
li $v0, 4
# syscall for printing string
la $a0, instructions # Load instructions address
syscall
jr $ra
initGame:
# Initialize game state
# Set player position
lw $t0, xPos
lw $t1, yPos
lw $t2, xConversion
mult $t1, $t2
mflo $t3
add $t3, $t3, $t0
lw $t2, yConversion
mult $t3, $t2
mflo $t0
la $t1, frameBuffer
add $t0, $t1, $t0
lw $t2, shadow
sw $t2, 0($t0)
lw $t2, PlayerColor
sw $t2, 0($t0)
addi $t0, $t0, -256
sw $t2, 0($t0)
# Set finish line position
lw $t0, FinishX
lw $t1, FinishY
lw $t2, xConversion
mult $t1, $t2
mflo $t3
add $t3, $t3, $t0
lw $t2, yConversion
mult $t3, $t2
mflo $t0
la $t1, frameBuffer
add $t0, $t1, $t0
lw $t2, FinishColor
sw $t2, 0($t0)
# Place obstacles randomly
lw $t0, numRows
lw $t1, numCols
lw $t2, numObstacles
li $t3, 0
add $t4, $t0, $t1
# Calculate the total number of cells
sub $t4, $t4, 1
# Subtract 1 to avoid placing obstacles on the finish line
addi $t5, $t4, 1
# Increment by 1 to include the upper bound
add $t6, $t0, $t1
# Use a different register for the upper bound calculation
subi $t6, $t6, 1
li $t7, 0x00230000 # ASCII code for ‘#’
placeObstaclesLoop:
beq $t3, $t2, initGameDone # Check if all obstacles have been placed
li $v0, 42
# Syscall 42 is for generating random integer
move $a0, $t4
# Load lower bound
move $a1, $t5
# Load upper bound
syscall
move $t8, $v0
# Store random number in $t8
li $v0, 42
# Syscall 42 is for generating random integer
move $a0, $t0
# Load lower bound
move $a1, $t6
# Load upper bound
syscall
move $t9, $v0
# Store random number in $t9
mult $t9, $t1
# Multiply random number with number of columns
mflo $t9
# Result in $t9
add $t9, $t9, $t8
# Add random column index to random row index
sll $t9, $t9, 2
# Multiply the cell index by 4 to align memory access to word boundary
lw $t0, frameBuffer # Load frame buffer address
add $t0, $t0, $t9
# Calculate the address of the obstacle cell
sw $t7, 0($t0)
# Place obstacle in the cell
addi $t3, $t3, 1
# Increment obstacle counter
j placeObstaclesLoop # Repeat until all obstacles are placed
initGameDone:
jr $ra
gameLoop:
# Get keyboard input
lw $t0, KEY_INPUT
# Load address of MMIO simulator for keyboard input
lb $t1, 0($t0)
# Load keyboard input
# Check for quit command
li $t2, ‘q’
# Load ASCII value of ‘q’
beq $t1, $t2, exit
# Check if ‘q’ is pressed
# Move character based on keyboard input
li $t2, ‘w’
# Load ASCII value of ‘w’
beq $t1, $t2, moveUp # Check if ‘w’ is pressed
li $t2, ‘s’
# Load ASCII value of ‘s’
beq $t1, $t2, moveDown # Check if ‘s’ is pressed
li $t2, ‘a’
# Load ASCII value of ‘a’
beq $t1, $t2, moveLeft # Check if ‘a’ is pressed
li $t2, ‘d’
# Load ASCII value of ‘d’
beq $t1, $t2, moveRight # Check if ‘d’ is pressed
j gameLoop
moveUp:
# Move player up
lw $t0, yPos
subi $t0, $t0, 1
sw $t0, yPos
# Load current y position
# Move up by decrementing y position
# Store updated y position
# Implement code to update frame buffer for player’s new position when moving up
# This part will update the frame buffer to reflect the player’s new position after moving up
j gameLoop
moveDown:
# Move player down
lw $t0, yPos
# Load current y position
addi $t0, $t0, 1 # Move down by incrementing y position
sw $t0, yPos
# Store updated y position
# Implement code to update frame buffer for player’s new position when moving down
# This part will update the frame buffer to reflect the player’s new position after moving down
j gameLoop
moveLeft:
# Move player left
lw $t0, xPos
# Load current x position
subi $t0, $t0, 1 # Move left by decrementing x position
sw $t0, xPos
# Store updated x position
# Implement code to update frame buffer for player’s new position when moving left
# This part will update the frame buffer to reflect the player’s new position after moving left
j gameLoop
moveRight:
# Move player right
lw $t0, xPos
# Load current x position
addi $t0, $t0, 1 # Move right by incrementing x position
sw $t0, xPos
# Store updated x position
# Implement code to update frame buffer for player’s new position when moving right
# This part will update the frame buffer to reflect the player’s new position after moving right
j gameLoop
exit:
# Exit the program
li $v0, 10 # syscall code for exit
syscall

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

. when you type those letters in the keyboard , the player should move: ‘w’ to move up, ‘s’ to move down, ‘a’ to move left, and ‘d’ to move right. I want to have obstacles in the game, so the player can try to avoid them to get to the finish line and reach the finish line to score points. if you press ‘q’ or ‘Q’ to quit the game. you got 60 seconds to score every point or you will lose.

#include
#include
#include
#include
#include
#define ROWS 10
#define COLS 10
#define TIME_LIMIT_SECONDS 60 // Set your desired time limit in seconds
int posX, posY; // Character position
char gameMap[ROWS][COLS];
int score = 0; // Game score
struct termios originalTermios; // Store the original terminal settings
time_t startTime; // Store the start time
void clearScreen() {
printf(“\033[H\033[J”); // ANSI escape code to clear screen
}
void disableRawMode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &originalTermios);
}
void enableRawMode() {
tcgetattr(STDIN_FILENO, &originalTermios);
atexit(disableRawMode);
struct termios raw = originalTermios;
raw.c_lflag &= ~(ECHO | ICANON); // Disable echo and canonical mode
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void initializeGame() {
// Initialize the game map with empty spaces
for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { gameMap[i][j] = ' '; } } // Place the character at the center posX = ROWS / 2; posY = COLS / 2; gameMap[posX][posY] = '@'; // Place obstacles in random positions for (int k = 0; k < 3; ++k) { int obsX, obsY; do { obsX = rand() % ROWS; obsY = rand() % COLS; } while (gameMap[obsX][obsY] != ' '); // Find a new empty position gameMap[obsX][obsY] = '#'; } // Place the finish line at a random position int finishX, finishY; do { finishX = rand() % ROWS; finishY = rand() % COLS; } while (gameMap[finishX][finishY] != ' ' && (finishX != posX || finishY != posY)); // Avoid player's position and obstacles gameMap[finishX][finishY] = '$'; // Record the start time startTime = time(NULL); } void printGame() { clearScreen(); // Clear console screen // Print the game map for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { printf("%c ", gameMap[i][j]); } printf("\n"); } // Print the current score and time remaining time_t currentTime = time(NULL); int timeRemaining = TIME_LIMIT_SECONDS - (int)(currentTime startTime); printf("Score: %d Time: %d seconds remaining\n", score, timeRemaining); } void moveCharacter(char direction) { // Move the character based on the direction gameMap[posX][posY] = ' '; // Clear current position switch (direction) { case 'w': posX--; break; case 's': posX++; break; case 'a': posY--; break; case 'd': posY++; break; } // Check for collisions with obstacles if (gameMap[posX][posY] == '#') { printf("Game Over! You collided with an obstacle.\n"); exit(0); } // Check for reaching the finish line if (gameMap[posX][posY] == '$') { printf("Congratulations! You reached the finish line.\n"); score++; // Increment the score initializeGame(); // Reset the game for the next round } gameMap[posX][posY] = '@'; // Set new position } int main() { char move; srand(time(NULL)); initializeGame(); enableRawMode(); // Enable raw mode for non-blocking input while (1) { printGame(); // Non-blocking input without conio.h if (read(STDIN_FILENO, &move, 1) == 1) { // Check for quitting if (move == 'q' || move == 'Q') { break; } // Move the character moveCharacter(move); // Check for time limit time_t currentTime = time(NULL); if (currentTime - startTime >= TIME_LIMIT_SECONDS) {
printf(“Time’s up! Game Over.\n”);
break;
}
}
}
return 0;
}
.data
ROWS: .word 10
COLS: .word 10
TIME_LIMIT: .word 60
numObstacles: .word 3
score: .word 0
gameMap: .space 100 # 10×10 game map
posX: .word 5
# X position of the character
posY: .word 5
# Y position of the character
intro_msg: .asciiz “Welcome to the game!\n”
instructions: .asciiz “You are ‘@’, obstacles are ‘#’, and the finish line is ‘$’.\nUse ‘w’ to move up,
‘s’ to move down, ‘a’ to move left, and ‘d’ to move right.\nAvoid the obstacles and reach the
finish line to score points.\nPress ‘q’ or ‘Q’ to quit the game.\nYou have 60 seconds. Good
luck!\n”
gameOver_msg: .asciiz “Game Over! You collided with an obstacle.\n”
finish_msg: .asciiz “Congratulations! You reached the finish line.\n”
timeUp_msg: .asciiz “Time’s up! Game Over.\n”
score_msg: .asciiz “Score: ”
time_msg: .asciiz ” Time: %d seconds remaining\n”
TIME_LIMIT_SECONDS: .word 60
.text
.globl main
main:
# Seed random number generator
li $v0, 30
# Syscall 30 is for getting the current time in MARS
syscall
move $a0, $v0 # Use the current time as the seed
li $v0, 41
# Syscall 41 is for seeding the random number generator in MARS
syscall
# Call initializeGame to set up the map
jal initializeGame
# Print game introduction
la $a0, intro_msg # Load intro message address
li $v0, 4
# syscall for printing string
syscall
# Print instructions
la $a0, instructions # Load instructions address
li $v0, 4
# syscall for printing string
syscall
# Wait for the first key press
li $v0, 12
# syscall code for reading character
li $a1, 1
# Read a single character with no echo
syscall
# Call printGame to display the game state
jal printGame
loop:
# Read user input
li $v0, 12
# syscall code for reading character
li $a1, 1
# Read a single character with no echo
syscall
move $t0, $v0 # Store user input in $t0
# Check for quitting
beq $t0, ‘q’, exit # Branch if ‘q’ entered
beq $t0, ‘Q’, exit # Branch if ‘Q’ entered
# Check for movement keys (optional, depending on your game’s logic)
beq $t0, ‘w’, move_up
# If input is ‘w’, jump to move_up
beq $t0, ‘a’, move_left # If input is ‘a’, jump to move_left
beq $t0, ‘s’, move_down # If input is ‘s’, jump to move_down
beq $t0, ‘d’, move_right # If input is ‘d’, jump to move_right
# If input is not ‘q’, ‘Q’, ‘w’, ‘a’, ‘s’, or ‘d’, continue looping
j loop
initializeGame:
# Fill gameMap with spaces
li $t1, 0
# Loop counter for rows
loop_row:
li $t2, 0
# Loop counter for columns
loop_col:
li $t0, 32
# Character code for space (ASCII ‘ ‘)
mul $t3, $t1, 10
# Calculate row offset
add $t3, $t3, $t2
# Add column offset
sb $t0, gameMap($t3)
# Store space in gameMap
addi $t2, $t2, 1
# Increment column counter
blt $t2, 10, loop_col # Loop until all columns filled
addi $t1, $t1, 1
# Increment row counter
blt $t1, 10, loop_row # Loop until all rows filled
# Place character at center
li $t1, 5
# Calculate center row
li $t2, 5
# Calculate center column
li $t0, 64
# Character code for ‘@’ (player)
mul $t3, $t1, 10 # Calculate row offset
add $t3, $t3, $t2 # Add column offset
sb $t0, gameMap($t3) # Store ‘@’ at new position
# Place obstacles (replace with a loop for multiple obstacles)
li $t0, 35
# Character code for ‘#’ (obstacle)
li $t1, 0
# Loop counter for obstacles
li $t2, 3
# Number of obstacles
place_obstacles:
# Generate a random position for the obstacle
li $v0, 42 # Syscall 42 is for generating a random number in MARS
syscall
remu $v0, $v0, 100 # Ensure the random position is within the bounds of the 10×10 game
map
# Store the obstacle at the random position
sb $t0, gameMap($v0)
addi $t1, $t1, 1
blt $t1, $t2, place_obstacles
# Place finish line (random position)
li $t0, 36
# Character code for ‘$’ (finish line)
# Generate a random position for the finish line
li $v0, 42 # Syscall 42 is for generating a random number in MARS
syscall
remu $v0, $v0, 100 # Ensure the random position is within the bounds of the 10×10 game
map
# Store the finish line at the random position
sb $t0, gameMap($v0)
# Return from the function
jr $ra
printGame:
# Clear console screen
li $v0, 12
# syscall code for clear screen
syscall
# Print each row of the game map
li $t1, 0
# Loop counter for rows
loop_row_print:
# Print a newline character
li $v0, 10
# syscall for printing newline
syscall
# Print each character in the current row
li $t2, 0
# Loop counter for columns
loop_col_print:
mul $t3, $t1, 10
# Calculate row offset
add $t3, $t3, $t2 # Add column offset
lb $t0, gameMap($t3) # Load character at current position
move $a0, $t0
# Set character code to print
li $v0, 11
# syscall for printing character
syscall
addi $t2, $t2, 1
# Increment column counter
blt $t2, 10, loop_col_print # Loop until all columns in the row printed
addi $t1, $t1, 1
# Increment row counter
blt $t1, 10, loop_row_print # Loop until all rows printed
# Print current score and remaining time
la $a0, score_msg
# Load address of score message string
li $v0, 4
# syscall for printing string
syscall
jal get_time
# Hypothetical function to get current time
sub $t2, $t2, $v0
# Calculate time difference (current – start)
lw $t3, TIME_LIMIT_SECONDS
# Load time limit
sub $t4, $t3, $t2
# Calculate remaining time
la $a0, time_msg
# Load address of time message string
li $v0, 4
# syscall for printing string
syscall
# Print the remaining time value
move $a0, $t4
# Set remaining time as argument for printing
li $v0, 1
# syscall for printing integer (may differ on platforms)
syscall
# Return from the function
jr $ra
moveCharacter:
# Move the character based on input
beq $t0, ‘w’, move_up
# If input is ‘w’, jump to move_up
beq $t0, ‘a’, move_left # If input is ‘a’, jump to move_left
beq $t0, ‘s’, move_down # If input is ‘s’, jump to move_down
beq $t0, ‘d’, move_right # If input is ‘d’, jump to move_right
j loop
# If input is not one of ‘w’, ‘a’, ‘s’, ‘d’, continue looping
move_up:
# Move the character up
addi $t1, $t1, -1
# Decrement row counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
move_left:
# Move the character left
addi $t2, $t2, -1
# Decrement column counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
move_down:
# Move the character down
addi $t1, $t1, 1
# Increment row counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
move_right:
# Move the character right
addi $t2, $t2, 1
# Increment column counter
jal check_and_move
# Call check_and_move function
j loop
# Continue looping
check_and_move:
# Check boundaries and update position if valid
blt $t1, 0, stay
# If row is before top boundary, branch to stay
bge $t1, 10, stay
# If row is after bottom boundary, branch to stay
blt $t2, 0, stay
# If column is before left boundary, branch to stay
bge $t2, 10, stay
# If column is after right boundary, branch to stay
# Check for collision or reaching the finish line
# Calculate the effective address
mul $t3, $t1, 10
# Multiply the row index by the number of columns (10)
add $t3, $t3, $t2 # Add the column index
# Load the byte at the calculated memory address
lb $t3, gameMap($t3) # Load character at new position
beq $t3, ‘#’, collision
# If character is ‘#’, branch to collision
beq $t3, ‘$’, finish_line # If character is ‘$’, branch to finish_line
# Update character position in gameMap
li $t4, 64
# Character code for ‘@’ (player)
# Calculate the offset
mul $t3, $t1, 10
# Multiply the row index by the number of columns (10)
add $t3, $t3, $t2 # Add the column index
# Store the value in $t4 at the calculated memory address
sb $t4, gameMap($t3) # Store ‘@’ at new position
# Clear previous position (overwrite with space)
li $t5, 32
# Character code for space
# Calculate the offset
mul $t3, $t1, 10
# Multiply the row index by the number of columns (10)
add $t3, $t3, $t2 # Add the column index
# Store the value in $t5 at the calculated memory address
sb $t5, gameMap($t3) # Store space at old position
j stay
# Jump to stay
stay:
# Update character’s position back to the original position
move $t1, $t4 # Restore the original row from $t4
move $t2, $t5 # Restore the original column from $t5
jr $ra
# Return from the function
collision:
# Print collision message
la $a0, gameOver_msg
# Load address of game over message string
li $v0, 4
# syscall for printing string
syscall
j loop
# Jump back to loop
finish_line:
# Print finish line message
la $a0, finish_msg
# Load address of finish message string
li $v0, 4
# syscall for printing string
syscall
# Increment score
lw $t6, score
addi $t6, $t6, 1
sw $t6, score
# Load the score from memory
# Increment the score
# Store the updated score back to memory
jal initializeGame
j loop
# Reset the game for the next round
# Jump back to loop
checkTimeLimit:
jal get_time
sub $t2, $t2, $v0
lw $t3, TIME_LIMIT
sub $t4, $t3, $t2
# Check if time’s up
bltz $t4, time_up
# Get current time
# Calculate time difference (current – start)
# Load time limit
# Calculate remaining time
# If time is less than zero, branch to time_up
# If time is not up, return 1
li $v0, 1
# Set return value to 1 (not time up)
jr $ra
# Return from the function
time_up:
# Print time’s up message
la $a0, timeUp_msg
# Load address of time’s up message string
li $v0, 4
# syscall for printing string
syscall
# Exit the loop
li $v0, 0
jr $ra
# Set return value to 0 (time up)
# Return from the function
get_time:
# Hypothetical function to get current time
# Placeholder for actual implementation
li $v0, 42
# syscall code for get current time (not available on all MIPS simulators)
syscall
jr $ra
# Return from the function
exit:
# Print score
la $a0, score_msg
# Load address of score message string
li $v0, 4
# syscall for printing string
syscall
lw $a0, score
# Load score
li $v0, 1
syscall
# syscall for printing integer
# Print time remaining
jal get_time
# Get current time
sub $t2, $t2, $v0
# Calculate time difference (current – start)
lw $t3, TIME_LIMIT_SECONDS
# Load time limit
sub $t4, $t3, $t2
# Calculate remaining time
la $a0, time_msg
# Load address of time message string
li $v0, 4
# syscall for printing string
syscall
move $a0, $t4
# Set remaining time as argument for printing
li $v0, 1
# syscall for printing integer
syscall
# Exit the program
li $v0, 10
# syscall code for exit
syscall
.data
ROWS: .word 10
COLS: .word 10
numObstacles: .word 3
score: .word 0
gameMap: .space 100 # 10×10 game map
frameBuffer: .space 0x80000 # 512 wide x 256 high pixels
xVel: .word 0 # x velocity start 0
yVel: .word 0 # y velocity start 0
xPos: .word 50 # x position
yPos: .word 27 # y position
shadow: .word 7624 # location of rail on bitmap display
FinishX: .word 32 # Finish x position
FinishY: .word 16 # Finish y position
PlayerColor: .word 0x00ff0000 # red pixel for player ‘@’
ObstacleColor: .word 0x0000ff00 # green pixel for obstacles ‘#’
FinishColor: .word 0x000000ff # blue pixel for finish line ‘$’
xConversion: .word 64 # x value for converting xPos to bitmap display
yConversion: .word 4 # y value for converting (x, y) to bitmap display
KEY_INPUT: .word 0xFFFF0004 # Address of MMIO simulator for keyboard input
numRows: .word 10
numCols: .word 10
intro_msg: .asciiz “Welcome to the game!\n”
instructions: .asciiz “You are ‘@’, obstacles are ‘#’, and the finish line is ‘$’.\nUse ‘w’ to move up,
‘s’ to move down, ‘a’ to move left, and ‘d’ to move right.\nAvoid the obstacles and reach the
finish line to score points.\nPress ‘q’ or ‘Q’ to quit the game.\nGood luck!\n”
gameOver_msg: .asciiz “Game Over! You collided with an obstacle.\n”
finish_msg: .asciiz “Congratulations! You reached the finish line.\n”
timeUp_msg: .asciiz “Time’s up! Game Over.\n”
score_msg: .asciiz “Score: ”
time_msg: .asciiz ” Time: %d seconds remaining\n”
.text
.globl main
main:
# Initialize frame buffer
jal initFrameBuffer
# Seed random number generator
li $v0, 30
# Syscall 30 is for getting the current time in MARS
syscall
move $a0, $v0 # Use the current time as the seed
li $v0, 41
syscall
# Syscall 41 is for seeding the random number generator in MARS
# Print game introduction
jal printIntro
# Print instructions
jal printInstructions
# Initialize game state
jal initGame
# Start game loop
jal gameLoop
# Exit game
j exit
initFrameBuffer:
# Initialize frame buffer without filling background color
jr $ra
printIntro:
# Print game introduction message
li $v0, 4
# syscall for printing string
la $a0, intro_msg # Load intro message address
syscall
jr $ra
printInstructions:
# Print game instructions
li $v0, 4
# syscall for printing string
la $a0, instructions # Load instructions address
syscall
jr $ra
initGame:
# Initialize game state
# Set player position
lw $t0, xPos
lw $t1, yPos
lw $t2, xConversion
mult $t1, $t2
mflo $t3
add $t3, $t3, $t0
lw $t2, yConversion
mult $t3, $t2
mflo $t0
la $t1, frameBuffer
add $t0, $t1, $t0
lw $t2, shadow
sw $t2, 0($t0)
lw $t2, PlayerColor
sw $t2, 0($t0)
addi $t0, $t0, -256
sw $t2, 0($t0)
# Set finish line position
lw $t0, FinishX
lw $t1, FinishY
lw $t2, xConversion
mult $t1, $t2
mflo $t3
add $t3, $t3, $t0
lw $t2, yConversion
mult $t3, $t2
mflo $t0
la $t1, frameBuffer
add $t0, $t1, $t0
lw $t2, FinishColor
sw $t2, 0($t0)
# Place obstacles randomly
lw $t0, numRows
lw $t1, numCols
lw $t2, numObstacles
li $t3, 0
add $t4, $t0, $t1
# Calculate the total number of cells
sub $t4, $t4, 1
# Subtract 1 to avoid placing obstacles on the finish line
addi $t5, $t4, 1
# Increment by 1 to include the upper bound
add $t6, $t0, $t1
# Use a different register for the upper bound calculation
subi $t6, $t6, 1
li $t7, 0x00230000 # ASCII code for ‘#’
placeObstaclesLoop:
beq $t3, $t2, initGameDone # Check if all obstacles have been placed
li $v0, 42
# Syscall 42 is for generating random integer
move $a0, $t4
# Load lower bound
move $a1, $t5
# Load upper bound
syscall
move $t8, $v0
# Store random number in $t8
li $v0, 42
# Syscall 42 is for generating random integer
move $a0, $t0
# Load lower bound
move $a1, $t6
# Load upper bound
syscall
move $t9, $v0
# Store random number in $t9
mult $t9, $t1
# Multiply random number with number of columns
mflo $t9
# Result in $t9
add $t9, $t9, $t8
# Add random column index to random row index
sll $t9, $t9, 2
# Multiply the cell index by 4 to align memory access to word boundary
lw $t0, frameBuffer # Load frame buffer address
add $t0, $t0, $t9
# Calculate the address of the obstacle cell
sw $t7, 0($t0)
# Place obstacle in the cell
addi $t3, $t3, 1
# Increment obstacle counter
j placeObstaclesLoop # Repeat until all obstacles are placed
initGameDone:
jr $ra
gameLoop:
# Get keyboard input
lw $t0, KEY_INPUT
# Load address of MMIO simulator for keyboard input
lb $t1, 0($t0)
# Load keyboard input
# Check for quit command
li $t2, ‘q’
# Load ASCII value of ‘q’
beq $t1, $t2, exit
# Check if ‘q’ is pressed
# Move character based on keyboard input
li $t2, ‘w’
# Load ASCII value of ‘w’
beq $t1, $t2, moveUp # Check if ‘w’ is pressed
li $t2, ‘s’
# Load ASCII value of ‘s’
beq $t1, $t2, moveDown # Check if ‘s’ is pressed
li $t2, ‘a’
# Load ASCII value of ‘a’
beq $t1, $t2, moveLeft # Check if ‘a’ is pressed
li $t2, ‘d’
# Load ASCII value of ‘d’
beq $t1, $t2, moveRight # Check if ‘d’ is pressed
j gameLoop
moveUp:
# Move player up
lw $t0, yPos
subi $t0, $t0, 1
sw $t0, yPos
# Load current y position
# Move up by decrementing y position
# Store updated y position
# Implement code to update frame buffer for player’s new position when moving up
# This part will update the frame buffer to reflect the player’s new position after moving up
j gameLoop
moveDown:
# Move player down
lw $t0, yPos
# Load current y position
addi $t0, $t0, 1 # Move down by incrementing y position
sw $t0, yPos
# Store updated y position
# Implement code to update frame buffer for player’s new position when moving down
# This part will update the frame buffer to reflect the player’s new position after moving down
j gameLoop
moveLeft:
# Move player left
lw $t0, xPos
# Load current x position
subi $t0, $t0, 1 # Move left by decrementing x position
sw $t0, xPos
# Store updated x position
# Implement code to update frame buffer for player’s new position when moving left
# This part will update the frame buffer to reflect the player’s new position after moving left
j gameLoop
moveRight:
# Move player right
lw $t0, xPos
# Load current x position
addi $t0, $t0, 1 # Move right by incrementing x position
sw $t0, xPos
# Store updated x position
# Implement code to update frame buffer for player’s new position when moving right
# This part will update the frame buffer to reflect the player’s new position after moving right
j gameLoop
exit:
# Exit the program
li $v0, 10 # syscall code for exit
syscall

Are you stuck with your online class?
Get help from our team of writers!

Order your essay today and save 20% with the discount code RAPID