Android Shapes是一个简单的图形匹配的游戏!~~适合儿童!

该项目只有六个目标文件,效果如图:


游戏关键实现类:

public class GameView extends View{// Logging for this activityprivate String TAG;// Game modesprivate int mMode;private static final int STATE_LOADING = 0; // loading for first timeprivate static final int STATE_RUNNING = 1;private static final int STATE_PAUSED = 2;// private static final int STATE_WIN = 3;// private static final int STATE_LOSE = 4;private int mLevel;// Whether the main shape is getting moved by the userprivate boolean movingObject;// private Context mContext;private int mBoardWidth = 1;private int mBoardHeight = 1;// Each shape's width will be the screen width divided by this numberprivate final int WIDTH_RATIO = 7;// Coordinates of each shapeprivate Rect squareRect;private Rect circleRect;private Rect triangleRect;private Rect centreRect; // centre of game board - used as default position// for the moveable shapeprivate Rect currentShapeRect; // moveable shapeprivate Rect pauseButtonRect;private Paint squarePaint;private Paint circlePaint;private Paint trianglePaint;private Paint pausePaint;private Path trianglePath;// Which shape is the moveable shapeprivate int mCurrentShape;private static final int SQUARE = 0;private static final int CIRCLE = 1;private static final int TRIANGLE = 2;private static final int PAUSE = 3;private Handler mHandler;private Vibrator mVibrator;private int VIBRATE_DURATION = 100;private boolean willVibrate;public GameView(Context context, String loggingString, Handler handler,Vibrator vibrator, boolean returning){super(context);TAG = loggingString;mHandler = handler;mVibrator = vibrator;if (returning){mMode = STATE_RUNNING;} else{mMode = STATE_LOADING; // we're still loading the game up (not yet// running)}squarePaint = new Paint();squarePaint.setStyle(Paint.Style.FILL);squarePaint.setColor(Color.RED);circlePaint = new Paint();circlePaint.setStyle(Paint.Style.FILL);circlePaint.setColor(Color.BLUE);trianglePaint = new Paint();trianglePaint.setStyle(Paint.Style.FILL);trianglePaint.setColor(Color.YELLOW);pausePaint = new Paint();pausePaint.setColor(Color.WHITE);pausePaint.setStrokeWidth((float) 5);trianglePath = new Path();}public void startGame(){mLevel = 0;nextLevel();mMode = STATE_RUNNING;showBeginningAnimation();}// Displays a simple animation before the beginning of the gameprivate void showBeginningAnimation(){// TODO add in some animation showing multiple shapes (can block UI)invalidate();}// Move on to the next levelprivate void nextLevel(){mLevel++;mCurrentShape = Math.round((float) Math.random() * 2);movingObject = false;currentShapeRect = new Rect(centreRect);sendLevel(mLevel);}private void sendLevel(int level){Message msg = mHandler.obtainMessage();Bundle b = new Bundle();b.putInt("level", level);msg.setData(b);mHandler.sendMessage(msg);}// User has let go of the moveable shape on something other than any other// shape// so just move it back to the beginning locationprivate void wrongShape(){movingObject = false;currentShapeRect.set(centreRect);// TODO: add in some animation moving shape back to centre}protected void onDraw(Canvas canvas){drawShape(canvas, SQUARE, squareRect);drawShape(canvas, CIRCLE, circleRect);drawShape(canvas, TRIANGLE, triangleRect);drawShape(canvas, PAUSE, pauseButtonRect);if (mMode == STATE_RUNNING){Log.d(TAG, "current shape = " + mCurrentShape);drawShape(canvas, mCurrentShape, currentShapeRect);}}// Draws "shape" on the canvas, in position rectprivate void drawShape(Canvas canvas, int shape, Rect rect){switch (shape){case SQUARE:canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom,squarePaint);break;case CIRCLE:canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2,circlePaint);break;case TRIANGLE:trianglePath.reset();trianglePath.moveTo(rect.left, rect.bottom);trianglePath.lineTo(rect.right, rect.bottom);trianglePath.lineTo(rect.centerX(), rect.top);trianglePath.lineTo(rect.left, rect.bottom);trianglePath.close();canvas.drawPath(trianglePath, trianglePaint);break;case PAUSE:int heightCoefficient = (rect.bottom - rect.top) / 5;int widthCoefficient = (rect.right - rect.left) / 3;canvas.drawLine(rect.left + widthCoefficient, rect.top+ heightCoefficient, rect.left + widthCoefficient,rect.bottom - heightCoefficient, pausePaint);canvas.drawLine(rect.right - widthCoefficient, rect.top+ heightCoefficient, rect.right - widthCoefficient,rect.bottom - heightCoefficient, pausePaint);break;default:Log.d(TAG, "Shape not found (shape = " + mCurrentShape);}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh){super.onSizeChanged(w, h, oldw, oldh);mBoardWidth = w;mBoardHeight = h;//// Create all the shapes here so that onDraw is as slim as possible//int shapeWidth, shapeHeight;if (mBoardHeight >= mBoardWidth){shapeWidth = mBoardWidth / WIDTH_RATIO;shapeHeight = mBoardWidth / WIDTH_RATIO;} else{shapeWidth = mBoardHeight / WIDTH_RATIO;shapeHeight = mBoardHeight / WIDTH_RATIO;}// Position of the main shapes, each represented as a rectanglesquareRect = new Rect(0, 0, shapeWidth, shapeHeight);circleRect = new Rect(0, mBoardHeight - shapeHeight, shapeWidth,mBoardHeight);triangleRect = new Rect(mBoardWidth - shapeWidth, mBoardHeight- shapeHeight, mBoardWidth, mBoardHeight);// Position of the shape which is moveablecentreRect = new Rect(mBoardWidth / 2 - shapeWidth / 2, mBoardHeight/ 2 - shapeHeight / 2, mBoardWidth / 2 + shapeWidth / 2,mBoardHeight / 2 + shapeHeight / 2);currentShapeRect = new Rect(centreRect);// Position of pause "button"pauseButtonRect = new Rect(mBoardWidth - shapeWidth, 0, mBoardWidth,shapeHeight);if (mMode == STATE_LOADING){startGame(); // loading up for first time so start game// automatically} else{invalidate(); // re-draw screen with changed dimensions}}// User has pressed down on the screenprivate void touchDown(int x, int y){if (currentShapeRect.contains(x, y)){movingObject = true;touchMove(x, y);}}// User is dragging finger against screenprivate void touchMove(int x, int y){if (movingObject){currentShapeRect.offsetTo(x - currentShapeRect.width() / 2, y- currentShapeRect.height() / 2);}}// User has removed finger from screenprivate void touchUp(int x, int y){// if landed on pause button or clicked pause buttonif (mMode == STATE_RUNNING){if (pauseButtonRect.contains(x, y)){pause();}} else if (mMode == STATE_PAUSED){unPause();}// if we are moving the shape then check if we have landed on another// shapeif (movingObject){if (mCurrentShape == SQUARE){if (Rect.intersects(currentShapeRect, squareRect)){nextLevel();vibratePhone(VIBRATE_DURATION);return;}} else if (mCurrentShape == CIRCLE){if (Rect.intersects(currentShapeRect, circleRect)){nextLevel();vibratePhone(VIBRATE_DURATION);return;}} else if (mCurrentShape == TRIANGLE){if (Rect.intersects(currentShapeRect, triangleRect)){nextLevel();vibratePhone(VIBRATE_DURATION);return;}}wrongShape();}}@Overridepublic boolean onTouchEvent(MotionEvent event){int x = (int) event.getX();int y = (int) event.getY();switch (event.getAction()){case MotionEvent.ACTION_DOWN:touchDown(x, y);invalidate();break;case MotionEvent.ACTION_MOVE:touchMove(x, y);invalidate();break;case MotionEvent.ACTION_UP:touchUp(x, y);invalidate();break;}return true;}public void setWillVibrate(boolean willVibrate){this.willVibrate = willVibrate;}private void vibratePhone(int duration){if (willVibrate){mVibrator.vibrate(duration);}}public void pause(){if (mMode == STATE_RUNNING){mMode = STATE_PAUSED;movingObject = false;currentShapeRect = new Rect(centreRect);Message msg = mHandler.obtainMessage();Bundle b = new Bundle();b.putString("mode", "pause");msg.setData(b);mHandler.sendMessage(msg);}}public void unPause(){mMode = STATE_RUNNING;invalidate();}@Overridepublic void onWindowFocusChanged(boolean hasWindowFocus){// if (!hasWindowFocus) pause();}public Bundle saveState(Bundle map){if (map != null){map.putInt("mLevel", Integer.valueOf(mLevel));map.putInt("mCurrentShape", Integer.valueOf(mCurrentShape));}return map;}public void restoreState(Bundle savedState){mLevel = savedState.getInt("mLevel");mCurrentShape = savedState.getInt("mCurrentShape");movingObject = false;mMode = STATE_RUNNING;sendLevel(mLevel);invalidate();}}

代码简单易懂!~~

学习的目标是成熟!~~~


更多相关文章

  1. haproxy根据客户端浏览器进行跳转
  2. 转:Game Engines for Android(安卓)(Android游戏引擎)
  3. Android内容提供者源码
  4. 分享20个Android游戏源码,希望大家喜欢哈!
  5. Android(安卓)SDK与ADT不匹配的问题 This Android(安卓)SDK requ
  6. 【Android】自动提示匹配之AutoCompleteTextView
  7. Android(安卓)studio 基于BaseAdapter 的翻牌游戏
  8. Gradle Sync Failed,报错"could not find com.android.support:ap
  9. 近百个Android优秀开源项目,覆盖Android开发的每个领域

随机推荐

  1. MySQL设置global变量和session变量的两种
  2. Linux中 MySQL 授权远程连接的方法步骤
  3. 详解grep获取MySQL错误日志信息的方法
  4. 提升MongoDB性能的方法
  5. 详解如何在阿里云上安装mysql
  6. mysql生成指定位数的随机数及批量生成随
  7. mysql-5.7.21-winx64免安装版安装--Windo
  8. 详解mysql8.0创建用户授予权限报错解决方
  9. 最新mysql-5.7.21安装和配置方法
  10. MySQL5.7.21解压版安装详细教程图解