A simplified version of Galaga that can be run on an mbed

Dependencies:   4DGL-uLCD-SE DebounceIn mbed

Fork of AsteroidDefender by Ryan Quinn

Committer:
ndaniel7
Date:
Wed Oct 28 15:46:00 2015 +0000
Revision:
4:6fc77e25bbcf
Parent:
2:13ba45ceb03f
Added some additional comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rquinn7 0:bbc2ad180020 1 #include <vector>
rquinn7 0:bbc2ad180020 2 #include "mbed.h"
rquinn7 0:bbc2ad180020 3
ndaniel7 4:6fc77e25bbcf 4 //used for x,y coordinates
ndaniel7 2:13ba45ceb03f 5 struct Pair {
rquinn7 0:bbc2ad180020 6 double x;
rquinn7 0:bbc2ad180020 7 double y;
rquinn7 0:bbc2ad180020 8 };
rquinn7 0:bbc2ad180020 9
ndaniel7 2:13ba45ceb03f 10 class Enemy{
rquinn7 0:bbc2ad180020 11 private:
rquinn7 0:bbc2ad180020 12
rquinn7 0:bbc2ad180020 13 public:
rquinn7 0:bbc2ad180020 14 bool moved;
ndaniel7 2:13ba45ceb03f 15 Pair spawn;
ndaniel7 2:13ba45ceb03f 16 Pair speed;
ndaniel7 2:13ba45ceb03f 17 Pair coord;
ndaniel7 2:13ba45ceb03f 18 Pair old_coord;
ndaniel7 2:13ba45ceb03f 19 Pair size;
ndaniel7 4:6fc77e25bbcf 20
ndaniel7 4:6fc77e25bbcf 21 //Enemies take in a spawn locatio (x,y) and a speed(x,y)
ndaniel7 2:13ba45ceb03f 22 Enemy(Pair tSpawn, Pair tSpeed) : moved(false)
rquinn7 0:bbc2ad180020 23 {
ndaniel7 2:13ba45ceb03f 24 spawn = tSpawn;
ndaniel7 2:13ba45ceb03f 25 speed = tSpeed;
ndaniel7 2:13ba45ceb03f 26 coord = tSpawn;
rquinn7 0:bbc2ad180020 27 old_coord = coord;
ndaniel7 2:13ba45ceb03f 28 size.x = 5;
ndaniel7 2:13ba45ceb03f 29 size.y = 7;
rquinn7 0:bbc2ad180020 30 }
rquinn7 0:bbc2ad180020 31
ndaniel7 4:6fc77e25bbcf 32 //Enemies move by updating their location based on their speed
ndaniel7 4:6fc77e25bbcf 33 //if they hit a left/right wall their x speed is reversed
rquinn7 0:bbc2ad180020 34 void move(){
rquinn7 0:bbc2ad180020 35 old_coord = coord;
rquinn7 0:bbc2ad180020 36 moved = true;
ndaniel7 2:13ba45ceb03f 37 coord.x = coord.x + speed.x;
ndaniel7 2:13ba45ceb03f 38 coord.y = coord.y + speed.y;
ndaniel7 2:13ba45ceb03f 39 if (coord.x <= 0) {
ndaniel7 2:13ba45ceb03f 40 coord.x = 0;
ndaniel7 2:13ba45ceb03f 41 speed.x = -speed.x;
ndaniel7 2:13ba45ceb03f 42 }
ndaniel7 2:13ba45ceb03f 43 else if (coord.x >= 128-size.x) {
ndaniel7 2:13ba45ceb03f 44 coord.x = 128-size.x;
ndaniel7 2:13ba45ceb03f 45 speed.x = -speed.x;
ndaniel7 2:13ba45ceb03f 46 }
rquinn7 0:bbc2ad180020 47 }
rquinn7 0:bbc2ad180020 48 };
rquinn7 0:bbc2ad180020 49
ndaniel7 4:6fc77e25bbcf 50 //What the ship shoots
ndaniel7 2:13ba45ceb03f 51 class Bullet{
rquinn7 0:bbc2ad180020 52 private:
rquinn7 0:bbc2ad180020 53
rquinn7 0:bbc2ad180020 54 public:
rquinn7 0:bbc2ad180020 55 bool moved;
ndaniel7 2:13ba45ceb03f 56 Pair old_coord;
ndaniel7 2:13ba45ceb03f 57 Pair coord;
ndaniel7 2:13ba45ceb03f 58 Pair size;
rquinn7 0:bbc2ad180020 59
ndaniel7 2:13ba45ceb03f 60 Bullet(Pair tCoord) : moved(false)
rquinn7 0:bbc2ad180020 61 {
ndaniel7 2:13ba45ceb03f 62 coord = tCoord;
ndaniel7 2:13ba45ceb03f 63 //old_coord = coord;
rquinn7 0:bbc2ad180020 64 size.x = 3;
rquinn7 0:bbc2ad180020 65 size.y = 3;
rquinn7 0:bbc2ad180020 66 }
ndaniel7 4:6fc77e25bbcf 67 //Can only move up
rquinn7 0:bbc2ad180020 68 void move(){
rquinn7 0:bbc2ad180020 69 old_coord = coord;
rquinn7 0:bbc2ad180020 70 moved = true;
ndaniel7 2:13ba45ceb03f 71 coord.y = coord.y - 5;
rquinn7 0:bbc2ad180020 72 }
rquinn7 0:bbc2ad180020 73 };
rquinn7 0:bbc2ad180020 74
ndaniel7 4:6fc77e25bbcf 75 //What you control
ndaniel7 2:13ba45ceb03f 76 class Ship{
rquinn7 0:bbc2ad180020 77 private:
rquinn7 0:bbc2ad180020 78 public:
rquinn7 0:bbc2ad180020 79 bool moved;
ndaniel7 2:13ba45ceb03f 80 Pair coord;
ndaniel7 2:13ba45ceb03f 81 Pair old_coord;
ndaniel7 2:13ba45ceb03f 82 Pair size;
ndaniel7 4:6fc77e25bbcf 83 //Always starts in the center of the screen at the bottom
ndaniel7 2:13ba45ceb03f 84 Ship() : moved(false)
rquinn7 0:bbc2ad180020 85 {
ndaniel7 2:13ba45ceb03f 86 size.x = 10;
ndaniel7 2:13ba45ceb03f 87 size.y = 5;
ndaniel7 2:13ba45ceb03f 88 coord.x = (128/2)-size.x;
ndaniel7 2:13ba45ceb03f 89 coord.y = 128-size.y;
rquinn7 0:bbc2ad180020 90 old_coord = coord;
rquinn7 0:bbc2ad180020 91 }
rquinn7 0:bbc2ad180020 92 };