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@15:47ea86f1ed70, 2018-05-07 (annotated)
- Committer:
- weiway
- Date:
- Mon May 07 11:57:22 2018 +0000
- Revision:
- 15:47ea86f1ed70
- Parent:
- 13:4026781772cb
- Child:
- 17:7c8cf5f7878c
the game runs well but the fruit reborn outside of the screen. and the snake would not die even if hit the wall
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 | 15:47ea86f1ed70 | 5 | |
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 | 12:d45bc5d878ad | 17 | |
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 | 12:d45bc5d878ad | 22 | lcd.drawRect(0,8,WIDTH ,HEIGHT -8,FILL_TRANSPARENT); |
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 | 13:4026781772cb | 30 | void snake_engine::update(Gamepad &pad, N5110 &lcd) |
weiway | 12:d45bc5d878ad | 31 | { |
weiway | 12:d45bc5d878ad | 32 | s.update(_d,_mag); |
weiway | 15:47ea86f1ed70 | 33 | |
weiway | 12:d45bc5d878ad | 34 | |
weiway | 15:47ea86f1ed70 | 35 | if(getfruit(pad)) { |
weiway | 15:47ea86f1ed70 | 36 | f.reborn(); |
weiway | 15:47ea86f1ed70 | 37 | s.point(); |
weiway | 15:47ea86f1ed70 | 38 | |
weiway | 12:d45bc5d878ad | 39 | } |
weiway | 12:d45bc5d878ad | 40 | } |
weiway | 12:d45bc5d878ad | 41 | |
weiway | 12:d45bc5d878ad | 42 | |
weiway | 13:4026781772cb | 43 | void snake_engine::read_input(Gamepad &pad) |
weiway | 13:4026781772cb | 44 | { |
weiway | 13:4026781772cb | 45 | _d = pad.get_direction(); |
weiway | 13:4026781772cb | 46 | _mag = pad.get_mag(); |
weiway | 13:4026781772cb | 47 | } |
weiway | 12:d45bc5d878ad | 48 | |
weiway | 12:d45bc5d878ad | 49 | |
weiway | 15:47ea86f1ed70 | 50 | bool snake_engine::getfruit(Gamepad &pad) |
weiway | 15:47ea86f1ed70 | 51 | { |
weiway | 15:47ea86f1ed70 | 52 | Vector2D _f_pos = f.get_pos(); |
weiway | 15:47ea86f1ed70 | 53 | Vector2D _s_pos = s.get_pos(); |
weiway | 15:47ea86f1ed70 | 54 | |
weiway | 15:47ea86f1ed70 | 55 | if ((_f_pos.y >= _s_pos.y || _f_pos.y == _s_pos.y-1) && |
weiway | 15:47ea86f1ed70 | 56 | (_f_pos.y <= _s_pos.y || _f_pos.y+1 == _s_pos.y-1) && |
weiway | 15:47ea86f1ed70 | 57 | (_f_pos.x >= _s_pos.x || _f_pos.x+1 >= _s_pos.x || _f_pos.x+1 >= _s_pos.x ) && |
weiway | 15:47ea86f1ed70 | 58 | (_f_pos.x <= _s_pos.x || _f_pos.x+1 <= _s_pos.x)) { |
weiway | 15:47ea86f1ed70 | 59 | |
weiway | 15:47ea86f1ed70 | 60 | return true; |
weiway | 15:47ea86f1ed70 | 61 | } else { |
weiway | 15:47ea86f1ed70 | 62 | return false; |
weiway | 15:47ea86f1ed70 | 63 | } |
weiway | 15:47ea86f1ed70 | 64 | } |
weiway | 15:47ea86f1ed70 | 65 | |
weiway | 15:47ea86f1ed70 | 66 | |
weiway | 15:47ea86f1ed70 | 67 | |
weiway | 15:47ea86f1ed70 | 68 | void snake_engine::printpoint(N5110 &lcd) |
weiway | 15:47ea86f1ed70 | 69 | { |
weiway | 15:47ea86f1ed70 | 70 | int snakepoint = s.get_point(); |
weiway | 15:47ea86f1ed70 | 71 | char buffer[14]; |
weiway | 15:47ea86f1ed70 | 72 | sprintf(buffer,"%2d",snakepoint); |
weiway | 15:47ea86f1ed70 | 73 | lcd.printString(buffer,WIDTH/2 - 40,0); |
weiway | 15:47ea86f1ed70 | 74 | } |
weiway | 15:47ea86f1ed70 | 75 |