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-15
- Revision:
- 5:acd809cc824c
- Parent:
- 4:0df2b85e47f1
- Child:
- 6:12e8433382b3
File content as of revision 5:acd809cc824c:
#include "Spaceship.h"
//Serial usb(USBTX, USBRX);
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;
spaceship_sprite_direction_ = true;
}
void Spaceship::draw(N5110 &lcd) {
// 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);
}
// printf to find position of spaceship at bottom of screen
//usb.printf("Spaceship Y postion = %d\n",position_y_spaceship_);
}
void Spaceship::clear_spaceship(N5110 &lcd){
// Clears spaceship from LCD by drawing a white sprite
lcd.drawRect(position_x_spaceship_, position_y_spaceship_, 13, 4, FILL_WHITE);
}
// NEED TO REDUCE FUNTION LENGTH ONCE WORKING
void Spaceship::movement(Gamepad &pad){
Direction joystick_direction = pad.get_direction();
// Changes x and y postion values depending on joystick input
if(joystick_direction == N){
position_y_spaceship_+= -1;
}else if(joystick_direction == NE){
position_x_spaceship_+= 2;
position_y_spaceship_+= -1;
spaceship_sprite_direction_ = true;
}else if(joystick_direction == E){
position_x_spaceship_+= 2;
spaceship_sprite_direction_ = true;
}else if(joystick_direction == SE){
position_x_spaceship_+= 2;
position_y_spaceship_+= 1;
spaceship_sprite_direction_ = true;
}else if(joystick_direction == S){
position_y_spaceship_+= 1;
}else if(joystick_direction == SW){
position_y_spaceship_+= 1;
position_x_spaceship_-= 2;
spaceship_sprite_direction_ = false;
}else if(joystick_direction == W){
position_x_spaceship_-= 2;
spaceship_sprite_direction_ = false;
}else if(joystick_direction == NW){
position_x_spaceship_-= 2;
position_y_spaceship_+= -1;
spaceship_sprite_direction_ = false;
}
// Checks y position of spaceship and stops it from going of screen
if (position_y_spaceship_ < 1) {
position_y_spaceship_ = 1;
}
if (position_y_spaceship_ > 44) {
position_y_spaceship_ = 44;
}
}