deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Tue May 12 15:54:51 2020 +0000
Revision:
4:9fa0c5edd1a1
Parent:
3:1db91ad3ab84
Child:
5:32dbfaf578dd
ok

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 4:9fa0c5edd1a1 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 1:8c48fb8ca5e0 46 int StarcraftEngine::find_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 1:8c48fb8ca5e0 51 int StarcraftEngine::find_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 1:8c48fb8ca5e0 80 // important to update battleship, laser 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 1:8c48fb8ca5e0 94 void StarcraftEngine::check_Swarm_collisions(Gamepad &pad)
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 3:1db91ad3ab84 114 void StarcraftEngine::check_Boss_collisions(Gamepad &pad)
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 1:8c48fb8ca5e0 134 void StarcraftEngine::check_goal(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 135 {
haoyan 1:8c48fb8ca5e0 136 Vector2D Swarm_pos = _Swarm.get_pos();
haoyan 3:1db91ad3ab84 137 Vector2D Boss_pos = _Boss.get_pos();
haoyan 1:8c48fb8ca5e0 138 Vector2D Battleship_pos = _Battleship.get_pos();
haoyan 1:8c48fb8ca5e0 139 Vector2D Laser_pos = _Laser.get_pos();
haoyan 1:8c48fb8ca5e0 140
haoyan 1:8c48fb8ca5e0 141 Vector2D Laser_velocity = _Laser.get_velocity();
haoyan 1:8c48fb8ca5e0 142 Vector2D Swarm_velocity = _Swarm.get_velocity();
haoyan 3:1db91ad3ab84 143 Vector2D Boss_velocity = _Boss.get_velocity();
haoyan 1:8c48fb8ca5e0 144
haoyan 1:8c48fb8ca5e0 145 // Player get score
haoyan 1:8c48fb8ca5e0 146 if ((Laser_pos.x >= Swarm_pos.x)&&
haoyan 1:8c48fb8ca5e0 147 (Laser_pos.x <= Swarm_pos.x + 6)&&
haoyan 1:8c48fb8ca5e0 148 (Laser_pos.y <= Swarm_pos.y + 6)&&
haoyan 1:8c48fb8ca5e0 149 (Laser_pos.y >= Swarm_pos.y))
haoyan 1:8c48fb8ca5e0 150 {
haoyan 1:8c48fb8ca5e0 151 _Battleship.add_score();
haoyan 1:8c48fb8ca5e0 152 Laser_pos.x = Battleship_pos.x + 3;
haoyan 1:8c48fb8ca5e0 153 Laser_pos.y = Battleship_pos.y - 1;
haoyan 1:8c48fb8ca5e0 154 Swarm_pos.x = rand() % 64;
haoyan 1:8c48fb8ca5e0 155 Swarm_pos.y = 2;
haoyan 1:8c48fb8ca5e0 156
haoyan 1:8c48fb8ca5e0 157 pad.tone(800.0,0.25);
haoyan 1:8c48fb8ca5e0 158 wait(0.1);
haoyan 1:8c48fb8ca5e0 159 }
haoyan 3:1db91ad3ab84 160
haoyan 3:1db91ad3ab84 161 if ((Laser_pos.x >= Boss_pos.x)&&
haoyan 3:1db91ad3ab84 162 (Laser_pos.x <= Boss_pos.x + 6)&&
haoyan 3:1db91ad3ab84 163 (Laser_pos.y <= Boss_pos.y + 4)&&
haoyan 3:1db91ad3ab84 164 (Laser_pos.y >= Boss_pos.y))
haoyan 3:1db91ad3ab84 165 {
haoyan 3:1db91ad3ab84 166 _Battleship.add_score();
haoyan 3:1db91ad3ab84 167 Laser_pos.x = Battleship_pos.x + 3;
haoyan 3:1db91ad3ab84 168 Laser_pos.y = Battleship_pos.y - 1;
haoyan 3:1db91ad3ab84 169 Boss_pos.x = rand() % 64;
haoyan 3:1db91ad3ab84 170 Boss_pos.y = 2;
haoyan 3:1db91ad3ab84 171
haoyan 3:1db91ad3ab84 172 pad.tone(800.0,0.25);
haoyan 3:1db91ad3ab84 173 wait(0.1);
haoyan 3:1db91ad3ab84 174 }
haoyan 1:8c48fb8ca5e0 175
haoyan 2:03cd3bb32511 176 if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20))
haoyan 1:8c48fb8ca5e0 177 {
haoyan 1:8c48fb8ca5e0 178 Laser_pos.x = Battleship_pos.x + 3;
haoyan 1:8c48fb8ca5e0 179 Laser_pos.y = Battleship_pos.y - 1;
haoyan 2:03cd3bb32511 180 }
haoyan 2:03cd3bb32511 181
haoyan 1:8c48fb8ca5e0 182 _Laser.set_velocity(Laser_velocity);
haoyan 1:8c48fb8ca5e0 183 _Swarm.set_velocity(Swarm_velocity);
haoyan 3:1db91ad3ab84 184 _Boss.set_velocity(Boss_velocity);
haoyan 1:8c48fb8ca5e0 185 _Laser.set_pos(Laser_pos);
haoyan 3:1db91ad3ab84 186 _Swarm.set_pos(Swarm_pos);
haoyan 3:1db91ad3ab84 187 _Boss.set_pos(Boss_pos);
haoyan 1:8c48fb8ca5e0 188 }
haoyan 1:8c48fb8ca5e0 189
haoyan 4:9fa0c5edd1a1 190 void StarcraftEngine::check_Battleship_collisions(Gamepad &pad)
haoyan 4:9fa0c5edd1a1 191 {
haoyan 4:9fa0c5edd1a1 192 Vector2D Boss_pos = _Boss.get_pos();
haoyan 4:9fa0c5edd1a1 193 Vector2D Battleship_pos = _Battleship.get_pos();
haoyan 4:9fa0c5edd1a1 194 Vector2D Acid_pos = _Acid.get_pos();
haoyan 4:9fa0c5edd1a1 195
haoyan 4:9fa0c5edd1a1 196 Vector2D Acid_velocity = _Acid.get_velocity();
haoyan 4:9fa0c5edd1a1 197 Vector2D Boss_velocity = _Boss.get_velocity();
haoyan 4:9fa0c5edd1a1 198
haoyan 4:9fa0c5edd1a1 199 // Player get score
haoyan 4:9fa0c5edd1a1 200 if ((Acid_pos.x >= Battleship_pos.x)&&
haoyan 4:9fa0c5edd1a1 201 (Acid_pos.x <= Battleship_pos.x + 6)&&
haoyan 4:9fa0c5edd1a1 202 (Acid_pos.y + 3 >= Battleship_pos.y)&&
haoyan 4:9fa0c5edd1a1 203 (Acid_pos.y <= Battleship_pos.y + 3))
haoyan 4:9fa0c5edd1a1 204 {
haoyan 4:9fa0c5edd1a1 205 _Battleship.minus_life();
haoyan 4:9fa0c5edd1a1 206 pad.tone(800.0,0.1); // Audio feedback
haoyan 4:9fa0c5edd1a1 207 }
haoyan 4:9fa0c5edd1a1 208
haoyan 4:9fa0c5edd1a1 209
haoyan 4:9fa0c5edd1a1 210 if ((Acid_pos.y > HEIGHT - 3)||(Acid_pos.y < 6))
haoyan 4:9fa0c5edd1a1 211 {
haoyan 4:9fa0c5edd1a1 212 Acid_pos.x = Boss_pos.x + 3;
haoyan 4:9fa0c5edd1a1 213 Acid_pos.y = Boss_pos.y + 5;
haoyan 4:9fa0c5edd1a1 214 }
haoyan 4:9fa0c5edd1a1 215
haoyan 4:9fa0c5edd1a1 216 _Acid.set_velocity(Acid_velocity);
haoyan 4:9fa0c5edd1a1 217 _Boss.set_velocity(Boss_velocity);
haoyan 4:9fa0c5edd1a1 218 _Acid.set_pos(Acid_pos);
haoyan 4:9fa0c5edd1a1 219 _Boss.set_pos(Boss_pos);
haoyan 4:9fa0c5edd1a1 220 }
haoyan 4:9fa0c5edd1a1 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