Stephen Ralph / Mbed 2 deprecated Asteroids

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

Committer:
sralph3
Date:
Fri Nov 30 05:37:28 2018 +0000
Revision:
1:79577bd1e4cb
Parent:
0:f2cc64948895
Child:
2:fe6ab9956767
2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sralph3 1:79577bd1e4cb 1 #include "uLCD_4DGL.h"
sralph3 1:79577bd1e4cb 2 #include "MMA8452.h"
sralph3 1:79577bd1e4cb 3
sralph3 1:79577bd1e4cb 4
sralph3 1:79577bd1e4cb 5 #include "SSE.h"
sralph3 1:79577bd1e4cb 6 #include "ConcreteAsteroid1.h"
sralph3 1:79577bd1e4cb 7 #include "ConcreteAsteroid2.h"
sralph3 1:79577bd1e4cb 8 #include "ConcreteAsteroid3.h"
sralph3 1:79577bd1e4cb 9 #include "ConcreteAsteroid4.h"
sralph3 1:79577bd1e4cb 10 #include "overlap.h"
sralph3 1:79577bd1e4cb 11
sralph3 1:79577bd1e4cb 12 #include <math.h>
sralph3 1:79577bd1e4cb 13
sralph3 1:79577bd1e4cb 14
sralph3 1:79577bd1e4cb 15 #define ASTEROID_HEIGHT 12
sralph3 1:79577bd1e4cb 16 #define ASTEROID_WIDTH 15
sralph3 1:79577bd1e4cb 17 #define SPRITE_MAX 15
sralph3 1:79577bd1e4cb 18 #define EARTH_WIDTH 10
sralph3 1:79577bd1e4cb 19 #define EARTH_HEIGHT 10
sralph3 1:79577bd1e4cb 20 #define EXPLOSION1_WIDTH 20
sralph3 1:79577bd1e4cb 21
sralph3 1:79577bd1e4cb 22 #define SCREEN_MAX 125
sralph3 1:79577bd1e4cb 23 #define SCREEN_MIN 1
sralph3 1:79577bd1e4cb 24 #define NUM_ASTEROIDS 4
sralph3 1:79577bd1e4cb 25
sralph3 1:79577bd1e4cb 26 #define Q 0x808000 //OLIVE
sralph3 1:79577bd1e4cb 27 #define I 0x008000 //GREEN
sralph3 1:79577bd1e4cb 28 #define S 0xC0C0C0 //SILVER
sralph3 1:79577bd1e4cb 29 #define C 0x17202A //UFO GLASS
sralph3 1:79577bd1e4cb 30 #define D 0x797D7F //DARK GREY
sralph3 1:79577bd1e4cb 31 #define L 0x00FF00 //LIME
sralph3 1:79577bd1e4cb 32 #define P 0xFF00FF //PINK
sralph3 1:79577bd1e4cb 33 #define R 0xF1C40F //YELLOW
sralph3 1:79577bd1e4cb 34 #define O 0xF39C12 //ORANGE
sralph3 1:79577bd1e4cb 35 #define G 0xAAB7B8 //GREY
sralph3 1:79577bd1e4cb 36 #define _ 0x000000 //BLACK
sralph3 1:79577bd1e4cb 37 #define X 0xFFFFFF //WHITE
sralph3 1:79577bd1e4cb 38 #define B 0x0000FF //BLUE
sralph3 1:79577bd1e4cb 39 #define r 0xFF0000 //RED
sralph3 1:79577bd1e4cb 40
sralph3 1:79577bd1e4cb 41 uLCD_4DGL uLCD(p28, p27, p29);
sralph3 1:79577bd1e4cb 42 Serial pc(USBTX,USBRX);
sralph3 1:79577bd1e4cb 43 MMA8452 acc(p9, p10, 40000); //instantiate an acc object!
sralph3 1:79577bd1e4cb 44
sralph3 1:79577bd1e4cb 45 int asteroid_sprite_1[ASTEROID_HEIGHT * ASTEROID_WIDTH] = {_,_,_,_,X,X,X,X,X,X,X,X,_,_,_,_,_,_,X,_,_,_,_,_,_,_,_,X,_,_,_,_,X,_,_,_,_,_,_,_,_,_,X,_,_,_,X,_,_,_,_,_,_,_,_,_,_,X,_,_,X,X,X,X,_,_,_,_,_,_,_,_,_,X,_,_,_,_,X,_,_,_,_,_,_,_,_,_,X,_,_,_,X,_,_,_,_,_,_,_,_,_,_,X,_,_,X,_,_,_,_,_,X,_,_,_,_,_,X,_,X,_,_,_,_,_,X,X,_,_,_,_,X,_,_,_,X,_,_,_,X,_,X,_,_,_,_,X,_,_,_,_,X,_,X,_,_,X,_,_,_,X,_,_,_,_,_,_,X,_,_,_,X,X,X,X,_,_,_,_};
sralph3 1:79577bd1e4cb 46
sralph3 1:79577bd1e4cb 47 int astero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,_,_,_,X,_,_,X,X,X,X,_,_,_,_,_,_,_,_,_,X,_,_,_,_,X,_,_,_,_,_,_,_,_,_,X,_,_,_,X,_,_,_,_,_,_,_,_,_,_,X,_,_,X,_,_,_,_,_,X,_,_,_,_,_,X,_,X,_,_,_,_,_,X,X,_,_,_,_,X,_,_,_,X,_,_,_,X,_,X,_,_,_,_,X,_,_,_,_,X,_,X,_,_,X,_,_,_,X,_,_,_,_,_,_,X,_,_,_,X,X,X,X,_,_,_,_};
sralph3 1:79577bd1e4cb 52
sralph3 1:79577bd1e4cb 53
sralph3 1:79577bd1e4cb 54 int spaceship_earth1[EARTH_WIDTH *EARTH_HEIGHT] = {_,_,S,S,S,S,S,S,_,_,_,S,I,I,I,I,I,I,S,_,S,I,I,I,I,I,I,I,I,S,S,I,I,I,I,I,I,I,I,S,S,I,I,I,I,I,I,I,I,S,S,I,I,I,I,I,I,I,I,S,S,I,I,I,I,I,I,I,I,S,S,I,I,I,I,I,I,I,I,S,S,I,I,I,I,I,I,I,S,_,_,S,S,S,S,S,S,S,_,_,};
sralph3 1:79577bd1e4cb 55
sralph3 1:79577bd1e4cb 56 int main() {
sralph3 1:79577bd1e4cb 57 uLCD.baudrate(300000);
sralph3 1:79577bd1e4cb 58 wait(0.2);
sralph3 1:79577bd1e4cb 59 srand(time(0)); // do this srandcall here ONLY... no where else in the code!
sralph3 1:79577bd1e4cb 60 ScreenObject *AstAry[NUM_ASTEROIDS];
sralph3 1:79577bd1e4cb 61
sralph3 1:79577bd1e4cb 62 AstAry[0] = new ConcreteAsteroid1;
sralph3 1:79577bd1e4cb 63 AstAry[1] = new ConcreteAsteroid2;
sralph3 1:79577bd1e4cb 64 AstAry[2] = new ConcreteAsteroid3;
sralph3 1:79577bd1e4cb 65 AstAry[3] = new ConcreteAsteroid4;
sralph3 1:79577bd1e4cb 66
sralph3 1:79577bd1e4cb 67 SSE ship;
sralph3 1:79577bd1e4cb 68
sralph3 1:79577bd1e4cb 69
sralph3 1:79577bd1e4cb 70 //set parameters -- use these and don't change
sralph3 1:79577bd1e4cb 71 acc.setBitDepth(MMA8452::BIT_DEPTH_12);
sralph3 1:79577bd1e4cb 72 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
sralph3 1:79577bd1e4cb 73 acc.setDataRate(MMA8452::RATE_100);
sralph3 1:79577bd1e4cb 74
sralph3 1:79577bd1e4cb 75 time_t startTime;
sralph3 1:79577bd1e4cb 76 startTime = time(0);
sralph3 1:79577bd1e4cb 77 time_t timeElapsed = time(0)-startTime;
sralph3 1:79577bd1e4cb 78 while (true){
sralph3 1:79577bd1e4cb 79 uLCD.cls();
sralph3 1:79577bd1e4cb 80
sralph3 1:79577bd1e4cb 81
sralph3 1:79577bd1e4cb 82 timeElapsed = time(0)-startTime;
sralph3 1:79577bd1e4cb 83 double t = (double)timeElapsed;
sralph3 1:79577bd1e4cb 84 double tw = (t/30)*SCREEN_MAX;
sralph3 1:79577bd1e4cb 85 uLCD.filled_rectangle(0, 0 , SCREEN_MAX-ceil(tw) , 5, G);
sralph3 1:79577bd1e4cb 86
sralph3 1:79577bd1e4cb 87 if (timeElapsed >= 30){
sralph3 1:79577bd1e4cb 88 uLCD.printf("\nYOU WIN!\n PRESS BUTTON TO RESTART");
sralph3 1:79577bd1e4cb 89 wait(1);
sralph3 1:79577bd1e4cb 90 return 0;
sralph3 1:79577bd1e4cb 91 }
sralph3 1:79577bd1e4cb 92
sralph3 1:79577bd1e4cb 93 ship.draw();
sralph3 1:79577bd1e4cb 94 ship.update();
sralph3 1:79577bd1e4cb 95
sralph3 1:79577bd1e4cb 96 for(int i=0; i < NUM_ASTEROIDS; i++){
sralph3 1:79577bd1e4cb 97 AstAry[i]->draw();
sralph3 1:79577bd1e4cb 98 AstAry[i]->update();
sralph3 1:79577bd1e4cb 99
sralph3 1:79577bd1e4cb 100 if(AstAry[i]->outB()){
sralph3 1:79577bd1e4cb 101 int rando = (rand() % 4);
sralph3 1:79577bd1e4cb 102
sralph3 1:79577bd1e4cb 103 if (rando == 0) {
sralph3 1:79577bd1e4cb 104 AstAry[i] = new ConcreteAsteroid1;
sralph3 1:79577bd1e4cb 105 } else if (rando == 1) {
sralph3 1:79577bd1e4cb 106 AstAry[i] = new ConcreteAsteroid2;
sralph3 1:79577bd1e4cb 107 } else if (rando == 2) {
sralph3 1:79577bd1e4cb 108 AstAry[i] = new ConcreteAsteroid3;
sralph3 1:79577bd1e4cb 109 } else {
sralph3 1:79577bd1e4cb 110 AstAry[i] = new ConcreteAsteroid4;
sralph3 1:79577bd1e4cb 111 }
sralph3 1:79577bd1e4cb 112 }
sralph3 1:79577bd1e4cb 113
sralph3 1:79577bd1e4cb 114 if(overlap( ship, *AstAry[i])){
sralph3 1:79577bd1e4cb 115 uLCD.printf("\nGAME OVER\n");
sralph3 1:79577bd1e4cb 116 wait(2);
sralph3 1:79577bd1e4cb 117 return 0;
sralph3 1:79577bd1e4cb 118 }
sralph3 1:79577bd1e4cb 119 }
sralph3 1:79577bd1e4cb 120 wait(.2);
sralph3 1:79577bd1e4cb 121 }
sralph3 1:79577bd1e4cb 122 }