編輯:關於Android編程
其實,拋開android平台,任何一個平台,做這樣一個游戲,都需要這些邏輯。針對android平台,我們看一下,SurfaceView的繪制框架。直接貼代碼:
package edu.njupt.zhb.game.view; /** * * @author Zheng Haibo * @webset: http://www.mobctrl.net * @android開發聯盟QQ群:272209595 */ public class PlaneView extends SurfaceView implements Callback, Runnable { private SurfaceHolder surfaceHolder; private long sleep_time = 16;//繪制周期 private int screenHeight; private int screenWidth; private Thread thread; private Canvas canvas; private Paint paint; private GameScreen currentScreen; private int level = 0; private int backgroundSpeed = 1; public PlaneView(Context context) { super(context); System.out.println("debug:PlaneView()"); surfaceHolder = this.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setFormat(PixelFormat.TRANSLUCENT); paint = new Paint(); paint.setAntiAlias(true); paint.setDither(true); } @Override public void surfaceCreated(SurfaceHolder holder) { System.out.println("debug:surfaceCreated"); setZOrderOnTop(false); isGameOver = false; if (isPause) { return; } screenHeight = this.getHeight(); screenWidth = this.getWidth(); initPlane(); thread = new Thread(this); thread.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { System.out.println("debug:surfaceChanged"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { System.out.println("debug:surfaceDestroyed"); if (lift > 0) { planeViewCallback.onGamePause(); } isPause = true; } @Override public void run() { while (!isGameOver) {//控制繪制周期 if (isPause) { try { Thread.sleep(sleep_time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } continue; } long starttime = System.currentTimeMillis(); drawScreen(); long time = System.currentTimeMillis() - starttime; if (time < sleep_time) { try { Thread.sleep(sleep_time - time); } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 繪制場景 */ private void drawScreen() { canvas = surfaceHolder.lockCanvas(); if (null == canvas) { return; } //清除 canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR); paint.setAlpha(255); gameLogic(); gameDraw(); if (null != canvas) { surfaceHolder.unlockCanvasAndPost(canvas); } } /** * 游戲邏輯 */ private void gameLogic() { //TO DO 控制游戲邏輯 ... } private void gameDraw() { //先繪制游戲背景 drawBackground(backgroundSpeed * frameSeq); if (currentScreen == GameScreen.NORMAL) { synchronized (planes) { drawPlanes(); drawBullets(); drawMasterPlane(); } } else if (currentScreen == GameScreen.BOSS) { drawBullets(); drawBossPlane(); drawBossBullets(); drawMasterPlane(); } } private void drawBossPlane() { if (null != bossPlane) { if (bossPlane.isClicked()) {// draw blast img bossPlane.onBlastDraw(canvas, paint); if (bossPlane.isBlastFrameEnd()) { bossPlane.setClicked(false); } } bossPlane.onDraw(canvas, paint); } } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); System.out.println("debug:onDraw"); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); System.out.println("debug:onDetachedFromWindow..."); isPause = false; isGameOver = true; //釋放資源 for (PlaneRes plane : planesRes) { plane.getBitmap().recycle(); } for (BulletRes bulletRes : bulletsRes) { bulletRes.getBitmap().recycle(); } } /** * 用戶交互 */ @Override public boolean onTouchEvent(MotionEvent e) { int x = (int) e.getX(); int y = (int) e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_DOWN: if (masterPlane.isContainPoint(x, y)) { isMove = true; } break; case MotionEvent.ACTION_MOVE: if (isMove) { synchronized (masterPlane) { masterPlane.updatePosition(x, y);//控制玩家飛機的移動 } } break; case MotionEvent.ACTION_UP: isMove = false; break; } return true; } }
private void drawBackground(int yOffset) { yOffset %= screenHeight; if (yOffset == 0) { canvas.drawBitmap(backgroundBmp, 0, 0, paint); } else { canvas.drawBitmap(backgroundBmp, new Rect(0, screenHeight - yOffset, screenWidth, screenHeight), new Rect(0, 0, screenWidth, yOffset + 1), paint); canvas.drawBitmap(backgroundBmp, new Rect(0, 0, screenWidth, screenHeight - yOffset), new Rect(0, yOffset, screenWidth, screenHeight), paint); } }
如:
planeView = new PlaneView(this); planeView.setPlaneViewCallback(this); planeView.setGameOverCallback(this); planeView.isMediaOpen = this.isMediaOpen; LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); rl_plane.addView(planeView, lp);
時候我們需要監聽ScroView的滑動情況,比如滑動了多少距離,是否滑到布局的頂部或者底部。可惜的是SDK並沒有相應的方法,不過倒是提供了一個 復制代碼 代碼如下:pro
andriod first app-computer 個人信息:就讀於燕大本科軟件工程專業 目前大三; 本人博客:google搜索“cqs_2012”即可; 個人愛
Android Widget開發案例實現是本文要介紹的內容,主要是來了解並學習Android Widget開發應用,今天我們要寫一下Android Widget的開發,由
如果是列表(單列多行形式)的使用ListView,如果是多行多列網狀形式的優先使用GridView。<?xml version=1.0 encoding=u
錯誤描述 今天在Android Studio項目中加入了jackson的