LoginSignup
0
1

More than 3 years have passed since last update.

スマートデバイスプログラミング③(制限時間内にボールを反射させてブロックを壊すゲーム)

Posted at

UnityとGameCanvasのダウンロード

1:UnityHubをDLしてインストール。(一部のwindows環境では拡張子を.exeに変更する必要があります)

2:UnityHubを起動して、Unity2019.3.11f1をインストール。

3:GameCanvasのダウンロード
https://github.com/sfc-sdp/GameCanvas-Unity/
でGameCanvasをダウンロード

Game.csの編集

Assets内のGame.csを別エディタで編集


using Sequence = System.Collections.IEnumerator;

/// <summary>
/// ゲームクラス。
/// </summary>
public sealed class Game : GameBase
{
  int ball_x;
  int ball_y;
  int ball_speed_x;
  int ball_speed_y;
  int player_x;
  int player_y;
  int player_w;
  int player_h;

  const int BLOCK_NUM = 50;
  int[] block_x = new int [BLOCK_NUM];
  int[] block_y = new int [BLOCK_NUM];
  bool[] block_alive_flag = new bool [BLOCK_NUM];
  int block_w = 64;
  int block_h = 20;
  int time ;

    /// <summary>
    /// 初期化処理
    /// </summary>
    public override void InitGame()
    {
      gc.SetResolution(640, 480);
      ball_x = 0;
      ball_y = 0;
      ball_speed_x = 3;
      ball_speed_y = 3;
      player_x = 270;
      player_y = 460;
      player_w = 100;
      player_h = 20;

      for(int i =0 ; i < BLOCK_NUM ; i ++ )
      {
        block_x[i] = (i % 10 ) * block_w;
        block_y[i] = (i / 10 ) * block_h;
        block_alive_flag[i] = true;
      }
      time = 0;
    }

    /// <summary>
    /// 動きなどの更新処理
    /// </summary>
    public override void UpdateGame()
    {
      if(countBlock()!=0){
        time++;
      }
      player_y = gc.GetPointerY(0) - player_h/2;

      ball_x = ball_x + ball_speed_x;
      ball_y = ball_y + ball_speed_y;

      if( ball_x < 0 ) {
        ball_x = 0;
        ball_speed_x = -ball_speed_x;
      }

      if( ball_y < 0 ) {
        ball_y = 0;
        ball_speed_y = -ball_speed_y;
      }

      if( ball_x > 616 ) {
        ball_x = 616;
        ball_speed_x = -ball_speed_x;
      }

      if( ball_y > 456 ) {
        ball_y = 456;
        ball_speed_y = -ball_speed_y;
      }
      if(gc.GetPointerFrameCount(0) > 0 ){
        player_x = gc.GetPointerX(0) - player_w/2;
      }

      if(gc.CheckHitRect(ball_x,ball_y,24,24,player_x,player_y,player_w,player_h)){
        if(ball_speed_y>0){
          ball_speed_y = -ball_speed_y;
        }
      }

      for(int i = 0; i<BLOCK_NUM; i++){
        if(gc.CheckHitRect(ball_x,ball_y,24,24,block_x[i],block_y[i],block_w,block_h)){
          block_alive_flag[i]=false;
        }
      }
    }

    /// <summary>
    /// 描画の処理
    /// </summary>
    public override void DrawGame()
    {
      // 画面を白で塗りつぶします
        gc.ClearScreen();

        // 0番の画像を描画します
        gc.DrawImage(0, 0, 0);

          gc.DrawImage(1,ball_x,ball_y);

        gc.SetColor(0, 0, 255);
        gc.FillRect(player_x,player_y,player_w,player_h);



        for(int i = 0; i<BLOCK_NUM; i++){
          if(block_alive_flag[i]){
            gc.FillRect(block_x[i],block_y[i],block_w,block_h);
          }
        }


        gc.DrawString("time:"+time,60,0);
        if(countBlock()==0){
          gc.DrawString("clear",60,30);
        }


    }
    int countBlock(){
      int num = 0;
      for(int i =0 ; i < BLOCK_NUM ; i ++ ){
        if(block_alive_flag[i]){
          num++;
        }
      }
      return num;
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1