简单的手机游戏追踪跟随

三个java文件

//  testApp.java /

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

public class testApp extends MIDlet{

  Display display;
  MyCanvas myCanvas;

  public testApp(){
      display = Display.getDisplay(this);
      myCanvas = new MyCanvas();
  }

  protected void startApp() {
      display.setCurrent(myCanvas);
  }

  protected void pauseApp() {}

  protected void destroyApp(boolean unconditional) {}

}

///  MyCanvas.java   /

import java.util.Vector;
import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Graphics;

public class MyCanvas extends Canvas implements Runnable{
   
    private Thread thread;
   
    private static boolean Running;
   
    private long FRAME_INTERVAL = 100; //刷新时间
   
//    parabola myparabola;
   
    public MyCanvas(){
        this.setFullScreenMode(true);
        Running = true;
        myPlayer = new MyPlayer();
        myPlayer.setPlayerStartStation(playerStartStation[0],playerStartStation[1]);
        myPlayer.setPlayerArrivalStation(playerArrivalStation[0],playerArrivalStation[1]);
//        myparabola = new parabola(20,40,45,150,160,1);
        thread = new Thread(this);
        thread.start();
    }
   
    protected void hideNotify(){}
   
    protected void showNotify(){}
   
    protected void paint(Graphics g){
        drawWorld(g);
//        myparabola.paint(g);
    }
   
    protected void keyPressed(int keyCode){
        switch (keyCode){
            case -1://up
                myPlayer.setAimKeyMove(0,-1);
                break;
            case -2://down
                myPlayer.setAimKeyMove(0, 1);
                break;
            case -3://left
                myPlayer.setAimKeyMove(-1,0);
                break;
            case -4://right
                myPlayer.setAimKeyMove( 1,0);
                break;
        }
       
    }
   
    protected void keyReleased(int keyCode) {}
   
    public void run() {
       
        long systemTime;
       
        while (Running) {
           
            systemTime = System.currentTimeMillis();
            systemCounter++;
            if(systemCounter>10000000)systemCounter=0;
            if(systemCounter%5==0){
                myPlayer.playerMoveFrame();
                if(myPlayer.readyNewPlayStep){
                    myPlayer.setMapData(mapsData);
                    myPlayer.accountPlayMove();
                }
            }
//            myparabola.runWorld();
            repaint();
            serviceRepaints();
           
            systemTime = System.currentTimeMillis() - systemTime;
            try{
                if (systemTime < FRAME_INTERVAL)
                    Thread.sleep((long)(FRAME_INTERVAL - systemTime));
                else
                    Thread.sleep((long)5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
   
//*******************************************************************************************
//*******************************************************************************************
    private int mapCellDx = 16;
    private int mapCellDy = 16;
    private int mapBackgroundColor = 0xcccccc;
//    private int mapEmptyColor = 0xffffff;
    private int mapBarColor = 0x0;
    private int mapLineColor = 0x0000ff;
    private int playerStartStationColor = 0x00ff00;
    private int playerArrivalStationColor  = 0xff0000;
    private int playerMoveStationColor  = 0xff00ff;
   
    private int systemCounter = 0;
   
    private void drawWorld(Graphics g){
        int x,y;
        g.setColor(mapBackgroundColor);
        g.fillRect(0,0,mapXNumber*mapCellDx,mapYNumber*mapCellDy);
        g.setColor(mapBarColor);
        for(y=0;y<mapYNumber;y++){
            for(x=0;x<mapXNumber;x++){
                if(mapsData[y][x] == -1){
                    g.fillRect(x*mapCellDx,y*mapCellDy,mapCellDx,mapCellDy);
                }
            }
        }
       
        //start
        g.setColor(playerStartStationColor);
        g.fillRect(myPlayer.getPlayerStartX()*mapCellDx,myPlayer.getPlayerStartY()*mapCellDy,mapCellDx,mapCellDy);
       
        //arrival
        g.setColor(playerArrivalStationColor);
        g.fillRect(myPlayer.getPlayerArrivalX()*mapCellDx,myPlayer.getPlayerArrivalY()*mapCellDy,mapCellDx,mapCellDy);
       
        //player
        if(myPlayer.enablePlayerStepPath()){
            g.setColor(playerMoveStationColor);
            g.fillRect(myPlayer.getPlayerCurrentX()*mapCellDx,myPlayer.getPlayerCurrentY()*mapCellDy,mapCellDx,mapCellDy);
        }
       
        //draw line
        g.setColor(mapLineColor);
        for(y=0;y<mapYNumber;y++)
            g.drawLine(0,y*mapCellDy,mapXNumber*mapCellDx,y*mapCellDy);
        for(x=0;x<mapXNumber;x++)
            g.drawLine(x*mapCellDx,0,x*mapCellDx,mapYNumber*mapCellDy);
       
    }
   
//*******************************************************************************************
//*******************************************************************************************
    private int[][]  mapsData = {
        //0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//0
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},//2
        {-1, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1},//3
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},//4
        {-1, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1},//5
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},
        {-1, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1},
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},
        { 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1},
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//10
        {-1, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1},
        {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1},
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
    };
   
