Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: snake/snake.cpp
- Revision:
- 20:980b37fde361
- Parent:
- 18:e58a1f8e72ad
diff -r dfa241957855 -r 980b37fde361 snake/snake.cpp
--- a/snake/snake.cpp Tue May 08 12:51:40 2018 +0000
+++ b/snake/snake.cpp Tue May 08 13:41:08 2018 +0000
@@ -1,6 +1,5 @@
#include "snake.h"
-
snake::snake(){
}
@@ -10,19 +9,20 @@
}
-void snake::init(){
- for (int i = 0; i <_point ; i++) {
+void snake::init(){ //initialise
+ for (int i = 0; i <_point ; i++) {//the initial length of snake = 6 pixel. i++ until it = 6 .
+ //the position both old and new x are changing as the snake move to east initially. And old and new y keep same position.
_xoldpos[i] = 30;
_yoldpos[i] = 23;
_xnewpos[i] = 30 ;
_ynewpos[i] = 23;
}
- dir_snake = 3;
+ dir_snake = 3; // 3 = east . initial direction
_array = 100;
_point = 6;
}
-
+//direction of the snake
void snake::update(Direction d,float mag){
if (dir_snake == 1){
s() ;}
@@ -43,7 +43,7 @@
}
-
+//draw the snake . the length of the snake is changing as the got point changing
void snake::draw(N5110 &lcd)
{
for(int i=0; i<_point; i++) {
@@ -51,13 +51,13 @@
}
}
-Vector2D snake::get_pos()
+Vector2D snake::get_pos()//get the position of snake in 2D vector. From pong sample code
{
Vector2D p = {_xnewpos[0],_ynewpos[0]};
return p;
}
-void snake::set_pos(Vector2D p)
+void snake::set_pos(Vector2D p)//set the position in 2D vector . From pong sample code
{
_xnewpos[0] = p.x;
_ynewpos[0] = p.y;
@@ -65,21 +65,21 @@
}
-void snake::point()
+void snake::point()//To add the point that the snake ate.
{
_point++;
}
-int snake::get_point()
+int snake::get_point()//used to display the number of point
{
return _point -6 ;
}
-void snake::n() {
- _xnewpos[0] = _xoldpos[0] ;
+void snake::n() {//snake move to north
+ _xnewpos[0] = _xoldpos[0] ;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
}
@@ -95,7 +95,7 @@
}
-void snake::s(){
+void snake::s(){//snake move to south
_xnewpos[0] = _xoldpos[0] ;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
@@ -112,7 +112,7 @@
}
-void snake::w(){
+void snake::w(){//snake move to west
_xnewpos[0] = _xoldpos[0]-1;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];
@@ -129,7 +129,7 @@
}
-void snake::e(){
+void snake::e(){//snake move to east
_xnewpos[0] = _xoldpos[0]+1;
for (int i = 0; i < _array; i++) {
_xnewpos[i+1] = _xoldpos[i];