1、/方格的边长 protected static int mXTileCount;/X轴上方格的个数 protected static int mYTileCount;/Y轴上方格的个数 private static int mXOffset;/绘图时X轴上的起始坐标 private static int mYOffset;/绘图时Y轴上的起始坐标 private Bitmap mTileArray;/位图数组 private int mTileGrid;/映射整个游戏画面的数组 private final Paint mPaint = new Paint();/画笔 public TileVi
2、ew(Context context, AttributeSet attrs) super(context, attrs); Log.i(tag, TileView Constructor);mTileSize= + mTileSize); /重置位图数组的长度 public void resetTiles(int tilecount) mTileArray = new Bitmaptilecount; Override /适应各种分辨率的屏幕,当改变屏幕大小尺寸时,同时修改tile的相关计数指标 protected void onSizeChanged(int w, int h, int o
3、ldw, int oldh) onSizeChanged, + w= + w + h= + h + oldw= + oldw + oldh= + oldh); mXTileCount = (int) Math.floor(w / mTileSize); mYTileCount = (int) Math.floor(h / mTileSize);mXTileCount= + mXTileCount);mYTileCount= + mYTileCount); mXOffset = (w - (mTileSize * mXTileCount) / 2); mYOffset = (h - (mTile
4、Size * mYTileCount) / 2);mXOffset= + mXOffset);mYOffset= + mYOffset); mTileGrid = new intmXTileCountmYTileCount; clearTiles(); /这里做了一个 Drawable 到 bitmap 的转换 public void loadTile(int key, Drawable tile) Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888); Canvas canvas
5、= new Canvas(bitmap); tile.setBounds(0, 0, mTileSize, mTileSize); tile.draw(canvas); mTileArraykey = bitmap;/清空图形显示 public void clearTiles() TileView.clearTiles for (int x = 0; x mXTileCount; x+) for (int y = 0; y 0) bmp = mTileArraymTileGridxy; left = x * mTileSize + mXOffset; top = y * mTileSize +
6、 mYOffset; canvas.drawBitmap(bmp, left, top, mPaint); SnakeView类定义了游戏运行时的画面改变及蛇体和果子的更新方法。首先用Handler机制实现定时刷新,代码如下:class RefreshHandler extends Handler /获取消息并处理 Override public void handleMessage(Message msg) SnakeView.this.update(); SnakeView.this.invalidate();/刷新view为基类的界面 Log.i(tag, handleMessage|T
7、hread Name=+Thread.currentThread().getName(); /定时发送消息给UI线程,以此达到更新效果 public void sleep(long delayMillis) this.removeMessages(0); /清空消息队列,Handler进入对新消息的等待 sleep|Thread Name= /定时发送新消息,激活handler sendMessageDelayed(obtainMessage(0), delayMillis); ;定义SnakeView()函数初始化游戏,代码如下:public SnakeView(Context contex
8、t, AttributeSet attrs) SnakeView Constructor initSnakeView();/构造函数中,初始化游戏定义initSnakeView()初始化SnakeView类,代码如下:private void initSnakeView() Log.e(tag, initSnakeView /设置焦点,由于存在 文字界面 和 游戏界面的跳转。这个focus是不可或缺的。 setFocusable(true); /取得资源中的图片,加载到 砖块字典 中。 Resources r = this.getContext().getResources(); resetT
9、iles(4); loadTile(RED_STAR, r.getDrawable(R.drawable.redstar); loadTile(YELLOW_STAR, r.getDrawable(R.drawable.yellowstar); loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar);此外还定义了updateWalls()、updateApples()、updateSnake()绘制墙、果子、蛇,以及更新画面函数、保存位置坐标函数等,具体代码请详见目录。界面设计界面设计包括游戏主界面(Snake)、作者信息界面、游戏说明
10、界面、游戏界面的设计。界面的设计采用的是xml文档形式,在xml文档中规划好界面布局布局后,分别在java文件中声明。主界面设计:主界面包括一个 TextView、一个imagebutton、三个button,其main.xml文档如下:AbsoluteLayout xmlns:android= android:layout_width=fill_parentlayout_height=background=drawable/icon LinearLayout android:orientation=verticalgravity=center ImageButton +id/ImageBut
11、ton1/ImageButtonButton+id/Button1/Button+id/Button5+id/Button3/LinearLayout/AbsoluteLayout其对应的Snake.java文件如下:public class Snake extends Activity /* Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layou
12、t.main); initialstart();/初始化 public void initialstart() ImageButton imageButton = (ImageButton)this.findViewById(R.id.ImageButton1); Button button1 = (Button)this.findViewById(R.id.Button1); Button button3 = (Button)this.findViewById(R.id.Button3); Button button5 = (Button)this.findViewById(R.id.But
13、ton5); button1.setText(作者信息 button3.setText(退出 button5.setText(游戏说明 imageButton.setImageResource(R.drawable.icon1); /按钮注册到各自的监听器 imageButton.setOnClickListener(new View.OnClickListener() public void onClick(View view) Intent intent=new Intent(); intent.setClass( Snake.this,Game.class); startActivity
14、(intent);/跳转到Game界面 Snake.this.finish(); ); button1.setOnClickListener(new View.OnClickListener() /setContentView(R.layout.author); intent.setClass( Snake.this,AuthorView.class);/跳转到作者信息界面/注销跳转之前的界面 button3.setOnClickListener(new View.OnClickListener() /退出应用程序 button5.setOnClickListener(new View.OnC
15、lickListener() intent.setClass( Snake.this,Help.class);/跳转到游戏说明界面游戏界面设计:游戏界面包括一个 button,一个my.game.snake.SnakeView,其game.xml文档如下:FrameLayout xmlns:my.game.snake.SnakeView+id/snakedrawable/background/RelativeLayout android:+id/relativeLayout1 /RelativeLayout/FrameLayout相对应的Game.java文件如下:public class G
16、ame extends Activity /定义游戏参数 private SnakeView mSnakeView; private static String ICICLE_KEY = snake-view private int mMode = READY; public static final int PAUSE = 0; public static final int READY = 1; public static final int RUNNING = 2; public static final int LOSE = 3; setContentView(R.layout.gam
17、e); mSnakeView = (SnakeView) findViewById(R.id.snake); /设置界面背景 mSnakeView.setBackgroundResource(R.drawable.background); TextView tv = (TextView) findViewById(R.id.text); Button button4 = (Button)this.findViewById(R.id.button4); /根据TextView状态判断是否开始游戏 mSnakeView.setStatusTextView(tv); if (savedInstanc
18、eState = null) mSnakeView.setMode(SnakeView.READY); else Bundle bundle = savedInstanceState.getBundle(ICICLE_KEY); if (bundle != null) mSnakeView.restoreState(bundle); else mSnakeView.setMode(SnakeView.PAUSE); /按钮注册到监听器 button4.setOnClickListener(new View.OnClickListener() intent.setClass( Game.this,Snake.clas
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1