    private int mapXNumber = mapsData[0].length;
    private int mapYNumber = mapsData.length;
    private int[] playerStartStation ={1,1};
    private int[] playerArrivalStation={11,12};
   
    private MyPlayer myPlayer;
}

///  MyPlayer.java  /

import java.util.Vector;

public class MyPlayer {
    private int[][]  mapData;
    private int playerMoveFrame = 0;
    /** Creates a new instance of MyPlayer */
    public MyPlayer() {
        readyNewPlayStep = true;
    }
   
    private int[] playerStartStation = new int[2];
    private int[] playerArrivalStation= new int[2];
   
    private int mapXNumber;
    private int mapYNumber;
   
    private int playStep = 0;
    private Vector playrStepData;
    private short[] playrStepVectorData = new short[20]; //playStep max 50-1
    private int[][]  playrMoveStep;
   
    protected boolean readyNewPlayStep=false;
   
   
    protected void setMapData(int[][] worldMap){
        int x,y;
        mapData = null;
        mapXNumber = worldMap[0].length;
        mapYNumber = worldMap.length;
        mapData = new int[mapYNumber][mapXNumber];
        for(y=0;y<mapYNumber;y++){
            System.arraycopy(worldMap[y],0,mapData[y],0,mapXNumber);
        }
    }
   
    protected void setAimKeyMove(int newX,int newY){//Arrival change
        if(mapData[playerArrivalStation[1]+newY][playerArrivalStation[0]+newX] != -1){
            playerArrivalStation[0]+=newX;
            playerArrivalStation[1]+=newY;
            readyNewPlayStep = true;
        }
    }
   
    protected void playerMoveFrame(){//frame move
        if(enablePlayerStepPath()){
            playerMoveFrame++;
            if(playerMoveFrame>=playrMoveStep.length)playerMoveFrame=playrMoveStep.length-1;
        }
    }
   
    private void playerInitialization(){
        playerMoveFrame = 0;
        playStep = 0;
        playrMoveStep = null;
        playrStepData = null;
        playrStepVectorData = new short[20];
    }
   
    protected void accountPlayMove(){
        if(playrMoveStep == null){
           
        }else{
            //get new playMove
            //initialization
            setPlayerStartStation(playrMoveStep[playerMoveFrame][0],playrMoveStep[playerMoveFrame][1]);
            playerInitialization();
        }
       
        playMovePath(playerStartStation[0],playerStartStation[1],playerArrivalStation[0],playerArrivalStation[1]);
        getPlayMoveStep(playerArrivalStation[0],playerArrivalStation[1]);
        readyNewPlayStep = false;
    }
   
    //play search move path
    private void playMovePath(int startX,int startY,int aimX,int aimY){
        int i;
        //step initialize
        playrStepData = new Vector();
        int[] temp = {startX,startY,playStep};
        mapData[startY][startX] = playStep;
        playrStepData.addElement(temp);
        playrStepVectorData[0] = 0;
       
        while(true){
            //ps:finally no save
            playrStepVectorData = getNewArry(playrStepVectorData,playStep,(short)(playrStepData.size()));
           
            playStep++;
            int VectorLength = playrStepData.size();
            int VectorStart;
            if(playStep>1)
                VectorStart = playStep-1;
            else
                VectorStart = 0;
           
            for(i=VectorStart;i<VectorLength;i++){
                int[] getArry = (int[])playrStepData.elementAt(i);
                if(getArry[2] == playStep-1){
                    if(pointTestMove(getArry[0],getArry[1],aimX,aimY))
                        return;
                }
            }
        }
    }
   
