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.
Character/Character.cpp
- Committer:
- Albutt
- Date:
- 2020-05-18
- Revision:
- 4:b16b6078a432
- Parent:
- 3:fcc9cf213a61
- Child:
- 9:62fe47a1374f
File content as of revision 4:b16b6078a432:
#include "Character.h"
#include <Bitmap.h>
Serial pcc(USBTX, USBRX);
// nothing doing in the constructor and destructor
Character::Character()
{
}
Character::~Character()
{
}
void Character::init(int x,int y)
{
_x = x; // initial x
_y = y; // initial y
_speed = 1; // default speed
_level = 0; // start score from zero
}
void Character::draw(N5110 &lcd)
{ //sprite is facing left
static int sprite_data[] = {
0,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1,
0,1,1,1,1
};
//turning the sprite
for(int i = 0; i < 25; i++){
sprite_data[i] = 1;
}
if (_dir == 0) {
sprite_data[0] = 0;
sprite_data[4] = 0;
} else if (_dir == 1) {
sprite_data[4] = 0;
sprite_data[24] = 0;
} else if (_dir == 2) {
sprite_data[20] = 0;
sprite_data[24] = 0;
} else if (_dir == 3) {
sprite_data[0] = 0;
sprite_data[20] = 0;
}
// Instantiate the Bitmap
Bitmap sprite(sprite_data, 5, 5);
// Rendered at X and Y
sprite.render(lcd, _x, _y);
}
void Character::update(Direction d,float mag)
{
_speed = 1; //scale of speed
//printf statements for speed
//pcc.printf("speed = %d \n", _speed);
// update x and y value depending on direction of movement
// Set direction and speed according to north south directions
if (d == N) {
_y-=_speed;
_dir = 0;
} else if (d == S) {
_y+=_speed;
_dir = 2;
} else if (d == E) {
_x+=_speed;
_dir = 1;
} else if (d == W) {
_x-=_speed;
_dir = 3;
}
//testing _x and _y
//pcc.printf("x = %d \n", _x);
//pcc.printf("y = %d \n", _y);
// check the x and y position] to ensure that the paddle doesn't go off screen
if (_x < 1) {
_x = 1;
}
if (_y < 1) {
_y = 1;
}
if (_x > 79){
_x = 79;
}
if (_y > 43){
_y = 43;
}
}
void Character::level_up()
{ //Mutator function for leveling up
_level++;
}
int Character::get_level()
{ //Accessor function for level
return _level;
}
int Character::get_x()
{
return _x;
}
int Character::get_y()
{
return _y;
}
int Character::get_direction(){
return _dir;
}