Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9763

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

$
0
0

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を別エディタで編集

usingSequence=System.Collections.IEnumerator;/// <summary>/// ゲームクラス。/// </summary>publicsealedclassGame:GameBase{intball_x;intball_y;intball_speed_x;intball_speed_y;intplayer_x;intplayer_y;intplayer_w;intplayer_h;constintBLOCK_NUM=50;int[]block_x=newint[BLOCK_NUM];int[]block_y=newint[BLOCK_NUM];bool[]block_alive_flag=newbool[BLOCK_NUM];intblock_w=64;intblock_h=20;inttime;/// <summary>/// 初期化処理/// </summary>publicoverridevoidInitGame(){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(inti=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>publicoverridevoidUpdateGame(){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(inti=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>publicoverridevoidDrawGame(){// 画面を白で塗りつぶします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(inti=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);}}intcountBlock(){intnum=0;for(inti=0;i<BLOCK_NUM;i++){if(block_alive_flag[i]){num++;}}returnnum;}}

Viewing all articles
Browse latest Browse all 9763

Trending Articles