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: 4DGL-uLCD-SE PinDetect mbed
main.cpp
00001 #include "mbed.h" 00002 #include "uLCD_4DGL.h" 00003 #include "Player.h" 00004 #include "Enemy.h" 00005 #include "PinDetect.h" 00006 #include "Circle.h" 00007 #include "Shot.h" 00008 #include "Point.h" 00009 #include "Star.h" 00010 #include <vector> 00011 00012 #define PlayerShotColor 0xFF0000 00013 #define EnemyShotColor 0x00FF00 00014 00015 class Nav_Switch 00016 { 00017 public: 00018 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); 00019 int read(); 00020 //boolean functions to test each switch 00021 bool up(); 00022 bool down(); 00023 bool left(); 00024 bool right(); 00025 bool fire(); 00026 //automatic read on RHS 00027 operator int (); 00028 //index to any switch array style 00029 bool operator[](int index) { 00030 return _pins[index]; 00031 }; 00032 private: 00033 BusIn _pins; 00034 00035 }; 00036 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): 00037 _pins(up, down, left, right, fire) 00038 { 00039 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise 00040 wait(0.001); //delays just a bit for pullups to pull inputs high 00041 } 00042 inline bool Nav_Switch::up() 00043 { 00044 return !(_pins[0]); 00045 } 00046 inline bool Nav_Switch::down() 00047 { 00048 return !(_pins[1]); 00049 } 00050 inline bool Nav_Switch::left() 00051 { 00052 return !(_pins[2]); 00053 } 00054 inline bool Nav_Switch::right() 00055 { 00056 return !(_pins[3]); 00057 } 00058 inline bool Nav_Switch::fire() 00059 { 00060 return !(_pins[4]); 00061 } 00062 inline int Nav_Switch::read() 00063 { 00064 return _pins.read(); 00065 } 00066 inline Nav_Switch::operator int () 00067 { 00068 return _pins.read(); 00069 } 00070 00071 Nav_Switch myNav( p24, p22, p23, p21, p13); 00072 uLCD_4DGL uLCD(p28, p27, p29); 00073 Player player(60,100,&uLCD); 00074 Ticker switchScreen; 00075 Ticker setCanShoot; 00076 Ticker spawnEnemy; 00077 Ticker enemyShoot; 00078 Ticker spawnStar; 00079 PinDetect pbFire(p25); 00080 int playerSpeed = 8; 00081 int enemySpeed = 4; 00082 int shotSpeed = 8; 00083 int enemyShotSpeed = 8; 00084 int numShots = 0; 00085 int numEnemyShots = 0; 00086 int canShoot = 1; 00087 int numEnemies = 0; 00088 int numStars = 0; 00089 int gameOver = 0; 00090 std::vector<Shot> playerShots; 00091 std::vector<Shot> enemyShots; 00092 std::vector<Enemy> enemies; 00093 std::vector<Star> stars; 00094 00095 00096 void changeScreen() { 00097 uLCD.media_init(); 00098 uLCD.set_sector_address(0x0000, 0x0041); 00099 uLCD.display_image(0,0); 00100 } 00101 00102 void setShoot() { 00103 canShoot = 1; 00104 } 00105 00106 void fire(void) { 00107 if(canShoot) { 00108 Point leftShot = player.getLeftGunLoc(); 00109 Point rightShot = player.getRightGunLoc(); 00110 Shot shot1(leftShot.x, leftShot.y, -shotSpeed, PlayerShotColor, &uLCD); 00111 Shot shot2(rightShot.x, rightShot.y, -shotSpeed, PlayerShotColor, &uLCD); 00112 playerShots.push_back(shot1); 00113 playerShots.push_back(shot2); 00114 numShots += 2; 00115 canShoot = 0; 00116 setCanShoot.attach(&setShoot, 0.6); 00117 } 00118 } 00119 00120 void spawnNewEnemy() { 00121 Enemy e((int)(((float) rand() / (RAND_MAX))*127), &uLCD); 00122 enemies.push_back(e); 00123 numEnemies++; 00124 spawnEnemy.attach(&spawnNewEnemy, ((float) rand() / (RAND_MAX))*5); 00125 } 00126 00127 void enemiesFire() { 00128 for(int j=numEnemies-1; j>=0; j--) { 00129 Point firePoint = enemies[j].getGunLoc(); 00130 Shot enemyShot(firePoint.x, firePoint.y, enemyShotSpeed, EnemyShotColor, &uLCD); 00131 enemyShots.push_back(enemyShot); 00132 numEnemyShots++; 00133 } 00134 enemyShoot.attach(&enemiesFire, 3.0); 00135 } 00136 00137 void checkPlayerShotCollision() { 00138 for(int i=numShots-1; i>=0; i--) { 00139 for(int j=numEnemies-1; j>=0; j--) { 00140 Point hitStart = enemies[j].hitBoxStart(); 00141 Point hitBounds = enemies[j].hitBoxDim(); 00142 if(playerShots[i].getX() > hitStart.x && playerShots[i].getX() < hitStart.x + hitBounds.x) { 00143 if(playerShots[i].getY() > hitStart.y && playerShots[i].getY() < hitStart.y + hitBounds.y) { 00144 enemies[j].damage(1); 00145 playerShots.erase(playerShots.begin() + i); 00146 numShots--; 00147 if(enemies[j].getHealth() <= 0) { 00148 enemies.erase(enemies.begin() + j); 00149 numEnemies--; 00150 } 00151 } 00152 } 00153 } 00154 } 00155 } 00156 00157 void checkEnemyShotCollision() { 00158 for(int k=numEnemyShots-1; k>=0; k--) { 00159 Point hitStart = player.hitBoxStart(); 00160 Point hitBounds = player.hitBoxDim(); 00161 if(enemyShots[k].getX() > hitStart.x && enemyShots[k].getX() < hitStart.x + hitBounds.x) { 00162 if(enemyShots[k].getY() > hitStart.y && enemyShots[k].getY() < hitStart.y + hitBounds.y) { 00163 player.damage(1); 00164 enemyShots.erase(enemyShots.begin() + k); 00165 numEnemyShots--; 00166 if(player.getHealth() <= 0) { 00167 gameOver = 1; 00168 uLCD.media_init(); 00169 uLCD.set_sector_address(0x0000, 0x0082); 00170 uLCD.display_image(0,0); 00171 } 00172 } 00173 } 00174 } 00175 } 00176 00177 void createStar() { 00178 Star star1(&uLCD); 00179 stars.push_back(star1); 00180 Star star2(&uLCD); 00181 stars.push_back(star2); 00182 numStars += 2; 00183 spawnStar.attach(&createStar, 1.0); 00184 } 00185 00186 int main() { 00187 uLCD.media_init(); 00188 uLCD.set_sector_address(0x0000, 0x0000); 00189 uLCD.display_image(0,0); 00190 wait(2); 00191 pbFire.mode(PullUp); 00192 wait(.01); 00193 pbFire.attach_deasserted(&fire); 00194 pbFire.setSampleFrequency(); 00195 spawnNewEnemy(); 00196 enemyShoot.attach(&enemiesFire, 2.0); 00197 createStar(); 00198 uLCD.baudrate(3000000); 00199 while(1) { 00200 if(gameOver) { 00201 break; 00202 } 00203 if(myNav.up()) { 00204 player.addY(-playerSpeed); 00205 } 00206 else if(myNav.down()) { 00207 player.addY(playerSpeed); 00208 } 00209 if(myNav.left()) { 00210 player.addX(-playerSpeed); 00211 } 00212 else if(myNav.right()) { 00213 player.addX(playerSpeed); 00214 } 00215 uLCD.filled_rectangle(0, 0, 128, 128, 0x000000); 00216 player.drawPlayer(); 00217 for(int i=numShots-1; i>=0; i--) { 00218 playerShots[i].update(); 00219 if(playerShots[i].getY() < 0) { 00220 playerShots.erase(playerShots.begin() + i); 00221 numShots--; 00222 } 00223 } 00224 for(int k=numEnemyShots-1; k>=0; k--) { 00225 enemyShots[k].update(); 00226 if(enemyShots[k].getY() > 128) { 00227 enemyShots.erase(enemyShots.begin() + k); 00228 numEnemyShots--; 00229 } 00230 } 00231 for(int j=numEnemies-1; j>=0; j--) { 00232 enemies[j].addY(enemySpeed); 00233 enemies[j].drawEnemy(); 00234 if(enemies[j].getY() > 140) { 00235 enemies.erase(enemies.begin() + j); 00236 numEnemies--; 00237 } 00238 } 00239 for(int n=numStars-1; n>=0; n--) { 00240 if(stars[n].offScreen()) { 00241 stars.erase(stars.begin() + n); 00242 numStars--; 00243 } 00244 stars[n].update(); 00245 } 00246 checkPlayerShotCollision(); 00247 checkEnemyShotCollision(); 00248 wait(.1); 00249 } 00250 }
Generated on Sun Jul 17 2022 07:39:56 by
1.7.2