マイコンArduinoを使って電光掲示板を作る。

 

 

マイコンArduinoを使って電光掲示板を作る。

 

続き

今回は新しいゲームを作って行きます。

 

作るゲームは「シューティングゲーム」です。

 

プログラム自体は前回のテニスと似ている部分が多いので分かり易いかと思います。

 

最初のプログラム

int count = 0;
int x_pos = 0;
int y_pos = 0;
int fort_pos = 7;        // 砲台の位置
boolean shot = false;    // ビーム発射
int velocity = 0;        // 落下速度


void setup() {
  // put your setup code here, to run once:
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,INPUT_PULLUP);
  pinMode(8,INPUT_PULLUP);
  pinMode(9,INPUT_PULLUP);
  pinMode(10,INPUT_PULLUP);
  pinMode(11,OUTPUT);
  
  x_pos = random(1, 15); // 爆弾の落ちてくる位置 ランダム
 }

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0;i < 16;i++) {
    for (int j = 0;j < 16;j++) {
      digitalWrite(2,LOW);
      // ビームを描く
      if (shot) {
        if (x_pos == j) {
          if (j == fort_pos + 1 && i > y_pos)
            digitalWrite(2,HIGH);
        }
        else {
          if (j == fort_pos + 1)
            digitalWrite(2,HIGH);
        }
      }
      // 爆弾を描く
      if (j == x_pos && i == y_pos)
        digitalWrite(2,HIGH);
      // 砲身
      if (i == 14) {          // 15段目
        if (fort_pos + 1 == j)
        digitalWrite(2,HIGH);
      }
      // 砲台
      if (i == 15) {          // 1番下の16段目
        if (fort_pos <= j && j < fort_pos + 3)
        digitalWrite(2,HIGH);
      }
      digitalWrite(4,HIGH);
      digitalWrite(4,LOW);
    }
    digitalWrite(5,HIGH);
    digitalWrite(5,LOW);
  }
  digitalWrite(6,HIGH);
  digitalWrite(6,LOW);
  
  shot = false;
  count++;
  if (count > 10) {
    count = 0;

    // 爆弾の位置 y軸
    velocity++;
    if (velocity > 5) {   // 落下速度
      velocity = 0;
      y_pos++;
      if (y_pos > 15) {
        y_pos = 0;
        x_pos = random(1, 15);
      }
    }
    
    if (digitalRead(7) == 0) {  // 発射
      shot = true;
    }

    if (digitalRead(8) == 0) {  // NOP
    }

    // 砲台の位置
    if (digitalRead(9) == 0)
      if (fort_pos > 0)
        fort_pos--;         // 左へ移動

    if (digitalRead(10) == 0)
      if (fort_pos < 13)
        fort_pos++;         // 右へ移動
  }
}

ほぼ作りはテニスと同じ。

砲台の位置 = ラケットの位置

爆弾の位置 = ボールの位置

ビームは縦に一筋のラインでこれも最初のころに経験済み

 

テストの様子


www.youtube.com

今のところ単独で動いているだけです。

 

次回、撃破のアクションやサウンド等を作って行きます。

 

では、また。

 

mckeechan.hatenadiary.com