Multi-Function Clock
Dependencies: Clock DHT22 DigitDisplay mbed my8x8RGB
main.cpp@0:1ef228e02562, 2015-12-07 (annotated)
- Committer:
- mmoodevil
- Date:
- Mon Dec 07 11:24:49 2015 +0000
- Revision:
- 0:1ef228e02562
- Child:
- 1:f9ce8c057b73
edit wait
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mmoodevil | 0:1ef228e02562 | 1 | #include "mbed.h" |
mmoodevil | 0:1ef228e02562 | 2 | #include "Clock.h" |
mmoodevil | 0:1ef228e02562 | 3 | #include "DHT22.h" |
mmoodevil | 0:1ef228e02562 | 4 | #include "my8x8RGB.h" |
mmoodevil | 0:1ef228e02562 | 5 | |
mmoodevil | 0:1ef228e02562 | 6 | Ticker UpdateScreen,TCK_player; |
mmoodevil | 0:1ef228e02562 | 7 | Timeout w8; |
mmoodevil | 0:1ef228e02562 | 8 | Timer timer1; |
mmoodevil | 0:1ef228e02562 | 9 | InterruptIn BTN_Up(PA_14),BTN_Down(PA_15),BTN_Shoot(PA_13); |
mmoodevil | 0:1ef228e02562 | 10 | |
mmoodevil | 0:1ef228e02562 | 11 | class bullet |
mmoodevil | 0:1ef228e02562 | 12 | { |
mmoodevil | 0:1ef228e02562 | 13 | public: |
mmoodevil | 0:1ef228e02562 | 14 | int y,x; |
mmoodevil | 0:1ef228e02562 | 15 | uint8_t enable; |
mmoodevil | 0:1ef228e02562 | 16 | void display() { |
mmoodevil | 0:1ef228e02562 | 17 | |
mmoodevil | 0:1ef228e02562 | 18 | if(enable==1) { |
mmoodevil | 0:1ef228e02562 | 19 | rgb_point(y,6-x,0,0,100); |
mmoodevil | 0:1ef228e02562 | 20 | } |
mmoodevil | 0:1ef228e02562 | 21 | } |
mmoodevil | 0:1ef228e02562 | 22 | void RST() { |
mmoodevil | 0:1ef228e02562 | 23 | rgb_point(y,7-x,0,0,0); |
mmoodevil | 0:1ef228e02562 | 24 | y=0; |
mmoodevil | 0:1ef228e02562 | 25 | x=0; |
mmoodevil | 0:1ef228e02562 | 26 | enable=0; |
mmoodevil | 0:1ef228e02562 | 27 | } |
mmoodevil | 0:1ef228e02562 | 28 | |
mmoodevil | 0:1ef228e02562 | 29 | }; |
mmoodevil | 0:1ef228e02562 | 30 | |
mmoodevil | 0:1ef228e02562 | 31 | class enemy |
mmoodevil | 0:1ef228e02562 | 32 | { |
mmoodevil | 0:1ef228e02562 | 33 | public: |
mmoodevil | 0:1ef228e02562 | 34 | int y,x; |
mmoodevil | 0:1ef228e02562 | 35 | uint8_t level,enable; |
mmoodevil | 0:1ef228e02562 | 36 | int t_delay; |
mmoodevil | 0:1ef228e02562 | 37 | void display() { |
mmoodevil | 0:1ef228e02562 | 38 | if(enable==1) { |
mmoodevil | 0:1ef228e02562 | 39 | if(level==2 && x>=0)rgb_point(y,x,100,100,0); |
mmoodevil | 0:1ef228e02562 | 40 | if(level==1 && x>=0)rgb_point(y,x,100,0,0); |
mmoodevil | 0:1ef228e02562 | 41 | |
mmoodevil | 0:1ef228e02562 | 42 | } |
mmoodevil | 0:1ef228e02562 | 43 | } |
mmoodevil | 0:1ef228e02562 | 44 | |
mmoodevil | 0:1ef228e02562 | 45 | void RST() { |
mmoodevil | 0:1ef228e02562 | 46 | rgb_point(y,x,0,0,0); |
mmoodevil | 0:1ef228e02562 | 47 | y=0; |
mmoodevil | 0:1ef228e02562 | 48 | x=0; |
mmoodevil | 0:1ef228e02562 | 49 | enable=0; |
mmoodevil | 0:1ef228e02562 | 50 | level=0; |
mmoodevil | 0:1ef228e02562 | 51 | } |
mmoodevil | 0:1ef228e02562 | 52 | |
mmoodevil | 0:1ef228e02562 | 53 | }; |
mmoodevil | 0:1ef228e02562 | 54 | |
mmoodevil | 0:1ef228e02562 | 55 | /**************OEY******************/ |
mmoodevil | 0:1ef228e02562 | 56 | //define game variable |
mmoodevil | 0:1ef228e02562 | 57 | #define enemy_size1 10 |
mmoodevil | 0:1ef228e02562 | 58 | #define enemy_size2 15 |
mmoodevil | 0:1ef228e02562 | 59 | #define enemy_size3 20 |
mmoodevil | 0:1ef228e02562 | 60 | #define bullet_speed 50 |
mmoodevil | 0:1ef228e02562 | 61 | #define btn_delay 0.02 |
mmoodevil | 0:1ef228e02562 | 62 | uint8_t player_location=1; // in Colum |
mmoodevil | 0:1ef228e02562 | 63 | bullet mybullet[10]; |
mmoodevil | 0:1ef228e02562 | 64 | enemy wave1[enemy_size1]; |
mmoodevil | 0:1ef228e02562 | 65 | enemy wave2[enemy_size2]; |
mmoodevil | 0:1ef228e02562 | 66 | enemy wave3[enemy_size3]; |
mmoodevil | 0:1ef228e02562 | 67 | enemy wave4[enemy_size2]; |
mmoodevil | 0:1ef228e02562 | 68 | enemy wave5[enemy_size3]; |
mmoodevil | 0:1ef228e02562 | 69 | uint8_t currentbullet=0; |
mmoodevil | 0:1ef228e02562 | 70 | uint8_t gameLevel=1; |
mmoodevil | 0:1ef228e02562 | 71 | int t_ms_easy[4]= {800,1000,1200,1400}; |
mmoodevil | 0:1ef228e02562 | 72 | int t_ms_normal[4]= {500,600,700,800}; |
mmoodevil | 0:1ef228e02562 | 73 | int t_ms_hard[4]= {500,400,400,500}; |
mmoodevil | 0:1ef228e02562 | 74 | int CN[2]= {2,1}; |
mmoodevil | 0:1ef228e02562 | 75 | /*************FUNCTION*****************/ |
mmoodevil | 0:1ef228e02562 | 76 | void createWall(); |
mmoodevil | 0:1ef228e02562 | 77 | void createWall2(uint8_t r,uint8_t g,uint8_t b); |
mmoodevil | 0:1ef228e02562 | 78 | void gameOver(); |
mmoodevil | 0:1ef228e02562 | 79 | |
mmoodevil | 0:1ef228e02562 | 80 | void PMD(); |
mmoodevil | 0:1ef228e02562 | 81 | void PMU(); |
mmoodevil | 0:1ef228e02562 | 82 | void playerMoveDown(); |
mmoodevil | 0:1ef228e02562 | 83 | void playerMoveUp(); |
mmoodevil | 0:1ef228e02562 | 84 | void SHT(); |
mmoodevil | 0:1ef228e02562 | 85 | void shooting(); |
mmoodevil | 0:1ef228e02562 | 86 | void bulletUpdate(); |
mmoodevil | 0:1ef228e02562 | 87 | |
mmoodevil | 0:1ef228e02562 | 88 | void genarate_enemy(enemy stage[],int enemy_size,int t_ms[]); |
mmoodevil | 0:1ef228e02562 | 89 | void genarate_enemy_rand(enemy stage[],int enemy_size,int t_ms[]); |
mmoodevil | 0:1ef228e02562 | 90 | void newSetting(); |
mmoodevil | 0:1ef228e02562 | 91 | void enemyUpdate(enemy temp[],int enemy_size); |
mmoodevil | 0:1ef228e02562 | 92 | void TomaTosetting(); |
mmoodevil | 0:1ef228e02562 | 93 | void TomaToloop(); |
mmoodevil | 0:1ef228e02562 | 94 | |
mmoodevil | 0:1ef228e02562 | 95 | /**************MOO****************/ |
mmoodevil | 0:1ef228e02562 | 96 | Clock Clk(I2C_SDA,I2C_SCL); // I2C CLOCK DS1307 |
mmoodevil | 0:1ef228e02562 | 97 | DigitDisplay Dis(PC_8, PC_6); // Display 7segment TM1636 |
mmoodevil | 0:1ef228e02562 | 98 | InterruptIn K1(PC_10); // Inc Min (Alarm) |
mmoodevil | 0:1ef228e02562 | 99 | InterruptIn K2(PA_11); // Inc Hours (Alarm) |
mmoodevil | 0:1ef228e02562 | 100 | InterruptIn K3(PB_12); // Set Alarm |
mmoodevil | 0:1ef228e02562 | 101 | InterruptIn KS(PB_2); // Change mode |
mmoodevil | 0:1ef228e02562 | 102 | DigitalOut Beep(PC_5); // Buzzer |
mmoodevil | 0:1ef228e02562 | 103 | int M=0,H=0,N=0,I=0; // Cache Alarm |
mmoodevil | 0:1ef228e02562 | 104 | int SetH = 0 , SetM = 0; // Cache Alarm |
mmoodevil | 0:1ef228e02562 | 105 | int mode=3; // Init Mode = 0 (Default) |
mmoodevil | 0:1ef228e02562 | 106 | int flag = 0; // Init Flag for Alarm |
mmoodevil | 0:1ef228e02562 | 107 | int temperature; //Init for Memmory Temp |
mmoodevil | 0:1ef228e02562 | 108 | int Ft=0,St=0; // Cache Temp |
mmoodevil | 0:1ef228e02562 | 109 | DHT22 dht(PB_1); // OneWire for Temp get |
mmoodevil | 0:1ef228e02562 | 110 | /**************MOO****************/ |
mmoodevil | 0:1ef228e02562 | 111 | |
mmoodevil | 0:1ef228e02562 | 112 | int alarmset(int sH,int sM) // Function Set Alarm |
mmoodevil | 0:1ef228e02562 | 113 | { |
mmoodevil | 0:1ef228e02562 | 114 | int I,J,K; |
mmoodevil | 0:1ef228e02562 | 115 | Clk.getAlarm(&I,&J,&K); |
mmoodevil | 0:1ef228e02562 | 116 | if(sH == K && sM == J ) |
mmoodevil | 0:1ef228e02562 | 117 | return 1; |
mmoodevil | 0:1ef228e02562 | 118 | else |
mmoodevil | 0:1ef228e02562 | 119 | return 0; |
mmoodevil | 0:1ef228e02562 | 120 | } |
mmoodevil | 0:1ef228e02562 | 121 | |
mmoodevil | 0:1ef228e02562 | 122 | void sent() // Set time |
mmoodevil | 0:1ef228e02562 | 123 | { |
mmoodevil | 0:1ef228e02562 | 124 | wait(0.3); |
mmoodevil | 0:1ef228e02562 | 125 | SetM = (N*10) + M ; |
mmoodevil | 0:1ef228e02562 | 126 | SetH = (I*10) + H ; |
mmoodevil | 0:1ef228e02562 | 127 | //alarmset(SetH,int SetM) |
mmoodevil | 0:1ef228e02562 | 128 | printf("%d:%d",SetH,SetM); |
mmoodevil | 0:1ef228e02562 | 129 | } |
mmoodevil | 0:1ef228e02562 | 130 | |
mmoodevil | 0:1ef228e02562 | 131 | void minU(){ |
mmoodevil | 0:1ef228e02562 | 132 | M++; |
mmoodevil | 0:1ef228e02562 | 133 | wait(0.3); |
mmoodevil | 0:1ef228e02562 | 134 | if(M>9 && N<5){ |
mmoodevil | 0:1ef228e02562 | 135 | M = 0; |
mmoodevil | 0:1ef228e02562 | 136 | N++; |
mmoodevil | 0:1ef228e02562 | 137 | } |
mmoodevil | 0:1ef228e02562 | 138 | if(M>9 && N==5) |
mmoodevil | 0:1ef228e02562 | 139 | { |
mmoodevil | 0:1ef228e02562 | 140 | M = 0; |
mmoodevil | 0:1ef228e02562 | 141 | N = 0; |
mmoodevil | 0:1ef228e02562 | 142 | } |
mmoodevil | 0:1ef228e02562 | 143 | } |
mmoodevil | 0:1ef228e02562 | 144 | |
mmoodevil | 0:1ef228e02562 | 145 | void hoursU(){ |
mmoodevil | 0:1ef228e02562 | 146 | H++; |
mmoodevil | 0:1ef228e02562 | 147 | wait(0.3); |
mmoodevil | 0:1ef228e02562 | 148 | if(H>9 && I<2){ |
mmoodevil | 0:1ef228e02562 | 149 | H = 0; |
mmoodevil | 0:1ef228e02562 | 150 | I++; |
mmoodevil | 0:1ef228e02562 | 151 | } |
mmoodevil | 0:1ef228e02562 | 152 | if(H>3 && I==2) |
mmoodevil | 0:1ef228e02562 | 153 | { |
mmoodevil | 0:1ef228e02562 | 154 | H = 0; |
mmoodevil | 0:1ef228e02562 | 155 | I = 0; |
mmoodevil | 0:1ef228e02562 | 156 | } |
mmoodevil | 0:1ef228e02562 | 157 | } |
mmoodevil | 0:1ef228e02562 | 158 | |
mmoodevil | 0:1ef228e02562 | 159 | void modep(){ |
mmoodevil | 0:1ef228e02562 | 160 | wait(0.5); |
mmoodevil | 0:1ef228e02562 | 161 | screen_off(); |
mmoodevil | 0:1ef228e02562 | 162 | mode++; |
mmoodevil | 0:1ef228e02562 | 163 | if(mode == 4) |
mmoodevil | 0:1ef228e02562 | 164 | { mode = 0 ;} |
mmoodevil | 0:1ef228e02562 | 165 | wait(0.5); |
mmoodevil | 0:1ef228e02562 | 166 | |
mmoodevil | 0:1ef228e02562 | 167 | } |
mmoodevil | 0:1ef228e02562 | 168 | |
mmoodevil | 0:1ef228e02562 | 169 | int main(){ |
mmoodevil | 0:1ef228e02562 | 170 | KS.rise(&modep); |
mmoodevil | 0:1ef228e02562 | 171 | K1.rise(&minU); |
mmoodevil | 0:1ef228e02562 | 172 | K2.rise(&hoursU); |
mmoodevil | 0:1ef228e02562 | 173 | K3.rise(&sent); |
mmoodevil | 0:1ef228e02562 | 174 | TomaTosetting(); |
mmoodevil | 0:1ef228e02562 | 175 | while(1){ |
mmoodevil | 0:1ef228e02562 | 176 | while(mode == 0){ |
mmoodevil | 0:1ef228e02562 | 177 | |
mmoodevil | 0:1ef228e02562 | 178 | Clk.displays(); |
mmoodevil | 0:1ef228e02562 | 179 | wait(1); |
mmoodevil | 0:1ef228e02562 | 180 | flag = alarmset(SetH,SetM); |
mmoodevil | 0:1ef228e02562 | 181 | while(flag == 1){ |
mmoodevil | 0:1ef228e02562 | 182 | flag = alarmset(SetH,SetM); |
mmoodevil | 0:1ef228e02562 | 183 | Clk.displays(); |
mmoodevil | 0:1ef228e02562 | 184 | Beep = 1; |
mmoodevil | 0:1ef228e02562 | 185 | wait(0.2); |
mmoodevil | 0:1ef228e02562 | 186 | Beep = 0; |
mmoodevil | 0:1ef228e02562 | 187 | wait(0.2); |
mmoodevil | 0:1ef228e02562 | 188 | Beep = 1; |
mmoodevil | 0:1ef228e02562 | 189 | wait(0.2); |
mmoodevil | 0:1ef228e02562 | 190 | Beep = 0; |
mmoodevil | 0:1ef228e02562 | 191 | wait(0.5); |
mmoodevil | 0:1ef228e02562 | 192 | alarmset(SetH,SetM); |
mmoodevil | 0:1ef228e02562 | 193 | } |
mmoodevil | 0:1ef228e02562 | 194 | } |
mmoodevil | 0:1ef228e02562 | 195 | |
mmoodevil | 0:1ef228e02562 | 196 | while(mode == 1){ |
mmoodevil | 0:1ef228e02562 | 197 | Dis.write(3,M); |
mmoodevil | 0:1ef228e02562 | 198 | Dis.write(2,N); |
mmoodevil | 0:1ef228e02562 | 199 | Dis.write(1,H); |
mmoodevil | 0:1ef228e02562 | 200 | Dis.write(0,I); |
mmoodevil | 0:1ef228e02562 | 201 | } |
mmoodevil | 0:1ef228e02562 | 202 | while(mode == 2){ |
mmoodevil | 0:1ef228e02562 | 203 | dht.sample(); |
mmoodevil | 0:1ef228e02562 | 204 | wait(0.5); |
mmoodevil | 0:1ef228e02562 | 205 | temperature = dht.getTemperature(); |
mmoodevil | 0:1ef228e02562 | 206 | temperature = temperature/10; |
mmoodevil | 0:1ef228e02562 | 207 | Dis.clear(); |
mmoodevil | 0:1ef228e02562 | 208 | Ft = temperature/10; |
mmoodevil | 0:1ef228e02562 | 209 | St = temperature%10; |
mmoodevil | 0:1ef228e02562 | 210 | Dis.write(3,12); |
mmoodevil | 0:1ef228e02562 | 211 | Dis.write(2,St); |
mmoodevil | 0:1ef228e02562 | 212 | Dis.write(1,Ft); |
mmoodevil | 0:1ef228e02562 | 213 | |
mmoodevil | 0:1ef228e02562 | 214 | //printf("%d \n",temperature); |
mmoodevil | 0:1ef228e02562 | 215 | } |
mmoodevil | 0:1ef228e02562 | 216 | |
mmoodevil | 0:1ef228e02562 | 217 | while(mode == 3){ |
mmoodevil | 0:1ef228e02562 | 218 | TomaToloop();} |
mmoodevil | 0:1ef228e02562 | 219 | } |
mmoodevil | 0:1ef228e02562 | 220 | } |
mmoodevil | 0:1ef228e02562 | 221 | |
mmoodevil | 0:1ef228e02562 | 222 | /************OEY FUNCTION*****************/ |
mmoodevil | 0:1ef228e02562 | 223 | void createWall() |
mmoodevil | 0:1ef228e02562 | 224 | { |
mmoodevil | 0:1ef228e02562 | 225 | for(int i=0; i<8; i++) { |
mmoodevil | 0:1ef228e02562 | 226 | rgb_point(0,i,50,50,50); |
mmoodevil | 0:1ef228e02562 | 227 | rgb_point(7,i,50,50,50); |
mmoodevil | 0:1ef228e02562 | 228 | } |
mmoodevil | 0:1ef228e02562 | 229 | |
mmoodevil | 0:1ef228e02562 | 230 | } |
mmoodevil | 0:1ef228e02562 | 231 | |
mmoodevil | 0:1ef228e02562 | 232 | void createWall2(uint8_t r,uint8_t g,uint8_t b) |
mmoodevil | 0:1ef228e02562 | 233 | { |
mmoodevil | 0:1ef228e02562 | 234 | for(int i=0; i<8; i++) { |
mmoodevil | 0:1ef228e02562 | 235 | rgb_point(0,i,r,g,b); |
mmoodevil | 0:1ef228e02562 | 236 | rgb_point(7,i,r,g,b); |
mmoodevil | 0:1ef228e02562 | 237 | } |
mmoodevil | 0:1ef228e02562 | 238 | |
mmoodevil | 0:1ef228e02562 | 239 | } |
mmoodevil | 0:1ef228e02562 | 240 | |
mmoodevil | 0:1ef228e02562 | 241 | void PMD() |
mmoodevil | 0:1ef228e02562 | 242 | { |
mmoodevil | 0:1ef228e02562 | 243 | rgb_point(player_location,7,0,0,0); |
mmoodevil | 0:1ef228e02562 | 244 | player_location++; |
mmoodevil | 0:1ef228e02562 | 245 | if(player_location>=7) { |
mmoodevil | 0:1ef228e02562 | 246 | player_location=6; |
mmoodevil | 0:1ef228e02562 | 247 | } |
mmoodevil | 0:1ef228e02562 | 248 | rgb_point(player_location,7,0,50,0); |
mmoodevil | 0:1ef228e02562 | 249 | } |
mmoodevil | 0:1ef228e02562 | 250 | void playerMoveDown() |
mmoodevil | 0:1ef228e02562 | 251 | { |
mmoodevil | 0:1ef228e02562 | 252 | |
mmoodevil | 0:1ef228e02562 | 253 | w8.attach(&PMD,btn_delay); |
mmoodevil | 0:1ef228e02562 | 254 | } |
mmoodevil | 0:1ef228e02562 | 255 | |
mmoodevil | 0:1ef228e02562 | 256 | void PMU() |
mmoodevil | 0:1ef228e02562 | 257 | { |
mmoodevil | 0:1ef228e02562 | 258 | rgb_point(player_location,7,0,0,0); |
mmoodevil | 0:1ef228e02562 | 259 | player_location--; |
mmoodevil | 0:1ef228e02562 | 260 | if(player_location<=0) { |
mmoodevil | 0:1ef228e02562 | 261 | player_location=1; |
mmoodevil | 0:1ef228e02562 | 262 | } |
mmoodevil | 0:1ef228e02562 | 263 | rgb_point(player_location,7,0,50,0); |
mmoodevil | 0:1ef228e02562 | 264 | |
mmoodevil | 0:1ef228e02562 | 265 | } |
mmoodevil | 0:1ef228e02562 | 266 | void playerMoveUp() |
mmoodevil | 0:1ef228e02562 | 267 | { |
mmoodevil | 0:1ef228e02562 | 268 | |
mmoodevil | 0:1ef228e02562 | 269 | w8.attach(&PMU,btn_delay); |
mmoodevil | 0:1ef228e02562 | 270 | } |
mmoodevil | 0:1ef228e02562 | 271 | |
mmoodevil | 0:1ef228e02562 | 272 | void SHT() |
mmoodevil | 0:1ef228e02562 | 273 | { |
mmoodevil | 0:1ef228e02562 | 274 | mybullet[currentbullet].y=player_location; |
mmoodevil | 0:1ef228e02562 | 275 | mybullet[currentbullet].enable=1; |
mmoodevil | 0:1ef228e02562 | 276 | currentbullet++; |
mmoodevil | 0:1ef228e02562 | 277 | if(currentbullet>=10)currentbullet=0; |
mmoodevil | 0:1ef228e02562 | 278 | } |
mmoodevil | 0:1ef228e02562 | 279 | |
mmoodevil | 0:1ef228e02562 | 280 | void shooting() |
mmoodevil | 0:1ef228e02562 | 281 | { |
mmoodevil | 0:1ef228e02562 | 282 | w8.attach(&SHT,btn_delay); |
mmoodevil | 0:1ef228e02562 | 283 | } |
mmoodevil | 0:1ef228e02562 | 284 | |
mmoodevil | 0:1ef228e02562 | 285 | void bulletUpdate() |
mmoodevil | 0:1ef228e02562 | 286 | { |
mmoodevil | 0:1ef228e02562 | 287 | |
mmoodevil | 0:1ef228e02562 | 288 | for(int i=0; i<10; i++) { |
mmoodevil | 0:1ef228e02562 | 289 | if(mybullet[i].enable==1) { |
mmoodevil | 0:1ef228e02562 | 290 | mybullet[i].display(); |
mmoodevil | 0:1ef228e02562 | 291 | if(timer1.read_ms()%bullet_speed==0) { |
mmoodevil | 0:1ef228e02562 | 292 | rgb_point(mybullet[i].y,6-mybullet[i].x,0,0,0); |
mmoodevil | 0:1ef228e02562 | 293 | mybullet[i].x++; |
mmoodevil | 0:1ef228e02562 | 294 | if(mybullet[i].x>=7)mybullet[i].RST(); |
mmoodevil | 0:1ef228e02562 | 295 | } |
mmoodevil | 0:1ef228e02562 | 296 | } |
mmoodevil | 0:1ef228e02562 | 297 | |
mmoodevil | 0:1ef228e02562 | 298 | } |
mmoodevil | 0:1ef228e02562 | 299 | } |
mmoodevil | 0:1ef228e02562 | 300 | |
mmoodevil | 0:1ef228e02562 | 301 | |
mmoodevil | 0:1ef228e02562 | 302 | void genarate_enemy(enemy stage[],int enemy_size,int t_ms[]) |
mmoodevil | 0:1ef228e02562 | 303 | { |
mmoodevil | 0:1ef228e02562 | 304 | for(int i=0; i<enemy_size; i++) { |
mmoodevil | 0:1ef228e02562 | 305 | stage[i].x=0; |
mmoodevil | 0:1ef228e02562 | 306 | stage[i].y=(rand()%6) +1; |
mmoodevil | 0:1ef228e02562 | 307 | for(int j=0; j< enemy_size && i!=j; j++) { |
mmoodevil | 0:1ef228e02562 | 308 | if(stage[i].y==stage[j].y && stage[i].x==stage[j].x)stage[i].x++; |
mmoodevil | 0:1ef228e02562 | 309 | |
mmoodevil | 0:1ef228e02562 | 310 | } |
mmoodevil | 0:1ef228e02562 | 311 | stage[i].level=1; |
mmoodevil | 0:1ef228e02562 | 312 | stage[i].enable=1; |
mmoodevil | 0:1ef228e02562 | 313 | stage[i].t_delay=t_ms[rand()%4 +1]; |
mmoodevil | 0:1ef228e02562 | 314 | if(stage[i].y==0 || stage[i].y==7) { |
mmoodevil | 0:1ef228e02562 | 315 | stage[i].enable=0; |
mmoodevil | 0:1ef228e02562 | 316 | stage[i].RST(); |
mmoodevil | 0:1ef228e02562 | 317 | } |
mmoodevil | 0:1ef228e02562 | 318 | |
mmoodevil | 0:1ef228e02562 | 319 | } |
mmoodevil | 0:1ef228e02562 | 320 | } |
mmoodevil | 0:1ef228e02562 | 321 | |
mmoodevil | 0:1ef228e02562 | 322 | |
mmoodevil | 0:1ef228e02562 | 323 | |
mmoodevil | 0:1ef228e02562 | 324 | void genarate_enemy_rand(enemy stage[],int enemy_size,int t_ms[]) |
mmoodevil | 0:1ef228e02562 | 325 | { |
mmoodevil | 0:1ef228e02562 | 326 | for(int i=0; i<enemy_size; i++) { |
mmoodevil | 0:1ef228e02562 | 327 | stage[i].x=0; |
mmoodevil | 0:1ef228e02562 | 328 | stage[i].y=rand()%6 +1; |
mmoodevil | 0:1ef228e02562 | 329 | for(int j=0; j< enemy_size && i!=j; j++) { |
mmoodevil | 0:1ef228e02562 | 330 | if(stage[i].y==stage[j].y)stage[i].x++; |
mmoodevil | 0:1ef228e02562 | 331 | stage[i].enable=1; |
mmoodevil | 0:1ef228e02562 | 332 | } |
mmoodevil | 0:1ef228e02562 | 333 | stage[i].level=rand()%2 +1; |
mmoodevil | 0:1ef228e02562 | 334 | stage[i].enable=1; |
mmoodevil | 0:1ef228e02562 | 335 | stage[i].t_delay=t_ms[rand()%4 +1]; |
mmoodevil | 0:1ef228e02562 | 336 | if(stage[i].y==0 || stage[i].y==7) { |
mmoodevil | 0:1ef228e02562 | 337 | stage[i].enable=0; |
mmoodevil | 0:1ef228e02562 | 338 | stage[i].RST(); |
mmoodevil | 0:1ef228e02562 | 339 | } |
mmoodevil | 0:1ef228e02562 | 340 | |
mmoodevil | 0:1ef228e02562 | 341 | } |
mmoodevil | 0:1ef228e02562 | 342 | } |
mmoodevil | 0:1ef228e02562 | 343 | |
mmoodevil | 0:1ef228e02562 | 344 | void newSetting(){ |
mmoodevil | 0:1ef228e02562 | 345 | screen_off(); |
mmoodevil | 0:1ef228e02562 | 346 | createWall(); |
mmoodevil | 0:1ef228e02562 | 347 | rgb_point(player_location,7,0,50,0); |
mmoodevil | 0:1ef228e02562 | 348 | } |
mmoodevil | 0:1ef228e02562 | 349 | |
mmoodevil | 0:1ef228e02562 | 350 | void enemyUpdate(enemy temp[],int enemy_size) |
mmoodevil | 0:1ef228e02562 | 351 | { |
mmoodevil | 0:1ef228e02562 | 352 | uint8_t status=0; |
mmoodevil | 0:1ef228e02562 | 353 | for(int i=0; i<enemy_size; i++) { |
mmoodevil | 0:1ef228e02562 | 354 | if(temp[i].enable==1) { |
mmoodevil | 0:1ef228e02562 | 355 | temp[i].display(); |
mmoodevil | 0:1ef228e02562 | 356 | if(timer1.read_ms()%temp[i].t_delay==0) { |
mmoodevil | 0:1ef228e02562 | 357 | rgb_point(temp[i].y,temp[i].x,0,0,0); |
mmoodevil | 0:1ef228e02562 | 358 | temp[i].x++; |
mmoodevil | 0:1ef228e02562 | 359 | if(temp[i].x>7) { //Game Over |
mmoodevil | 0:1ef228e02562 | 360 | newSetting(); |
mmoodevil | 0:1ef228e02562 | 361 | createWall2(255,10,255); |
mmoodevil | 0:1ef228e02562 | 362 | |
mmoodevil | 0:1ef228e02562 | 363 | temp[i].RST(); |
mmoodevil | 0:1ef228e02562 | 364 | // screen_color(100,10,10); |
mmoodevil | 0:1ef228e02562 | 365 | gameLevel=1; |
mmoodevil | 0:1ef228e02562 | 366 | genarate_enemy(wave1,enemy_size1,t_ms_easy); |
mmoodevil | 0:1ef228e02562 | 367 | } |
mmoodevil | 0:1ef228e02562 | 368 | } |
mmoodevil | 0:1ef228e02562 | 369 | for(int j =0; j<10; j++) { |
mmoodevil | 0:1ef228e02562 | 370 | if(temp[i].x==(6-mybullet[j].x) && temp[i].y==mybullet[j].y) { |
mmoodevil | 0:1ef228e02562 | 371 | mybullet[j].RST(); |
mmoodevil | 0:1ef228e02562 | 372 | if(temp[i].level==2)temp[i].level=1; |
mmoodevil | 0:1ef228e02562 | 373 | else |
mmoodevil | 0:1ef228e02562 | 374 | temp[i].RST(); |
mmoodevil | 0:1ef228e02562 | 375 | } |
mmoodevil | 0:1ef228e02562 | 376 | } |
mmoodevil | 0:1ef228e02562 | 377 | } else status++; |
mmoodevil | 0:1ef228e02562 | 378 | } |
mmoodevil | 0:1ef228e02562 | 379 | if(status>=enemy_size) { |
mmoodevil | 0:1ef228e02562 | 380 | wait_ms(500); |
mmoodevil | 0:1ef228e02562 | 381 | gameLevel++; |
mmoodevil | 0:1ef228e02562 | 382 | newSetting(); |
mmoodevil | 0:1ef228e02562 | 383 | if(gameLevel==2)genarate_enemy(wave2,enemy_size2,t_ms_normal); |
mmoodevil | 0:1ef228e02562 | 384 | if(gameLevel==3)genarate_enemy(wave3,enemy_size2,t_ms_hard); |
mmoodevil | 0:1ef228e02562 | 385 | if(gameLevel==4)genarate_enemy_rand(wave4,enemy_size1,t_ms_easy); |
mmoodevil | 0:1ef228e02562 | 386 | if(gameLevel==5)genarate_enemy_rand(wave5,enemy_size2,t_ms_normal); |
mmoodevil | 0:1ef228e02562 | 387 | if(gameLevel==6){ |
mmoodevil | 0:1ef228e02562 | 388 | genarate_enemy_rand(wave5,enemy_size3,t_ms_normal); |
mmoodevil | 0:1ef228e02562 | 389 | createWall2(0,30,255); |
mmoodevil | 0:1ef228e02562 | 390 | } |
mmoodevil | 0:1ef228e02562 | 391 | } |
mmoodevil | 0:1ef228e02562 | 392 | |
mmoodevil | 0:1ef228e02562 | 393 | } |
mmoodevil | 0:1ef228e02562 | 394 | |
mmoodevil | 0:1ef228e02562 | 395 | void TomaTosetting(){ |
mmoodevil | 0:1ef228e02562 | 396 | timer1.start(); |
mmoodevil | 0:1ef228e02562 | 397 | char wb[3]= {128,128,128}; |
mmoodevil | 0:1ef228e02562 | 398 | INITRGB(wb); |
mmoodevil | 0:1ef228e02562 | 399 | /***************Genarate Enemy*********************/ |
mmoodevil | 0:1ef228e02562 | 400 | genarate_enemy(wave1,enemy_size1,t_ms_easy); |
mmoodevil | 0:1ef228e02562 | 401 | |
mmoodevil | 0:1ef228e02562 | 402 | /***************Setting Button*******************/ |
mmoodevil | 0:1ef228e02562 | 403 | BTN_Down.rise(&playerMoveDown); |
mmoodevil | 0:1ef228e02562 | 404 | BTN_Down.mode(PullUp); |
mmoodevil | 0:1ef228e02562 | 405 | BTN_Up.rise(&playerMoveUp); |
mmoodevil | 0:1ef228e02562 | 406 | BTN_Up.mode(PullUp); |
mmoodevil | 0:1ef228e02562 | 407 | BTN_Shoot.rise(&shooting); |
mmoodevil | 0:1ef228e02562 | 408 | BTN_Shoot.mode(PullUp); |
mmoodevil | 0:1ef228e02562 | 409 | |
mmoodevil | 0:1ef228e02562 | 410 | /************************************/ |
mmoodevil | 0:1ef228e02562 | 411 | //gameSetting |
mmoodevil | 0:1ef228e02562 | 412 | newSetting(); |
mmoodevil | 0:1ef228e02562 | 413 | /***********************************/ |
mmoodevil | 0:1ef228e02562 | 414 | |
mmoodevil | 0:1ef228e02562 | 415 | } |
mmoodevil | 0:1ef228e02562 | 416 | void TomaToloop(){ |
mmoodevil | 0:1ef228e02562 | 417 | |
mmoodevil | 0:1ef228e02562 | 418 | displayRGB(); |
mmoodevil | 0:1ef228e02562 | 419 | bulletUpdate(); |
mmoodevil | 0:1ef228e02562 | 420 | displayRGB(); |
mmoodevil | 0:1ef228e02562 | 421 | //displayRGB(); |
mmoodevil | 0:1ef228e02562 | 422 | if(gameLevel==1)enemyUpdate(wave1,enemy_size1); |
mmoodevil | 0:1ef228e02562 | 423 | if(gameLevel==2)enemyUpdate(wave2,enemy_size2); |
mmoodevil | 0:1ef228e02562 | 424 | if(gameLevel==3)enemyUpdate(wave3,enemy_size3); |
mmoodevil | 0:1ef228e02562 | 425 | if(gameLevel==4)enemyUpdate(wave4,enemy_size1); |
mmoodevil | 0:1ef228e02562 | 426 | if(gameLevel==5)enemyUpdate(wave5,enemy_size2); |
mmoodevil | 0:1ef228e02562 | 427 | if(gameLevel==6)enemyUpdate(wave5,enemy_size3);//6 |
mmoodevil | 0:1ef228e02562 | 428 | displayRGB(); |
mmoodevil | 0:1ef228e02562 | 429 | |
mmoodevil | 0:1ef228e02562 | 430 | } |
mmoodevil | 0:1ef228e02562 | 431 | |
mmoodevil | 0:1ef228e02562 | 432 |