C. Program Description for Developers

Snake.m

List of variables:

scores – Stores the scores of the high score from the excel file.

names – Stores the names of the high score from the excel file.

[y,Fs] – Stores the audio data of the carrotnom.wav.

paused – Boolean for if the game is paused or not.

speed – This is the refresh rate of the screen. The time between each frame.

currently – If the game is running.

snake_direction – This determines the direction of the snake UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. Start snake going in the right direction.

snake_speed – This controls the space between each segment of the snake.

disp – This holds the figure object.

SCREEN_DIMENSIONS – This holds the current screen size of the monitor.

PLAYER_SCORE – The current score of the player.

running – This is if the snake is currently moving and the plot is refreshing.

WIDTH & HEIGHT – These are the dimensions of the window of the figure.

GRAPH_WIDTH & GRAPH_HEIGHT – These are the dimensions of the x and y axii. This is what the snake uses.

snake_body – This holds the values of each snake segment.

egg_position – This holds the position of the egg.

egg_plot – This holds the plot data of the egg.

txt1,txt2,txt3 – These store text data for the menus.

 

This file, snake.m, is a script file in which there is one main function, snake, which contains all the code for the game. Within this main function, subfunctions are declared and while loops are used to control the game. The list of subfunctions and their description are as follows:

mainMenu() – This method has no parameters or returns any values. This function displays the mainMenu and waits for the user to enter a key to continue while using a randomColor() to change the text color. After receiving a character input, it nulls out the main menu screen and continues the game.

countDown() – This method displays a countdown of text from 3, 2, 1, GO! on the screen waiting a second between each display.

reset() – This function utilizes updateScores() and resets all of the variables for the snake to their default position and clears the screen. Also, the egg is set to a new position and is ready to be drawn. running is then set to 1.

keyPress(src, event) – This function is called whenever a key is pressed on the disp figure. This is hooked into the figure itself and utilizes a switch statement depending on the data received as a parameter via event.key. Depending on which arrow key is pressed, it will either turn the snake clockwise or counter clock wise, using the functions, turnCCW() and turnCW(). Then sets paused to 0. Most of the time, paused will be 0, but this is utilized when the game is paused and is waiting for the user to press a key, such as in mainMenu() or closeMenu().

turnCCW() – This changes the snakes moving direction counter clock wise. This utilizes the idea that the snake can head in 4 directions, UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. This is determined by the variable snake_direction. turnCCW simply adds 1 to snake_direction to rotate the snake. If snake_direction value is 3, sets it back to 0.

turnCW() – This changes the snakes moving direction clock wise. This utilizes the idea that the snake can head in 4 directions, UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. This is determined by the variable snake_direction. turnCW simply subtracts 1 to snake_direction to rotate the snake. If snake_direction value is 0, sets it back to 3.

drawSnake() – This updates the X and Y data of the snake and redraws it.

moveSnake() – This function increase the snake positions by setting the entire body excluding the head = to the current position of
the head and body minus the last body length, essentially removing the last body segment. Then depending on the snake_direction, sets the new head location in the correct direction exactly snake_speed away from the last segment.

growSnake() – This function receives an integer parameter, size, which determines how many snake segments are added. This function works by finding the difference between the second to last body segment and the last segment, then uses this difference to find the new position of the new segment. Essentially, this difference tells the direction of how the snake will grow.

bool=collision() – This function returns a boolean value if the snake is colliding. If the function returns a value of 1 if there is a collision, 0 if there isn’t.

collisionEgg() – This function checks if the snake head is within 1 unit of the marker position, if so, grow snake and reset egg.

setEgg() – This function gets a random egg position and updates the egg on the plot.

getEggPosition() – This function retrieves a random x y coordinate that is not colliding with the snake and within bounds, then sets that as the new egg_position.

setScore() – This function updates the score below the x axis.

rgb=randomColor() – This function returns a 1×3 array of random values between 0-1, essentially a random RGB value.

updateScores() – This function updates the high score variables from the file highscores.xlsx, variables scores and names.

newHighScore() – This function takes in the index of the new high score and then shifts the scores and names accordingly, also it receives the name value from the user by using a inputdlg.

writeScores() – This function writes the current values of scores and names to the Excel file in the appropriate columns.

arr=displayScoreBoard() – This function creates the string for the score board that will be displayed in a text object.

