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.
StarcraftEngine/StarcraftEngine.cpp@3:1db91ad3ab84, 2020-05-12 (annotated)
- Committer:
- haoyan
- Date:
- Tue May 12 15:13:22 2020 +0000
- Revision:
- 3:1db91ad3ab84
- Parent:
- 2:03cd3bb32511
- Child:
- 4:9fa0c5edd1a1
finish2
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:1db91ad3ab84 | 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 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 | 1:8c48fb8ca5e0 | 24 | _speed = speed; |
haoyan | 1:8c48fb8ca5e0 | 25 | |
haoyan | 1:8c48fb8ca5e0 | 26 | // x position on screen - WIDTH is defined in N5110.h |
haoyan | 1:8c48fb8ca5e0 | 27 | _Battleshipx = GAP; |
haoyan | 1:8c48fb8ca5e0 | 28 | |
haoyan | 1:8c48fb8ca5e0 | 29 | // Initializing Battleship,Laser and Swarm |
haoyan | 1:8c48fb8ca5e0 | 30 | _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width); |
haoyan | 1:8c48fb8ca5e0 | 31 | _Laser.init(_Laser_height, _Laser_width, _speed); |
haoyan | 1:8c48fb8ca5e0 | 32 | _Swarm.init(_Swarm_height, _Swarm_width, _speed); |
haoyan | 3:1db91ad3ab84 | 33 | _Boss.init(_Boss_height, _Boss_width, _speed); |
haoyan | 1:8c48fb8ca5e0 | 34 | |
haoyan | 1:8c48fb8ca5e0 | 35 | } |
haoyan | 1:8c48fb8ca5e0 | 36 | |
haoyan | 1:8c48fb8ca5e0 | 37 | void StarcraftEngine::read_input(Gamepad &pad) |
haoyan | 1:8c48fb8ca5e0 | 38 | { |
haoyan | 1:8c48fb8ca5e0 | 39 | _d = pad.get_direction(); |
haoyan | 1:8c48fb8ca5e0 | 40 | _mag = pad.get_mag(); |
haoyan | 1:8c48fb8ca5e0 | 41 | } |
haoyan | 1:8c48fb8ca5e0 | 42 | |
haoyan | 1:8c48fb8ca5e0 | 43 | int StarcraftEngine::find_life() |
haoyan | 1:8c48fb8ca5e0 | 44 | { int find_life = _Battleship.get_life(); |
haoyan | 1:8c48fb8ca5e0 | 45 | return find_life; |
haoyan | 1:8c48fb8ca5e0 | 46 | } |
haoyan | 1:8c48fb8ca5e0 | 47 | |
haoyan | 1:8c48fb8ca5e0 | 48 | int StarcraftEngine::find_score() |
haoyan | 1:8c48fb8ca5e0 | 49 | { int find_score = _Battleship.get_score(); |
haoyan | 1:8c48fb8ca5e0 | 50 | return find_score; |
haoyan | 1:8c48fb8ca5e0 | 51 | } |
haoyan | 1:8c48fb8ca5e0 | 52 | |
haoyan | 1:8c48fb8ca5e0 | 53 | void StarcraftEngine::draw(N5110 &lcd) |
haoyan | 1:8c48fb8ca5e0 | 54 | { |
haoyan | 1:8c48fb8ca5e0 | 55 | // draw the elements in the LCD buffer |
haoyan | 1:8c48fb8ca5e0 | 56 | // pitch |
haoyan | 1:8c48fb8ca5e0 | 57 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
haoyan | 1:8c48fb8ca5e0 | 58 | //score |
haoyan | 1:8c48fb8ca5e0 | 59 | print_scores(lcd); |
haoyan | 1:8c48fb8ca5e0 | 60 | // Battleship |
haoyan | 1:8c48fb8ca5e0 | 61 | _Battleship.draw(lcd); |
haoyan | 1:8c48fb8ca5e0 | 62 | // Swarm |
haoyan | 1:8c48fb8ca5e0 | 63 | _Swarm.draw(lcd); |
haoyan | 3:1db91ad3ab84 | 64 | // Boss |
haoyan | 3:1db91ad3ab84 | 65 | _Boss.draw(lcd); |
haoyan | 1:8c48fb8ca5e0 | 66 | // Laser |
haoyan | 1:8c48fb8ca5e0 | 67 | _Laser.draw(lcd); |
haoyan | 1:8c48fb8ca5e0 | 68 | |
haoyan | 1:8c48fb8ca5e0 | 69 | } |
haoyan | 1:8c48fb8ca5e0 | 70 | |
haoyan | 1:8c48fb8ca5e0 | 71 | void StarcraftEngine::update(Gamepad &pad) |
haoyan | 1:8c48fb8ca5e0 | 72 | { |
haoyan | 1:8c48fb8ca5e0 | 73 | check_goal(pad); |
haoyan | 1:8c48fb8ca5e0 | 74 | |
haoyan | 1:8c48fb8ca5e0 | 75 | // important to update battleship, laser and swarm before checking collisions so can |
haoyan | 1:8c48fb8ca5e0 | 76 | // correct for it before updating the display |
haoyan | 1:8c48fb8ca5e0 | 77 | |
haoyan | 1:8c48fb8ca5e0 | 78 | _Battleship.update(_d,_mag); |
haoyan | 1:8c48fb8ca5e0 | 79 | _Swarm.update(); |
haoyan | 3:1db91ad3ab84 | 80 | _Boss.update(); |
haoyan | 1:8c48fb8ca5e0 | 81 | _Laser.update(); |
haoyan | 1:8c48fb8ca5e0 | 82 | |
haoyan | 1:8c48fb8ca5e0 | 83 | check_Swarm_collisions(pad); |
haoyan | 3:1db91ad3ab84 | 84 | check_Boss_collisions(pad); |
haoyan | 1:8c48fb8ca5e0 | 85 | } |
haoyan | 1:8c48fb8ca5e0 | 86 | |
haoyan | 1:8c48fb8ca5e0 | 87 | void StarcraftEngine::check_Swarm_collisions(Gamepad &pad) |
haoyan | 1:8c48fb8ca5e0 | 88 | { |
haoyan | 1:8c48fb8ca5e0 | 89 | // read current swarm attributes |
haoyan | 1:8c48fb8ca5e0 | 90 | Vector2D Swarm_pos = _Swarm.get_pos(); |
haoyan | 1:8c48fb8ca5e0 | 91 | Vector2D Swarm_velocity = _Swarm.get_velocity(); |
haoyan | 1:8c48fb8ca5e0 | 92 | |
haoyan | 3:1db91ad3ab84 | 93 | // check if swarm over the battleship |
haoyan | 1:8c48fb8ca5e0 | 94 | if ( |
haoyan | 1:8c48fb8ca5e0 | 95 | (Swarm_pos.y >= HEIGHT - 1) |
haoyan | 1:8c48fb8ca5e0 | 96 | ) { |
haoyan | 1:8c48fb8ca5e0 | 97 | _Battleship.minus_life(); |
haoyan | 1:8c48fb8ca5e0 | 98 | pad.tone(800.0,0.1); // Audio feedback |
haoyan | 1:8c48fb8ca5e0 | 99 | } |
haoyan | 1:8c48fb8ca5e0 | 100 | |
haoyan | 1:8c48fb8ca5e0 | 101 | |
haoyan | 1:8c48fb8ca5e0 | 102 | // write new attributes |
haoyan | 1:8c48fb8ca5e0 | 103 | _Swarm.set_velocity(Swarm_velocity); |
haoyan | 1:8c48fb8ca5e0 | 104 | _Swarm.set_pos(Swarm_pos); |
haoyan | 1:8c48fb8ca5e0 | 105 | } |
haoyan | 1:8c48fb8ca5e0 | 106 | |
haoyan | 3:1db91ad3ab84 | 107 | void StarcraftEngine::check_Boss_collisions(Gamepad &pad) |
haoyan | 3:1db91ad3ab84 | 108 | { |
haoyan | 3:1db91ad3ab84 | 109 | // read current Boss attributes |
haoyan | 3:1db91ad3ab84 | 110 | Vector2D Boss_pos = _Boss.get_pos(); |
haoyan | 3:1db91ad3ab84 | 111 | Vector2D Boss_velocity = _Boss.get_velocity(); |
haoyan | 3:1db91ad3ab84 | 112 | |
haoyan | 3:1db91ad3ab84 | 113 | // check if Boss over the battleship |
haoyan | 3:1db91ad3ab84 | 114 | if ( |
haoyan | 3:1db91ad3ab84 | 115 | (Boss_pos.y >= HEIGHT - 1) |
haoyan | 3:1db91ad3ab84 | 116 | ) { |
haoyan | 3:1db91ad3ab84 | 117 | _Battleship.minus_life(); |
haoyan | 3:1db91ad3ab84 | 118 | pad.tone(800.0,0.1); // Audio feedback |
haoyan | 3:1db91ad3ab84 | 119 | } |
haoyan | 3:1db91ad3ab84 | 120 | |
haoyan | 3:1db91ad3ab84 | 121 | |
haoyan | 3:1db91ad3ab84 | 122 | // write new attributes |
haoyan | 3:1db91ad3ab84 | 123 | _Boss.set_velocity(Boss_velocity); |
haoyan | 3:1db91ad3ab84 | 124 | _Boss.set_pos(Boss_pos); |
haoyan | 3:1db91ad3ab84 | 125 | } |
haoyan | 3:1db91ad3ab84 | 126 | |
haoyan | 1:8c48fb8ca5e0 | 127 | void StarcraftEngine::check_goal(Gamepad &pad) |
haoyan | 1:8c48fb8ca5e0 | 128 | { |
haoyan | 1:8c48fb8ca5e0 | 129 | Vector2D Swarm_pos = _Swarm.get_pos(); |
haoyan | 3:1db91ad3ab84 | 130 | Vector2D Boss_pos = _Boss.get_pos(); |
haoyan | 1:8c48fb8ca5e0 | 131 | Vector2D Battleship_pos = _Battleship.get_pos(); |
haoyan | 1:8c48fb8ca5e0 | 132 | Vector2D Laser_pos = _Laser.get_pos(); |
haoyan | 1:8c48fb8ca5e0 | 133 | |
haoyan | 1:8c48fb8ca5e0 | 134 | Vector2D Laser_velocity = _Laser.get_velocity(); |
haoyan | 1:8c48fb8ca5e0 | 135 | Vector2D Swarm_velocity = _Swarm.get_velocity(); |
haoyan | 3:1db91ad3ab84 | 136 | Vector2D Boss_velocity = _Boss.get_velocity(); |
haoyan | 1:8c48fb8ca5e0 | 137 | |
haoyan | 1:8c48fb8ca5e0 | 138 | // Player get score |
haoyan | 1:8c48fb8ca5e0 | 139 | if ((Laser_pos.x >= Swarm_pos.x)&& |
haoyan | 1:8c48fb8ca5e0 | 140 | (Laser_pos.x <= Swarm_pos.x + 6)&& |
haoyan | 1:8c48fb8ca5e0 | 141 | (Laser_pos.y <= Swarm_pos.y + 6)&& |
haoyan | 1:8c48fb8ca5e0 | 142 | (Laser_pos.y >= Swarm_pos.y)) |
haoyan | 1:8c48fb8ca5e0 | 143 | { |
haoyan | 1:8c48fb8ca5e0 | 144 | _Battleship.add_score(); |
haoyan | 1:8c48fb8ca5e0 | 145 | Laser_pos.x = Battleship_pos.x + 3; |
haoyan | 1:8c48fb8ca5e0 | 146 | Laser_pos.y = Battleship_pos.y - 1; |
haoyan | 1:8c48fb8ca5e0 | 147 | Swarm_pos.x = rand() % 64; |
haoyan | 1:8c48fb8ca5e0 | 148 | Swarm_pos.y = 2; |
haoyan | 1:8c48fb8ca5e0 | 149 | |
haoyan | 1:8c48fb8ca5e0 | 150 | pad.tone(800.0,0.25); |
haoyan | 1:8c48fb8ca5e0 | 151 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 152 | } |
haoyan | 3:1db91ad3ab84 | 153 | |
haoyan | 3:1db91ad3ab84 | 154 | if ((Laser_pos.x >= Boss_pos.x)&& |
haoyan | 3:1db91ad3ab84 | 155 | (Laser_pos.x <= Boss_pos.x + 6)&& |
haoyan | 3:1db91ad3ab84 | 156 | (Laser_pos.y <= Boss_pos.y + 4)&& |
haoyan | 3:1db91ad3ab84 | 157 | (Laser_pos.y >= Boss_pos.y)) |
haoyan | 3:1db91ad3ab84 | 158 | { |
haoyan | 3:1db91ad3ab84 | 159 | _Battleship.add_score(); |
haoyan | 3:1db91ad3ab84 | 160 | Laser_pos.x = Battleship_pos.x + 3; |
haoyan | 3:1db91ad3ab84 | 161 | Laser_pos.y = Battleship_pos.y - 1; |
haoyan | 3:1db91ad3ab84 | 162 | Boss_pos.x = rand() % 64; |
haoyan | 3:1db91ad3ab84 | 163 | Boss_pos.y = 2; |
haoyan | 3:1db91ad3ab84 | 164 | |
haoyan | 3:1db91ad3ab84 | 165 | pad.tone(800.0,0.25); |
haoyan | 3:1db91ad3ab84 | 166 | wait(0.1); |
haoyan | 3:1db91ad3ab84 | 167 | } |
haoyan | 1:8c48fb8ca5e0 | 168 | |
haoyan | 2:03cd3bb32511 | 169 | if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20)) |
haoyan | 1:8c48fb8ca5e0 | 170 | { |
haoyan | 1:8c48fb8ca5e0 | 171 | Laser_pos.x = Battleship_pos.x + 3; |
haoyan | 1:8c48fb8ca5e0 | 172 | Laser_pos.y = Battleship_pos.y - 1; |
haoyan | 2:03cd3bb32511 | 173 | } |
haoyan | 2:03cd3bb32511 | 174 | |
haoyan | 1:8c48fb8ca5e0 | 175 | _Laser.set_velocity(Laser_velocity); |
haoyan | 1:8c48fb8ca5e0 | 176 | _Swarm.set_velocity(Swarm_velocity); |
haoyan | 3:1db91ad3ab84 | 177 | _Boss.set_velocity(Boss_velocity); |
haoyan | 1:8c48fb8ca5e0 | 178 | _Laser.set_pos(Laser_pos); |
haoyan | 3:1db91ad3ab84 | 179 | _Swarm.set_pos(Swarm_pos); |
haoyan | 3:1db91ad3ab84 | 180 | _Boss.set_pos(Boss_pos); |
haoyan | 1:8c48fb8ca5e0 | 181 | } |
haoyan | 1:8c48fb8ca5e0 | 182 | |
haoyan | 1:8c48fb8ca5e0 | 183 | void StarcraftEngine::print_scores(N5110 &lcd) |
haoyan | 1:8c48fb8ca5e0 | 184 | { |
haoyan | 1:8c48fb8ca5e0 | 185 | // get scores from battleship |
haoyan | 1:8c48fb8ca5e0 | 186 | int Battleship_score = _Battleship.get_score(); |
haoyan | 1:8c48fb8ca5e0 | 187 | int Battleship_life = _Battleship.get_life(); |
haoyan | 1:8c48fb8ca5e0 | 188 | |
haoyan | 1:8c48fb8ca5e0 | 189 | // print to LCD |
haoyan | 1:8c48fb8ca5e0 | 190 | char buffer[14]; |
haoyan | 1:8c48fb8ca5e0 | 191 | sprintf(buffer,"%2d",Battleship_score); |
haoyan | 1:8c48fb8ca5e0 | 192 | lcd.printString(buffer,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
haoyan | 1:8c48fb8ca5e0 | 193 | } |
haoyan | 1:8c48fb8ca5e0 | 194 | |
haoyan | 1:8c48fb8ca5e0 | 195 | |
haoyan | 1:8c48fb8ca5e0 | 196 | |
haoyan | 1:8c48fb8ca5e0 | 197 | |
haoyan | 1:8c48fb8ca5e0 | 198 |