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 Gamepad N5110
snake/snake.cpp@1:b49c36604125, 2019-04-13 (annotated)
- Committer:
- 694617778
- Date:
- Sat Apr 13 10:07:50 2019 +0000
- Revision:
- 1:b49c36604125
- Parent:
- 0:b67614a0c4cf
- Child:
- 2:934daa65f73d
test2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| 694617778 | 0:b67614a0c4cf | 1 | #include "snake.h" |
| 694617778 | 0:b67614a0c4cf | 2 | |
| 694617778 | 0:b67614a0c4cf | 3 | // nothing doing in the constructor and destructor |
| 694617778 | 0:b67614a0c4cf | 4 | snake::snake() |
| 694617778 | 0:b67614a0c4cf | 5 | { |
| 694617778 | 0:b67614a0c4cf | 6 | |
| 694617778 | 0:b67614a0c4cf | 7 | } |
| 694617778 | 0:b67614a0c4cf | 8 | |
| 694617778 | 0:b67614a0c4cf | 9 | snake::~snake() |
| 694617778 | 0:b67614a0c4cf | 10 | { |
| 694617778 | 0:b67614a0c4cf | 11 | |
| 694617778 | 0:b67614a0c4cf | 12 | } |
| 694617778 | 0:b67614a0c4cf | 13 | |
| 694617778 | 1:b49c36604125 | 14 | void snake::init(int size,int length) |
| 694617778 | 0:b67614a0c4cf | 15 | { |
| 694617778 | 0:b67614a0c4cf | 16 | _size = size; |
| 694617778 | 1:b49c36604125 | 17 | _length = length; |
| 694617778 | 1:b49c36604125 | 18 | _x[0] = WIDTH/2 - _size/2; |
| 694617778 | 1:b49c36604125 | 19 | _y[0] = HEIGHT/2 - _size/2; |
| 694617778 | 0:b67614a0c4cf | 20 | |
| 694617778 | 1:b49c36604125 | 21 | _x[1] = WIDTH/2 - _size/2 -1*size; |
| 694617778 | 1:b49c36604125 | 22 | _y[1] = HEIGHT/2 - _size/2; |
| 694617778 | 1:b49c36604125 | 23 | |
| 694617778 | 1:b49c36604125 | 24 | _x[2] = WIDTH/2 - _size/2 -2*size; |
| 694617778 | 1:b49c36604125 | 25 | _y[2] = HEIGHT/2 - _size/2; |
| 694617778 | 0:b67614a0c4cf | 26 | |
| 694617778 | 0:b67614a0c4cf | 27 | _score = 0; // start score from zero |
| 694617778 | 0:b67614a0c4cf | 28 | |
| 694617778 | 0:b67614a0c4cf | 29 | } |
| 694617778 | 0:b67614a0c4cf | 30 | |
| 694617778 | 1:b49c36604125 | 31 | int snake::get_length(){ |
| 694617778 | 1:b49c36604125 | 32 | int d=_length; |
| 694617778 | 1:b49c36604125 | 33 | return d; |
| 694617778 | 1:b49c36604125 | 34 | } |
| 694617778 | 1:b49c36604125 | 35 | |
| 694617778 | 1:b49c36604125 | 36 | void snake::update(int direction, int length) |
| 694617778 | 1:b49c36604125 | 37 | { |
| 694617778 | 1:b49c36604125 | 38 | for(int k = length - 1; k > 0; k--){ |
| 694617778 | 1:b49c36604125 | 39 | _x[k] = _x[k-1]; |
| 694617778 | 1:b49c36604125 | 40 | _y[k] = _y[k-1]; |
| 694617778 | 1:b49c36604125 | 41 | } |
| 694617778 | 1:b49c36604125 | 42 | if(direction == 0){ |
| 694617778 | 1:b49c36604125 | 43 | _x[0] = _x[0] + 1; |
| 694617778 | 1:b49c36604125 | 44 | }else if(direction == 1){ |
| 694617778 | 1:b49c36604125 | 45 | _y[0] = _y[0] + 1; |
| 694617778 | 1:b49c36604125 | 46 | }else if(direction == 2){ |
| 694617778 | 1:b49c36604125 | 47 | _x[0] = _x[0] - 1; |
| 694617778 | 1:b49c36604125 | 48 | }else if(direction == 3){ |
| 694617778 | 1:b49c36604125 | 49 | _y[0] = _y[0] - 1; |
| 694617778 | 1:b49c36604125 | 50 | } |
| 694617778 | 1:b49c36604125 | 51 | |
| 694617778 | 1:b49c36604125 | 52 | |
| 694617778 | 1:b49c36604125 | 53 | } |
| 694617778 | 1:b49c36604125 | 54 | |
| 694617778 | 1:b49c36604125 | 55 | |
| 694617778 | 1:b49c36604125 | 56 | void snake::draw(N5110 &lcd, int length) |
| 694617778 | 0:b67614a0c4cf | 57 | { |
| 694617778 | 0:b67614a0c4cf | 58 | |
| 694617778 | 0:b67614a0c4cf | 59 | // draw snake in screen buffer. |
| 694617778 | 1:b49c36604125 | 60 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
| 694617778 | 1:b49c36604125 | 61 | for (int i = 0; i < length; i++){ |
| 694617778 | 1:b49c36604125 | 62 | lcd.drawRect(_x[i],_y[i],_size,_size,FILL_BLACK); |
| 694617778 | 1:b49c36604125 | 63 | } |
| 694617778 | 1:b49c36604125 | 64 | |
| 694617778 | 0:b67614a0c4cf | 65 | } |
| 694617778 | 0:b67614a0c4cf | 66 | |
| 694617778 | 0:b67614a0c4cf | 67 | |
| 694617778 | 0:b67614a0c4cf | 68 | void snake::add_score() |
| 694617778 | 0:b67614a0c4cf | 69 | { |
| 694617778 | 0:b67614a0c4cf | 70 | _score++; |
| 694617778 | 0:b67614a0c4cf | 71 | } |
| 694617778 | 0:b67614a0c4cf | 72 | |
| 694617778 | 0:b67614a0c4cf | 73 | int snake::get_score() |
| 694617778 | 0:b67614a0c4cf | 74 | { |
| 694617778 | 0:b67614a0c4cf | 75 | return _score; |
| 694617778 | 0:b67614a0c4cf | 76 | } |
| 694617778 | 0:b67614a0c4cf | 77 | |
| 694617778 | 1:b49c36604125 | 78 |