Dependencies: 4DGL-uLCD-SE PinDetect mbed
Revision 0:5c666e5cd22d, committed 2016-03-17
- Comitter:
- jaspinall3
- Date:
- Thu Mar 17 20:47:11 2016 +0000
- Commit message:
- March 17, 2016 Vertical Scrolling Space Shooter
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Circle.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,20 @@ +#include "Circle.h" + +Circle::Circle() { + +} + +void Circle::init(int x, int y, uLCD_4DGL *uLCD) { + initSprite(x, y, uLCD); + setRadius(2); +} + +void Circle::setRadius(int r) { + _radius = r; +} + +void Circle::drawCircle(int baseX, int baseY, int color) { + int newX = baseX + _x; + int newY = baseY + _y; + _uLCDptr->filled_circle(newX, newY, _radius, color); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Circle.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,17 @@ +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "Sprite.h" + +class Circle : public Sprite { +public: + Circle(); + void init(int x, int y, uLCD_4DGL *uLCD); + void setRadius(int r); + void drawCircle(int baseX, int baseY, int color); + +protected: + int _radius; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Enemy.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,75 @@ + #include "Enemy.h" + +Enemy::Enemy(int x, uLCD_4DGL *uLCD) { + _health = 2; + _x = x; + _y = -10; + _uLCDptr = uLCD; + _cockpit.init(0,0,uLCD); + _cockpit.setRadius(10); + _glare1.init(3,3,uLCD); + _glare1.setRadius(4); + _glare2.init(-3,-3,uLCD); + _glare2.setRadius(3); + _gun.init(0,10,uLCD); + _gun.setDimensions(4,2); + _engine.init(-2,-15,uLCD); + _engine.setDimensions(5,5); + _healthBar.init(-9, 16,uLCD); + _healthBar.setDimensions(1,20); +} + +void Enemy::drawEnemy() { + _cockpit.drawCircle(_x, _y, EnemyCockpitColor); + _glare1.drawCircle(_x, _y, EnemyGlareColor1); + _glare2.drawCircle(_x, _y, EnemyGlareColor2); + _gun.drawRect(_x, _y, EnemyGunColor); + _engine.drawRect(_x, _y, EnemyEngineColor); + if(_health == 2) { + _healthBar.setDimensions(1,20); + } + else if(_health == 1) { + _healthBar.setDimensions(1,10); + } + else { + _healthBar.setDimensions(1,1); + } + _healthBar.drawRect(_x,_y, HealthColor); +} + +void Enemy::addY(int dy) { + _y += dy; +} + +int Enemy::getY() { + return _y; +} + +Point Enemy::hitBoxStart() { + Point hitStart; + hitStart.x = _x - 11; + hitStart.y = _y - 11; + return hitStart; +} + +Point Enemy::hitBoxDim() { + Point hitDim; + hitDim.x = 21; + hitDim.y = 21; + return hitDim; +} + +Point Enemy::getGunLoc() { + Point gunLocation; + gunLocation.x = _x; + gunLocation.y = _y + 5; + return gunLocation; +} + +void Enemy::damage(int dmg) { + _health -= dmg; +} + +int Enemy::getHealth() { + return _health; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Enemy.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,44 @@ +#ifndef ENEMY_H +#define ENEMY_H + +#define EnemyCockpitColor 0x3300FF +#define EnemyGlareColor1 0x330000 +#define EnemyGlareColor2 0xCC3300 +#define EnemyGunColor 0xCCCCCC +#define EnemyEngineColor 0xFFFFFF +#define HealthColor 0xFF0000 + +#include "mbed.h" +#include "Circle.h" +#include "Rectangle.h" +#include "Point.h" + +class Enemy { +public: + void fireToggle(); + Enemy(int x, uLCD_4DGL *uLCD); + void drawEnemy(); + void addY(int dy); + void damage(int dmg); + int getY(); + int getHealth(); + Point hitBoxStart(); + Point hitBoxDim(); + Point getGunLoc(); + + +private: + int _x; + int _y; + int _health; + + Circle _cockpit; + Circle _glare1; + Circle _glare2; + Rectangle _gun; + Rectangle _engine; + Rectangle _healthBar; + uLCD_4DGL *_uLCDptr; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Player.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,110 @@ +#include "Player.h" + +Player::Player(int x, int y, uLCD_4DGL *uLCD) { + _health = 4; + _x = x; + _y = y; + _uLCDptr = uLCD; + _cockpit.init(0,0,uLCD); + _cockpit.setRadius(6); + _glare.init(1,-2,uLCD); + _glare.setRadius(2); + _body.init(-4,0,uLCD); + _body.setDimensions(16,8); + _leftWing.init(-14,11,uLCD); + _leftWing.setDimensions(4,10); + _rightWing.init(4,11,uLCD); + _rightWing.setDimensions(4,10); + _leftGun.init(-11,9,uLCD); + _leftGun.setDimensions(2,4); + _rightGun.init(8,9,uLCD); + _rightGun.setDimensions(2,4); + _engine1.init(-2,16,uLCD); + _engine1.setDimensions(2,3); + _engine2.init(-3,16,uLCD); + _engine2.setDimensions(5,6); + _healthBar.init(-10, -10,uLCD); + _healthBar.setDimensions(1,20); +} + +void Player::addX(int dx) { + if((dx > 0) && (_x + 14 <= 128)) { + _x += dx; + } + if((dx < 0) && (_x - 14 >= 0)) { + _x += dx; + } +} + +void Player::addY(int dy) { + if((dy > 0) && (_y + 21 <= 128)) { + _y += dy; + } + if((dy < 0) && (_y - 3 >= 0)) { + _y += dy; + } +} + +Point Player::hitBoxStart() { + Point hitStart; + hitStart.x = _x - 3; + hitStart.y = _y - 3; + return hitStart; +} + +Point Player::hitBoxDim() { + Point hitDim; + hitDim.x = 8; + hitDim.y = 24; + return hitDim; +} + +Point Player::getLeftGunLoc() { + Point gunLoc; + gunLoc.x = _x - 10; + gunLoc.y = _y + 9; + return gunLoc; +} + +Point Player::getRightGunLoc() { + Point gunLoc; + gunLoc.x = _x + 9; + gunLoc.y = _y + 9; + return gunLoc; +} + +void Player::drawPlayer() { + _engine2.drawRect(_x,_y,Engine1Color); + _engine1.drawRect(_x,_y,Engine2Color); + _leftGun.drawRect(_x,_y,GunColor); + _rightGun.drawRect(_x,_y,GunColor); + _leftWing.drawRect(_x,_y,WingColor); + _rightWing.drawRect(_x,_y,WingColor); + _body.drawRect(_x,_y,BodyColor); + _cockpit.drawCircle(_x, _y, CockpitColor); + _glare.drawCircle(_x,_y,GlareColor); + if(_health == 4) { + _healthBar.setDimensions(1,20); + } + else if(_health == 3) { + _healthBar.setDimensions(1,15); + } + else if(_health == 2) { + _healthBar.setDimensions(1,10); + } + else if(_health == 1) { + _healthBar.setDimensions(1,5); + } + else { + _healthBar.setDimensions(1,1); + } + _healthBar.drawRect(_x,_y, HealthColor); +} + +void Player::damage(int dmg) { + _health -= dmg; +} + +int Player::getHealth() { + return _health; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Player.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,48 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#define CockpitColor 0x660000 +#define GlareColor 0x666666 +#define BodyColor 0x999999 +#define WingColor 0x333333 +#define GunColor 0xB50000 +#define Engine1Color 0xFF3300 +#define Engine2Color 0xFFFF00 +#define HealthColor 0xFF0000 + +#include "mbed.h" +#include "Circle.h" +#include "Rectangle.h" +#include "Point.h" + +class Player { +public: + Player(int x, int y, uLCD_4DGL *uLCD); + void drawPlayer(); + void addX(int dx); + void addY(int dy); + Point hitBoxStart(); + Point hitBoxDim(); + Point getLeftGunLoc(); + Point getRightGunLoc(); + void damage(int dmg); + int getHealth(); + +private: + int _x; + int _y; + int _health; + Circle _cockpit; + Circle _glare; + Rectangle _body; + Rectangle _leftWing; + Rectangle _rightWing; + Rectangle _leftGun; + Rectangle _rightGun; + Rectangle _engine1; + Rectangle _engine2; + Rectangle _healthBar; + uLCD_4DGL *_uLCDptr; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Point.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,11 @@ +#include "Point.h" + +Point::Point() { + x = 0; + y = 0; +} + +Point::Point(int sx, int sy) { + x = sx; + y = sy; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Point.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,14 @@ +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point(); + Point(int sx, int sy); + int x; + int y; + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Rectangle.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,21 @@ +#include "Rectangle.h" + +Rectangle::Rectangle() { + +} + +void Rectangle::init(int x, int y, uLCD_4DGL *uLCD) { + initSprite(x, y, uLCD); + setDimensions(4,4); +} + +void Rectangle::setDimensions(int height, int width) { + _height = height; + _width = width; +} + +void Rectangle::drawRect(int baseX, int baseY, int color) { + int newX = baseX + _x; + int newY = baseY + _y; + _uLCDptr->filled_rectangle(newX, newY, newX + _width, newY + _height, color); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Rectangle.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,19 @@ +#ifndef RECTANGLE_H +#define RECTANGLE_H + +#include "Sprite.h" + +class Rectangle : public Sprite { +public: + Rectangle(); + void init(int x, int y, uLCD_4DGL *uLCD); + void setDimensions(int height, int width); + void drawRect(int baseX, int baseY, int color); + +private: + int _height; + int _width; +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Shot.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,20 @@ +#include "Shot.h" + +Shot::Shot(int x, int y, int dy, int color, uLCD_4DGL *uLCD) { + init(x,y,uLCD); + _dy = dy; + _color = color; + setRadius(1); +} +int Shot::getY() { + return _y; +} + +int Shot::getX() { + return _x; +} + +void Shot::update() { + _y = _y + _dy; + drawCircle(0,0,_color); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Shot.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,18 @@ +#ifndef SHOT_H +#define SHOT_H + +#include "Circle.h" + +class Shot : public Circle { +public: + Shot(int x, int y, int dy, int color, uLCD_4DGL *uLCD); + int getY(); + int getX(); + void update(); + +protected: + int _dy; + int _color; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Sprite.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,11 @@ +#include "Sprite.h" + +Sprite::Sprite() { + +} + +void Sprite::initSprite(int x, int y, uLCD_4DGL *uLCD) { + _x = x; + _y = y; + _uLCDptr = uLCD; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Sprite.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,17 @@ +#ifndef SPRITE_H +#define SPRITE_H + +#include "uLCD_4DGL.h" + +class Sprite { +public: + Sprite(); + void initSprite(int x, int y, uLCD_4DGL *uLCD); + +protected: + int _x; + int _y; + uLCD_4DGL *_uLCDptr; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Star.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,22 @@ +#include "Star.h" + +Star::Star( uLCD_4DGL *uLCD) { + _x = (int)(((float) rand() / (RAND_MAX))*127); + _y = 0; + _dy = 10; + _uLCDptr = uLCD; +} + +void Star::update() { + _y += _dy; + _uLCDptr->pixel(_x, _y, 0xFFFFFF); +} + +int Star::offScreen() { + if(_y + _dy >= 127) { + return 1; + } + else { + return 0; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Display/Star.h Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,21 @@ +#ifndef STAR_H +#define STAR_H + + +#include "uLCD_4DGL.h" + +class Star { +public: + Star( uLCD_4DGL *uLCD); + void update(); + int offScreen(); + +private: + int _x; + int _y; + int _dy; + uLCD_4DGL *_uLCDptr; + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,250 @@ +#include "mbed.h" +#include "uLCD_4DGL.h" +#include "Player.h" +#include "Enemy.h" +#include "PinDetect.h" +#include "Circle.h" +#include "Shot.h" +#include "Point.h" +#include "Star.h" +#include <vector> + +#define PlayerShotColor 0xFF0000 +#define EnemyShotColor 0x00FF00 + +class Nav_Switch +{ +public: + Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); + int read(); +//boolean functions to test each switch + bool up(); + bool down(); + bool left(); + bool right(); + bool fire(); +//automatic read on RHS + operator int (); +//index to any switch array style + bool operator[](int index) { + return _pins[index]; + }; +private: + BusIn _pins; + +}; +Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): + _pins(up, down, left, right, fire) +{ + _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise + wait(0.001); //delays just a bit for pullups to pull inputs high +} +inline bool Nav_Switch::up() +{ + return !(_pins[0]); +} +inline bool Nav_Switch::down() +{ + return !(_pins[1]); +} +inline bool Nav_Switch::left() +{ + return !(_pins[2]); +} +inline bool Nav_Switch::right() +{ + return !(_pins[3]); +} +inline bool Nav_Switch::fire() +{ + return !(_pins[4]); +} +inline int Nav_Switch::read() +{ + return _pins.read(); +} +inline Nav_Switch::operator int () +{ + return _pins.read(); +} + +Nav_Switch myNav( p24, p22, p23, p21, p13); +uLCD_4DGL uLCD(p28, p27, p29); +Player player(60,100,&uLCD); +Ticker switchScreen; +Ticker setCanShoot; +Ticker spawnEnemy; +Ticker enemyShoot; +Ticker spawnStar; +PinDetect pbFire(p25); +int playerSpeed = 8; +int enemySpeed = 4; +int shotSpeed = 8; +int enemyShotSpeed = 8; +int numShots = 0; +int numEnemyShots = 0; +int canShoot = 1; +int numEnemies = 0; +int numStars = 0; +int gameOver = 0; +std::vector<Shot> playerShots; +std::vector<Shot> enemyShots; +std::vector<Enemy> enemies; +std::vector<Star> stars; + + +void changeScreen() { + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x0041); + uLCD.display_image(0,0); +} + +void setShoot() { + canShoot = 1; +} + +void fire(void) { + if(canShoot) { + Point leftShot = player.getLeftGunLoc(); + Point rightShot = player.getRightGunLoc(); + Shot shot1(leftShot.x, leftShot.y, -shotSpeed, PlayerShotColor, &uLCD); + Shot shot2(rightShot.x, rightShot.y, -shotSpeed, PlayerShotColor, &uLCD); + playerShots.push_back(shot1); + playerShots.push_back(shot2); + numShots += 2; + canShoot = 0; + setCanShoot.attach(&setShoot, 0.6); + } +} + +void spawnNewEnemy() { + Enemy e((int)(((float) rand() / (RAND_MAX))*127), &uLCD); + enemies.push_back(e); + numEnemies++; + spawnEnemy.attach(&spawnNewEnemy, ((float) rand() / (RAND_MAX))*5); +} + +void enemiesFire() { + for(int j=numEnemies-1; j>=0; j--) { + Point firePoint = enemies[j].getGunLoc(); + Shot enemyShot(firePoint.x, firePoint.y, enemyShotSpeed, EnemyShotColor, &uLCD); + enemyShots.push_back(enemyShot); + numEnemyShots++; + } + enemyShoot.attach(&enemiesFire, 3.0); +} + +void checkPlayerShotCollision() { + for(int i=numShots-1; i>=0; i--) { + for(int j=numEnemies-1; j>=0; j--) { + Point hitStart = enemies[j].hitBoxStart(); + Point hitBounds = enemies[j].hitBoxDim(); + if(playerShots[i].getX() > hitStart.x && playerShots[i].getX() < hitStart.x + hitBounds.x) { + if(playerShots[i].getY() > hitStart.y && playerShots[i].getY() < hitStart.y + hitBounds.y) { + enemies[j].damage(1); + playerShots.erase(playerShots.begin() + i); + numShots--; + if(enemies[j].getHealth() <= 0) { + enemies.erase(enemies.begin() + j); + numEnemies--; + } + } + } + } + } +} + +void checkEnemyShotCollision() { + for(int k=numEnemyShots-1; k>=0; k--) { + Point hitStart = player.hitBoxStart(); + Point hitBounds = player.hitBoxDim(); + if(enemyShots[k].getX() > hitStart.x && enemyShots[k].getX() < hitStart.x + hitBounds.x) { + if(enemyShots[k].getY() > hitStart.y && enemyShots[k].getY() < hitStart.y + hitBounds.y) { + player.damage(1); + enemyShots.erase(enemyShots.begin() + k); + numEnemyShots--; + if(player.getHealth() <= 0) { + gameOver = 1; + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x0082); + uLCD.display_image(0,0); + } + } + } + } +} + +void createStar() { + Star star1(&uLCD); + stars.push_back(star1); + Star star2(&uLCD); + stars.push_back(star2); + numStars += 2; + spawnStar.attach(&createStar, 1.0); +} + +int main() { + uLCD.media_init(); + uLCD.set_sector_address(0x0000, 0x0000); + uLCD.display_image(0,0); + wait(2); + pbFire.mode(PullUp); + wait(.01); + pbFire.attach_deasserted(&fire); + pbFire.setSampleFrequency(); + spawnNewEnemy(); + enemyShoot.attach(&enemiesFire, 2.0); + createStar(); + uLCD.baudrate(3000000); + while(1) { + if(gameOver) { + break; + } + if(myNav.up()) { + player.addY(-playerSpeed); + } + else if(myNav.down()) { + player.addY(playerSpeed); + } + if(myNav.left()) { + player.addX(-playerSpeed); + } + else if(myNav.right()) { + player.addX(playerSpeed); + } + uLCD.filled_rectangle(0, 0, 128, 128, 0x000000); + player.drawPlayer(); + for(int i=numShots-1; i>=0; i--) { + playerShots[i].update(); + if(playerShots[i].getY() < 0) { + playerShots.erase(playerShots.begin() + i); + numShots--; + } + } + for(int k=numEnemyShots-1; k>=0; k--) { + enemyShots[k].update(); + if(enemyShots[k].getY() > 128) { + enemyShots.erase(enemyShots.begin() + k); + numEnemyShots--; + } + } + for(int j=numEnemies-1; j>=0; j--) { + enemies[j].addY(enemySpeed); + enemies[j].drawEnemy(); + if(enemies[j].getY() > 140) { + enemies.erase(enemies.begin() + j); + numEnemies--; + } + } + for(int n=numStars-1; n>=0; n--) { + if(stars[n].offScreen()) { + stars.erase(stars.begin() + n); + numStars--; + } + stars[n].update(); + } + checkPlayerShotCollision(); + checkEnemyShotCollision(); + wait(.1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Mar 17 20:47:11 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/c0f6e94411f5 \ No newline at end of file