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
SpaceEngine/SpaceEngine.cpp
- Committer:
- fy14lkaa
- Date:
- 2019-05-05
- Revision:
- 121:6d7f16af9e9c
- Parent:
- 120:53e47c1e58f3
- Child:
- 122:d1fd8cbe6633
File content as of revision 121:6d7f16af9e9c:
#include"SpaceEngine.h"
SpaceEngine::SpaceEngine()
{
}
SpaceEngine::~SpaceEngine()
{
}
void SpaceInvadersEngine::init(int space_ship_width,int space_ship_height,int bullet_size, int Alien_size, int speed)
{
// initialise the game parameters
_space_ship_width= space_ship_width;
_space_ship_height= space_ship_height;
_bullet_size= bullet_size;
_Alien_size= Alien_size;
_speed= speed;
}
void SpaceInvadersEngine::read_input(Gamepad &pad)
{
_d = pad.get_direction();
_mag = pad.get_mag();
}
void SpaceInvadersEngine::draw(N5110 &lcd)
{
// draw the elements in the LCD buffer
// pitch
//score
print_scores(lcd);
alien.draw(lcd);
space_ship1.draw(lcd);
bullet1.draw(lcd);
}
void SpaceInvadersEngine::update(Gamepad &pad)
{
check_goal(pad);
bullet1.update();
space_ship1.update(_d,_mag);
alien.update();
check_Alien_collision(pad);
checkspace_ship_collisions(pad);
}
/*void SpaceInvadersEngine::check_Alien_collision(Gamepad &pad)
{
// read current bullet attributes
Vector2D bullet_pos = bullet.get_pos();
Vector2D bullet_velocity = bullet1.get_velocity();
//check if the bullet hit the top of the alien
// check if the bullet hits top of the Alien
if (bullet_pos.y <= 1) { // 1 due to 1 pixel boundary
bullet_pos.y = 1; // bounce off ceiling without going off screen
bullet_velocity.y = -bullet_velocity.y;
// audio feedback
pad.tone(750.0,0.1);
}
// check if the bullet hits bottom of the Alien
else if (bullet_pos.y + bullet1_size >= (HEIGHT-1) ) { // bottom pixel is 47
// hit bottom
bullet_pos.y = (HEIGHT-1) - bullet1_size; // stops bullet going off screen
bullet_velocity.y = -bullet_velocity.y;
// audio feedback
pad.tone(750.0,0.1);
}
// update bullet parameters
bullet1.set_velocity(bullet_velocity);
bullet1.set_pos(bullet_pos);
}
*/
/*void SpaceInvadersEngine::checkspace_ship_collisions(Gamepad &pad)
{
// read current bullet attributes
Vector2D bullet_pos = bullet1.get_pos();
Vector2D bullet_velocity = bullet1.get_velocity();
/ check p1 first
Vector2D p1_pos = space_ship1.get_pos();
// see if bullet has hit the paddle by checking for overlaps
if (
(bullet_pos.y >= space_ship.y) && //top
(bullet_pos.y <= space_ship_pos.y + _paddle_height) && //bottom
(bullet_pos.x >= space_ship1x) && //left
(bullet_pos.x <= space_ship1x + space_ship1_width) //right
) {
// if it has, fix position and reflect x velocity
bullet_pos.x = _p1x + space_ship1_width;
bullet_velocity.x = -bullet_velocity.x;
// audio feedback
pad.tone(1000.0,0.1);
}
}
*/
/*void SpaceInvadersEngin::check_goal(Gamepad &pad)
{
Vector2D bullet_pos = bullet1.get_pos();
// spaceship has scored
if (bullet_pos.x + bullet1_size < 0) {
_sace_ship.add_score();
bullet1.init(bullet1_size,_speed);
pad.tone(1500.0,0.5);
pad.leds_on();
wait(0.5);
pad.leds_off();
}
void SpaceInvadersEngin::print_scores(N5110 &lcd) {
// get scores from the spaceship
int space_ship_score = space_ship1.get_score();
// print to LCD i
char buffer1[14];
sprintf(buffer1,"%2d",space_ship_score);
lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
}
*/