deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Thu May 14 05:21:50 2020 +0000
Revision:
5:32dbfaf578dd
Parent:
4:9fa0c5edd1a1
Child:
6:b59bc5e15cf3
final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haoyan 1:8c48fb8ca5e0 1 #include "StarcraftEngine.h"
haoyan 1:8c48fb8ca5e0 2
haoyan 1:8c48fb8ca5e0 3 StarcraftEngine::StarcraftEngine()
haoyan 1:8c48fb8ca5e0 4 {
haoyan 1:8c48fb8ca5e0 5
haoyan 1:8c48fb8ca5e0 6 }
haoyan 1:8c48fb8ca5e0 7
haoyan 1:8c48fb8ca5e0 8 StarcraftEngine::~StarcraftEngine()
haoyan 1:8c48fb8ca5e0 9 {
haoyan 1:8c48fb8ca5e0 10
haoyan 1:8c48fb8ca5e0 11 }
haoyan 1:8c48fb8ca5e0 12
haoyan 4:9fa0c5edd1a1 13 void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width,int Boss_height, int Boss_width, int Acid_height, int Acid_width, int speed)
haoyan 1:8c48fb8ca5e0 14 {
haoyan 1:8c48fb8ca5e0 15 // initialise the game parameters
haoyan 1:8c48fb8ca5e0 16 _Battleship_height = Battleship_height;
haoyan 1:8c48fb8ca5e0 17 _Battleship_width = Battleship_width;
haoyan 1:8c48fb8ca5e0 18 _Laser_height = Laser_height;
haoyan 1:8c48fb8ca5e0 19 _Laser_width = Laser_width;
haoyan 1:8c48fb8ca5e0 20 _Swarm_height = Swarm_height;
haoyan 1:8c48fb8ca5e0 21 _Swarm_width = Swarm_width;
haoyan 3:1db91ad3ab84 22 _Boss_height = Boss_height;
haoyan 3:1db91ad3ab84 23 _Boss_width = Boss_width;
haoyan 4:9fa0c5edd1a1 24 _Acid_height = Acid_height;
haoyan 4:9fa0c5edd1a1 25 _Acid_width = Acid_width;
haoyan 1:8c48fb8ca5e0 26 _speed = speed;
haoyan 1:8c48fb8ca5e0 27
haoyan 1:8c48fb8ca5e0 28 // x position on screen - WIDTH is defined in N5110.h
haoyan 1:8c48fb8ca5e0 29 _Battleshipx = GAP;
haoyan 1:8c48fb8ca5e0 30
haoyan 1:8c48fb8ca5e0 31 // Initializing Battleship,Laser and Swarm
haoyan 1:8c48fb8ca5e0 32 _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width);
haoyan 1:8c48fb8ca5e0 33 _Laser.init(_Laser_height, _Laser_width, _speed);
haoyan 1:8c48fb8ca5e0 34 _Swarm.init(_Swarm_height, _Swarm_width, _speed);
haoyan 3:1db91ad3ab84 35 _Boss.init(_Boss_height, _Boss_width, _speed);
haoyan 5:32dbfaf578dd 36 _Acid.init(_Acid_height, _Acid_width, _speed);
haoyan 1:8c48fb8ca5e0 37
haoyan 1:8c48fb8ca5e0 38 }
haoyan 1:8c48fb8ca5e0 39
haoyan 1:8c48fb8ca5e0 40 void StarcraftEngine::read_input(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 41 {
haoyan 1:8c48fb8ca5e0 42 _d = pad.get_direction();
haoyan 1:8c48fb8ca5e0 43 _mag = pad.get_mag();
haoyan 1:8c48fb8ca5e0 44 }
haoyan 1:8c48fb8ca5e0 45
haoyan 5:32dbfaf578dd 46 int StarcraftEngine::find_life() //get battleship's life
haoyan 1:8c48fb8ca5e0 47 { int find_life = _Battleship.get_life();
haoyan 1:8c48fb8ca5e0 48 return find_life;
haoyan 1:8c48fb8ca5e0 49 }
haoyan 1:8c48fb8ca5e0 50
haoyan 5:32dbfaf578dd 51 int StarcraftEngine::find_score() //get player's score
haoyan 1:8c48fb8ca5e0 52 { int find_score = _Battleship.get_score();
haoyan 1:8c48fb8ca5e0 53 return find_score;
haoyan 1:8c48fb8ca5e0 54 }
haoyan 1:8c48fb8ca5e0 55
haoyan 1:8c48fb8ca5e0 56 void StarcraftEngine::draw(N5110 &lcd)
haoyan 1:8c48fb8ca5e0 57 {
haoyan 1:8c48fb8ca5e0 58 // draw the elements in the LCD buffer
haoyan 1:8c48fb8ca5e0 59 // pitch
haoyan 1:8c48fb8ca5e0 60 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
haoyan 1:8c48fb8ca5e0 61 //score
haoyan 1:8c48fb8ca5e0 62 print_scores(lcd);
haoyan 1:8c48fb8ca5e0 63 // Battleship
haoyan 1:8c48fb8ca5e0 64 _Battleship.draw(lcd);
haoyan 1:8c48fb8ca5e0 65 // Swarm
haoyan 1:8c48fb8ca5e0 66 _Swarm.draw(lcd);
haoyan 3:1db91ad3ab84 67 // Boss
haoyan 3:1db91ad3ab84 68 _Boss.draw(lcd);
haoyan 1:8c48fb8ca5e0 69 // Laser
haoyan 1:8c48fb8ca5e0 70 _Laser.draw(lcd);
haoyan 4:9fa0c5edd1a1 71 // Acid
haoyan 4:9fa0c5edd1a1 72 _Acid.draw(lcd);
haoyan 1:8c48fb8ca5e0 73
haoyan 1:8c48fb8ca5e0 74 }
haoyan 1:8c48fb8ca5e0 75
haoyan 1:8c48fb8ca5e0 76 void StarcraftEngine::update(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 77 {
haoyan 1:8c48fb8ca5e0 78 check_goal(pad);
haoyan 1:8c48fb8ca5e0 79
haoyan 5:32dbfaf578dd 80 // important to update battleship, laser,boss and swarm before checking collisions so can
haoyan 1:8c48fb8ca5e0 81 // correct for it before updating the display
haoyan 1:8c48fb8ca5e0 82
haoyan 1:8c48fb8ca5e0 83 _Battleship.update(_d,_mag);
haoyan 1:8c48fb8ca5e0 84 _Swarm.update();
haoyan 3:1db91ad3ab84 85 _Boss.update();
haoyan 1:8c48fb8ca5e0 86 _Laser.update();
haoyan 4:9fa0c5edd1a1 87 _Acid.update();
haoyan 1:8c48fb8ca5e0 88
haoyan 1:8c48fb8ca5e0 89 check_Swarm_collisions(pad);
haoyan 3:1db91ad3ab84 90 check_Boss_collisions(pad);
haoyan 4:9fa0c5edd1a1 91 check_Battleship_collisions(pad);
haoyan 1:8c48fb8ca5e0 92 }
haoyan 1:8c48fb8ca5e0 93
haoyan 5:32dbfaf578dd 94 void StarcraftEngine::check_Swarm_collisions(Gamepad &pad) //check if the swarm over the bottom line and minus battleship life
haoyan 1:8c48fb8ca5e0 95 {
haoyan 1:8c48fb8ca5e0 96 // read current swarm attributes
haoyan 1:8c48fb8ca5e0 97 Vector2D Swarm_pos = _Swarm.get_pos();
haoyan 1:8c48fb8ca5e0 98 Vector2D Swarm_velocity = _Swarm.get_velocity();
haoyan 1:8c48fb8ca5e0 99
haoyan 3:1db91ad3ab84 100 // check if swarm over the battleship
haoyan 1:8c48fb8ca5e0 101 if (
haoyan 1:8c48fb8ca5e0 102 (Swarm_pos.y >= HEIGHT - 1)
haoyan 1:8c48fb8ca5e0 103 ) {
haoyan 1:8c48fb8ca5e0 104 _Battleship.minus_life();
haoyan 1:8c48fb8ca5e0 105 pad.tone(800.0,0.1); // Audio feedback
haoyan 1:8c48fb8ca5e0 106 }
haoyan 1:8c48fb8ca5e0 107
haoyan 1:8c48fb8ca5e0 108
haoyan 1:8c48fb8ca5e0 109 // write new attributes
haoyan 1:8c48fb8ca5e0 110 _Swarm.set_velocity(Swarm_velocity);
haoyan 1:8c48fb8ca5e0 111 _Swarm.set_pos(Swarm_pos);
haoyan 1:8c48fb8ca5e0 112 }
haoyan 1:8c48fb8ca5e0 113
haoyan 5:32dbfaf578dd 114 void StarcraftEngine::check_Boss_collisions(Gamepad &pad) //check if the boss over the bottom line and minus battleship life
haoyan 3:1db91ad3ab84 115 {
haoyan 3:1db91ad3ab84 116 // read current Boss attributes
haoyan 3:1db91ad3ab84 117 Vector2D Boss_pos = _Boss.get_pos();
haoyan 3:1db91ad3ab84 118 Vector2D Boss_velocity = _Boss.get_velocity();
haoyan 3:1db91ad3ab84 119
haoyan 3:1db91ad3ab84 120 // check if Boss over the battleship
haoyan 3:1db91ad3ab84 121 if (
haoyan 3:1db91ad3ab84 122 (Boss_pos.y >= HEIGHT - 1)
haoyan 3:1db91ad3ab84 123 ) {
haoyan 3:1db91ad3ab84 124 _Battleship.minus_life();
haoyan 3:1db91ad3ab84 125 pad.tone(800.0,0.1); // Audio feedback
haoyan 3:1db91ad3ab84 126 }
haoyan 3:1db91ad3ab84 127
haoyan 3:1db91ad3ab84 128
haoyan 3:1db91ad3ab84 129 // write new attributes
haoyan 3:1db91ad3ab84 130 _Boss.set_velocity(Boss_velocity);
haoyan 3:1db91ad3ab84 131 _Boss.set_pos(Boss_pos);
haoyan 3:1db91ad3ab84 132 }
haoyan 3:1db91ad3ab84 133
haoyan 5:32dbfaf578dd 134 void StarcraftEngine::check_Battleship_collisions(Gamepad &pad) //check if the acid hit the battleship and minus life
haoyan 5:32dbfaf578dd 135 {
haoyan 5:32dbfaf578dd 136 Vector2D Boss_pos = _Boss.get_pos();
haoyan 5:32dbfaf578dd 137 Vector2D Battleship_pos = _Battleship.get_pos();
haoyan 5:32dbfaf578dd 138 Vector2D Acid_pos = _Acid.get_pos();
haoyan 5:32dbfaf578dd 139
haoyan 5:32dbfaf578dd 140 Vector2D Acid_velocity = _Acid.get_velocity();
haoyan 5:32dbfaf578dd 141 Vector2D Boss_velocity = _Boss.get_velocity();
haoyan 5:32dbfaf578dd 142
haoyan 5:32dbfaf578dd 143 // Player get score
haoyan 5:32dbfaf578dd 144 if ((Acid_pos.x >= Battleship_pos.x)&&
haoyan 5:32dbfaf578dd 145 (Acid_pos.x <= Battleship_pos.x + 6)&&
haoyan 5:32dbfaf578dd 146 (Acid_pos.y + 3 >= Battleship_pos.y)&&
haoyan 5:32dbfaf578dd 147 (Acid_pos.y <= Battleship_pos.y + 3))
haoyan 5:32dbfaf578dd 148 {
haoyan 5:32dbfaf578dd 149 _Battleship.minus_life();
haoyan 5:32dbfaf578dd 150 pad.tone(800.0,0.1); // Audio feedback
haoyan 5:32dbfaf578dd 151 }
haoyan 5:32dbfaf578dd 152
haoyan 5:32dbfaf578dd 153
haoyan 5:32dbfaf578dd 154 if ((Acid_pos.y > HEIGHT - 3)||(Acid_pos.y < 10)) //if acid over the bottom line, initialize acid's position
haoyan 5:32dbfaf578dd 155 {
haoyan 5:32dbfaf578dd 156 Acid_pos.x = Boss_pos.x + 3;
haoyan 5:32dbfaf578dd 157 Acid_pos.y = Boss_pos.y + 5;
haoyan 5:32dbfaf578dd 158 }
haoyan 5:32dbfaf578dd 159
haoyan 5:32dbfaf578dd 160 _Acid.set_velocity(Acid_velocity);
haoyan 5:32dbfaf578dd 161 _Boss.set_velocity(Boss_velocity);
haoyan 5:32dbfaf578dd 162 _Acid.set_pos(Acid_pos);
haoyan 5:32dbfaf578dd 163 _Boss.set_pos(Boss_pos);
haoyan 5:32dbfaf578dd 164 }
haoyan 5:32dbfaf578dd 165
haoyan 5:32dbfaf578dd 166 void StarcraftEngine::check_goal(Gamepad &pad) //check if the laser hit the swarn or boss and add score
haoyan 1:8c48fb8ca5e0 167 {
haoyan 1:8c48fb8ca5e0 168 Vector2D Swarm_pos = _Swarm.get_pos();
haoyan 3:1db91ad3ab84 169 Vector2D Boss_pos = _Boss.get_pos();
haoyan 1:8c48fb8ca5e0 170 Vector2D Battleship_pos = _Battleship.get_pos();
haoyan 1:8c48fb8ca5e0 171 Vector2D Laser_pos = _Laser.get_pos();
haoyan 1:8c48fb8ca5e0 172
haoyan 1:8c48fb8ca5e0 173 Vector2D Laser_velocity = _Laser.get_velocity();
haoyan 1:8c48fb8ca5e0 174 Vector2D Swarm_velocity = _Swarm.get_velocity();
haoyan 3:1db91ad3ab84 175 Vector2D Boss_velocity = _Boss.get_velocity();
haoyan 1:8c48fb8ca5e0 176
haoyan 1:8c48fb8ca5e0 177 // Player get score
haoyan 1:8c48fb8ca5e0 178 if ((Laser_pos.x >= Swarm_pos.x)&&
haoyan 1:8c48fb8ca5e0 179 (Laser_pos.x <= Swarm_pos.x + 6)&&
haoyan 1:8c48fb8ca5e0 180 (Laser_pos.y <= Swarm_pos.y + 6)&&
haoyan 1:8c48fb8ca5e0 181 (Laser_pos.y >= Swarm_pos.y))
haoyan 1:8c48fb8ca5e0 182 {
haoyan 1:8c48fb8ca5e0 183 _Battleship.add_score();
haoyan 1:8c48fb8ca5e0 184 Laser_pos.x = Battleship_pos.x + 3;
haoyan 1:8c48fb8ca5e0 185 Laser_pos.y = Battleship_pos.y - 1;
haoyan 1:8c48fb8ca5e0 186 Swarm_pos.x = rand() % 64;
haoyan 1:8c48fb8ca5e0 187 Swarm_pos.y = 2;
haoyan 1:8c48fb8ca5e0 188
haoyan 1:8c48fb8ca5e0 189 pad.tone(800.0,0.25);
haoyan 1:8c48fb8ca5e0 190 wait(0.1);
haoyan 1:8c48fb8ca5e0 191 }
haoyan 3:1db91ad3ab84 192
haoyan 3:1db91ad3ab84 193 if ((Laser_pos.x >= Boss_pos.x)&&
haoyan 3:1db91ad3ab84 194 (Laser_pos.x <= Boss_pos.x + 6)&&
haoyan 3:1db91ad3ab84 195 (Laser_pos.y <= Boss_pos.y + 4)&&
haoyan 3:1db91ad3ab84 196 (Laser_pos.y >= Boss_pos.y))
haoyan 3:1db91ad3ab84 197 {
haoyan 3:1db91ad3ab84 198 _Battleship.add_score();
haoyan 3:1db91ad3ab84 199 Laser_pos.x = Battleship_pos.x + 3;
haoyan 3:1db91ad3ab84 200 Laser_pos.y = Battleship_pos.y - 1;
haoyan 3:1db91ad3ab84 201 Boss_pos.x = rand() % 64;
haoyan 3:1db91ad3ab84 202 Boss_pos.y = 2;
haoyan 3:1db91ad3ab84 203
haoyan 3:1db91ad3ab84 204 pad.tone(800.0,0.25);
haoyan 3:1db91ad3ab84 205 wait(0.1);
haoyan 3:1db91ad3ab84 206 }
haoyan 1:8c48fb8ca5e0 207
haoyan 5:32dbfaf578dd 208 if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20)) //set the laser's range and initialize laser's position
haoyan 1:8c48fb8ca5e0 209 {
haoyan 1:8c48fb8ca5e0 210 Laser_pos.x = Battleship_pos.x + 3;
haoyan 1:8c48fb8ca5e0 211 Laser_pos.y = Battleship_pos.y - 1;
haoyan 2:03cd3bb32511 212 }
haoyan 2:03cd3bb32511 213
haoyan 1:8c48fb8ca5e0 214 _Laser.set_velocity(Laser_velocity);
haoyan 1:8c48fb8ca5e0 215 _Swarm.set_velocity(Swarm_velocity);
haoyan 3:1db91ad3ab84 216 _Boss.set_velocity(Boss_velocity);
haoyan 1:8c48fb8ca5e0 217 _Laser.set_pos(Laser_pos);
haoyan 3:1db91ad3ab84 218 _Swarm.set_pos(Swarm_pos);
haoyan 3:1db91ad3ab84 219 _Boss.set_pos(Boss_pos);
haoyan 1:8c48fb8ca5e0 220 }
haoyan 1:8c48fb8ca5e0 221
haoyan 1:8c48fb8ca5e0 222 void StarcraftEngine::print_scores(N5110 &lcd)
haoyan 1:8c48fb8ca5e0 223 {
haoyan 1:8c48fb8ca5e0 224 // get scores from battleship
haoyan 1:8c48fb8ca5e0 225 int Battleship_score = _Battleship.get_score();
haoyan 1:8c48fb8ca5e0 226 int Battleship_life = _Battleship.get_life();
haoyan 1:8c48fb8ca5e0 227
haoyan 1:8c48fb8ca5e0 228 // print to LCD
haoyan 1:8c48fb8ca5e0 229 char buffer[14];
haoyan 1:8c48fb8ca5e0 230 sprintf(buffer,"%2d",Battleship_score);
haoyan 1:8c48fb8ca5e0 231 lcd.printString(buffer,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
haoyan 1:8c48fb8ca5e0 232 }
haoyan 1:8c48fb8ca5e0 233
haoyan 1:8c48fb8ca5e0 234
haoyan 1:8c48fb8ca5e0 235
haoyan 1:8c48fb8ca5e0 236
haoyan 1:8c48fb8ca5e0 237