ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18s2a_2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Snake.cpp Source File

Snake.cpp

00001 #include "Snake.h"
00002 
00003 // nothing doing in the constructor and destructor
00004 Snake::Snake()
00005 {
00006 
00007 }
00008 
00009 Snake::~Snake()
00010 {
00011 
00012 }
00013 
00014 void Snake::init(int x,int y, int score, int speed, int snakeheight, int snakewidth) //Initial values for the snake
00015 {
00016     _x = WIDTH/2; // Sets the position to be in the middle of the screen
00017     _y = HEIGHT/2;
00018     _score = score;
00019     _speed = speed; // The value that holds the speed of the snake
00020     _snakewidth = snakewidth;
00021     _snakeheight = snakeheight;
00022 
00023 
00024 }
00025 
00026 void Snake::draw(N5110 &lcd)
00027 {
00028  
00029     // draw Snake in screen buffer. 
00030   lcd.drawRect(_x,_y,_snakewidth,_snakeheight,FILL_BLACK);  // Draws the initial snake head is a 5x5 Square matches well with the cirlce of the apple when set to 2
00031 
00032 }
00033 
00034 
00035 void Snake::update (Direction d,float mag)
00036 {
00037               
00038                  _x += _move.x;        //Needed to move in the x-axis 
00039                  _y += _move.y;        //Needed to move in the y-axis
00040               _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.
00041               
00042                          //When moving the thubstick this is read by the game and controls the movement of the snake, 
00043                          //moving it a small ammount will make the snake move slowly holding down for too long and your snake will get some speed!
00044             if (d == N){
00045                         _move.y -=_speed;  //  Snake Moves Up
00046                         _move.x = 0;      // Makes it so the snake can only go in compass directions (N,E,S,W) same for all directions
00047                         }
00048             if (d == S){
00049                         _move.y +=_speed; // Snake moves Down
00050                         _move.x = 0;
00051                         } 
00052              if (d == E){
00053                         _move.y = 0;
00054                         _move.x +=_speed;  // Snake moves right
00055                         } 
00056              if (d == W){
00057                         _move.y = 0;
00058                         _move.x -=_speed; // Snake moves Left
00059                         } 
00060 
00061 
00062 /*  Deadly Wall turned on
00063 
00064    //Uncomment this and comment the section in SnakeEngine.cpp if you want the snake to not die when he touches the edges as this will keep 
00065    //him in safely in the game area 
00066 
00067 
00068     // Check that the Snakes coordinates aren't greater the the size of the screen which 
00069     // would mean the snake has gone off the screen
00070              if (_x>=(WIDTH-6)) { // 5 pixles for snake and 1 pixle for border
00071                _x=(WIDTH-6);
00072                     }
00073             if (_x<=1) {   // 1 pixle offset due to border
00074                  _x = 1;
00075                         }
00076             if (_y >=(HEIGHT-6)) { // 5 pixles for snake and 1 pixle for border
00077                 _y=(HEIGHT-6);
00078                    }
00079              if (_y<=9) {    // Checks that its in the game area as there is a line (8 pixels missing due to scoredisplay and 1 pixle due to border) 
00080                   _y=9;
00081                     }
00082       
00083        // End of Save wall Section   */  
00084           
00085                  
00086 }
00087 
00088 void Snake::add_score()  // Not in use yet but will add to score when apple is ate by the snake
00089 {
00090     _score++;
00091 }
00092 int Snake::get_score () //Allows the game to get the value of the score
00093 {
00094     return _score;
00095 }
00096 
00097 Vector2D Snake::get_move () {  // Allows the game to see the value stored in the movement(m) which shows how fast the snake will move
00098  
00099     Vector2D m = {_move.x, _move.y};    
00100     return m;
00101 }
00102 
00103 
00104 Vector2D Snake::get_pos () { //Gets the position of the snake this will be used for collision detection soon
00105     Vector2D p = {_x,_y};
00106     return p;    
00107 }