十章:ちょっとしたゲームを作ってみる

タイトル通り今回は簡単なゲームを作ってみる。

モグラたたきのようなゲームを作ってみた。
内容的には、ほとんど今まで使ってきた内容で作成されている。
復習ついでにやってみよう。

↓今回使用した画像(ダウンロードして使ってください)

+ ソース構成の説明
  • MainActivity(親アクティビティ)+activity_main.xml(レイアウトファイル)
  • GameActivity(子アクティビティその1)
  • HelpActivity(子アクティビティその2)+help_activity.xml(新規追加レイアウトファイル)
help_activity.xmlについてはHelpActivityの項目で解説する。

+ MainActivity周りのソースと解説
といっても、ボタンを二つ用意してインテントを用いてGameActvityとHelpActivityという二つの
子アクティビティに振り分けているだけで今までと大して変わらない。
レイアウトファイルに関しては適当に作った。

+ MainActivity.java
  1. public class MainActivity extends Activity implements OnClickListener{
  2.  
  3. Intent intent;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. //ボタンのクリックイベントを使えるようにする
  11. Button button = (Button) findViewById(R.id.button1);
  12. button.setOnClickListener(this);
  13. Button button2 = (Button) findViewById(R.id.button2);
  14. button2.setOnClickListener(this);
  15. }
  16.  
  17. @Override
  18. public void onClick(View v) {
  19. switch(v.getId())
  20. {
  21. case R.id.button1:
  22. //インテントを使用する
  23. intent = new Intent(MainActivity.this, GameActivity.class);
  24. //新たなアクティビティを開始する
  25. startActivity(intent);
  26. break;
  27. case R.id.button2:
  28. //インテントを使用する
  29. intent = new Intent(MainActivity.this, HelpActivity.class);
  30. //新たなアクティビティを開始する
  31. startActivity(intent);
  32. break;
  33. }
  34. }
  35.  
  36. }
  37.  

+ activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <TextView
  12. android:id="@+id/textView1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentTop="true"
  16. android:layout_centerHorizontal="true"
  17. android:layout_marginTop="106dp"
  18. android:text="SAMPLE GAME" />
  19.  
  20. <Button
  21. android:id="@+id/button1"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_below="@+id/textView1"
  25. android:layout_centerHorizontal="true"
  26. android:layout_marginTop="48dp"
  27. android:text="PLAY" />
  28.  
  29. <Button
  30. android:id="@+id/button2"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignLeft="@+id/button1"
  34. android:layout_below="@+id/button1"
  35. android:layout_marginTop="43dp"
  36. android:text="HELP" />
  37.  
  38. </RelativeLayout>
  39.  


+ GameActivity周りのソースと解説
ここのソースは五章:画像を出すⅡで用いたソースを改装して作っている。

