CS112 Final Project
Abigail Perry
Phase I

Project description:
I will create the game called "Snake" that is commonly found on cell phones (or at least they used to be... now they have fancy smancy games!) using MATLAB's GUIDE program. Rules:

  1. The user controls the motion of the snake. The snake can move up, down, left, right.
  2. You lose if you (the snake) runs into one of the four walls (the borders of the screen) or also if you run into yourself (if it does, it's game over).
  3. You gain points by eating the food that is randomly displayed on the screen, one at a time. To eat the food, you simply run the snake's head into it.
  4. In addition, the snake generally comes with different levels. The harder the level is, faster the snake moves.

A little more on how the snake moves: once you press up, down, left, or right, the direction of the snake changes. Each element of the snake (its body) changes direction when it gets to the point initially occupied by the snake's head.

Further, everytime the snake eats, its length increases, making it harder for the user to avoid running the snake into itself.

Coding for the game:

Phase I: My first goal is to get a snake of fixed length to move around the screen that listens to the command of the user (move up, down, left, right). Thus the snake only moves a single increment every time the user presses a button. Also I would like the random dots of fodo to appear on the screen that the snake can eat.

Phase II: The snake will increase in length every time it eats. Further, the snake will continue moving in the same direction until the user tells it otherwise (if this is possible to program).

Phase III: Keep track of scores. Every time the snake eats, the user gains points. Also create different levels. Harder the level, the faster the snake moves.

Functions:

  1. I need a function that creates the motion of the snake. Given the input of the user (the direction), this function will tell the snake where to go. This function will change the coordinates of each single element of the snake. As said before, each successive body part element of the snake after the head only changes direction when it comes to the initial position of the head (right before it changes direction).

    Everytime the snake moves an increment, a "new" body part is created in the direction of motion of the snake, and the last element of the snake is removed. This is how the snake will move around the screen. Therefore I will need a vector that stores all points of the snake. In the initial phase when the snake does not change lengths, the number of points for the coordinates of the snake will remain constant. In the second phase, the vector will accommodate the increase in the snake length.

    The snake will consist of blocks of squares "tied" together. This way it does not take as long for the snake's images to be created for each rame (it would take more points, and therefore more time to create a snake made up of little starts, for example).

  2. I also need a function that creates a randomly positioned food item for everytime the snake eats food. The food will be the size of a single element of the snake, so that when the snake runs into it, it covers it completely. Therefore, knowing the position of the food, I can write code that says: if the coordinates of the head of the snake are equal to that of the food, then the snake has eaten and points are gained.
  3. A function to keep track of the user's score. In the initial phase, the score will increase by a fixed amount every time the snake eats food. In the second phase, the amount of points for food will increase as the snake increases in length and the game gets harder.

What my GUI display might look like: