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@17:7c8cf5f7878c, 2018-05-08 (annotated)
- Committer:
- weiway
- Date:
- Tue May 08 11:38:55 2018 +0000
- Revision:
- 17:7c8cf5f7878c
- Parent:
- 15:47ea86f1ed70
- Child:
- 18:e58a1f8e72ad
collision
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 | 17:7c8cf5f7878c | 33 | collision(pad, lcd); |
weiway | 15:47ea86f1ed70 | 34 | |
weiway | 12:d45bc5d878ad | 35 | |
weiway | 15:47ea86f1ed70 | 36 | if(getfruit(pad)) { |
weiway | 15:47ea86f1ed70 | 37 | f.reborn(); |
weiway | 15:47ea86f1ed70 | 38 | s.point(); |
weiway | 15:47ea86f1ed70 | 39 | |
weiway | 12:d45bc5d878ad | 40 | } |
weiway | 12:d45bc5d878ad | 41 | } |
weiway | 12:d45bc5d878ad | 42 | |
weiway | 12:d45bc5d878ad | 43 | |
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 | 15:47ea86f1ed70 | 51 | bool snake_engine::getfruit(Gamepad &pad) |
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 | 15:47ea86f1ed70 | 68 | |
weiway | 15:47ea86f1ed70 | 69 | void snake_engine::printpoint(N5110 &lcd) |
weiway | 15:47ea86f1ed70 | 70 | { |
weiway | 15:47ea86f1ed70 | 71 | int snakepoint = s.get_point(); |
weiway | 15:47ea86f1ed70 | 72 | char buffer[14]; |
weiway | 15:47ea86f1ed70 | 73 | sprintf(buffer,"%2d",snakepoint); |
weiway | 15:47ea86f1ed70 | 74 | lcd.printString(buffer,WIDTH/2 - 40,0); |
weiway | 15:47ea86f1ed70 | 75 | } |
weiway | 15:47ea86f1ed70 | 76 | |
weiway | 17:7c8cf5f7878c | 77 | |
weiway | 17:7c8cf5f7878c | 78 | |
weiway | 17:7c8cf5f7878c | 79 | |
weiway | 17:7c8cf5f7878c | 80 | void snake_engine::collision(Gamepad &pad,N5110 &lcd) |
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 | 17:7c8cf5f7878c | 84 | lcd.clear(); |
weiway | 17:7c8cf5f7878c | 85 | } |
weiway | 17:7c8cf5f7878c | 86 | |
weiway | 17:7c8cf5f7878c | 87 | } |
weiway | 17:7c8cf5f7878c | 88 | |
weiway | 17:7c8cf5f7878c | 89 | |
weiway | 17:7c8cf5f7878c | 90 | |
weiway | 17:7c8cf5f7878c | 91 | |
weiway | 17:7c8cf5f7878c | 92 | |
weiway | 17:7c8cf5f7878c | 93 | |
weiway | 17:7c8cf5f7878c | 94 | |
weiway | 17:7c8cf5f7878c | 95 | |
weiway | 17:7c8cf5f7878c | 96 | |
weiway | 17:7c8cf5f7878c | 97 | |
weiway | 17:7c8cf5f7878c | 98 | |
weiway | 17:7c8cf5f7878c | 99 | |
weiway | 17:7c8cf5f7878c | 100 |