↓ソース長いので注意。
+ GameActivity.java
  1. public class GameActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6.  
  7. //カスタムビュークラスを設定する
  8. MyView view = new MyView(this);
  9. setContentView(view);
  10. }
  11. }
  12. class MyView extends View
  13. {
  14. private int x; //座標のx座標
  15. private int y; //座標のy座標
  16. private int targetW;//ターゲットのサイズ
  17. private int targetH;//ターゲットのサイズ
  18. private int score; //スコア
  19. private Bitmap bmp; //変換後の画像
  20. private Paint paint = new Paint();
  21. private Activity activity = (Activity)this.getContext();
  22. Resources res = this.getContext().getResources();//変換後の画像の格納先
  23.  
  24. public MyView(Context context)
  25. {
  26. super(context);
  27. setFocusable(true);
  28.  
  29. //リソースファイル内の画像をBitmapに変換して格納する
  30. bmp = BitmapFactory.decodeResource(res, R.drawable.tap);
  31. //変換されたBitmapのサイズを取得する。
  32. targetW = bmp.getWidth();
  33. targetH = bmp.getHeight();
  34.  
  35. //座標の初期化
  36. x = 0;
  37. y = 0;
  38.  
  39. score = 0;//スコア初期化
  40. }
  41.  
  42. /********
  43. * 描画
  44. *******/
  45. protected void onDraw(Canvas canvas)
  46. {
  47. if(score < 200)
  48. {
  49. //背景色を指定
  50. canvas.drawColor(Color.WHITE); //白い背景に設定する。
  51. //変換したBitmapで描画
  52. canvas.drawBitmap(bmp, x, y, paint);
  53. //スコアの描画を行う
  54. canvas.drawText("SCORE:"+score+"  CLEAR:200", 20.0f, 20.0f, paint);
  55. }
  56. else
  57. {
  58. paint.setTextSize(30.0f);
  59. canvas.drawText("CREAR", 320.0f, 450.0f, paint);
  60. canvas.drawText("Please Tap to Finsh the Game.", 170.0f, 530.0f, paint);
  61. }
  62. }
  63.  
  64. /********
  65. * タッチ
  66. *******/
  67. public boolean onTouchEvent(MotionEvent event)
  68. {
  69. //画面サイズの取得
  70. int width = getWidth();
  71. int height = getHeight();
  72.  
  73. //タッチ座標を取得してターゲットとの当たり判定を取る
  74. int touchX = (int)event.getX();
  75. int touchY = (int)event.getY();
  76. if(x < touchX && touchX < x + targetW)
  77. {
  78. if(y < touchY && touchY < y + targetH)
  79. {
  80. //ディスプレイ内に収まるようにランダムな位置を生成する
  81. x = (int)Math.round(Math.random() * width) - (targetW / 2);
  82. y = (int)Math.round(Math.random() * height) - (targetH / 2);
  83.  
  84. //スコア加算
  85. score += 10;
  86.  
  87. //再描画指令
  88. invalidate();
  89. }
  90. }
  91.  
  92. //スコアが一定数いったらfinish出来るようになる
  93. if(score > 190)
  94. {
  95. //タッチパネルが敏感なのでちょっと余分にとっておく
  96. if(score > 230)
  97. {
  98. activity.finish();
  99. }
  100. score += 10;
  101. }
  102.  
  103. return true;
  104. }
  105.  
  106. }
  107.  

+ ソース解説
五章では画像を出しただけだったが、今回はテキストも同時に出した。
こういう複合描画も可能。
また、描画の際に一番後ろにあった引数(paint)に今回は設定を追加した。
paint.set***()のように使うことで見た目を変えたりもできる。

あとはカスタムビュークラス内でアクティビティクラスでしか使えない命令を使いたいとき、
カスタムビュークラス内でActivity activity = (Activity)this.getContext();を宣言することで
カスタムビュークラスが親のアクティビティクラス(今回はGameActivity)の命令を使えたりする。
本ソース内では、98行目のactivity.finish();で使用している。


+ HelpActivity周りのソースと解説
ソース側に関しては九章の遷移先のソースそのままなので特に書かないが、
レイアウトファイルを新規作成してそちらをアクティビティで利用しているので
その作成方法を書いておく。
layoutフォルダーで新規→AndroidXMLファイルを選択して目的に適した項目を選択して
名前を付けて作成する。
あとはいつも通り適当にやってしまえばいい。

+ 一応レイアウトファイルのソース
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context=".HelpActivity" >
  11.  
  12. <ImageView
  13. android:id="@+id/imageView1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentTop="true"
  17. android:layout_alignRight="@+id/button1"
  18. android:layout_marginRight="27dp"
  19. android:layout_marginTop="56dp"
  20. android:src="@drawable/tap" />
  21.  
  22. <TextView
  23. android:id="@+id/textView1"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_alignTop="@+id/imageView1"
  27. android:layout_marginTop="54dp"
  28. android:layout_toRightOf="@+id/imageView1"
  29. android:text="タップする" />
  30.  
  31. <Button
  32. android:id="@+id/button1"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_below="@+id/imageView1"
  36. android:layout_centerHorizontal="true"
  37. android:layout_marginTop="67dp"
  38. android:text="BACK" />
  39.  
  40. </RelativeLayout>
  41.  
  42.  

+ 恒例の小話
実行してみた人はすぐに分かったと思うが、(ソースの時点で気づいた人もいると思うけど)
これ、実は相当制御の仕方やサイズの取り方など、かなり雑な作りなので
自分以外の端末との互換性を全力で無視している。

どこを直したらいいか、何を追加するべきか、そういうことを考えられて且つ実装するべき内容を
調べてでもいいから実装できるようになれば、自信を持って開発者を名乗れるようになると思うよ!
最終更新:2013年09月06日 10:23
添付ファイル