Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
ผู้อ่านจะสังเกตว่าโค้ดบรรทัด XOGameActivity ซึ่งเป็นหน้าจอหลักสำหรับเล่นเกมจะยังมี Error อยู่<br />
โดยผู้เขียนจะกล่าวถึงขั้นตอนการสร้างในหัวข้อถัดไป<br />
อธิบายโค้ดการทำงานของเกม<br />
ผู้อ่านจะสังเกตเห็นว่ามีการเขียนโปรแกรมลักษณะคล้ายกับคลาส GameMenuActivity ซึ่งสร้าง<br />
ขึ้นในหัวข้อก่อนหน้านี้ ซึ่งส่วนที่แตกต่างกันก็คือ มีการสร้างการเชื่อมโยงไปยังคลาส XOGameActivity เมื่อ<br />
คลิกปุ่ม bt_start โดยจะมีการส่งข้อมูล 3 ค่า ได้แก่<br />
• "PLAYER" เก็บรูปแบบการเล่น<br />
• "PLAYER_ONE_NAME" เก็บชื่อผู้เล่นคนแรก<br />
• "PLAYER_TWO_NAME" เก็บชื่อผู้เล่นคนที่สอง<br />
Intent intent = new Intent(getBaseContext(),XOGameActivity.class)<br />
intent.putExtra("PLAYER", player);<br />
intent.putExtra("PLAYER_ONE_NAME", player1.getText().toString());<br />
intent.putExtra("PLAYER_TWO_NAME", player2.getText().toString());<br />
startActivity(intent);<br />
การสร้างหน้าจอสำหรับเล่นเกม<br />
ในหัวข้อนี้ผู้เขียนจะอธิบายการสร้างหน้าจอสำหรับเล่นเกม โดยสร้างเป็นหน้าจอเปล่าๆ ไว้ก่อน<br />
ซึ่งรายละเอียดจะเขียนโค้ดเพิ่มเติมในภายหลัง<br />
ในการสร้างหน้าจอสำหรับเล่นเกมนั้น จะไม่ใช้งานไฟล์ XMLในการออกแบบหน้าจอ แต่จะสร้างด้วย<br />
โค้ด Java แทน Graphic โดยแก้ไขโค้ดในไฟล์ XOGameActivity.java ดังนี้<br />
package com.android.example.xogame;<br />
import android.app.Activity;<br />
import android.content.Context;<br />
import android.content.Intent;<br />
import android.graphics.Canvas;<br />
import android.graphics.Color;<br />
import android.graphics.Paint;<br />
import android.graphics.Paint.Cap;<br />
import android.graphics.Point;<br />
import android.os.Bundle;<br />
import android.util.Log;<br />
import android.view.MotionEvent;<br />
import android.view.View;<br />
import android.view.View.OnTouchListener;<br />
เพิ่มคลาส<br />
public class XOGameActivity extends Activity {<br />
String TAG = "OX Game";<br />
public Paint paint;<br />
public static float block_width = 0;<br />
public static int current_player = 1;<br />
public static boolean one_player = false;<br />
public static int count = 0;<br />
public static int winner = -1;<br />
public boolean pause_flag = false;<br />
254<br />
<strong>Android</strong> <strong>App</strong> <strong>Development</strong> <strong>ฉบับสมบูรณ์</strong>