diff --git a/OurSketch.ino b/OurSketch.ino index 7909cfc..57fc201 100644 --- a/OurSketch.ino +++ b/OurSketch.ino @@ -21,70 +21,73 @@ } int bar_x = 0; int bar_right = 1; +double o_x=30; //ボールの初期座標 +double o_y = 30; +int ball_degree = 45;//ボールの初期方向 +int game = 1; void loop() { - //int bar_x = 0; - //int bar_right = 1; + if(game != 1){//ゲームオーバになっていたら初期化 + o_x = 30;//ボールの初期座標 + o_y = 30; + ball_degree = 45;//ボールの初期方向 + game = 1; + } - double o_x=30; //ボールの初期座標 - double o_y = 30; - int ball_degree = 45;//ボールの初期方向 + M5.update(); - while (1){ - - M5.Display.startWrite(); + M5.Display.startWrite(); - M5.Display.fillScreen(BLACK); + M5.Display.fillScreen(BLACK); - M5.Display.drawFastHLine(bar_x,BAR_Y,BAR_SIZE,BLUE); - M5.Display.fillCircle(round(o_x),round(o_y),O_SIZE,RED); - M5.Display.endWrite(); - - o_x += BALL_SPEED * cos(ball_degree/180.0 * 3.14); - o_y += BALL_SPEED * sin(ball_degree/180.0 * 3.14); - bar_x+=BAR_SPEED * bar_right; + M5.Display.drawFastHLine(bar_x,BAR_Y,BAR_SIZE,BLUE); + M5.Display.fillCircle(round(o_x),round(o_y),O_SIZE,RED); + M5.Display.endWrite(); - //ボールが画面の横に出ないようにする - if (o_x < 0 + O_SIZE || o_x > 240 - O_SIZE){ - ball_degree = 180 - ball_degree; - if(ball_degree > 180) - ball_degree -= 360; - o_x = (240 - O_SIZE - (o_x - (240 - O_SIZE)))*(o_x > 240 - O_SIZE) +((0 + O_SIZE) - (o_x-O_SIZE))*(o_x < 0 + O_SIZE); - } - /*if (o_y < 0 + O_SIZE || o_y > 135 - O_SIZE){ - ball_degree = -ball_degree; - o_y = ((0 + O_SIZE) - (o_y-O_SIZE))*(o_y < 0 + O_SIZE) + (135 - O_SIZE - (o_y - (135 - O_SIZE)))*(o_y > 135 - O_SIZE); - }*/ - //ボールが画面の上に出ないようにする - if (o_y < 0 + O_SIZE){ - ball_degree = -ball_degree; - o_y = ((0 + O_SIZE) - (o_y-O_SIZE)); - } - //ボールがバーに当たった時の処理 - if (o_y > BAR_Y - O_SIZE && o_y < 130 - O_SIZE && (o_x >= bar_x && o_x <= bar_x + BAR_SIZE)){ - ball_degree = -1*(90 - (o_x - (bar_x + BAR_SIZE/2)) * 80 / (BAR_SIZE/2)); + o_x += BALL_SPEED * cos(ball_degree/180.0 * 3.14); + o_y += BALL_SPEED * sin(ball_degree/180.0 * 3.14); + bar_x+=BAR_SPEED * bar_right; - o_y = BAR_Y - O_SIZE -1; //ボールの位置をバーの上にする - } - - //バーが画面外に出ないようにする - if (bar_x > 240 - BAR_SIZE || bar_x < 0){ - bar_x = (240 - BAR_SIZE - (bar_x - (240 - BAR_SIZE)))*(bar_x > 240 - BAR_SIZE) +(0 - (bar_x))*(bar_x < 0); - bar_right = bar_right*(-1); - } - - //ボールが並行に飛んでいる時の処理 - if(ball_degree <= 3 && ball_degree >= -3){ - ball_degree = 6 * (2 *(ball_degree > 0) -1 ); - } - // ボールが画面下に出た時の処理 - if(o_y > 135 - O_SIZE){ - break; - } - M5.delay(10); - + //ボールが画面の横に出ないようにする + if (o_x < 0 + O_SIZE || o_x > 240 - O_SIZE){ + ball_degree = 180 - ball_degree; + if(ball_degree > 180) + ball_degree -= 360; + o_x = (240 - O_SIZE - (o_x - (240 - O_SIZE)))*(o_x > 240 - O_SIZE) +((0 + O_SIZE) - (o_x-O_SIZE))*(o_x < 0 + O_SIZE); } - M5.Display.fillScreen(RED); - M5.delay(1000); + /*if (o_y < 0 + O_SIZE || o_y > 135 - O_SIZE){ + ball_degree = -ball_degree; + o_y = ((0 + O_SIZE) - (o_y-O_SIZE))*(o_y < 0 + O_SIZE) + (135 - O_SIZE - (o_y - (135 - O_SIZE)))*(o_y > 135 - O_SIZE); + }*/ + //ボールが画面の上に出ないようにする + if (o_y < 0 + O_SIZE){ + ball_degree = -ball_degree; + o_y = ((0 + O_SIZE) - (o_y-O_SIZE)); + } + //ボールがバーに当たった時の処理 + if (o_y > BAR_Y - O_SIZE && o_y < BAR_Y + 2 && (o_x >= bar_x && o_x <= bar_x + BAR_SIZE)){ + ball_degree = -1*(90 - (o_x - (bar_x + BAR_SIZE/2)) * 80 / (BAR_SIZE/2)); + + o_y = BAR_Y - O_SIZE -1; //ボールの位置をバーの上にする + } + + //バーが画面外に出ないようにする + if (bar_x > 240 - BAR_SIZE || bar_x < 0){ + bar_x = (240 - BAR_SIZE - (bar_x - (240 - BAR_SIZE)))*(bar_x > 240 - BAR_SIZE) +(0 - (bar_x))*(bar_x < 0); + bar_right = bar_right*(-1); + } + + //ボールが並行に飛んでいる時の処理 + if(ball_degree <= 3 && ball_degree >= -3){ + ball_degree = 6 * (2 *(ball_degree > 0) -1 ); + } + // ボールが画面下に出た時の処理 + if(o_y > 135 - O_SIZE){ + game = 0; //ゲームオーバ + M5.Display.fillScreen(RED); + M5.delay(1000);} + M5.delay(10); + + }