我刚刚在Android上写的一个五子棋的小程序,在这里跟大家分享一下。
写完以后感觉Android的SDK,虽然也是使用Java的,但是跟Java ME还是有很大不一样。
首先就是Android的SDK没有实现所有的Java ME标准,原来运行在KJava上的应用程序是不能在Android上直接跑的。
另外就是Android的SDK有大量的API是Android自己的,需要开发人员去了解。
Android的开发框架也跟别的不一样,需要学习一下。
这个五子棋游戏是我参照Android 的Snake这个Demo还有别的例子,加上自己的需求写出来的。
其中实现了棋盘、下棋、判断输赢、重新开局等功能。目前暂时没有实现机器智能走棋子的功能。
Android的触屏功能是比较好用的,前一段时间见人演示的G1,触屏很好用,而且Android的“Window” 窗、"Shade"帘加上触摸,显得很炫。
呃,这个五子棋,也是用触摸屏实现走棋的。点一下棋盘的位子,把棋子落到棋盘上。
先贴个图看看效果吧。
好了,下面直接贴代码:
/*
* Five In a Row. (五子棋)
* 这是一个简单的五子棋程序,是我自己的一个练习,贴出来跟大家分享。
* 希望跟大家一起多交流。 我的GoogleTalk: lixinso <at> gmail.com
*
*
*/
//----------------------
//TBD:AI,悔棋
//---------------------
package lixinsong.game.gobang;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
//这是主程序,继承自Activity,实现onCreate方法。:
public class gobang extends Activity {
GobangView gbv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gbv = (GobangView)this.findViewById(R.id.gobangview);
gbv.setTextView((TextView)this.findViewById(R.id.text));
}
里面的R.id.gobangview是在res中定义的View。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<lixinsong.game.gobang.GobangView android:id="@+id/gobangview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="aaaaa" tileSize="24" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<TextView
android:id="@+id/text"
android:text="hahahhaha"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ffff0000"
android:textStyle="bold"
android:textSize="24sp" />
</RelativeLayout>
</FrameLayout>
//五子棋的View
package lixinsong.game.gobang;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
/*棋盘一共10×10格
* 棋盘居中
*
*
*
*/
//public class GobangView extends View implements Runnable {
public class GobangView extends View{
protected static int GRID_SIZE = 10;
protected static int GRID_WIDTH = 30; // 棋盘格的宽度
protected static int CHESS_DIAMETER = 26; // 棋的直径
protected static int mStartX;// 棋盘定位的左上角X
protected static int mStartY;// 棋盘定位的左上角Y
private Bitmap[] mChessBW; // 黑棋和白棋
private static int[][] mGridArray; // 网格
boolean key = false;
int wbflag = 1; //该下白棋了=2,该下黑棋了=1. 这里先下黑棋(黑棋以后设置为机器自动下的棋子)
int mLevel = 1; //游戏难度
int mWinFlag = 0;
private final int BLACK=1;
private final int WHITE=2;
int mGameState = GAMESTATE_RUN; //游戏阶段:0=尚未游戏,1=正在进行游戏,2=游戏结束
static final int GAMESTATE_PRE = 0;
static final int GAMESTATE_RUN = 1;
static final int GAMESTATE_PAUSE = 2;
static final int GAMESTATE_END = 3;
//private TextView mStatusTextView; // 根据游戏状态设置显示的文字
public TextView mStatusTextView; // 根据游戏状态设置显示的文字
private Bitmap btm1;
priv

这篇博客分享了一位开发者在Android平台上实现五子棋游戏的过程,详细介绍了如何使用Android SDK进行游戏开发,包括棋盘布局、下棋逻辑、胜负判断及重新开局功能。文章指出,Android SDK与Java ME有所不同,开发者需要熟悉特定的API和开发框架。虽然目前游戏仅支持手动落子,但作者展示了关键代码,并提到了未来可能加入的人工智能对战功能。

355

被折叠的 条评论
为什么被折叠?