    private boolean pointTestMove(int x,int y,int aimX,int aimY){
        int nowX = x;
        int nowY = y;
       
        //right
        if(enablePlayMove(nowX+1,nowY)){
            int[] temp = {nowX+1,nowY,playStep};
            mapData[nowY][nowX+1] = playStep;
            playrStepData.addElement(temp);
            if(nowX+1 == aimX && nowY == aimY){
                return true;
            }
        }
        //left
        if(enablePlayMove(nowX-1,nowY)){
            int[] temp = {nowX-1,nowY,playStep};
            mapData[nowY][nowX-1] = playStep;
            playrStepData.addElement(temp);
            if(nowX-1 == aimX && nowY == aimY){
                return true;
            }
        }
        //down
        if(enablePlayMove(nowX,nowY+1)){
            int[] temp = {nowX,nowY+1,playStep};
            mapData[nowY+1][nowX] = playStep;
            playrStepData.addElement(temp);
            if(nowX == aimX && nowY+1 == aimY){
                return true;
            }
        }
        //up
        if(enablePlayMove(nowX,nowY-1)){
            int[] temp = {nowX,nowY-1,playStep};
            mapData[nowY-1][nowX] = playStep;
            playrStepData.addElement(temp);
            if(nowX == aimX && nowY-1 == aimY){
                return true;
            }
        }
        return false;
    }
   
    private boolean enablePlayMove(int x,int y){
        if(x<0 || x>=mapXNumber)return false;
        if(y<0 || y>=mapYNumber)return false;
        if(mapData[y][x] == 0)
            return true;
        else
            return false;
    }
   
    //get step path
    private void getPlayMoveStep(int aimX,int aimY){
        int i;
        playrMoveStep = new int[playStep][2];
        int playStepTemp = playStep;
        //lastest
        playrMoveStep[playStepTemp-1][0] = aimX;
        playrMoveStep[playStepTemp-1][1] = aimY;
       
       
        while(playStepTemp>1){
            playStepTemp--;
            for(i=playrStepVectorData[playStepTemp-1];i<playrStepVectorData[playStepTemp];i++){
                int[] getArry = (int[])playrStepData.elementAt(i);
                if(getPreStep(getArry[0],getArry[1],
                        playrMoveStep[playStepTemp][0],playrMoveStep[playStepTemp][1],playStepTemp)){
                    continue;
                }
            }
        }
    }
   
    private boolean getPreStep(int x,int y,int aimX,int aimY,int step){
        if( x-1 == aimX && y == aimY ){
            playrMoveStep[step-1][0] = x;
            playrMoveStep[step-1][1] = y;
            return true;
        }
        if( x+1 == aimX && y == aimY ){
            playrMoveStep[step-1][0] = x;
            playrMoveStep[step-1][1] = y;
            return true;
        }
        if( x == aimX && y-1 == aimY ){
            playrMoveStep[step-1][0] = x;
            playrMoveStep[step-1][1] = y;
            return true;
        }
        if( x == aimX && y+1 == aimY ){
            playrMoveStep[step-1][0] = x;
            playrMoveStep[step-1][1] = y;
            return true;
        }
        return false;
    }
   
//**************************************************************
    protected void setPlayerStartStation(int startX,int startY){
        playerStartStation[0] = startX;
        playerStartStation[1] = startY;
    }
   
    protected void setPlayerArrivalStation(int endX,int endY){
        playerArrivalStation[0] = endX;
        playerArrivalStation[1] = endY;
    }
   
    protected int getPlayerStartX(){
        return playerStartStation[0];
    }
   
    protected int getPlayerStartY(){
        return playerStartStation[1];
    }
   
    protected int getPlayerArrivalX(){
        return playerArrivalStation[0];
    }
   
    protected int getPlayerArrivalY(){
        return playerArrivalStation[1];
    }
   
    protected int getPlayerCurrentX(){
        return playrMoveStep[playerMoveFrame][0];
    }
   
    protected int getPlayerCurrentY(){
        return playrMoveStep[playerMoveFrame][1];
    }
   
    protected boolean enablePlayerStepPath(){
        return playrMoveStep != null;
    }
   
//***************************************************************
   
    private static short[] getNewArry(short[] aim,int index,short add ){
        int aimLength = aim.length;
        if(index>=aimLength){
            int lengthAdd = 10;
            if(index-aimLength>lengthAdd)lengthAdd = index-aimLength;
            short[] newArry = new short[aimLength+lengthAdd];
            System.arraycopy(aim,0,newArry,0,aim.length);
            newArry[index] = add;
            return newArry;
        }else{
            aim[index] = add;
            return aim;
        }
    }
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值