Newer
Older
OurSketch / OurSketch.ino
#include <M5Unified.h>
#include <math.h>

//Bar_Color BLUE
#define BAR_Y 120
#define BAR_SIZE 50
#define BAR_SPEED 4
#define O_SIZE 4
#define BALL_SPEED 2

void setup() {
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);
  M5.Display.setRotation(3);
  M5.Display.setFont(&fonts::lgfxJapanGothic_16);

  M5.Display.setTextScroll(true);
  M5.Display.setBrightness(255);
  M5.Display.fillScreen(BLACK);
}
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() {
  if(game != 1){//ゲームオーバになっていたら初期化
    o_x = 30;//ボールの初期座標
    o_y = 30;
    ball_degree = 45;//ボールの初期方向
    game = 1;
  } 



  M5.update();
  M5.Display.startWrite();

  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;

  //ボールが画面の横に出ないようにする
  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 < BAR_Y + 2 && (o_x >= bar_x && o_x <= bar_x + BAR_SIZE)){
    ball_degree = -1*(90 - (o_x - (bar_x + BAR_SIZE/2)) * 85 / (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);


}