首页 > 其他 > 详细

pushbox(3)

时间:2014-05-16 04:25:21      阅读:563      评论:0      收藏:0      [点我收藏+]

控制worker的移动,推动箱子到目标位置。

Point.java(点,用以计算人当前的位置)

bubuko.com,布布扣
package edu.pushbox.util;

public class Point {
    public int row;
    public int column;
    public Point(int row, int column) {
        this.row = row;
        this.column = column;
    }
}
bubuko.com,布布扣

Position.java(1,通过传入的二维数组,计算出人的位置,2,通过对二维数组的便利,判断是否完成本关,即把箱子推到目标位置)

bubuko.com,布布扣
package edu.pushbox.util;

/*
 * 找到map中人的位置
 * 传递map和worker的代表数字workerKey
 */
public class Position {
    public static Point getPosition(int map[][], int workerKey) {
        int column = 0, row = 0;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (map[i][j] == workerKey) {
                    column = j;
                    row = i;
                    break;
                }
            }
        }
        return new Point(column, row);
    }

    /*
     * 判断way[][]和map[][]的对应关系,及目标区域是否都变成了箱子
     */
    public static boolean isSuccess(int map[][], int way[][], int keyCode) {
        boolean flag = true;
        /*
         */
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (way[i][j] == keyCode) {
                    if (map[i][j] != keyCode + 3) {
                        flag = false;
                        break;
                    }
                }
            }
        }
        return flag;
    }
}
bubuko.com,布布扣

GameView.java

bubuko.com,布布扣
package edu.pushbox;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import edu.phshbox.R;
import edu.pushbox.util.Point;
import edu.pushbox.util.Position;

public class GameView extends View {
    int width = 0;
    int oxOff = 15;
    int oyOff = 30;
    GameMain gameMain = null;

    public final int WALL = 1;//
    public final int GOAL = 2;// 目標區域
    public final int ROAD = 3;//
    public final int BOX = 4;// 箱子
    public final int BOX_AT_GOAL = 5;// 箱子的目標位置
    public final int WORKER = 6;//

    private Point workPoint;
    public static int picSize = 30;
    private int map[][] = null;// 路的位置
    private int way[][] = null;// 原始路的位置
    private int row = 0;
    private int col = 0;
    private static int grade = 0;
    private Bitmap picture[] = null;

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gameMain = (GameMain) context;
        WindowManager win = gameMain.getWindowManager();
        width = win.getDefaultDisplay().getWidth();
        init();
    }

    public void init() {
        initMap();
        initWay();
        workPoint = Position.getPosition(map, WORKER);// 找到人的坐标
        reset();
        initPicture();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Log.d("workPoint", workPoint.column + "," + workPoint.row);
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            moveDown();
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            moveUp();
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            moveLeft();
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            moveRight();
        }
        if (Position.isSuccess(map, way, GOAL)) {
            Toast.makeText(this.getContext(), "SUCCESS", Toast.LENGTH_LONG)
                    .show();
            grade++;
            init();
        }
        invalidate();
        return super.onKeyDown(keyCode, event);
    }
  /*
   *当人移动时,要判断人的将要移动到的位置,如果该位置是路/或是目标区域则移动。
   *如果该位置是箱子,或者是箱子的目标位置,由于人要推箱子,则判断箱子的下一个位置,如果是目标区域/路,则人推着箱子移动,
   *如果不是则该箱子不能被推动。
   */
