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_engine/snake_engine.cpp
- Revision:
- 20:980b37fde361
- Parent:
- 18:e58a1f8e72ad
- Child:
- 21:7f7d09a27cc8
--- a/snake_engine/snake_engine.cpp Tue May 08 12:51:40 2018 +0000 +++ b/snake_engine/snake_engine.cpp Tue May 08 13:41:08 2018 +0000 @@ -2,7 +2,7 @@ snake_engine::snake_engine() { - clision = 0; + clision = 0;// 0 means there is no collision } snake_engine::~snake_engine() @@ -14,12 +14,12 @@ s.init(); } - +//draw the snake , fruit , and point void snake_engine::draw(N5110 &lcd) { lcd.refresh(); lcd.clear(); - lcd.drawRect(0,8,WIDTH ,HEIGHT -8,FILL_TRANSPARENT); + lcd.drawRect(0,8,WIDTH ,HEIGHT -8,FILL_TRANSPARENT); // the squre on the screen s.draw(lcd); f.draw(lcd); @@ -27,20 +27,20 @@ } -void snake_engine::update(Gamepad &pad, N5110 &lcd) +void snake_engine::update(Gamepad &pad, N5110 &lcd)//update the game { - s.update(_d,_mag); - collision(pad, lcd); + s.update(_d,_mag); //update direction of snake + collision(pad, lcd);//update collision - if(getfruit(pad)) { - f.reborn(); - s.point(); + if(getfruit(pad)) {//boolean value. return ture and false + f.reborn(); //reborn randomly the fruit when the old fruit is ate by snake + s.point(); //update the point } } - +//read the command from gamepad. From Pong sample code void snake_engine::read_input(Gamepad &pad) { _d = pad.get_direction(); @@ -48,7 +48,7 @@ } -bool snake_engine::getfruit(Gamepad &pad) +bool snake_engine::getfruit(Gamepad &pad)//boolean function. if the snake eat fruit . returns true, else false. { Vector2D _f_pos = f.get_pos(); Vector2D _s_pos = s.get_pos(); @@ -65,37 +65,27 @@ } - +//lcd print the point void snake_engine::printpoint(N5110 &lcd) { - int snakepoint = s.get_point(); + int snakepoint = s.get_point();// from the snake function, get the points char buffer[14]; sprintf(buffer,"%2d",snakepoint); - lcd.printString(buffer,WIDTH/2 - 40,0); + lcd.printString(buffer,WIDTH/2 - 40,0); //print the point on the left top corner on the screen } -void snake_engine::collision(Gamepad &pad,N5110 &lcd) +void snake_engine::collision(Gamepad &pad,N5110 &lcd)//check whether the snake hit the wall or not. { Vector2D _s_poss = s.get_pos(); if (((_s_poss.x >= WIDTH -2) || (_s_poss.x <= 1)) ||((_s_poss.y >= HEIGHT -1) || (_s_poss.y <= 8)) ) { lcd.clear(); - clision = 1; + clision = 1;//if the snake position on X and Y equal to the wall. then it happens. clision = 1. } } - - - - - - - - - -