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/Snake.cpp@2:c33ff63c0813, 2020-05-24 (annotated)
- Committer:
- el18lg
- Date:
- Sun May 24 20:12:08 2020 +0000
- Revision:
- 2:c33ff63c0813
- Parent:
- 1:bdafa20e71a0
Progress with snake movement in map;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el18lg | 2:c33ff63c0813 | 1 | #include "Snake.h" |
| el18lg | 2:c33ff63c0813 | 2 | |
| el18lg | 2:c33ff63c0813 | 3 | // nothing doing in the constructor and destructor |
| el18lg | 2:c33ff63c0813 | 4 | Snake::Snake() |
| el18lg | 2:c33ff63c0813 | 5 | { |
| el18lg | 2:c33ff63c0813 | 6 | |
| el18lg | 2:c33ff63c0813 | 7 | } |
| el18lg | 2:c33ff63c0813 | 8 | |
| el18lg | 2:c33ff63c0813 | 9 | Snake::~Snake() |
| el18lg | 2:c33ff63c0813 | 10 | { |
| el18lg | 2:c33ff63c0813 | 11 | |
| el18lg | 2:c33ff63c0813 | 12 | } |
| el18lg | 2:c33ff63c0813 | 13 | |
| el18lg | 2:c33ff63c0813 | 14 | void Snake::init(int x,int height,int width,int speed) |
| el18lg | 2:c33ff63c0813 | 15 | { |
| el18lg | 2:c33ff63c0813 | 16 | _x = x; // x value on screen is fixed |
| el18lg | 2:c33ff63c0813 | 17 | _y = HEIGHT/2 - height/2; // y depends on height of screen and height of snake |
| el18lg | 2:c33ff63c0813 | 18 | _height = height; |
| el18lg | 2:c33ff63c0813 | 19 | _width = width; |
| el18lg | 2:c33ff63c0813 | 20 | _speed = 1; // default speed |
| el18lg | 2:c33ff63c0813 | 21 | |
| el18lg | 2:c33ff63c0813 | 22 | } |
| el18lg | 2:c33ff63c0813 | 23 | |
| el18lg | 2:c33ff63c0813 | 24 | void Snake::draw(N5110 &lcd) |
| el18lg | 2:c33ff63c0813 | 25 | { |
| el18lg | 2:c33ff63c0813 | 26 | // draw paddle in screen buffer. |
| el18lg | 2:c33ff63c0813 | 27 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
| el18lg | 2:c33ff63c0813 | 28 | } |
| el18lg | 2:c33ff63c0813 | 29 | |
| el18lg | 2:c33ff63c0813 | 30 | void Snake::update(Direction d,float mag) |
| el18lg | 2:c33ff63c0813 | 31 | { |
| el18lg | 2:c33ff63c0813 | 32 | _speed = int(mag*4.0f); // scale is arbitrary, could be changed in future |
| el18lg | 2:c33ff63c0813 | 33 | |
| el18lg | 2:c33ff63c0813 | 34 | // update y value depending on direction of movement |
| el18lg | 2:c33ff63c0813 | 35 | // North is decrement as origin is at the top-left so decreasing moves up |
| el18lg | 2:c33ff63c0813 | 36 | if (d == N) { |
| el18lg | 2:c33ff63c0813 | 37 | _y-=_speed; |
| el18lg | 2:c33ff63c0813 | 38 | } else if (d == S) { |
| el18lg | 2:c33ff63c0813 | 39 | _y+=_speed; |
| el18lg | 2:c33ff63c0813 | 40 | } |
| el18lg | 2:c33ff63c0813 | 41 | |
| el18lg | 2:c33ff63c0813 | 42 | // check the y origin to ensure that the paddle doesn't go off screen |
| el18lg | 2:c33ff63c0813 | 43 | if (_y < 1) { |
| el18lg | 2:c33ff63c0813 | 44 | _y = 1; |
| el18lg | 2:c33ff63c0813 | 45 | } |
| el18lg | 2:c33ff63c0813 | 46 | if (_y > HEIGHT - _height - 1) { |
| el18lg | 2:c33ff63c0813 | 47 | _y = HEIGHT - _height - 1; |
| el18lg | 2:c33ff63c0813 | 48 | } |
| el18lg | 2:c33ff63c0813 | 49 | } |
| el18lg | 2:c33ff63c0813 | 50 | |
| el18lg | 2:c33ff63c0813 | 51 | |
| el18lg | 2:c33ff63c0813 | 52 | |
| el18lg | 2:c33ff63c0813 | 53 | Vector2D Snake::get_pos() { |
| el18lg | 2:c33ff63c0813 | 54 | Vector2D p = {_x,_y}; |
| el18lg | 2:c33ff63c0813 | 55 | return p; |
| el18lg | 2:c33ff63c0813 | 56 | } |