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.
Diff: StarcraftEngine/StarcraftEngine.cpp
- Revision:
- 1:8c48fb8ca5e0
- Child:
- 2:03cd3bb32511
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StarcraftEngine/StarcraftEngine.cpp Mon May 11 06:50:18 2020 +0000 @@ -0,0 +1,152 @@ +#include "StarcraftEngine.h" + +StarcraftEngine::StarcraftEngine() +{ + +} + +StarcraftEngine::~StarcraftEngine() +{ + +} + +void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width, int speed) +{ + // initialise the game parameters + _Battleship_height = Battleship_height; + _Battleship_width = Battleship_width; + _Laser_height = Laser_height; + _Laser_width = Laser_width; + _Swarm_height = Swarm_height; + _Swarm_width = Swarm_width; + _speed = speed; + + // x position on screen - WIDTH is defined in N5110.h + _Battleshipx = GAP; + + // Initializing Battleship,Laser and Swarm + _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width); + _Laser.init(_Laser_height, _Laser_width, _speed); + _Swarm.init(_Swarm_height, _Swarm_width, _speed); + +} + +void StarcraftEngine::read_input(Gamepad &pad) +{ + _d = pad.get_direction(); + _mag = pad.get_mag(); +} + +int StarcraftEngine::find_life() +{ int find_life = _Battleship.get_life(); + return find_life; +} + +int StarcraftEngine::find_score() +{ int find_score = _Battleship.get_score(); + return find_score; +} + +void StarcraftEngine::draw(N5110 &lcd) +{ + // draw the elements in the LCD buffer + // pitch + lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); + //score + print_scores(lcd); + // Battleship + _Battleship.draw(lcd); + // Swarm + _Swarm.draw(lcd); + // Laser + _Laser.draw(lcd); + +} + +void StarcraftEngine::update(Gamepad &pad) +{ + check_goal(pad); + + // important to update battleship, laser and swarm before checking collisions so can + // correct for it before updating the display + + _Battleship.update(_d,_mag); + _Swarm.update(); + _Laser.update(); + + check_Swarm_collisions(pad); +} + +void StarcraftEngine::check_Swarm_collisions(Gamepad &pad) +{ + // read current swarm attributes + Vector2D Swarm_pos = _Swarm.get_pos(); + Vector2D Swarm_velocity = _Swarm.get_velocity(); + + // check if swarm hit or over the battleship + if ( + (Swarm_pos.y >= HEIGHT - 1) + ) { + _Battleship.minus_life(); + pad.tone(800.0,0.1); // Audio feedback + } + + + // write new attributes + _Swarm.set_velocity(Swarm_velocity); + _Swarm.set_pos(Swarm_pos); +} + +void StarcraftEngine::check_goal(Gamepad &pad) +{ + Vector2D Swarm_pos = _Swarm.get_pos(); + Vector2D Battleship_pos = _Battleship.get_pos(); + Vector2D Laser_pos = _Laser.get_pos(); + + Vector2D Laser_velocity = _Laser.get_velocity(); + Vector2D Swarm_velocity = _Swarm.get_velocity(); + + // Player get score + if ((Laser_pos.x >= Swarm_pos.x)&& + (Laser_pos.x <= Swarm_pos.x + 6)&& + (Laser_pos.y <= Swarm_pos.y + 6)&& + (Laser_pos.y >= Swarm_pos.y)) + { + _Battleship.add_score(); + Laser_pos.x = Battleship_pos.x + 3; + Laser_pos.y = Battleship_pos.y - 1; + Swarm_pos.x = rand() % 64; + Swarm_pos.y = 2; + + pad.tone(800.0,0.25); + wait(0.1); + } + + if(Laser_pos.y < 6) + { + Laser_pos.x = Battleship_pos.x + 3; + Laser_pos.y = Battleship_pos.y - 1; + } + + _Laser.set_velocity(Laser_velocity); + _Swarm.set_velocity(Swarm_velocity); + _Laser.set_pos(Laser_pos); + _Swarm.set_pos(Swarm_pos); +} + +void StarcraftEngine::print_scores(N5110 &lcd) +{ + // get scores from battleship + int Battleship_score = _Battleship.get_score(); + int Battleship_life = _Battleship.get_life(); + + // print to LCD + char buffer[14]; + sprintf(buffer,"%2d",Battleship_score); + lcd.printString(buffer,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits +} + + + + + \ No newline at end of file