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.
Spaceship/Spaceship.cpp
- Committer:
- evanso
- Date:
- 2020-04-28
- Revision:
- 15:90b6821bcf64
- Parent:
- 13:12276eed13ac
- Child:
- 16:1ee3d3804557
File content as of revision 15:90b6821bcf64:
#include "Spaceship.h"
const int k_spaceship_sprite_E[4][13] = {
{ 1,1,1,1,1,1,0,0,0,0,0,0,0 },
{ 0,1,1,1,1,1,1,1,1,0,0,0,0 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 0,0,1,1,1,1,1,0,0,0,0,0,0 },
};
const int k_spaceship_sprite_W[4][13] = {
{ 0,0,0,0,0,0,0,1,1,1,1,1,1 },
{ 0,0,0,0,1,1,1,1,1,1,1,1,0 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 0,0,0,0,0,0,1,1,1,1,1,0,0 },
};
Spaceship::Spaceship() {
}
Spaceship::~Spaceship() {
}
void Spaceship::init() {
position_x_spaceship_ = 36;
position_y_spaceship_ = 22;
// sets origonal spaceship direction to facing East
spaceship_sprite_direction_ = true;
}
void Spaceship::draw(N5110 &lcd) {
off_screen_x_y_checker();
// Draws spaceships at defined x and y positions with different sprite direction depending on joystick postion
if (spaceship_sprite_direction_){
lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_E);
}else if (!spaceship_sprite_direction_){
lcd.drawSprite(position_x_spaceship_, position_y_spaceship_, 4, 13, (int*)k_spaceship_sprite_W);
}
//usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_);
}
void Spaceship::off_screen_x_y_checker(){
// checks y postion spand alters y position
if (position_y_spaceship_ < 0) {
position_y_spaceship_ = 0;
}else if (position_y_spaceship_ > 44) {
position_y_spaceship_ = 44;
}
// checks x postion of spaceship and alters x position
if (position_x_spaceship_ > 52) {
position_x_spaceship_ = 52;
}else if (position_x_spaceship_ < 22) {
position_x_spaceship_ = 22;
}
}
void Spaceship::movement(Direction d_){
// switch statement to change spaceship parameters depedning on joystick direction
switch (d_) {
case CENTRE: set_spaceship_peram(0, 0, false , false); break;
case N: set_spaceship_peram(0, -1, false , false); break;
case NE: set_spaceship_peram(1, -1, true, true); break;
case E: set_spaceship_peram(1, 0, true, true); break;
case SE: set_spaceship_peram(1, 1, true, true); break;
case S: set_spaceship_peram(0, 1, false, false); break;
case SW: set_spaceship_peram(-1, 1, true, false); break;
case W: set_spaceship_peram(-1, 0, true, false); break;
case NW: set_spaceship_peram(-1, -1, true, false); break;
}
//printf("y= %d\n", position_y_spaceship_);
}
void Spaceship::set_spaceship_peram(int x_change,int y_change, bool sprite_change, bool sprite_param){
position_x_spaceship_ += x_change;
position_y_spaceship_ += y_change;
// Only changes sprite direction when jostick direction isn't N or S
if (sprite_change) {
spaceship_sprite_direction_ = sprite_param;
}
}
int Spaceship::get_position_x_spaceship(){
return position_x_spaceship_;
}
int Spaceship::get_position_y_spaceship(){
return position_y_spaceship_;
}