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:
- 123:d68eb9023d88
- Parent:
- 122:d1fd8cbe6633
- Child:
- 124:77f379153715
File content as of revision 123:d68eb9023d88:
#include"SpaceEngine.h"
SpaceEngine::SpaceEngine()
{
}
SpaceEngine::~SpaceEngine()
{
}
void SpaceEngine::init(int x_spaceship,int y_spaceship, int x_bullet, int y_bullet,int fired_bullet, int x_alien,int y_alien, int speed_alien, int speed_bullet, int speed_spaceship)
{
_x_spaceship=x_spaceship;
_y_spaceship=y_spaceship;
_x_bullet=x_bullet;
_y_bullet=y_bullet;
_fired_bullet= fired_bullet;
_x_alien= x_alien;
_y_alien= y_alien;
_speed_alien= speed_alien;
_speed_bullet=speed_bullet;
_speed_spaceship=speed_spaceship;
_bullet.init(_x_bullet, _y_bullet, speed_bullet,fired_bullet);
_alien.init (_x_alien, _y_alien,_speed_alien);
_spaceship.init( _x_spaceship, _y_spaceship, _speed_spaceship);
}
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
}
*/