Simon Atkinson 201255483

Dependencies:   mbed

Snake Game

Summary

Hello and welcome to my Snake Game. Snake is a simple game where you control a snake and eat an apple which increases your size. Normally touching the walls will kill you, always touching yourself will make you die!

Controls

The Controls for this game are simple the Start button starts the game (who would have thought) this button is the left button on the bottom of the gamepad next to the two blue potentiometers. The Reset button on the right of the potentiometers resets the game. Once the game starts use the left thumbstick to control the movement of the snake.

If you hit the wall with your snake you will die and the game will end.

Bugs/Missing Features

Unfortunatley I wasn't able to get the game eating the apple to work, when I tried I could never get it to detect the collisions I tried a few different ways but ran out of time. Because that doesn't work the score doesn't increase and the apple doesn't spawn in a different place. I will try continue to work on this when I have time as I enjoyed doing this project even though it was very frustrating at times!

Revision:
10:3e37b58e8600
Parent:
9:25597bc0cecc
Child:
11:ba20e1b516a1
--- a/Snake/Snake.cpp	Fri Jun 05 10:51:30 2020 +0000
+++ b/Snake/Snake.cpp	Fri Jun 05 18:45:05 2020 +0000
@@ -11,69 +11,69 @@
 
 }
 
-void Snake::init(int x,int y, int speed, int score)
+void Snake::init(int x,int y, int score, int speed) //Initial values for the snake
 {
-    _x = WIDTH/2;
+    _x = WIDTH/2; // Sets the position to be in the middle of the screen
     _y = HEIGHT/2;
-    _speed = speed;
-    _score = score;  
+    _speed = speed; // The value that holds the speed of the snake
+
 
 }
 
 void Snake::draw(N5110 &lcd)
 {
+ 
     // draw Snake in screen buffer. 
-  lcd.drawRect(_x,_y,5,5,FILL_BLACK);  // Draws the initial snake head 
-}
-
-void Snake::control(Gamepad &pad, N5110 &lcd)
-{
-                    {
-            if (pad.A_pressed()){
-                        dir = RIGHT;
-                        }
-             if (pad.B_pressed()){
-                        dir = DOWN;
-                        }
-             if (pad.X_pressed()){
-                        dir = UP;
-                        }
-             if (pad.Y_pressed()){
-                        dir = LEFT;
-                        }       
-             if (pad.Y_pressed()){
-                        dir = LEFT;
-                        }  
-    if (dir == UP) {
-                 --_y;
-                 }
-    if   (dir == DOWN) {
-                 ++_y;
-                 }  
-    if   (dir == LEFT) {
-                --_x;
-                 }  
-    if   (dir == RIGHT) {
-                ++_x;
-                 }  
-                         
-                           
- 
-}
+  lcd.drawRect(_x,_y,5,5,FILL_BLACK);  // Draws the initial snake head is a 5x5 Square matches well with the cirlce of the apple when set to 2
 
 }
 
-void Snake::add_score()
+
+void Snake::update(Direction d,float mag)
+{
+              
+                 _x += _move.x;        //Needed to move in the x-axis 
+                 _y += _move.y;        //Needed to move in the y-axis
+              _speed = int(mag*1.5f);  // Sets the scaling for the speed the snake moves, by adjusting this you can change the speed of the snake.
+              
+                         //When moving the thubstick this is read by the game and controls the movement of the snake, 
+                         //moving it a small ammount will make the snake move slowly holding down for too long and your snake will get some speed!
+            if (d == N){
+                        _move.y -=_speed;  //  Snake Moves Up
+                        _move.x = 0;      // Makes it so the snake can only go in compass directions (N,E,S,W) same for all directions
+                        }
+            if (d == S){
+                        _move.y +=_speed; // Snake moves Down
+                        _move.x = 0;
+                        } 
+             if (d == E){
+                        _move.y = 0;
+                        _move.x +=_speed;  // Snake moves right
+                        } 
+             if (d == W){
+                        _move.y = 0;
+                        _move.x -=_speed; // Snake moves Left
+                        } 
+
+}
+
+void Snake::add_score()  // Not in use yet but will add to score when apple is ate by the snake
 {
     _score++;
 }
-int Snake::get_score()
+int Snake::get_score() //Allows the game to get the value of the score
 {
     return _score;
 }
 
+Vector2D Snake::get_move() {  // Allows the game to see the value stored in the movement(m) which shows how fast the snake will move
+ 
+    Vector2D m = {_move.x, _move.y};    
+    return m;
+}
 
-Vector2D Snake::get_pos() {
-    Vector2D s = {_x,_y};
-    return s;    
+
+Vector2D Snake::get_pos() { //Gets the position of the snake this will be used for collision detection soon
+    Vector2D pos = {_x,_y};
+    return pos;    
 }
\ No newline at end of file