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.
Fork of fy15raf by
Diff: Spaceship/Spaceship.cpp
- Revision:
- 16:106c27d03402
- Parent:
- 14:cf4a32245152
- Child:
- 18:53017c90bd26
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Spaceship/Spaceship.cpp Mon May 07 18:27:42 2018 +0000 @@ -0,0 +1,120 @@ +#include "Spaceship.h" + +Spaceship::Spaceship() +{ + +} + +Spaceship::~Spaceship() +{ + +} + +void Spaceship::init() +{ + //initialise the Spaceship position, speed and collisions + _x = 15 ; + _y = HEIGHT/2 ; + _speed = 1; + _collision=0; +} + +void Spaceship::draw(N5110 &lcd) +{ + int sprite[7][10] = { + + { 0,1,1,1,1,0,0,0,0,0, }, + { 0,0,1,0,0,1,1,0,0,0, }, + { 0,0,0,1,0,0,0,1,1,0, }, + { 0,0,0,1,1,1,1,1,1,1, }, + { 0,0,0,1,0,0,0,1,1,0, }, + { 0,0,1,0,0,1,1,0,0,0, }, + { 0,1,1,1,1,0,0,0,0,0, }, + }; + lcd.drawSprite(_x,_y,7,10,(int *)sprite); +} + +void Spaceship::update(Direction d,float mag) +{ + _speed = int(mag*10.0f); //shows how far from the center does the Spaceship move based on the joystick + + // update y and x values based on direction of movement + if (d == N) { + _y-=_speed; + } else if (d == S) { + _y+=_speed; + } else if (d == W) { + _x-=_speed; + } else if (d == E) { + _x+=_speed; + } + + // check the Spaceship coordinates so it doesn't go off screen + if (_y < 1) { + _y = 1; + } + if (_y > HEIGHT - 7) { + _y = HEIGHT - 7; + } + if (_x < 1) { + _x = 1; + } + if (_x > WIDTH - 9) { + _x = WIDTH - 9; + } +} + +Vector2D Spaceship::get_pos() +{ + Vector2D p = {_x,_y}; + return p; +} + +void Spaceship::add_collisions() +{ + _collision++; +} + +int Spaceship::get_collisions() +{ + return _collision; +} + +void Spaceship::drawFullHearts(N5110 &lcd) +{ + int sprite[5][17] = { + + { 1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1, }, + { 1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1, }, + { 1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1, }, + { 0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0, }, + { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0, }, + }; + lcd.drawSprite(0,0,5,17,(int *)sprite); +} + +void Spaceship::drawTwoHearts(N5110 &lcd) +{ + int sprite[5][17] = { + + { 1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1, }, + { 1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1, }, + { 1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,1, }, + { 0,1,1,1,0,0,0,1,1,1,0,0,0,1,0,1,0, }, + { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0, }, + }; + lcd.drawSprite(0,0,5,17,(int *)sprite); +} + +void Spaceship::drawOneHeart(N5110 &lcd) +{ + int sprite[5][17] = { + + { 1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1, }, + { 1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1, }, + { 1,1,1,1,1,0,1,0,0,0,1,0,1,0,0,0,1, }, + { 0,1,1,1,0,0,0,1,0,1,0,0,0,1,0,1,0, }, + { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0, }, + }; + lcd.drawSprite(1,1,5,17,(int *)sprite); +} \ No newline at end of file