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
SpaceInvadersEngine/SpaceInvadersEngine.cpp
- Committer:
- fy14lkaa
- Date:
- 2019-04-27
- Revision:
- 72:938d2d65199d
- Parent:
- 71:1f6b665fc047
- Child:
- 91:f9e2ff484014
File content as of revision 72:938d2d65199d:
#include "SpaceInvadersEngine.h"
SpaceInvadersEngine::SpaceInvadersEngine()
{
}
SpaceInvadersEngine::~SpaceInvadersEngine()
{
}
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_ship.draw(lcd);
_bullet.draw(lcd);
}
void SpaceInvadersEngine::update(Gamepad &pad)
{
check_goal(pad);
_bullet.update();
_space_ship.update(_d,_mag);
_A1ien.update(_d,_mag);
check_Alien_collision(pad);
check_space_ship_collisions(pad);
}
void SpaceInvadersEngine::check_Alien_collision(Gamepad &pad)
{
// read current bullet attributes
Vector2D bullet_pos = _bullet.get_pos();
Vector2D bullet_velocity = _bullet.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 + _bullet_size >= (HEIGHT-1) ) { // bottom pixel is 47
// hit bottom
bullet_pos.y = (HEIGHT-1) - _bullet_size; // stops bullet going off screen
bullet_velocity.y = -bullet_velocity.y;
// audio feedback
pad.tone(750.0,0.1);
}
// update bullet parameters
_bullet.set_velocity(bullet_velocity);
_bullet.set_pos(bullet_pos);
}
void SpaceInvadersEngine::check_space_ship_collisions(Gamepad &pad){
// read current bullet attributes
Vector2D bullet_pos = _bullet.get_pos();
Vector2D bullet_velocity = _bullet.get_velocity();
/ check p1 first
Vector2D p1_pos = _space_ship.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_shipx) && //left
(bullet_pos.x <= _space_shipx + _space_ship_width) //right
) {
// if it has, fix position and reflect x velocity
bullet_pos.x = _p1x + _space_ship_width;
bullet_velocity.x = -bullet_velocity.x;
// audio feedback
pad.tone(1000.0,0.1);
}
}
void SpaceInvadersEngin::check_goal(Gamepad &pad)
{
Vector2D bullet_pos = _bullet.get_pos();
// spaceship has scored
if (bullet_pos.x + _bullet_size < 0) {
_sace_ship.add_score();
_bullet.init(_bullet_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_ship.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
}