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: mbed 4DGL-uLCD-SE PinDetect
main.cpp@2:fe6ab9956767, 2018-11-30 (annotated)
- Committer:
- sralph3
- Date:
- Fri Nov 30 05:52:13 2018 +0000
- Revision:
- 2:fe6ab9956767
- Parent:
- 1:79577bd1e4cb
- Child:
- 3:8c6fa5e63937
3
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sralph3 | 1:79577bd1e4cb | 1 | #include "uLCD_4DGL.h" |
| sralph3 | 1:79577bd1e4cb | 2 | #include "MMA8452.h" |
| sralph3 | 2:fe6ab9956767 | 3 | #include "Speaker.h" |
| sralph3 | 1:79577bd1e4cb | 4 | |
| sralph3 | 1:79577bd1e4cb | 5 | |
| sralph3 | 1:79577bd1e4cb | 6 | #include "SSE.h" |
| sralph3 | 1:79577bd1e4cb | 7 | #include "ConcreteAsteroid1.h" |
| sralph3 | 1:79577bd1e4cb | 8 | #include "ConcreteAsteroid2.h" |
| sralph3 | 1:79577bd1e4cb | 9 | #include "ConcreteAsteroid3.h" |
| sralph3 | 1:79577bd1e4cb | 10 | #include "ConcreteAsteroid4.h" |
| sralph3 | 1:79577bd1e4cb | 11 | #include "overlap.h" |
| sralph3 | 1:79577bd1e4cb | 12 | |
| sralph3 | 1:79577bd1e4cb | 13 | #include <math.h> |
| sralph3 | 1:79577bd1e4cb | 14 | |
| sralph3 | 1:79577bd1e4cb | 15 | |
| sralph3 | 1:79577bd1e4cb | 16 | #define ASTEROID_HEIGHT 12 |
| sralph3 | 1:79577bd1e4cb | 17 | #define ASTEROID_WIDTH 15 |
| sralph3 | 1:79577bd1e4cb | 18 | #define SPRITE_MAX 15 |
| sralph3 | 1:79577bd1e4cb | 19 | #define EARTH_WIDTH 10 |
| sralph3 | 1:79577bd1e4cb | 20 | #define EARTH_HEIGHT 10 |
| sralph3 | 1:79577bd1e4cb | 21 | #define EXPLOSION1_WIDTH 20 |
| sralph3 | 1:79577bd1e4cb | 22 | |
| sralph3 | 1:79577bd1e4cb | 23 | #define SCREEN_MAX 125 |
| sralph3 | 1:79577bd1e4cb | 24 | #define SCREEN_MIN 1 |
| sralph3 | 1:79577bd1e4cb | 25 | #define NUM_ASTEROIDS 4 |
| sralph3 | 1:79577bd1e4cb | 26 | |
| sralph3 | 1:79577bd1e4cb | 27 | #define Q 0x808000 //OLIVE |
| sralph3 | 1:79577bd1e4cb | 28 | #define I 0x008000 //GREEN |
| sralph3 | 1:79577bd1e4cb | 29 | #define S 0xC0C0C0 //SILVER |
| sralph3 | 1:79577bd1e4cb | 30 | #define C 0x17202A //UFO GLASS |
| sralph3 | 1:79577bd1e4cb | 31 | #define D 0x797D7F //DARK GREY |
| sralph3 | 1:79577bd1e4cb | 32 | #define L 0x00FF00 //LIME |
| sralph3 | 1:79577bd1e4cb | 33 | #define P 0xFF00FF //PINK |
| sralph3 | 1:79577bd1e4cb | 34 | #define R 0xF1C40F //YELLOW |
| sralph3 | 1:79577bd1e4cb | 35 | #define O 0xF39C12 //ORANGE |
| sralph3 | 1:79577bd1e4cb | 36 | #define G 0xAAB7B8 //GREY |
| sralph3 | 1:79577bd1e4cb | 37 | #define _ 0x000000 //BLACK |
| sralph3 | 1:79577bd1e4cb | 38 | #define X 0xFFFFFF //WHITE |
| sralph3 | 1:79577bd1e4cb | 39 | #define B 0x0000FF //BLUE |
| sralph3 | 1:79577bd1e4cb | 40 | #define r 0xFF0000 //RED |
| sralph3 | 1:79577bd1e4cb | 41 | |
| sralph3 | 1:79577bd1e4cb | 42 | uLCD_4DGL uLCD(p28, p27, p29); |
| sralph3 | 1:79577bd1e4cb | 43 | Serial pc(USBTX,USBRX); |
| sralph3 | 2:fe6ab9956767 | 44 | MMA8452 acc(p9, p10, 40000); //instantiate an acc object! |
| sralph3 | 2:fe6ab9956767 | 45 | Speaker mySpeaker(p21); |
| sralph3 | 1:79577bd1e4cb | 46 | |
| sralph3 | 2:fe6ab9956767 | 47 | int asteroid_sprite_1[ASTEROID_HEIGHT * ASTEROID_WIDTH] = { |
| sralph3 | 2:fe6ab9956767 | 48 | _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 49 | _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 50 | _,_,X,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 51 | _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 52 | X,X,X,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 53 | _,_,_,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 54 | _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 55 | _,X,_,_,_,_,_,X,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 56 | X,_,_,_,_,_,X,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 57 | _,X,_,_,_,X,_,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 58 | _,_,X,_,X,_,_,X,_,_,_,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 59 | _,_,_,X,_,_,_,X,X,X,X,_,_,_,_ |
| sralph3 | 2:fe6ab9956767 | 60 | }; |
| sralph3 | 1:79577bd1e4cb | 61 | |
| sralph3 | 2:fe6ab9956767 | 62 | int asteroid_sprite_2[ASTEROID_HEIGHT * ASTEROID_WIDTH] = { |
| sralph3 | 2:fe6ab9956767 | 63 | _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 64 | _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 65 | _,_,X,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 66 | _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 67 | X,X,X,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 68 | _,_,_,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 69 | _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 70 | _,X,_,_,_,_,_,X,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 71 | X,_,_,_,_,_,X,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 72 | _,X,_,_,_,X,_,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 73 | _,_,X,_,X,_,_,X,_,_,_,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 74 | _,_,_,X,_,_,_,X,X,X,X,_,_,_,_ |
| sralph3 | 2:fe6ab9956767 | 75 | }; |
| sralph3 | 1:79577bd1e4cb | 76 | |
| sralph3 | 2:fe6ab9956767 | 77 | int asteroid_sprite_3[ASTEROID_HEIGHT * ASTEROID_WIDTH] = { |
| sralph3 | 2:fe6ab9956767 | 78 | _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 79 | _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 80 | _,_,X,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 81 | _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 82 | X,X,X,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 83 | _,_,_,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 84 | _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 85 | _,X,_,_,_,_,_,X,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 86 | X,_,_,_,_,_,X,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 87 | _,X,_,_,_,X,_,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 88 | _,_,X,_,X,_,_,X,_,_,_,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 89 | _,_,_,X,_,_,_,X,X,X,X,_,_,_,_ |
| sralph3 | 2:fe6ab9956767 | 90 | }; |
| sralph3 | 1:79577bd1e4cb | 91 | |
| sralph3 | 2:fe6ab9956767 | 92 | int asteroid_sprite_4[ASTEROID_HEIGHT * ASTEROID_WIDTH] = { |
| sralph3 | 2:fe6ab9956767 | 93 | _,_,_,_,X,X,X,X,X,X,X,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 94 | _,_,_,X,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 95 | _,_,X,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 96 | _,X,_,_,_,_,_,_,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 97 | X,X,X,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 98 | _,_,_,X,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 99 | _,_,X,_,_,_,_,_,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 100 | _,X,_,_,_,_,_,X,_,_,_,_,_,X,_, |
| sralph3 | 2:fe6ab9956767 | 101 | X,_,_,_,_,_,X,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 102 | _,X,_,_,_,X,_,X,_,_,_,_,X,_,_, |
| sralph3 | 2:fe6ab9956767 | 103 | _,_,X,_,X,_,_,X,_,_,_,X,_,_,_, |
| sralph3 | 2:fe6ab9956767 | 104 | _,_,_,X,_,_,_,X,X,X,X,_,_,_,_ |
| sralph3 | 2:fe6ab9956767 | 105 | }; |
| sralph3 | 1:79577bd1e4cb | 106 | |
| sralph3 | 2:fe6ab9956767 | 107 | int spaceship_earth1[EARTH_WIDTH *EARTH_HEIGHT] = { |
| sralph3 | 2:fe6ab9956767 | 108 | _,_,S,S,S,S,S,S,_,_, |
| sralph3 | 2:fe6ab9956767 | 109 | _,S,I,I,I,I,I,I,S,_, |
| sralph3 | 2:fe6ab9956767 | 110 | S,I,I,I,I,I,I,I,I,S, |
| sralph3 | 2:fe6ab9956767 | 111 | S,I,I,I,I,I,I,I,I,S, |
| sralph3 | 2:fe6ab9956767 | 112 | S,I,I,I,I,I,I,I,I,S, |
| sralph3 | 2:fe6ab9956767 | 113 | S,I,I,I,I,I,I,I,I,S, |
| sralph3 | 2:fe6ab9956767 | 114 | S,I,I,I,I,I,I,I,I,S, |
| sralph3 | 2:fe6ab9956767 | 115 | S,I,I,I,I,I,I,I,I,S, |
| sralph3 | 2:fe6ab9956767 | 116 | S,I,I,I,I,I,I,I,S,_, |
| sralph3 | 2:fe6ab9956767 | 117 | _,S,S,S,S,S,S,S,_,_, |
| sralph3 | 2:fe6ab9956767 | 118 | }; |
| sralph3 | 2:fe6ab9956767 | 119 | int exp1[EARTH_WIDTH *EARTH_HEIGHT] = { |
| sralph3 | 2:fe6ab9956767 | 120 | _,_,S,S,S,S,S,S,_,_, |
| sralph3 | 2:fe6ab9956767 | 121 | _,R,r,r,r,r,r,r,S,R, |
| sralph3 | 2:fe6ab9956767 | 122 | S,r,R,O,O,O,O,r,R,S, |
| sralph3 | 2:fe6ab9956767 | 123 | S,r,O,R,R,R,R,R,r,S, |
| sralph3 | 2:fe6ab9956767 | 124 | S,r,O,R,R,R,R,r,r,S, |
| sralph3 | 2:fe6ab9956767 | 125 | S,r,O,R,R,R,R,r,r,S, |
| sralph3 | 2:fe6ab9956767 | 126 | S,r,O,R,R,R,r,r,r,S, |
| sralph3 | 2:fe6ab9956767 | 127 | S,r,R,R,O,O,O,O,O,S, |
| sralph3 | 2:fe6ab9956767 | 128 | S,r,r,r,O,r,r,r,S,_, |
| sralph3 | 2:fe6ab9956767 | 129 | _,S,S,S,S,S,S,S,_,_, |
| sralph3 | 2:fe6ab9956767 | 130 | }; |
| sralph3 | 1:79577bd1e4cb | 131 | |
| sralph3 | 1:79577bd1e4cb | 132 | int main() { |
| sralph3 | 1:79577bd1e4cb | 133 | uLCD.baudrate(300000); |
| sralph3 | 1:79577bd1e4cb | 134 | wait(0.2); |
| sralph3 | 1:79577bd1e4cb | 135 | srand(time(0)); // do this srandcall here ONLY... no where else in the code! |
| sralph3 | 1:79577bd1e4cb | 136 | ScreenObject *AstAry[NUM_ASTEROIDS]; |
| sralph3 | 1:79577bd1e4cb | 137 | |
| sralph3 | 1:79577bd1e4cb | 138 | AstAry[0] = new ConcreteAsteroid1; |
| sralph3 | 1:79577bd1e4cb | 139 | AstAry[1] = new ConcreteAsteroid2; |
| sralph3 | 1:79577bd1e4cb | 140 | AstAry[2] = new ConcreteAsteroid3; |
| sralph3 | 1:79577bd1e4cb | 141 | AstAry[3] = new ConcreteAsteroid4; |
| sralph3 | 1:79577bd1e4cb | 142 | |
| sralph3 | 1:79577bd1e4cb | 143 | SSE ship; |
| sralph3 | 1:79577bd1e4cb | 144 | |
| sralph3 | 1:79577bd1e4cb | 145 | |
| sralph3 | 1:79577bd1e4cb | 146 | //set parameters -- use these and don't change |
| sralph3 | 1:79577bd1e4cb | 147 | acc.setBitDepth(MMA8452::BIT_DEPTH_12); |
| sralph3 | 1:79577bd1e4cb | 148 | acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); |
| sralph3 | 1:79577bd1e4cb | 149 | acc.setDataRate(MMA8452::RATE_100); |
| sralph3 | 1:79577bd1e4cb | 150 | |
| sralph3 | 1:79577bd1e4cb | 151 | time_t startTime; |
| sralph3 | 1:79577bd1e4cb | 152 | startTime = time(0); |
| sralph3 | 1:79577bd1e4cb | 153 | time_t timeElapsed = time(0)-startTime; |
| sralph3 | 1:79577bd1e4cb | 154 | while (true){ |
| sralph3 | 1:79577bd1e4cb | 155 | uLCD.cls(); |
| sralph3 | 1:79577bd1e4cb | 156 | |
| sralph3 | 1:79577bd1e4cb | 157 | |
| sralph3 | 1:79577bd1e4cb | 158 | timeElapsed = time(0)-startTime; |
| sralph3 | 1:79577bd1e4cb | 159 | double t = (double)timeElapsed; |
| sralph3 | 1:79577bd1e4cb | 160 | double tw = (t/30)*SCREEN_MAX; |
| sralph3 | 1:79577bd1e4cb | 161 | uLCD.filled_rectangle(0, 0 , SCREEN_MAX-ceil(tw) , 5, G); |
| sralph3 | 1:79577bd1e4cb | 162 | |
| sralph3 | 1:79577bd1e4cb | 163 | if (timeElapsed >= 30){ |
| sralph3 | 1:79577bd1e4cb | 164 | uLCD.printf("\nYOU WIN!\n PRESS BUTTON TO RESTART"); |
| sralph3 | 1:79577bd1e4cb | 165 | wait(1); |
| sralph3 | 1:79577bd1e4cb | 166 | return 0; |
| sralph3 | 1:79577bd1e4cb | 167 | } |
| sralph3 | 1:79577bd1e4cb | 168 | |
| sralph3 | 1:79577bd1e4cb | 169 | ship.update(); |
| sralph3 | 1:79577bd1e4cb | 170 | |
| sralph3 | 1:79577bd1e4cb | 171 | for(int i=0; i < NUM_ASTEROIDS; i++){ |
| sralph3 | 1:79577bd1e4cb | 172 | AstAry[i]->update(); |
| sralph3 | 1:79577bd1e4cb | 173 | if(AstAry[i]->outB()){ |
| sralph3 | 1:79577bd1e4cb | 174 | int rando = (rand() % 4); |
| sralph3 | 1:79577bd1e4cb | 175 | |
| sralph3 | 1:79577bd1e4cb | 176 | if (rando == 0) { |
| sralph3 | 1:79577bd1e4cb | 177 | AstAry[i] = new ConcreteAsteroid1; |
| sralph3 | 1:79577bd1e4cb | 178 | } else if (rando == 1) { |
| sralph3 | 1:79577bd1e4cb | 179 | AstAry[i] = new ConcreteAsteroid2; |
| sralph3 | 1:79577bd1e4cb | 180 | } else if (rando == 2) { |
| sralph3 | 1:79577bd1e4cb | 181 | AstAry[i] = new ConcreteAsteroid3; |
| sralph3 | 1:79577bd1e4cb | 182 | } else { |
| sralph3 | 1:79577bd1e4cb | 183 | AstAry[i] = new ConcreteAsteroid4; |
| sralph3 | 1:79577bd1e4cb | 184 | } |
| sralph3 | 1:79577bd1e4cb | 185 | } |
| sralph3 | 1:79577bd1e4cb | 186 | |
| sralph3 | 1:79577bd1e4cb | 187 | if(overlap( ship, *AstAry[i])){ |
| sralph3 | 2:fe6ab9956767 | 188 | uLCD.BLIT(ship.xpos, ship.ypos, EARTH_WIDTH, EARTH_HEIGHT, exp1); |
| sralph3 | 2:fe6ab9956767 | 189 | mySpeaker.PlayNote(800.0,0.25,0.1); |
| sralph3 | 2:fe6ab9956767 | 190 | mySpeaker.PlayNote(400.0,0.25,0.1); |
| sralph3 | 2:fe6ab9956767 | 191 | mySpeaker.PlayNote(200.0,0.25,0.1); |
| sralph3 | 1:79577bd1e4cb | 192 | uLCD.printf("\nGAME OVER\n"); |
| sralph3 | 1:79577bd1e4cb | 193 | wait(2); |
| sralph3 | 1:79577bd1e4cb | 194 | return 0; |
| sralph3 | 1:79577bd1e4cb | 195 | } |
| sralph3 | 2:fe6ab9956767 | 196 | AstAry[i]->draw(); |
| sralph3 | 1:79577bd1e4cb | 197 | } |
| sralph3 | 2:fe6ab9956767 | 198 | ship.draw(); |
| sralph3 | 2:fe6ab9956767 | 199 | wait(.1); |
| sralph3 | 1:79577bd1e4cb | 200 | } |
| sralph3 | 1:79577bd1e4cb | 201 | } |