スマートデバイスプログラミング④（上からランダムに降ってくるボールを拾ってスコアを競うゲーム）

UnityとGameCanvasのダウンロード

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

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

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

４：追加画像をダウンロードしてAssetsに追加
http://web.sfc.keio.ac.jp/~wadari/sdp/k04_res.zip

Game.csの編集

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

```c#

using Sequence = System.Collections.IEnumerator;

/// <summary>
/// ゲームクラス。
/// </summary>
public sealed class Game : GameBase
{
  const int BALL_NUM = 30;
  int[] ball_x = new int [BALL_NUM];
  int[] ball_y = new int [BALL_NUM];
  int[] ball_col = new int [BALL_NUM];
  int[] ball_speed = new int [BALL_NUM];
  int ball_w = 24;
  int ball_h = 24;
  int player_x = 304;
  int player_y = 448;
  int player_speed = 3;
  int player_w = 32;
  int player_h = 32;
  int player_img = 4;
  int score = 0;
  int time = 1800;
  int player_col = 4;
  int combo = 0;

    /// <summary>
    /// 初期化処理
    /// </summary>
    public override void InitGame()
    {
      gc.SetResolution(640, 480);
      for(int i =0 ; i < BALL_NUM ; i ++ )
      {
        resetBall(i);
      }
    }

    /// <summary>
    /// 動きなどの更新処理
    /// </summary>
    public override void UpdateGame()
    {
      time = time - 1;

      for(int i =0 ; i < BALL_NUM ; i ++ )
      {
        ball_y[i] = ball_y[i] + ball_speed[i];
        if(ball_y[i]> 480){
          resetBall(i);
        }

        if(gc.CheckHitRect(ball_x[i],ball_y[i],ball_w,ball_h,player_x,player_y,player_w,player_h)){
          if(time>=0){
            score=score+ball_col[i];//ballごとに違った点数を加える
            if(player_col == ball_col[i]) {
              combo++;
              score+= combo;
            }
            else {
              combo = 0;
            }

            player_col = ball_col[i];
          }
          resetBall(i);
        }

      }
      if(gc.GetPointerFrameCount(0) > 0 ){
        if(gc.GetPointerX(0) > 320) {
          player_x += player_speed;
          player_img = 4;
        }
        else {
          player_x -= player_speed;
          player_img = 5;
        }
      }
      if(player_x<=0){
        player_x=0;
      }
      if(player_x>=616){
        player_x=616;
      }


    }

    /// <summary>
    /// 描画の処理
    /// </summary>
    public override void DrawGame()
    {
      gc.ClearScreen();

      for(int i =0 ; i < BALL_NUM ; i ++ ){
        gc.DrawImage(ball_col[i],ball_x[i],ball_y[i]);
      }

      gc.SetColor(0,0,0);
      if(time>=0){
        gc.DrawString("time:"+time,0,0);
      }
      else {
        gc.DrawString("finished!!",0,0);
      }
      gc.DrawString("score:"+score,0,24);
      gc.DrawString("combo:"+combo,0,48);

      //gc.DrawClipImage(player_img,player_x,player_y,0,64,32,32);
      if(time>=0){
        int u = 32+ ((time%60)/30)*32;//0.5秒ごとにユーザー画像を切り替えx
        int v = (player_col - 1) *32;//最初4 色に応じてユーザーの色の画像を指定y
        gc.DrawClipImage(player_img,player_x,player_y, u,v,32,32);
      }
      else {
        gc.DrawClipImage(player_img,player_x,player_y, 96,(player_col - 1) *32,32,32);
      }


    }
    void resetBall(int id){
      ball_x[id] = gc.Random(0,616);
      ball_y[id] = -gc.Random(24,480);
      ball_speed[id] = gc.Random(3,6);
      ball_col[id] = gc.Random(1,3);
    }
}
```