Program Description for Developers

Assumptions

  • the player is able to interface with the screen via hands or stylus
  • the Proteus is functional
  • the player can read English

Variables

Constants

  • frame
    • the set “frame-rate” for the gameplay
    • the value is equivalent to the number of seconds in between each snake movement
  • size
    • the factor by which the play area is scaled
    • the game is structured in a grid with the side length of each cube being ‘size’ number of pixels wide or tall

Class Snake Variables

  • booleans
    • fed
      • the value of fed is true if the snake’s head has intersected with the location of the food and false if not
      • This variable is used to control snake growth and food generation
  • integers
    • len
      • the length of the snake
      • equivalent to the score
    • highscore
      • the highest value of len reached by the player over all of their games
    • x[] and y[]
      • arrays of snake body segment coordinates that are used in snake movement and collision detection
    • applex and appley
      • the coordinate locations of the last generated food
      • used to check if the snake has been fed and in food generation

Main Function Variables

  • booleans
    • game
      • the value of game is true if currently playing and false if in the menus
      • the value is used to stay in the game loop, end the game when the snake is killed, start the game, etc.
  • characters
    • direction
      • the currently set direction of travel for the snake
      • can be changed by the user via the input function and is used in the move function to coordinate snake movement
      • initialized as ‘L’ to make the snake move in the left direction
    • button
      • the current menu option the player has selected.
      • this is assigned a character A-D that corresponds to the main menu options via the MenuInput function
      • initialized as ‘Z’ to ensure that the program doesn’t automatically select a menu option
  • float
    • x and y
      • throwaway variables used when waiting for the player to touch the screen
      • stores the coordinate pair of the last spot the player touched on the screen
    • t
      • stored time at the start of a frame to be used in conjunction with the given TimeNow() function and frame to cause slow down the framerate of the game

Local function variables

  • input()
    • floats
      • x and y
        • stores the coordinate of the last place touched by the user
        • this is then used to modify direction based on the region touched by the user
  • checkcollision(bool *)
    •  integers
      • n
        • used as a counter for a for loop
  • move(char *, bool *)
    • integers
      • n
        • used as a counter for a loop
      • x2[] and y2[]
        • used to make copies of the x[] and y[] snake body arrays for use in shifting the array values and drawing the head of the snake
  • spawnfood()
    • integers
      • n
        • used as a counter for a loop
      • m
        • used to ensure that the food doesn’t spawn inside the snake
        • if the food is in the snake m is incremented
        • m is initialized at 0 and then later checked
        • depending on the end state of m, valid is modified
    • booleans
      • valid
        • used to check if the spawn location of food is valid and to continue generating new food coordinates as long as its value is false
        • this ensures no food is spawned inside the snake
  • MenuInput(char *)
    • floats
      • xC and yC
        • used to store the x and y coordinates of the user input, and then later used to determine which menu option was selected based on the region of the screen the coordinates are in
      • xJunk and yJunk
        • used as throw away variables to store and then discard incorrect negative coordinates that are registered

Functions

  • Snake Class
    • void checkcollision(bool *);
      • checks if the head of the snake has intersected with itself, the walls, or food, and changes variables accordingly
      • Ex.
        player.checkcollision(&game);
    • void move(char *, bool *);
      • makes the first n coordinate pairs of the snake’s segments shift one element down the snake body arrays and then makes the first element of the arrays the coordinates of the previous first element plus the direction input by the player
      • it then erases the last element of the array by drawing over it
        • UNLESS there has been food eaten in the current frame, in which case the last element is not erased and another element of the x and y array is occupied, lengthening the snake
      • Ex.
        player.move(&direction,&game);
    • void spawnfood();
      • spawns an apple at a position not on the snake by generating random coordinates and then checking the validity of them
      • Ex.
        player.spawnfood();
    • void DrawMenu();
      • draws all elements of the main menu
      • Ex.
        player.DrawMenu();
    • void MenuInput(char *);
      • takes player input and assigns a value to variable button to be used in the main menu switch case
      • Ex.
        player.MenuInput(&button);
  • void input(char *);
    • checks if and where the screen has been touched and changes the variable direction accordingly
    • this allows the user to control the snake via screen inputs
    • Ex.
      input(&direction);

Performance/Limitations

  • The code performs as expected, but is limited by the processing speeds of the Proteus controller. Because of this, the value of frame is not an actual representation of the time it takes for the snake to move, as it doesn’t factor in the processing time of the Proteus. Also as a result of this, there are some minor impacts on the performance. For example, the head of the snake is drawn a split second before the tail is erased. However, these impacts on the code are minimal and do not interfere with gameplay.
  • Some parts of the code could also have been structured more efficiently and better organized into class functions to help increase performance