private void moveDown() { /* * 推箱子的逻辑, * 路 * 目标区域 * 墙 * 箱子 * 箱子的目标位置 */ int wrow = workPoint.row, wcolumn = workPoint.column; if (map[wrow + 1][wcolumn] == BOX || map[wrow + 1][wcolumn] == BOX_AT_GOAL) {// 箱子或箱子的目標區域 if (map[wrow + 2][wcolumn] == GOAL || map[wrow + 2][wcolumn] == ROAD) { // map[wrow + 2][wcolumn] = map[wrow + 2][wcolumn] == GOAL ? BOX_AT_GOAL : BOX; map[wrow + 1][wcolumn] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.row++; } } else {// 路,目標區域 if (map[wrow + 1][wcolumn] == ROAD || map[wrow + 1][wcolumn] == GOAL) { map[wrow + 1][wcolumn] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.row++; } } } private void moveUp() { /* * 向上移動 */ int wrow = workPoint.row, wcolumn = workPoint.column; if (map[wrow - 1][wcolumn] == BOX || map[wrow - 1][wcolumn] == BOX_AT_GOAL) { // 箱子或箱子的目標區域 if (map[wrow - 2][wcolumn] == GOAL || map[wrow - 2][wcolumn] == ROAD) { map[wrow - 2][wcolumn] = map[wrow - 2][wcolumn] == GOAL ? BOX_AT_GOAL : BOX; map[wrow - 1][wcolumn] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.row--; } } else { if (map[wrow - 1][wcolumn] == GOAL || map[wrow - 1][wcolumn] == ROAD) { map[wrow - 1][wcolumn] = map[wrow - 1][wcolumn] == GOAL ? BOX_AT_GOAL : BOX; map[wrow - 1][wcolumn] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.row--; } } } private void moveRight() { /* * 向上移動 */ int wrow = workPoint.row, wcolumn = workPoint.column; if (map[wrow][wcolumn + 1] == BOX || map[wrow][wcolumn + 1] == BOX_AT_GOAL) { // 箱子或箱子的目標區域 if (map[wrow][wcolumn + 2] == GOAL || map[wrow][wcolumn + 2] == ROAD) { map[wrow][wcolumn + 2] = map[wrow][wcolumn + 2] == GOAL ? BOX_AT_GOAL : BOX; map[wrow][wcolumn + 1] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.column++; } } else { if (map[wrow][wcolumn + 1] == GOAL || map[wrow][wcolumn + 1] == ROAD) { map[wrow][wcolumn + 1] = map[wrow][wcolumn + 1] == GOAL ? BOX_AT_GOAL : BOX; map[wrow][wcolumn + 1] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.column++; } } } private void moveLeft() { /* * 向上移動 */ int wrow = workPoint.row, wcolumn = workPoint.column; if (map[wrow][wcolumn - 1] == BOX || map[wrow][wcolumn - 1] == BOX_AT_GOAL) { // 箱子或箱子的目標區域 if (map[wrow][wcolumn - 2] == GOAL || map[wrow][wcolumn - 2] == ROAD) { map[wrow][wcolumn - 2] = map[wrow][wcolumn - 2] == GOAL ? BOX_AT_GOAL : BOX; map[wrow][wcolumn - 1] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.column--; } } else { if (map[wrow][wcolumn - 1] == GOAL || map[wrow][wcolumn - 1] == ROAD) { map[wrow][wcolumn - 1] = map[wrow][wcolumn - 1] == GOAL ? BOX_AT_GOAL : BOX; map[wrow][wcolumn - 1] = WORKER; map[wrow][wcolumn] = roadOrGoal(); workPoint.column--; } } } private int roadOrGoal() { // 判斷row,column是目標false,或者是路true int result = ROAD; if (way[workPoint.row][workPoint.column] == GOAL) result = GOAL; return result; } /* * 重置pageSize col-row */ public void reset() { row = map.length; col = map[0].length; picSize = (int) Math.floor((width - oxOff - oyOff) / col); Log.d("width", width + ""); Log.d("pageSize", picSize + ""); } // 初始化路 private void initMap() { map = MapList.getMap(grade); // resetRowCol(); } // 初始化路 private void initWay() { way = MapList.getMap(grade); // resetRowCol(); } private void initPicture() { // resetPicSize(); picture = new Bitmap[7]; loadPicture(WALL, R.drawable.wall); loadPicture(GOAL, R.drawable.goal); loadPicture(ROAD, R.drawable.road); loadPicture(BOX, R.drawable.box); loadPicture(BOX_AT_GOAL, R.drawable.box_at_goal); loadPicture(WORKER, R.drawable.worker); } private void loadPicture(int key, int pictureId) { Drawable drawable = getResources().getDrawable(pictureId); Bitmap bitMap = Bitmap.createBitmap(picSize, picSize, Bitmap.Config.ARGB_8888); drawable.setBounds(0, 0, picSize, picSize); Canvas canvas = new Canvas(bitMap); drawable.draw(canvas); picture[key] = bitMap; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (map[i][j] > 0) canvas.drawBitmap(picture[map[i][j]], oxOff + picSize * j, oyOff + picSize * i, paint); } } super.onDraw(canvas); } }
bubuko.com,布布扣

bubuko.com,布布扣

pushbox(3),布布扣,bubuko.com

pushbox(3)

原文:http://www.cnblogs.com/jianfengyun/p/3729540.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!