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
snake_engine/snake_engine.cpp@20:980b37fde361, 2018-05-08 (annotated)
- Committer:
- weiway
- Date:
- Tue May 08 13:41:08 2018 +0000
- Revision:
- 20:980b37fde361
- Parent:
- 18:e58a1f8e72ad
- Child:
- 21:7f7d09a27cc8
adding doxygen documentation , comments and references
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
weiway | 13:4026781772cb | 1 | #include "snake_engine.h" |
weiway | 12:d45bc5d878ad | 2 | |
weiway | 12:d45bc5d878ad | 3 | snake_engine::snake_engine() |
weiway | 12:d45bc5d878ad | 4 | { |
weiway | 20:980b37fde361 | 5 | clision = 0;// 0 means there is no collision |
weiway | 12:d45bc5d878ad | 6 | } |
weiway | 12:d45bc5d878ad | 7 | |
weiway | 12:d45bc5d878ad | 8 | snake_engine::~snake_engine() |
weiway | 12:d45bc5d878ad | 9 | { |
weiway | 12:d45bc5d878ad | 10 | } |
weiway | 12:d45bc5d878ad | 11 | |
weiway | 13:4026781772cb | 12 | void snake_engine::init() |
weiway | 12:d45bc5d878ad | 13 | { |
weiway | 12:d45bc5d878ad | 14 | s.init(); |
weiway | 12:d45bc5d878ad | 15 | |
weiway | 12:d45bc5d878ad | 16 | } |
weiway | 20:980b37fde361 | 17 | //draw the snake , fruit , and point |
weiway | 13:4026781772cb | 18 | void snake_engine::draw(N5110 &lcd) |
weiway | 12:d45bc5d878ad | 19 | { |
weiway | 12:d45bc5d878ad | 20 | lcd.refresh(); |
weiway | 12:d45bc5d878ad | 21 | lcd.clear(); |
weiway | 20:980b37fde361 | 22 | lcd.drawRect(0,8,WIDTH ,HEIGHT -8,FILL_TRANSPARENT); // the squre on the screen |
weiway | 12:d45bc5d878ad | 23 | |
weiway | 12:d45bc5d878ad | 24 | s.draw(lcd); |
weiway | 12:d45bc5d878ad | 25 | f.draw(lcd); |
weiway | 15:47ea86f1ed70 | 26 | printpoint(lcd); |
weiway | 12:d45bc5d878ad | 27 | } |
weiway | 12:d45bc5d878ad | 28 | |
weiway | 15:47ea86f1ed70 | 29 | |
weiway | 20:980b37fde361 | 30 | void snake_engine::update(Gamepad &pad, N5110 &lcd)//update the game |
weiway | 12:d45bc5d878ad | 31 | { |
weiway | 20:980b37fde361 | 32 | s.update(_d,_mag); //update direction of snake |
weiway | 20:980b37fde361 | 33 | collision(pad, lcd);//update collision |
weiway | 15:47ea86f1ed70 | 34 | |
weiway | 12:d45bc5d878ad | 35 | |
weiway | 20:980b37fde361 | 36 | if(getfruit(pad)) {//boolean value. return ture and false |
weiway | 20:980b37fde361 | 37 | f.reborn(); //reborn randomly the fruit when the old fruit is ate by snake |
weiway | 20:980b37fde361 | 38 | s.point(); //update the point |
weiway | 15:47ea86f1ed70 | 39 | |
weiway | 12:d45bc5d878ad | 40 | } |
weiway | 12:d45bc5d878ad | 41 | } |
weiway | 12:d45bc5d878ad | 42 | |
weiway | 20:980b37fde361 | 43 | //read the command from gamepad. From Pong sample code |
weiway | 13:4026781772cb | 44 | void snake_engine::read_input(Gamepad &pad) |
weiway | 13:4026781772cb | 45 | { |
weiway | 13:4026781772cb | 46 | _d = pad.get_direction(); |
weiway | 13:4026781772cb | 47 | _mag = pad.get_mag(); |
weiway | 13:4026781772cb | 48 | } |
weiway | 12:d45bc5d878ad | 49 | |
weiway | 12:d45bc5d878ad | 50 | |
weiway | 20:980b37fde361 | 51 | bool snake_engine::getfruit(Gamepad &pad)//boolean function. if the snake eat fruit . returns true, else false. |
weiway | 15:47ea86f1ed70 | 52 | { |
weiway | 15:47ea86f1ed70 | 53 | Vector2D _f_pos = f.get_pos(); |
weiway | 15:47ea86f1ed70 | 54 | Vector2D _s_pos = s.get_pos(); |
weiway | 15:47ea86f1ed70 | 55 | |
weiway | 15:47ea86f1ed70 | 56 | if ((_f_pos.y >= _s_pos.y || _f_pos.y == _s_pos.y-1) && |
weiway | 15:47ea86f1ed70 | 57 | (_f_pos.y <= _s_pos.y || _f_pos.y+1 == _s_pos.y-1) && |
weiway | 15:47ea86f1ed70 | 58 | (_f_pos.x >= _s_pos.x || _f_pos.x+1 >= _s_pos.x || _f_pos.x+1 >= _s_pos.x ) && |
weiway | 15:47ea86f1ed70 | 59 | (_f_pos.x <= _s_pos.x || _f_pos.x+1 <= _s_pos.x)) { |
weiway | 15:47ea86f1ed70 | 60 | |
weiway | 15:47ea86f1ed70 | 61 | return true; |
weiway | 15:47ea86f1ed70 | 62 | } else { |
weiway | 15:47ea86f1ed70 | 63 | return false; |
weiway | 15:47ea86f1ed70 | 64 | } |
weiway | 15:47ea86f1ed70 | 65 | } |
weiway | 15:47ea86f1ed70 | 66 | |
weiway | 15:47ea86f1ed70 | 67 | |
weiway | 20:980b37fde361 | 68 | //lcd print the point |
weiway | 15:47ea86f1ed70 | 69 | void snake_engine::printpoint(N5110 &lcd) |
weiway | 15:47ea86f1ed70 | 70 | { |
weiway | 20:980b37fde361 | 71 | int snakepoint = s.get_point();// from the snake function, get the points |
weiway | 15:47ea86f1ed70 | 72 | char buffer[14]; |
weiway | 15:47ea86f1ed70 | 73 | sprintf(buffer,"%2d",snakepoint); |
weiway | 20:980b37fde361 | 74 | lcd.printString(buffer,WIDTH/2 - 40,0); //print the point on the left top corner on the screen |
weiway | 15:47ea86f1ed70 | 75 | } |
weiway | 15:47ea86f1ed70 | 76 | |
weiway | 17:7c8cf5f7878c | 77 | |
weiway | 17:7c8cf5f7878c | 78 | |
weiway | 17:7c8cf5f7878c | 79 | |
weiway | 20:980b37fde361 | 80 | void snake_engine::collision(Gamepad &pad,N5110 &lcd)//check whether the snake hit the wall or not. |
weiway | 17:7c8cf5f7878c | 81 | { |
weiway | 17:7c8cf5f7878c | 82 | Vector2D _s_poss = s.get_pos(); |
weiway | 17:7c8cf5f7878c | 83 | if (((_s_poss.x >= WIDTH -2) || (_s_poss.x <= 1)) ||((_s_poss.y >= HEIGHT -1) || (_s_poss.y <= 8)) ) { |
weiway | 18:e58a1f8e72ad | 84 | lcd.clear(); |
weiway | 20:980b37fde361 | 85 | clision = 1;//if the snake position on X and Y equal to the wall. then it happens. clision = 1. |
weiway | 17:7c8cf5f7878c | 86 | } |
weiway | 17:7c8cf5f7878c | 87 | |
weiway | 17:7c8cf5f7878c | 88 | } |
weiway | 17:7c8cf5f7878c | 89 | |
weiway | 17:7c8cf5f7878c | 90 | |
weiway | 17:7c8cf5f7878c | 91 |