closeMenu() – This function displays the closing menu including the players score, the high score board, and if the player made the Big Ten or not. Then prompts the user to continue or not.

MATLAB Built-in commands used:

close – Closes all windows

clear – Clears all variables in the workspace.

clc – Clears the command window.

xlsread(‘filename’) – This reads xls files and imports the data.

audioread(‘audio’) – This reads in audio data.

figure() – This is the main figure.

get() – This is used a lot to retrieve data from an object. There is no specific use.

set() – This is used to set data within an object. There is no specific use.

box on – This encapsulated the graph with a border.

axis() – This defined the axis size.

hold on – This allowed the plot to be actively updated.

zeros() – This preallocated space for matrices in order to speed up the game.

randi() – This generates a random integer.

plot() – This plots an object on the graph.

title() – This sets the title of the figure.

ismember() – This function tells if a certain matrix is apart of another larger matrix. More efficient than directly scanning a matrix via for loops.

sound() – This plays the sound data given.

xlabel() – This sets the x axis label.

inputdlg() – This function prompts the user with a yes or no.

xlswrite() – This writes data to an xls file.

sprintf() – Similar to fprintf, returns a formatted string.

text() – This function displays text on the graph.

questdlg() – This prompts the user and asks for a string in return.

isequal() – Direct way to compare two values which is faster with matrices.

pause() – This pauses the program for a certain amount of time.

How the main program functions:

Snake.m functions between two main while loops. The first main while loop is if the program is still running. The second main while loop, dependent on variable running, controls the frame rate and moves the snake. Basically, the snake position is updated, the snake is drawn, collisions are checked, then the program pauses for a very small amount of time determined by variable speed. This is the refresh rate of the game or the FPS. While the snake is moving, running will be true. After a collision is detected, running will be set to false and the while currently loop will continue. This will then display the closeMenu and depending on the users answer, reset the data and end, or repeat the currently loop, countdown, and then start the while running loop again. Then, if both loops break, the windows will close and the snake function will end.

The snake data and segments are stored in a matrix of [x y, x y, … ] called snake_body in which each row is the x y data of one segment of the snake. This data is then used to be plot. The actual segments of the snake are markers used on a normal graph. The size of these markers were increased and due to the closeness of the markers to each other and the amount, it gives the appearance of a snake on the screen. This snake_body is then plotted under snake_plot, which the x and y data is then updated as the snake moves. The square in which the snake moves is actually a recolored Cartesian plot that is normally used in MATLAB, just in disguise. The axis of this plot go from 0 to 50 in the X and the Y with the origin being in the bottom left corner. The egg functions the same way as the snake, except there is only 1 segment of the egg therefore only one set of X Y values. The egg is an asterisk marker with a randomly generated color.

Tron.m

List of variables:

paused – Boolean for if the game is paused or not.

speed – This is the refresh rate of the screen. The time between each frame.

currently – If the game is running.

player1_direction – This determines the direction of the player 1 UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. Start player 1 going in the right direction.

player2_direction – This determines the direction of the player 2 UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. Start player 2 going in the left direction.

bike_speed – This controls the rate at which the players move.

disp – This holds the figure object.

SCREEN_DIMENSIONS – This holds the current screen size of the monitor.

PLAYER1_SCORE – The current score of the player.

PLAYER2_SCORE – The current score of the player.

running – This is if the snake is currently moving and the plot is refreshing.

GRAPH_WIDTH & GRAPH_HEIGHT – These are the dimensions of the x and y axii. This is what the snake uses.

PLOT_DIMENSIONS – Screen dimensions.

XTick – Scale of the x axis

YTick – Scale of the y axis

player1_body – This holds the values of each player position.

player2_body – This holds the values of each player position.

player1_plot – This holds the plot object data for player 1.

player2_plot – This holds the plot object data for player 2.

txt1,txt2,txt3 – These store text data for the menus.

 

This file, tron.m, is a script file in which there is one main function, tron, which contains all the code for the game. Within this main function, subfunctions are declared and while loops are used to control the game. The list of subfunctions and their description are as follows:

mainMenu() – This method has no parameters or returns any values. This function displays the mainMenu and waits for the user to enter a key to continue while using a randomColor() to change the text color. After receiving a character input, it nulls out the main menu screen and continues the game.

