CandlePanic Game Details here https://kohacraft.com/archives/202009080924.html

Dependencies:   mbed

Committer:
kohacraft
Date:
Tue Sep 08 01:26:37 2020 +0000
Revision:
0:63665e88c170
ver1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kohacraft 0:63665e88c170 1 /*
kohacraft 0:63665e88c170 2 candle panic
kohacraft 0:63665e88c170 3 キャンドル パニック
kohacraft 0:63665e88c170 4
kohacraft 0:63665e88c170 5 詳しくはこちら
kohacraft 0:63665e88c170 6 https://kohacraft.com/candlepanic
kohacraft 0:63665e88c170 7
kohacraft 0:63665e88c170 8 public domain
kohacraft 0:63665e88c170 9 */
kohacraft 0:63665e88c170 10
kohacraft 0:63665e88c170 11 #include "mbed.h"
kohacraft 0:63665e88c170 12
kohacraft 0:63665e88c170 13 //LED&光センサー
kohacraft 0:63665e88c170 14 #define ArrayNum 10
kohacraft 0:63665e88c170 15 DigitalOut ledK[ArrayNum] = { dp1, dp2, dp4, dp6, dp9, dp10, dp11, dp13, dp14, dp17};
kohacraft 0:63665e88c170 16 DigitalOut ledA(dp25);
kohacraft 0:63665e88c170 17 DigitalIn ledSensor(dp26);
kohacraft 0:63665e88c170 18 DigitalOut flash(dp5);
kohacraft 0:63665e88c170 19 bool sensorArray[ArrayNum] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
kohacraft 0:63665e88c170 20 bool ledArray[ArrayNum] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
kohacraft 0:63665e88c170 21 #define LedA_Led 0 //LEDをLEDモードにする
kohacraft 0:63665e88c170 22 #define LedA_Sensor 1 //LEDをセンサーモードにする
kohacraft 0:63665e88c170 23
kohacraft 0:63665e88c170 24 //サウンド
kohacraft 0:63665e88c170 25 #define C1 1/261.626
kohacraft 0:63665e88c170 26 #define D1 1/293.665
kohacraft 0:63665e88c170 27 #define E1 1/329.628
kohacraft 0:63665e88c170 28 #define F1 1/349.228
kohacraft 0:63665e88c170 29 #define G1 1/391.995
kohacraft 0:63665e88c170 30 #define A1 1/440.000
kohacraft 0:63665e88c170 31 #define B1 1/493.883
kohacraft 0:63665e88c170 32 #define C2 C1/2
kohacraft 0:63665e88c170 33 #define D2 D1/2
kohacraft 0:63665e88c170 34 #define E2 E1/2
kohacraft 0:63665e88c170 35 #define F2 F1/2
kohacraft 0:63665e88c170 36 #define G2 G1/2
kohacraft 0:63665e88c170 37 #define A2 A1/2
kohacraft 0:63665e88c170 38 #define B2 B1/2
kohacraft 0:63665e88c170 39 #define C3 C1/4
kohacraft 0:63665e88c170 40 const float tone[] = { C1, D1, E1, F1, G1, A1, B1, C2, D2, E2, F2, G2, A2, B2, C3 };
kohacraft 0:63665e88c170 41 PwmOut sp(dp18);
kohacraft 0:63665e88c170 42 bool offBeepOn = 0; //LEDがOFFするときの音を出す
kohacraft 0:63665e88c170 43 bool onBeepOn = 0; //LEDがONするときの音を出す
kohacraft 0:63665e88c170 44 int beepDevide = 30;
kohacraft 0:63665e88c170 45
kohacraft 0:63665e88c170 46 int loopCount = 0;
kohacraft 0:63665e88c170 47 int offCount = 500; //loopCountがこの値になったらLEDを一つ消す
kohacraft 0:63665e88c170 48 int speedUpCount = 30; //loopCountがこの値になったらOffCountを減らす
kohacraft 0:63665e88c170 49 int goleOffCount = 50; //offCountがこの値になったらゲームクリア
kohacraft 0:63665e88c170 50
kohacraft 0:63665e88c170 51 DigitalOut led(dp17);
kohacraft 0:63665e88c170 52
kohacraft 0:63665e88c170 53 void lightDetect();
kohacraft 0:63665e88c170 54 void beep(float);
kohacraft 0:63665e88c170 55 void showLed();
kohacraft 0:63665e88c170 56 void offBeep();
kohacraft 0:63665e88c170 57 void onBeep();
kohacraft 0:63665e88c170 58
kohacraft 0:63665e88c170 59 int main() {
kohacraft 0:63665e88c170 60
kohacraft 0:63665e88c170 61
kohacraft 0:63665e88c170 62 //初期化
kohacraft 0:63665e88c170 63 for( int i=0 ; i< ArrayNum ; i++ )
kohacraft 0:63665e88c170 64 {
kohacraft 0:63665e88c170 65 ledK[i] = 1;
kohacraft 0:63665e88c170 66 ledArray[i] = 0;
kohacraft 0:63665e88c170 67 }
kohacraft 0:63665e88c170 68 flash = 1;
kohacraft 0:63665e88c170 69
kohacraft 0:63665e88c170 70 //LEDを順番に点灯しながら音を鳴らす
kohacraft 0:63665e88c170 71
kohacraft 0:63665e88c170 72 for( int i=0 ; i< ArrayNum ; i++ )
kohacraft 0:63665e88c170 73 {
kohacraft 0:63665e88c170 74 ledArray[i] = 1;
kohacraft 0:63665e88c170 75 showLed();
kohacraft 0:63665e88c170 76 beep( C3 );
kohacraft 0:63665e88c170 77 wait( 0.1 );
kohacraft 0:63665e88c170 78 }
kohacraft 0:63665e88c170 79 beep( 0 );
kohacraft 0:63665e88c170 80
kohacraft 0:63665e88c170 81 int offCounter = 0;
kohacraft 0:63665e88c170 82 while(1) {
kohacraft 0:63665e88c170 83
kohacraft 0:63665e88c170 84 //ランダムにどれか1つ消灯する
kohacraft 0:63665e88c170 85 if( offCounter > offCount )
kohacraft 0:63665e88c170 86 {
kohacraft 0:63665e88c170 87 while(1)
kohacraft 0:63665e88c170 88 {
kohacraft 0:63665e88c170 89 float numFloat = (float)rand()/RAND_MAX * ( ArrayNum - 1 );
kohacraft 0:63665e88c170 90 int num = int( numFloat + 0.5 );
kohacraft 0:63665e88c170 91 if( ledArray[ num ] == 1 )
kohacraft 0:63665e88c170 92 {
kohacraft 0:63665e88c170 93 ledArray[ num ] = 0;
kohacraft 0:63665e88c170 94 break;
kohacraft 0:63665e88c170 95 }
kohacraft 0:63665e88c170 96 }
kohacraft 0:63665e88c170 97
kohacraft 0:63665e88c170 98 offBeepOn = 1;
kohacraft 0:63665e88c170 99 offCounter = 0;
kohacraft 0:63665e88c170 100 }
kohacraft 0:63665e88c170 101 offCounter++;
kohacraft 0:63665e88c170 102
kohacraft 0:63665e88c170 103 showLed();
kohacraft 0:63665e88c170 104
kohacraft 0:63665e88c170 105 lightDetect(); //センサ状態を取得
kohacraft 0:63665e88c170 106 for( int i=0 ; i< ArrayNum ; i++ )
kohacraft 0:63665e88c170 107 {
kohacraft 0:63665e88c170 108 if( ledArray[i] == 0 && sensorArray[i] == 1 )
kohacraft 0:63665e88c170 109 {
kohacraft 0:63665e88c170 110 ledArray[i] = 1;
kohacraft 0:63665e88c170 111 onBeepOn = 1;
kohacraft 0:63665e88c170 112 }
kohacraft 0:63665e88c170 113 }
kohacraft 0:63665e88c170 114 showLed();
kohacraft 0:63665e88c170 115
kohacraft 0:63665e88c170 116 //全部消えたら終わり
kohacraft 0:63665e88c170 117 int result = 0;
kohacraft 0:63665e88c170 118 for( int i=0 ; i< ArrayNum ; i++ )
kohacraft 0:63665e88c170 119 {
kohacraft 0:63665e88c170 120 result += ledArray[i];
kohacraft 0:63665e88c170 121 }
kohacraft 0:63665e88c170 122 if( result == 0 )
kohacraft 0:63665e88c170 123 {
kohacraft 0:63665e88c170 124 break;
kohacraft 0:63665e88c170 125 }
kohacraft 0:63665e88c170 126
kohacraft 0:63665e88c170 127 onBeep();
kohacraft 0:63665e88c170 128 offBeep();
kohacraft 0:63665e88c170 129
kohacraft 0:63665e88c170 130 wait(0.002);
kohacraft 0:63665e88c170 131 loopCount++;
kohacraft 0:63665e88c170 132 if( loopCount%speedUpCount == 0 )
kohacraft 0:63665e88c170 133 {
kohacraft 0:63665e88c170 134 offCount--;
kohacraft 0:63665e88c170 135 if( offCount < goleOffCount )
kohacraft 0:63665e88c170 136 break;
kohacraft 0:63665e88c170 137 }
kohacraft 0:63665e88c170 138
kohacraft 0:63665e88c170 139 }
kohacraft 0:63665e88c170 140
kohacraft 0:63665e88c170 141 if( offCount >= goleOffCount )
kohacraft 0:63665e88c170 142 {
kohacraft 0:63665e88c170 143 float speed = 0.2;
kohacraft 0:63665e88c170 144 //失敗
kohacraft 0:63665e88c170 145 beep( D2 );
kohacraft 0:63665e88c170 146 wait( speed );
kohacraft 0:63665e88c170 147 beep( C2 );
kohacraft 0:63665e88c170 148 wait( speed );
kohacraft 0:63665e88c170 149 beep( B1 );
kohacraft 0:63665e88c170 150 wait( speed );
kohacraft 0:63665e88c170 151 beep( A1 );
kohacraft 0:63665e88c170 152 wait( speed );
kohacraft 0:63665e88c170 153 beep( G1 );
kohacraft 0:63665e88c170 154 wait( speed );
kohacraft 0:63665e88c170 155 beep( D1 );
kohacraft 0:63665e88c170 156 wait( speed );
kohacraft 0:63665e88c170 157 beep( C2 );
kohacraft 0:63665e88c170 158 wait( speed );
kohacraft 0:63665e88c170 159 beep(0);
kohacraft 0:63665e88c170 160
kohacraft 0:63665e88c170 161 while(1)
kohacraft 0:63665e88c170 162 {
kohacraft 0:63665e88c170 163 for( int j=0 ; j<4 ; j++ )
kohacraft 0:63665e88c170 164 {
kohacraft 0:63665e88c170 165 for( int i=0; i<ArrayNum ; i+=2 )
kohacraft 0:63665e88c170 166 {
kohacraft 0:63665e88c170 167 ledArray[i]=0;
kohacraft 0:63665e88c170 168 ledArray[i+1]=1;
kohacraft 0:63665e88c170 169 }
kohacraft 0:63665e88c170 170 showLed();
kohacraft 0:63665e88c170 171 wait(0.05);
kohacraft 0:63665e88c170 172 for( int i=0; i<ArrayNum ; i++ )
kohacraft 0:63665e88c170 173 {
kohacraft 0:63665e88c170 174 ledArray[i]=0;
kohacraft 0:63665e88c170 175 }
kohacraft 0:63665e88c170 176 showLed();
kohacraft 0:63665e88c170 177 wait(0.05);
kohacraft 0:63665e88c170 178 }
kohacraft 0:63665e88c170 179 wait(0.5);
kohacraft 0:63665e88c170 180 for( int j=0 ; j<4 ; j++ )
kohacraft 0:63665e88c170 181 {
kohacraft 0:63665e88c170 182 for( int i=0; i<ArrayNum ; i+=2 )
kohacraft 0:63665e88c170 183 {
kohacraft 0:63665e88c170 184 ledArray[i]=1;
kohacraft 0:63665e88c170 185 ledArray[i+1]=0;
kohacraft 0:63665e88c170 186 }
kohacraft 0:63665e88c170 187 showLed();
kohacraft 0:63665e88c170 188 wait(0.05);
kohacraft 0:63665e88c170 189 for( int i=0; i<ArrayNum ; i++ )
kohacraft 0:63665e88c170 190 {
kohacraft 0:63665e88c170 191 ledArray[i]=0;
kohacraft 0:63665e88c170 192 }
kohacraft 0:63665e88c170 193 showLed();
kohacraft 0:63665e88c170 194 wait(0.05);
kohacraft 0:63665e88c170 195 }
kohacraft 0:63665e88c170 196 wait(0.5);
kohacraft 0:63665e88c170 197 }
kohacraft 0:63665e88c170 198
kohacraft 0:63665e88c170 199 }
kohacraft 0:63665e88c170 200 else
kohacraft 0:63665e88c170 201 {
kohacraft 0:63665e88c170 202 float speed = 0.2;
kohacraft 0:63665e88c170 203 //成功
kohacraft 0:63665e88c170 204 beep( C1 );
kohacraft 0:63665e88c170 205 wait( speed );
kohacraft 0:63665e88c170 206 beep( G1 );
kohacraft 0:63665e88c170 207 wait( speed );
kohacraft 0:63665e88c170 208 beep( B1 );
kohacraft 0:63665e88c170 209 wait( speed );
kohacraft 0:63665e88c170 210 beep( C2 );
kohacraft 0:63665e88c170 211 wait( speed );
kohacraft 0:63665e88c170 212 beep( G2 );
kohacraft 0:63665e88c170 213 wait( speed );
kohacraft 0:63665e88c170 214 beep( B2 );
kohacraft 0:63665e88c170 215 wait( speed );
kohacraft 0:63665e88c170 216 beep( C3 );
kohacraft 0:63665e88c170 217 wait( speed );
kohacraft 0:63665e88c170 218 beep(0);
kohacraft 0:63665e88c170 219
kohacraft 0:63665e88c170 220
kohacraft 0:63665e88c170 221 while(1)
kohacraft 0:63665e88c170 222 {
kohacraft 0:63665e88c170 223 /*
kohacraft 0:63665e88c170 224 for( int i=0 ; i< ArrayNum ; i++ )
kohacraft 0:63665e88c170 225 ledArray[i] = 1;
kohacraft 0:63665e88c170 226 showLed();
kohacraft 0:63665e88c170 227 wait(0.2);
kohacraft 0:63665e88c170 228
kohacraft 0:63665e88c170 229 for( int i=0 ; i< ArrayNum ; i++ )
kohacraft 0:63665e88c170 230 ledArray[i] = 0;
kohacraft 0:63665e88c170 231 showLed();
kohacraft 0:63665e88c170 232 wait(0.2);
kohacraft 0:63665e88c170 233 */
kohacraft 0:63665e88c170 234 for( int i=0 ; i< ArrayNum ; i++ ){
kohacraft 0:63665e88c170 235 ledArray[i] = 0;
kohacraft 0:63665e88c170 236 ledArray[(i + 5)%10] = 1;
kohacraft 0:63665e88c170 237 showLed();
kohacraft 0:63665e88c170 238 wait(0.1);
kohacraft 0:63665e88c170 239 }
kohacraft 0:63665e88c170 240 }
kohacraft 0:63665e88c170 241 }
kohacraft 0:63665e88c170 242 while(1)
kohacraft 0:63665e88c170 243 {
kohacraft 0:63665e88c170 244 ;
kohacraft 0:63665e88c170 245 }
kohacraft 0:63665e88c170 246 }
kohacraft 0:63665e88c170 247
kohacraft 0:63665e88c170 248 //光センサとして光を検知する
kohacraft 0:63665e88c170 249 void lightDetect()
kohacraft 0:63665e88c170 250 {
kohacraft 0:63665e88c170 251 ledA = LedA_Sensor; //光センサのモードにする
kohacraft 0:63665e88c170 252 for( int i=0 ; i < ArrayNum ; i++ ) //全てのセンサをOFFにする
kohacraft 0:63665e88c170 253 {
kohacraft 0:63665e88c170 254 ledK[i] = 0;
kohacraft 0:63665e88c170 255 sensorArray[i] = 0;
kohacraft 0:63665e88c170 256 }
kohacraft 0:63665e88c170 257
kohacraft 0:63665e88c170 258 for( int i=0 ; i < ArrayNum ; i++ )
kohacraft 0:63665e88c170 259 {
kohacraft 0:63665e88c170 260 flash = 0;
kohacraft 0:63665e88c170 261 wait(0.0001);
kohacraft 0:63665e88c170 262 flash = 1;
kohacraft 0:63665e88c170 263
kohacraft 0:63665e88c170 264 ledK[i] = 1; //特定のセンサのみONにする
kohacraft 0:63665e88c170 265 wait(0.0001);
kohacraft 0:63665e88c170 266 bool res = ledSensor; //センサの状態を読み込む
kohacraft 0:63665e88c170 267 if( res == 0 )
kohacraft 0:63665e88c170 268 sensorArray[i] = 1;
kohacraft 0:63665e88c170 269 else
kohacraft 0:63665e88c170 270 sensorArray[i] = 0;
kohacraft 0:63665e88c170 271 ledK[i] = 0; //特定のセンサをOFFにする
kohacraft 0:63665e88c170 272 }
kohacraft 0:63665e88c170 273 return;
kohacraft 0:63665e88c170 274 }
kohacraft 0:63665e88c170 275
kohacraft 0:63665e88c170 276 //指定した周期でピープを鳴らす 0ならOFF
kohacraft 0:63665e88c170 277 void beep( float scale )
kohacraft 0:63665e88c170 278 {
kohacraft 0:63665e88c170 279 if( scale != 0 )
kohacraft 0:63665e88c170 280 {
kohacraft 0:63665e88c170 281 sp.period(scale);
kohacraft 0:63665e88c170 282 sp.write(0.5f);
kohacraft 0:63665e88c170 283 }
kohacraft 0:63665e88c170 284 else
kohacraft 0:63665e88c170 285 {
kohacraft 0:63665e88c170 286 sp.write(0.0f);
kohacraft 0:63665e88c170 287 }
kohacraft 0:63665e88c170 288 }
kohacraft 0:63665e88c170 289
kohacraft 0:63665e88c170 290 //ledArrayにしたがってLEDを点灯する
kohacraft 0:63665e88c170 291 void showLed()
kohacraft 0:63665e88c170 292 {
kohacraft 0:63665e88c170 293 for( int i=0 ; i<ArrayNum ; i++ )
kohacraft 0:63665e88c170 294 {
kohacraft 0:63665e88c170 295 if( ledArray[i] == 1)
kohacraft 0:63665e88c170 296 {
kohacraft 0:63665e88c170 297 ledK[i] = 0;
kohacraft 0:63665e88c170 298 }
kohacraft 0:63665e88c170 299 else
kohacraft 0:63665e88c170 300 {
kohacraft 0:63665e88c170 301 ledK[i] = 1;
kohacraft 0:63665e88c170 302 }
kohacraft 0:63665e88c170 303 }
kohacraft 0:63665e88c170 304 ledA = LedA_Led; //LEDのモードにする
kohacraft 0:63665e88c170 305 }
kohacraft 0:63665e88c170 306
kohacraft 0:63665e88c170 307 //LEDがOFFするときのサウンド
kohacraft 0:63665e88c170 308 bool offBeepRun = 0;
kohacraft 0:63665e88c170 309 int offBeepCount = 0;
kohacraft 0:63665e88c170 310 int offMelodyCounter = 0;
kohacraft 0:63665e88c170 311 void offBeep()
kohacraft 0:63665e88c170 312 {
kohacraft 0:63665e88c170 313 int melodyLength = 2;
kohacraft 0:63665e88c170 314 float melody[2] = { E1, C1 };
kohacraft 0:63665e88c170 315
kohacraft 0:63665e88c170 316 if( offBeepOn == 1 )
kohacraft 0:63665e88c170 317 {
kohacraft 0:63665e88c170 318 offBeepOn = 0;
kohacraft 0:63665e88c170 319 offBeepRun = 1;
kohacraft 0:63665e88c170 320 offBeepCount = 0;
kohacraft 0:63665e88c170 321 offMelodyCounter = 0;
kohacraft 0:63665e88c170 322 }
kohacraft 0:63665e88c170 323
kohacraft 0:63665e88c170 324 if( offBeepRun == 1 )
kohacraft 0:63665e88c170 325 {
kohacraft 0:63665e88c170 326 if( offBeepCount%beepDevide == 0 )
kohacraft 0:63665e88c170 327 {
kohacraft 0:63665e88c170 328 if( offMelodyCounter < melodyLength )
kohacraft 0:63665e88c170 329 {
kohacraft 0:63665e88c170 330 //メロディの長さ以下だったら音を鳴らす
kohacraft 0:63665e88c170 331 beep( melody[ offMelodyCounter++ ] );
kohacraft 0:63665e88c170 332 }
kohacraft 0:63665e88c170 333 else
kohacraft 0:63665e88c170 334 {
kohacraft 0:63665e88c170 335 //音を止める
kohacraft 0:63665e88c170 336 beep(0);
kohacraft 0:63665e88c170 337 offBeepRun = 0;
kohacraft 0:63665e88c170 338 }
kohacraft 0:63665e88c170 339 }
kohacraft 0:63665e88c170 340
kohacraft 0:63665e88c170 341 offBeepCount++;
kohacraft 0:63665e88c170 342 return;
kohacraft 0:63665e88c170 343 }
kohacraft 0:63665e88c170 344
kohacraft 0:63665e88c170 345 return;
kohacraft 0:63665e88c170 346 }
kohacraft 0:63665e88c170 347
kohacraft 0:63665e88c170 348 //LEDがONするときのサウンド
kohacraft 0:63665e88c170 349 bool onBeepRun = 0;
kohacraft 0:63665e88c170 350 int onBeepCount = 0;
kohacraft 0:63665e88c170 351 int onMelodyCounter = 0;
kohacraft 0:63665e88c170 352 void onBeep()
kohacraft 0:63665e88c170 353 {
kohacraft 0:63665e88c170 354 int melodyLength = 2;
kohacraft 0:63665e88c170 355 float melody[2] = { C2, E2 };
kohacraft 0:63665e88c170 356
kohacraft 0:63665e88c170 357 if( onBeepOn == 1 )
kohacraft 0:63665e88c170 358 {
kohacraft 0:63665e88c170 359 onBeepOn = 0;
kohacraft 0:63665e88c170 360 onBeepRun = 1;
kohacraft 0:63665e88c170 361 onBeepCount = 0;
kohacraft 0:63665e88c170 362 onMelodyCounter = 0;
kohacraft 0:63665e88c170 363 }
kohacraft 0:63665e88c170 364
kohacraft 0:63665e88c170 365 if( onBeepRun == 1 )
kohacraft 0:63665e88c170 366 {
kohacraft 0:63665e88c170 367 if( onBeepCount%beepDevide == 0 )
kohacraft 0:63665e88c170 368 {
kohacraft 0:63665e88c170 369 if( onMelodyCounter < melodyLength )
kohacraft 0:63665e88c170 370 {
kohacraft 0:63665e88c170 371 //メロディの長さ以下だったら音を鳴らす
kohacraft 0:63665e88c170 372 beep( melody[ onMelodyCounter++ ] );
kohacraft 0:63665e88c170 373 }
kohacraft 0:63665e88c170 374 else
kohacraft 0:63665e88c170 375 {
kohacraft 0:63665e88c170 376 //音を止める
kohacraft 0:63665e88c170 377 beep(0);
kohacraft 0:63665e88c170 378 onBeepRun = 0;
kohacraft 0:63665e88c170 379 }
kohacraft 0:63665e88c170 380 }
kohacraft 0:63665e88c170 381
kohacraft 0:63665e88c170 382 onBeepCount++;
kohacraft 0:63665e88c170 383 return;
kohacraft 0:63665e88c170 384 }
kohacraft 0:63665e88c170 385
kohacraft 0:63665e88c170 386 return;
kohacraft 0:63665e88c170 387 }