⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 man.java

📁 J2me手机游戏捉鬼源代码
💻 JAVA
字号:
//Man.java
//角色类
//Download by http://www.codefans.net
import java.lang.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Man{
    //角色状态 0-停止 1-走路 2-中招
    int status=0;
    public static Image Img=null;
    public static Sprite man=null;
    private final int spW=25;
    private final int spH=30;
    public int curDir=1;//记录当前移动的方向
    private static int[] leftMoveFrame={0,1,2,1};
    private static int[] leftStopFrame={1};
    private static int[] rightMoveFrame={3,4,5,4};
    private static int[] rightStopFrame={4};
    private static int[] downMoveFrame={7,8};
    private static int[] downStopFrame={6};
    private static int[] upMoveFrame={10,11};
    private static int[] upStopFrame={9};
    public boolean startMove=false;//是否在开始移动状态
    public int curGhost=0;//当前的鬼
    public static int point=0;//得到的分数
    public static int MaxPoint=0;
    public int catchGhosts=0;//捉到的鬼的数目
    public static int w,h;
    
    static int[] actX={0,0,-1,1};//移动时坐标改变值
    static int[] actY={-1,1,0,0};
    public static int curx,cury;//当前角色所在实际坐标
    public static int curX,curY;//当前角色所在格子坐标
    public static int nextx,nexty;//角度的中心位置
    public static int nextX,nextY;
    public static int nextLx,nextRx,nextUy,nextDy;//角色的四个边界位置
    static int moveRate=4;
    static int baseRate=4;
    static int propRate=4;
    static int initRate=8;
    //=========================================
    public static boolean bInHouse=false;
    public static boolean bHidden=false;
    public static int showVTime=0;
    public static Image showVImg=null;
    public static int Vx=0,Vy=0;
    public static boolean sos=false;
    public static boolean showSos=false;
    //==========================================
    //和道具相关的变量
    public static boolean showGetProp=false;
    public static int showGetTime=0;
    public static Image showGetImg=null;
    public static boolean isStop=false;
    public static int stopTime=3000;//定身时间
    public int stoptime=0;
    public int p_stop=0;
    public static boolean isReduRate=false;
    public static int reduRateTime=3000;//减速时间
    public int reduratetime=0;
    public int p_reduRate=0;
    public static boolean isSight=false;
    public static int sightTime=3000;//限制视线时间
    public int sighttime=0;
    public int p_sight=0;
    public static boolean isCanViolate=true;//能否被怪物触发
    public static int violateTime=5000;
    public int violatetime=0;
    public int p_violate=0;
    public static boolean isAddRate=false;
    public int p_addRate=0;//旱冰鞋
    public static boolean isCandle=false;//蜡烛
    public int p_candle=0;
    ////////////////////////////////////////////////////////////
    public Man(){
        
    }
    public void gameInit(){
        if (Img==null){
            try {
                Img=Image.createImage("/res/man.png");
            }catch(Exception e){}
        }
        man=new Sprite(Img,spW,spH);
        point=0;
        MaxPoint=svPoint.getMaxPoint();
    }
    public void unGame(){
        man=null;
        Img=null;
    }
    public void gateInit(){
        w=TollGate.w;
        h=TollGate.h;
        curDir=1;
        setPositionXY(0,0);
        man.setPosition(0,TollGate.bgY-10);
        man.setVisible(true);
        curGhost=0;
        propRate=initRate;
        isAddRate=false;
        isCandle=false;
        stopMove();
        showSos=false;
        sos=false;
        showGetProp=false;
        //================================
        isStop=false;
        isReduRate=false;
        isSight=false;
        isCanViolate=true;//能否被怪物触发
        isAddRate=false;
        isCandle=false;//蜡烛     
    }
    public void gateEnd(){
        man.setVisible(false);
    }
    //==============================================================
    public void changeDir(int dir){
        curDir=dir;
        startMove(dir);
    }
    public void stopMove(){//停止走路时的帧序列
        //改变精灵的帧序列
        //dir 方向0-上 1-下 2-左 3-右
        startMove=false;
        switch(curDir){
            case 0:
                man.setFrameSequence(upStopFrame);
                break;
            case 1:
                man.setFrameSequence(downStopFrame);
                break;
            case 2:
                man.setFrameSequence(leftStopFrame);
                break;
            case 3:
                man.setFrameSequence(rightStopFrame);
                break;
        }
        man.setFrame(0);
        
    }
    public void startMove(int dir){//开始走路进设定帧序列
        startMove=true;
        switch(dir){
            case 0:
                man.setFrameSequence(upMoveFrame);
                break;
            case 1:
                man.setFrameSequence(downMoveFrame);
                break;
            case 2:
                man.setFrameSequence(leftMoveFrame);
                break;
            case 3:
                man.setFrameSequence(rightMoveFrame);
                break;
        }
        man.setFrame(0);
    }
    public void getNextXy()//得到下一步要走到的位置
    {
        nextx=curx+actX[curDir]*baseRate;
        nexty=cury+actY[curDir]*baseRate;
        nextLx=nextx-man.getWidth()/2;
        nextRx=nextx+man.getWidth()/2;
        nextUy=nexty-man.getHeight();
        nextDy=nexty;
        nextX=nextx/25;
        nextY=nexty/25;
        while(isOverPale() || isHitStone()){
            moveRate--;
            nextx=curx+actX[curDir]*moveRate;
            nexty=cury+actY[curDir]*moveRate;
            nextLx=nextx-man.getWidth()/2;
            nextRx=nextx+man.getWidth()/2;
            nextUy=nexty-man.getHeight();
            nextDy=nexty;
            nextX=nextx/25;
            nextY=nexty/25;
        }
    }
    public boolean isOverPale(){//是否超出地图界线
        boolean b=false;
        if (nextLx<0 || nextRx>w*25 || nextUy<-20 || nextDy>174){
            b=true;
        }
        return b;
    }
    public boolean isHitStone(){
        boolean b=false;
        if (nextX<0 || nextX>w-1 || nextY<0 || nextY>h-1){
            b=true;
        } else{
            int t=TollGate.gateMap[nextX][nextY];
            if (t==4){
                b=true;
            }
        }
        return b;
    }
    public void manMove(int dir){
        curDir=dir;
        moveRate=baseRate;
        getNextXy();
        curx=nextx;
        curX=nextX;
        cury=nexty;
        curY=nextY;
        if (curx<=176/2){
            TollGate.bgX=0;
            man.move(actX[dir]*moveRate,actY[dir]*moveRate);
        } else if(curx>w*25-176/2){
            TollGate.bgX=-w*25+175;
            man.move(actX[dir]*moveRate,actY[dir]*moveRate);
        } else{
            
            TollGate.bgX=176/2-curx;
            man.setPosition(176/2-man.getWidth()/2,TollGate.bgY+cury-man.getHeight());
        }
        man.nextFrame();
        if (TollGate.gateMap[curX][curY]==2 || TollGate.gateMap[curX][curY]==3){
            if (!bHidden){
                inputHouse();
                bHidden=true;
            }
        }else{
            if (bHidden){
                outputHouse();
                bHidden=false;
            }
            
        }
    }
    public void Move(int dir){//移动
        if(isStop){
            
        } else{
            if (!isReduRate){
                baseRate=propRate;
            }
            manMove(dir);
            if (Prop.propMap[curX][curY]!=-1){
                useProp(curX,curY);
                Prop.propMap[curX][curY]=-1;
            }
        }
    }
    
    public void unUseProp(){
        if (showGetProp){
            showGetTime+=GhostCanvas.rate;
            if (showGetTime>1500){
                showGetProp=false;
                showGetImg=null;
            }
        }
        if (isStop){
            stoptime-=GhostCanvas.rate;
            if (stoptime<0){
                isStop=false;
                p_stop=0;
            }
        }
        if (isReduRate){
            reduratetime-=GhostCanvas.rate;
            if (reduratetime<0){
                isReduRate=false;
                baseRate=propRate;
                p_reduRate=0;
            }
        }
        if (isSight){//
            sighttime-=GhostCanvas.rate;
            if (sighttime<0){
                isSight=false;
                p_sight=0;
            }
        }
        if (!isCanViolate){//
            violatetime-=GhostCanvas.rate;
            if (violatetime<0){
                isCanViolate=true;
                p_violate=0;
            }
        }
    }
    public void useProp(int x,int y){
        point+=100;
        showGetProp=true;
        showGetTime=0;
        if (showGetImg==null){
            try{
                showGetImg=Image.createImage("/res/v100.png");
            }catch(Exception e){
            }
        }
        int prop=Prop.propMap[x][y];
        switch(prop){
            case 0://藤蔓
                isStop=true;
                stoptime=stopTime;
                p_stop++;
                break;
            case 1://鼠夹
                isReduRate=true;
                reduratetime=reduRateTime;
                if (baseRate>2){
                    baseRate=baseRate/2;
                }
                p_reduRate++;
                break;
            case 2://鬼火
                isSight=true;
                sighttime=sightTime;
                p_sight++;
                break;
            case 3://面具
                isCanViolate=false;
                violatetime=violateTime;
                p_violate++;
                break;
            case 4://溜冰鞋
                isAddRate=true;
                propRate+=2;
                p_addRate++;
                break;
            case 5://蜡烛
                isCandle=true;
                GhostCanvas.gateTime+=5000;
                p_candle++;
                break;
        }
    }
    private void setPositionXY(int X,int Y){//设定到地图数组相应位置
        setPositionxy(X*25+man.getWidth()/2,Y*25+man.getHeight()-10);
    }
    private void setPositionxy(int x,int y)//设定到地图相应位置
    {
        curx=x;
        cury=y;
        curX=curx/25;
        curY=cury/25;
    }
    public void inputHouse(){
        man.setVisible(false);
        bInHouse=true;
        if (TollGate.gateMap[curX][curY]==2){
            if(Ghost.ghostMap[curX][curY]!=0){
                if (curGhost!=0){
                    if(curGhost==Ghost.ghostMap[curX][curY]){
                        catchGhosts++;
                        point+=500;
                        Ghost.ghostMap[curX][curY]=0;
                        curGhost=0;
                        sos=true;
                    } else{
                        int t=curGhost;
                        curGhost=Ghost.ghostMap[curX][curY];
                        Ghost.ghostMap[curX][curY]=t;
                    }
                }else{
                    if (Ghost.ghostMap[curX][curY]==0){
                        
                    } else{
                        curGhost=Ghost.ghostMap[curX][curY];
                        Ghost.ghostMap[curX][curY]=0;
                    }
                    
                }
                
            }
        }
        
    }
    public void outputHouse(){
        man.setVisible(true);
        bHidden=false;
        bInHouse=false;
        if (sos){
            showVTime=0;
            showSos=true;
            sos=false;
            if(showVImg==null){
                try{
                    showVImg=Image.createImage("/res/v500.png");
                }catch(Exception e){}
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -