Home
JavaEE
Java GUI
Framework
DHtml
Android
插画
留言本
  • 我用android sdk做的一个扫雷游戏
  • Android中给Spinner或ListView添加数据
  • 在android的sdk中动态添加view的方法
  • 我和老婆的婚纱相,自已做成了web方式
  • Java下实现XP强制关机的方法
  • 哈哈,老婆怀上了!婚礼不变,还是在正月十二!
  • Android中,Drawable接口及其子类的使用方法
  • 必须转载的一篇非常感人的文章!
  • 三月雨后大王山下娇艳欲滴的杜鹃
  • s60v3的6120c与蓝牙耳机nokia BH-320的配对连接方法
  • 在客户端IE中使用jasperView,客户端显示JasperViewer
  • jspsmartupload取得参数的值是乱码的问题
  • 客户端显示JasperViewer并在Jasperview的applet中加超链接
  • Hibernate基于链接表的多对多many-to-many双向关联例子
  • Hibernate双主键的设置例子
  • Hibernate中的properties和formula的用法
  • Android中,Drawable接口及其子类的使用方法
  • Android中用Bitmap获取图片中某个区域的图象
  • Android中AnimationDrawable实现简单动画的例子
  • Animation与Interpolator及AnimationSet的用法
看看android扫雷游戏的主要代码
posted by David Chen at May 6, 2009, 5:22 PM

其实用Canvas来画网格还更好,更省内存,可惜只是在学习中,先了解一下组件的用法,以后再深入。
A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with
Drawable as the type of resource retrieved for drawing things to the screen;
Drawable一般用于处理res/drawable目录下的文件,
Activity.getResources().getDrawable(R.drawable.m0);
// 这样就可以取得目录下名字叫m0的资源文件了,比较方便,
ImageButton.setImageDrawable( m0 );
// 就可以设定要显示的图了,
Drawable可以是混合的一串图,比如某个动作的一连贯图或层次图,然后可以用
setBounds(Rect),setState(int[]), setLevel(int) 来取得要用的部分

// boomArr存放的是网格的数组指针,在boomArr中的随机置生成炸弹,根据难度不同生成不同数量的炸弹
void setBooms( final int level ) {
// 以线程方式在后台生成炸弹,level是难度,每个难度增加50个炸弹
new Thread() {
public void run() {
int count = 250 + level * 50;
int total = columnCount * rowCount;
for( int i=0; i<count; i++) {
//生成从[0,count-1]间的数字,即随机设置某些网格是炸弹
boomArr[(int) ((total) * java.lang.Math.random())].isBoom = true;
}
}
}.start();
}

// 生成网格,第一列为1,2,3...第二列是4,5,6...的顺序生成
void loadBooms( final int level ) {
int index = 0;
boomArr = new Boom[columnCount * rowCount];
int x = 0, y = 0, start_x = 10, start_y = 46;
for( int c=0; c<columnCount; c++ ) {
x = start_x + c + c * 14;
for( int r=0; r<rowCount; r++ ) {
y = start_y + r + r * 14;
boomArr[ index ] = new Boom(this, index);
//指向生成的网格的指针,可能是炸弹,也可能不是
layout.addView( boomArr[ index ++ ],
new AbsoluteLayout.LayoutParams(14, 14, x, y) );
}
}
setBooms( level );
}

void resetBooms( final int level ) {
for(int c=0; c<boomArr.length; c++) {
boomArr[c].reset();
}
setBooms( level );
}

void seeAllBooms() {
for(int c=0; c<boomArr.length; c++) {
if( !boomArr[c].hasClicked ) {
boomArr[c].performClick();
// 响应boom的点击事件,
}
}
}

最看看某个网格的点击代码:这个就是扫雷功能的主要算法了,比较简单,
int[] tmp = new int[8];
tmp[0] = index - Booms.rowCount - 1; // 左上角
tmp[1] = index - 1; // 正上方
tmp[2] = index + 25 - 1; // 右上角
tmp[3] = tmp[0] + 1; // 左边
tmp[4] = tmp[2] + 1; // 右边
tmp[5] = tmp[0] + 2; // 左下角
tmp[6] = index + 1; // 正下方
tmp[7] = tmp[2] + 2; // 右下角

int total = 0;
AbsoluteLayout.LayoutParams t1 = (AbsoluteLayout.LayoutParams) getLayoutParams();
AbsoluteLayout.LayoutParams t2;
for( int c=0; c<tmp.length; c++ ) {
if( tmp[c] < 0 || tmp[c] >= Booms.columnCount * Booms.rowCount)
continue ;
if( Booms.boomArr[ tmp[c] ].isBoom ) {
t2 = (AbsoluteLayout.LayoutParams) Booms.boomArr[ tmp[c] ].getLayoutParams();
if( Math.abs(t1.x - t2.x) > 14 * 2 || Math.abs(t1.y - t2.y) > 14 * 2)
continue ;
total ++;
}
}

// 载入数字对应的图
setImageDrawable( Booms.ms[ total ] );
Labels:   Android  
Trackback:   http://cwq.iou1314.com/android-game_a351
本站内的任何文章,只代表个人意见或学习所用,如有版权声明,请尊重作者的劳动成果,在转载时请保留原始链接并注明出处。
© 2007 - 2010 CWQ.IOU1314.COM All Rights Reserved.