countDown() – This method displays a countdown of text from 3, 2, 1, GO! on the screen waiting a second between each display.

reset() – This function utilizes updateScores() and resets all of the variables for the snake to their default position and clears the screen. Also, the egg is set to a new position and is ready to be drawn. running is then set to 1.

keyPress(src, event) – This function is called whenever a key is pressed on the disp figure. This is hooked into the figure itself and utilizes a switch statement depending on the data received as a parameter via event.key. Depending on which arrow key is pressed, it will either turn the snake clockwise or counter clock wise, using the functions, turnCCW() and turnCW(). Then sets paused to 0. Most of the time, paused will be 0, but this is utilized when the game is paused and is waiting for the user to press a key, such as in mainMenu() or closeMenu().

newDir=turnCCW(curDir) – This function receives the current direction of the player. Then it returns the new direction of the player if rotated CCW. This utilizes the idea that the player can head in 4 directions, UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. This is determined by the variable curDir. turnCCW simply adds 1 to curDir to rotate the players movement. If curDir value is 3, sets it back to 0.

turnCW() -This function receives the current direction of the player. Then it returns the new direction of the player if rotated CW. This utilizes the idea that the player can head in 4 directions, UP – 1 , RIGHT – 0 , DOWN – 3 , LEFT – 2. This is determined by the variable curDir. turnCW simply subtracts 1 to curDir to rotate the player. If curDir value is 0, sets it back to 3.

drawLine() – This updates the X and Y data of the two players.

player_body=movePlayers(currentDirection,player_body) – This function increase the current player’s positions by setting the entire body excluding the head equal to the current position of
the head and body minus the last body length, essentially removing the last body segment. Then depending on the currentDirection, sets the new head location in the correct direction exactly player_speed away from the last segment.

growSnake() – This function receives an integer parameter, size, which determines how many snake segments are added. This function works by finding the difference between the second to last body segment and the last segment, then uses this difference to find the new position of the new segment. Essentially, this difference tells the direction of how the snake will grow.

bool=collision() – This function returns a boolean value if the player is colliding. If the function returns a value of 1 if there is a collision, 0 if there isn’t.

 

setScore() – This function updates the score below the x axis.

rgb=randomColor() – This function returns a 1×3 array of random values between 0-1, essentially a random RGB value.

addScore() – This function will only be called when a collision has occurred. This function checks which player has made the collision and then increases the other players score by 1.

 

closeMenu() – This function displays the closing menu including the players score, the high score board, and if the player made the Big Ten or not. Then prompts the user to continue or not.

MATLAB Built-in commands used:

close – Closes all windows

clear – Clears all variables in the workspace.

clc – Clears the command window.

figure() – This is the main figure.

get() – This is used a lot to retrieve data from an object. There is no specific use.

set() – This is used to set data within an object. There is no specific use.

box on – This encapsulated the graph with a border.

axis() – This defined the axis size.

hold on – This allowed the plot to be actively updated.

zeros() – This preallocated space for matrices in order to speed up the game.

randi() – This generates a random integer.

plot() – This plots an object on the graph.

title() – This sets the title of the figure.

ismember() – This function tells if a certain matrix is apart of another larger matrix. More efficient than directly scanning a matrix via for loops.

sound() – This plays the sound data given.

xlabel() – This sets the x axis label.

inputdlg() – This function prompts the user with a yes or no.

sprintf() – Similar to fprintf, returns a formatted string.

text() – This function displays text on the graph.

isequal() – Direct way to compare two values which is faster with matrices.

pause() – This pauses the program for a certain amount of time.

How the main program functions:

Tron.m functions between two main while loops. The first main while loop is if the program is still running. The second main while loop, dependent on variable running, controls the frame rate and moves the players. Basically, the player position is updated, the players are drawn, collisions are checked, then the program pauses for a very small amount of time determined by variable speed. This is the refresh rate of the game or the FPS. While the players are moving, running will be true. After a collision is detected, running will be set to false and the while currently loop will continue. This will then update the score and if the score of either player is greater than 2, the closeMenu will display. If not, the players will be reset and the countDown will run again. The closeMenu will display the winner and prompt to play again. If the user selects no, then the main loop will end and the function tron will stop.

The players move and function in the same way described in Snake.m above.