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
- Committer:
- el18lg
- Date:
- 2020-05-24
- Revision:
- 2:c33ff63c0813
- Parent:
- 1:bdafa20e71a0
File content as of revision 2:c33ff63c0813:
#include "Snake.h"
// nothing doing in the constructor and destructor
Snake::Snake()
{
}
Snake::~Snake()
{
}
void Snake::init(int x,int height,int width,int speed)
{
_x = x; // x value on screen is fixed
_y = HEIGHT/2 - height/2; // y depends on height of screen and height of snake
_height = height;
_width = width;
_speed = 1; // default speed
}
void Snake::draw(N5110 &lcd)
{
// draw paddle in screen buffer.
lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
}
void Snake::update(Direction d,float mag)
{
_speed = int(mag*4.0f); // scale is arbitrary, could be changed in future
// update y value depending on direction of movement
// North is decrement as origin is at the top-left so decreasing moves up
if (d == N) {
_y-=_speed;
} else if (d == S) {
_y+=_speed;
}
// check the y origin to ensure that the paddle doesn't go off screen
if (_y < 1) {
_y = 1;
}
if (_y > HEIGHT - _height - 1) {
_y = HEIGHT - _height - 1;
}
}
Vector2D Snake::get_pos() {
Vector2D p = {_x,_y};
return p;
}