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
Revision 20:477d2ee5e461, committed 2019-05-09
- Comitter:
- henririgby98
- Date:
- Thu May 09 14:58:01 2019 +0000
- Parent:
- 19:538c93b1b0c8
- Commit message:
- finish;
Changed in this revision
--- a/Missiles/Missiles.h Thu May 09 14:08:12 2019 +0000 +++ b/Missiles/Missiles.h Thu May 09 14:58:01 2019 +0000 @@ -156,14 +156,15 @@ return _score; //returns the value of _score } -@endcode */ class Missiles { public: + /**constructor*/ Missiles(); + /**destructor*/ ~Missiles(); /** * @brief Randomises missile starting position
--- a/SpaceInvader/SpaceInvader.h Thu May 09 14:08:12 2019 +0000 +++ b/SpaceInvader/SpaceInvader.h Thu May 09 14:58:01 2019 +0000 @@ -15,94 +15,14 @@ @date May 2019 -@code - -#include "SpaceInvader.h" -SpaceInvader::SpaceInvader() -{ - -} - -SpaceInvader::~SpaceInvader() -{ - -} - -void SpaceInvader::init(int height,int width) -{ - _x = WIDTH/2 - width/2 - 2; //sets x position to be in centre of lcd display - _y = HEIGHT/2 - height/2 + 1; //sets y position to be in centre of lcd display - _height = height; - _width = width; - _speed = 1; //sets _speed to equal 1 -} - -void SpaceInvader::draw(N5110 &lcd) -{ - int invader[8][11] = { //sprite for spaceinvader - { 0,0,1,0,0,0,0,0,1,0,0 }, - { 0,0,0,1,0,0,0,1,0,0,0 }, - { 0,0,1,1,1,1,1,1,1,0,0 }, - { 0,1,1,0,1,1,1,0,1,1,0 }, - { 1,1,1,1,1,1,1,1,1,1,1 }, - { 1,0,1,1,1,1,1,1,1,0,1 }, - { 1,0,1,0,0,0,0,0,1,0,1 }, - { 0,0,0,1,1,0,1,1,0,0,0 }, -}; - lcd.drawSprite(_x,_y,_width,_height,(int *)invader); //draws spaceinvader sprite -} - -void SpaceInvader::update(Direction d,float mag) -{ - _speed = int(mag*7.0f); //sets _speed to equal 7 * the magnitude of joystick - if (d == N) { - _y-=_speed; //moves spaceinvader up - } else if (d == S) { - _y+=_speed; //moves spaceinvader down - } else if (d == W) { - _x-=_speed; //moves spaceinvader left - } else if (d == E) { - _x+=_speed; //moves spaceinvader right - } else if (d == NW) { - _y-=((2*_speed)^1/2)/2; //equation prevents spaceinvader strafing when moving diagonally - _x-=((2*_speed)^1/2)/2; //moves spaceinvader up and to the left - } else if (d == SW) { - _y+=((2*_speed)^1/2)/2; - _x-=((2*_speed)^1/2)/2; //moves spaceinvader down and to the left - } else if (d == NE) { - _y-=((2*_speed)^1/2)/2; - _x+=((2*_speed)^1/2)/2; //moves spaceinvader up and to the right - } else if (d == SE) { - _y+=((2*_speed)^1/2)/2; - _x+=((2*_speed)^1/2)/2; //moves spaceinvader down and to the right - } - if (_x < 1) { - _x = 1; //prevents spaceinvader going offscreen to the left - } - if (_x > WIDTH - _width - 4) { - _x = WIDTH - _width - 4; //prevents spaceinvader going offscreen to the right - } - if (_y < 1) { - _y = 1; //prevents spaceinvader going offscreen at the top - } - if (_y > HEIGHT - _height + 2) { - _y = HEIGHT - _height + 2; //prevents spaceinvader going offscreen at the bottom - } -} - -Vector2D SpaceInvader::get_pos() { - Vector2D p = {_x,_y}; //sets the value of position by taking both x and y coordinates - return p; -} - -@endcode */ class SpaceInvader { public: - + /**constructor*/ SpaceInvader(); + /**destructor*/ ~SpaceInvader(); /** * @brief Sets spaceinvader starting position and speed
--- a/SpaceRebEngine/SpaceRebEngine.h Thu May 09 14:08:12 2019 +0000 +++ b/SpaceRebEngine/SpaceRebEngine.h Thu May 09 14:58:01 2019 +0000 @@ -17,129 +17,15 @@ @date May 2019 -@code - -#include "SpaceRebEngine.h" - -SpaceRebEngine::SpaceRebEngine() -{ - -} - -SpaceRebEngine::~SpaceRebEngine() -{ - -} - -void SpaceRebEngine::init(int spaceinvader_width,int spaceinvader_height,int missiles_size,int speed) -{ - _spaceinvader_width = spaceinvader_width; - _spaceinvader_height = spaceinvader_height; - _missiles_size = missiles_size; - _speed = speed; - _player.init(_spaceinvader_height,_spaceinvader_width); //sets initial values of spaceinvader - _missiles.init(_missiles_size); //sets initial values of missilles - _missiles.set_score(); //sets score of game in missiles.cpp - _end = false; //ensures _end = false for each time the game is reset -} - -void SpaceRebEngine::read_input(Gamepad &pad) -{ - _d = pad.get_direction(); //sets _d by takeing the direction from the joystick - _mag = pad.get_mag(); //sets magnitude by takeing the magnitude from the joystick -} - -void SpaceRebEngine::draw(N5110 &lcd) -{ - lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw the play area - _player.draw(lcd); //draw the spaceinvader - _missiles.draw(lcd); // draw the missiles -} - -void SpaceRebEngine::update(Gamepad &pad) -{ - _player.update(_d,_mag); //updates the movement of the spaceinvader - _missiles.update(); //updates position of missiles - - check_spaceinvader_collision(pad); //checks to see if spaceinvader has been struck by missiles - check_wall_collision(pad); //checks to see if missiles has collided with the play area wall -} - -void SpaceRebEngine::check_wall_collision(Gamepad &pad) -{ - Vector2D missiles_pos = _missiles.get_pos(); - Vector2D missiles_velocity = _missiles.get_velocity(); - if (missiles_pos.y <= 1) { - missiles_pos.y = 1; //ensures missiles do not go beyond the y-axis play area at the top - missiles_velocity.y = -missiles_velocity.y *1.1; //used to bounce and increase the missiles_velocity.y with each bounce on the wall - _missiles.add_score(); //incrases score by 1 each time that the missiles hits the wall - if (missiles_velocity.y >= 8){ - missiles_velocity.y = 8; //sets a maximum missiles_velocity.y so missiles dont go 'supersonic' - } - } else if (missiles_pos.y + _missiles_size >= (HEIGHT-1) ) { - missiles_pos.y = (HEIGHT-1) - _missiles_size; //ensures missiles do not go beyond the y-axis play area at the bottom - missiles_velocity.y = -missiles_velocity.y * 1.1; - _missiles.add_score(); - if (missiles_velocity.y >= 8){ - missiles_velocity.y = 8; - } - } else if (missiles_pos.x + _missiles_size >= (WIDTH-1) ) { - missiles_pos.x = (WIDTH-1) - _missiles_size; //ensures missiles do not go beyond the x-axis play area to the right - missiles_velocity.x = -missiles_velocity.x * 1.1 ; //used to bounce and increase the missiles_velocity.x with each bounce on the wall - _missiles.add_score(); - if (missiles_velocity.x >= 8){ - missiles_velocity.x = 8; //sets a maximum missiles_velocity.x so missiles dont go 'supersonic' - } - } else if (missiles_pos.x <= 1) { - missiles_pos.x = 1; //ensures missiles do not go beyond the x-axis play area to the left - missiles_velocity.x = -missiles_velocity.x * 1.1; - _missiles.add_score(); - if (missiles_velocity.x >= 8){ - missiles_velocity.x = 8; - } - } - _missiles.set_velocity(missiles_velocity); //used to set the velocity of the missiles (velocity.y & velocity.x) - _missiles.set_pos(missiles_pos); //used to set the position of the missiles (_y & _x) -} - -void SpaceRebEngine::check_spaceinvader_collision(Gamepad &pad) -{ - Vector2D missiles_pos = _missiles.get_pos(); //gets the position of missiles - Vector2D missiles_velocity = _missiles.get_velocity(); //gets the velocity of the missiles - Vector2D player_pos = _player.get_pos(); //gets the position of the spaceinvader - if ( - (missiles_pos.y >= player_pos.y) && - (missiles_pos.y <= player_pos.y + _spaceinvader_height) && - (missiles_pos.x >= player_pos.x) && - (missiles_pos.x <= player_pos.x + _spaceinvader_width) //checks if there is a collision between spaceinvader & missiles - ) { - pad.tone(1500.0,0.1); //beep when spaceinvader collides with missiles - _end = true; //sets value to true and does this to end the play mechanic - } -} - -void SpaceRebEngine::print_scores(N5110 &lcd) -{ - _score = _missiles.get_score(); //gets the game score from missiles - char buffer1[14]; - sprintf(buffer1,"%2d",_score); - lcd.printString(buffer1,WIDTH - 23,2); //prints the score on the lcd display -} - -bool SpaceRebEngine::game_end() -{ - bool end = _end; - return end; //returns boolean value of end which is used to end play mechanic - } - -@endcode */ class SpaceRebEngine { public: + /**constructor*/ SpaceRebEngine(); + /**destructor*/ ~SpaceRebEngine(); /** * @brief Initialises spaceinvader and missiles