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

📄 gamemanage.java~1~

📁 java开发的手机游戏 泡泡堂 可以在手机上运行
💻 JAVA~1~
字号:
//-------------------------------------------
package game;

import javax.microedition.lcdui.*;
import java.util.*;
//import com.nokia.mid.ui.DeviceControl.*;
import com.nokia.mid.ui.*;
/*
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

//-----------------------
// 游戏管理类
//-----------------------
public class GameManage extends FullCanvas {


  /*---------------------
     // 成 员 属 性
   -------------------*/
  static GameManage instance;
  //------------------
  //状态消息
  public static int behaviorMsg = -1;
  final public static byte GAME_BEGIN = 0;
  final public static byte GAME_PLAYING = 1;
  final public static byte GAME_WIN	= 2;
  final public static byte GAME_LOST = 3;
  final public static byte GAME_END = 4;
  final public static byte PLAYER_BEGIN = 0;
  final public static byte PLAYER_DEAD = 1;
  final public static byte PLAYER_END = 2;
  public static byte  g_gameState = 0;
  public static boolean bIsRoleMsg = false;
  public static boolean bIsPaopMsg = false;  
  public static boolean bIsCptMsg = false;
  public static byte nPlayer1State = PLAYER_BEGIN;
  public static byte nPlayer2State = PLAYER_BEGIN; 
  public static byte nPlayer3State = PLAYER_BEGIN; 
  public static boolean bIsRescue = false;
  //------------------
  //成员定义
  GameCheck check;
  public Role role;
  public Role cpt;
  public Role goest;
  public Bomb bomb;
  public ComAI comAI;
  public ComAI comAI_goest;
  public Map map;
  GameHandle h;
  //------------------
  //记时器
  public Timer timCustomerPlayer;
  public Timer timComputerPlayer;
  public Timer timBomb;
  public static Graphics g ;
  public static Canvas canvas;
  //------------------
  // 按键状态
  public static boolean bIsUpKey = false; // 上
  public static boolean bIsLeftKey = false; // 左
  public static boolean bIsDownKey = false; // 下
  public static boolean bIsRightKey = false; // 右
  public static boolean bIsFightKey = false;//攻击
  public static boolean bIsOnkey = false;//是否按键
  //------------------
  //临时变量
  public static int  i = 0;
  public static boolean  bIsRoleDead = false;
  public static boolean  bIsCptDead = false;

  /*----------------------
     // 成 员 函 数
   ----------------------*/
  public GameManage() {
  
    instance = this;
    comAI_goest = new ComAI();
    comAI = new ComAI();
    role = new Role("/game/player1.png","/game/death.png");//人物一    
    cpt = new Role("/game/player0.png","/game/death.png");//人物二
    goest = new Role("/game/goest.png","/game/death.png");
    role.RoleType = Role.PLAYER;
    cpt.RoleType = Role.COMPUTTER;
    goest.RoleType = Role.COMPUTTER_2;
    goest.width = 23;
    goest.height = 31;
    goest.WI_TOTAL_FRAME = 4;
    goest.WI_TOTAL_MOVE_FRAME = 1;
    goest.velocity = 1;    
    goest.iTouchHeight = 8;
    goest.iTouchWidth = 23;
    comAI_goest.role = goest;
    comAI.role = cpt;
    
    //泡泡类,包含了泡泡膨胀图状态,泡泡爆炸状态,泡泡引起的死亡状态
    bomb = new Bomb("/game/swell.png","/game/blast.png","/game/death.png");
    role.bomb = bomb;    
    map = new Map("/game/tile.png","/game/box.png");

    comAI.role.x = map.mapPosit[3][3].ix;//初始位置3行3列
    comAI.role.y = map.mapPosit[3][3].iy;
		comAI_goest.role.x = map.mapPosit[3][6].ix;
		comAI_goest.role.y = map.mapPosit[3][6].iy;
    role.x = map.mapPosit[8][4].ix;//初始位置5行2列
    role.y = map.mapPosit[8][4].iy;
    check = new GameCheck();//role);
    h = new GameHandle();
    timCustomerPlayer = new Timer();
    timCustomerPlayer.schedule( new CustomerPlayerTask(),0,60);
    timComputerPlayer = new Timer();
    timComputerPlayer.schedule( new ComputerPlayerTask(),0,60);
    timBomb = new Timer();
    timBomb.schedule( new BombTask(),0,60);

  }

  protected void paint(Graphics g){

  		switch(g_gameState){
      		case GAME_BEGIN:{//游戏开始状态
        			map.paintAllFloor(g);
        			map.paintAllBuild(g);
        			role.paint(g);
        			cpt.paint(g);
        			goest.paint(g);
        			g_gameState++;
        			this.g=g;
      		}break;
      
      case GAME_PLAYING:{//游戏进行中状态
        	if(bIsRoleMsg){
          		h.RoleHandleProcess(role.nCurrentMsg,
          										role,comAI.role,comAI_goest.role,
          										map);          
		      }
    	    if(bIsCptMsg){  
      		  	comAI.getData(role,map,h);
          		h.RoleHandleProcess(comAI.role.nCurrentMsg,
          										comAI.role,role,comAI_goest.role,
          										map);
          		comAI_goest.getData(role,map,h);          										
          		h.RoleHandleProcess(comAI_goest.role.nCurrentMsg,
          										comAI_goest.role,role,comAI.role,
          										map);          
        	}
        	if(bIsPaopMsg){
          		h.PaopHandleProcess(role,comAI.role,comAI_goest.role,map);
        	}
        	checkGameFinale(role,comAI.role,comAI_goest.role,map);//检测结局
      }break;
    	
    	case GAME_LOST:{
    			g.drawString("You Lost",getWidth()/2-20,getHeight()/2-5,0);
					g_gameState = GAME_END;
    	}break;
    	
    	case GAME_WIN:{
    			g.drawString("You Win",getWidth()/2-20,getHeight()/2-5,0);
    			g_gameState = GAME_END;
    	}break;
    	
    	case GAME_END:{
    	}break;
    }



  }
  
  
  
	protected void checkGameFinale(Role r, Role c, Role g, Map map){
				
			if(r.nCurrentMsg == Role.MSG_END	||
					c.nCurrentMsg == Role.MSG_END){
					g_gameState	= GAME_LOST;
			}else if(g.nCurrentMsg == Role.MSG_END &&
			GameManage.bIsRescue){
					g_gameState	= GAME_WIN;
			}
						
			if(h.getPutPaopRow(r,map) == h.getPutPaopRow(g,map) &&
					h.getPutPaopCol(r,map) == h.getPutPaopCol(g,map) ){
					if(r.nCurrentMsg < Role.MSG_DEAD &&
					g.nCurrentMsg < Role.MSG_DEAD){
							r.nCurrentMsg =Role.MSG_DEAD;					
							GameManage.nPlayer1State = PLAYER_DEAD;
				}
			}
			if(h.getPutPaopRow(r,map) == h.getPutPaopRow(c,map) &&
					h.getPutPaopCol(r,map) == h.getPutPaopCol(c,map) ){
						GameManage.bIsRescue = true;
			}
			
			

	}
  

	protected void keyPressed(int keyCode){

		if (GameManage.nPlayer1State != PLAYER_BEGIN) return;
    
    switch(getGameAction(keyCode)){
      case Canvas.UP:{
        role.nCurrentMsg = Role.MSG_UP_MOVE;
        bIsRoleMsg = true;
        clearKey();
        bIsUpKey = true;
        repaint();
      }break;
      case Canvas.LEFT :{
        role.nCurrentMsg = Role.MSG_LEFT_MOVE;
        bIsRoleMsg = true;
        clearKey();
        bIsLeftKey = true;
        repaint();
      }break;
      case Canvas.DOWN:{        
       	role.nCurrentMsg = Role.MSG_DOWN_MOVE;
        bIsRoleMsg = true;
        clearKey();
        bIsDownKey = true;
        repaint();
      }break;
      case Canvas.RIGHT:{
        role.nCurrentMsg = Role.MSG_RIGHT_MOVE;
        bIsRoleMsg = true;
        clearKey();
        bIsRightKey = true;
        repaint();
      }break;
      case Canvas.FIRE :{
      	if(role.nCurrentMsg !=  Bomb.MSG_DOING){
      		role.nCurrentMsg = Bomb.MSG_DOING;
        	role.bomb.iCurrentMsg =role.bomb.MSG_DOING;
        	clearKey();
        	bIsFightKey = true;
        	repaint();
        }
      }break;
    }


  }


  protected void keyReleased(int keyCode) {

    switch(getGameAction(keyCode)){
      case Canvas.UP:
        bIsUpKey = false;
        break;
      case Canvas.LEFT :
        bIsLeftKey = false;
        break;
      case Canvas.DOWN:
        bIsDownKey = false;
        break;
      case Canvas.RIGHT:
        bIsRightKey = false;
        break;
      case Canvas.FIRE :
        bIsFightKey = false;
        break;
    }

  }

  /*----------------------------
     //清空按键标志
  ----------------------------*/
  protected void  clearKey(){
    bIsUpKey = false; // 上
    bIsLeftKey = false; // 左
    bIsDownKey = false; // 下
    bIsRightKey = false; // 右
    bIsFightKey = false;//攻击
  }


  public void  run(){
  }

  /*----------------------------
     //用户玩家记时器
  ----------------------------*/
  public class CustomerPlayerTask extends TimerTask{
    public void run(){

      while(!bIsRoleDead){
        try{
          Thread.currentThread().sleep(30);
        }catch(Exception e){
        }//end catch
        if(bIsUpKey ||bIsLeftKey ||bIsDownKey ||bIsRightKey ||bIsFightKey||
        (nPlayer1State == PLAYER_DEAD)){
          bIsRoleMsg = true;
          bIsOnkey = true;
          repaint();
        }else{
          bIsOnkey = false;
        }

      }//end while
    }//end fun

  }//end class
  
  


  //电脑玩家记时器
  public class ComputerPlayerTask extends TimerTask{
    public void run(){
    
    	while(!bIsCptDead){
        try{
     		  Thread.currentThread().sleep(30);
       	}catch(Exception e){
        }//end catch
        //comAI.getData();
        if(true){
          bIsCptMsg = true;
          repaint();
        }else{
        }

      }//end while
    }//end run
    
  }
  

  //泡泡记时器
  public class BombTask extends TimerTask{

    public void run(){
      while(true){

        if (role.bomb.nBlastNum >= 0){
          GameManage.bIsPaopMsg = true;//一个时间片之后又开始检测重绘
          //System.out.println("msg=:"+i++);
          repaint();
          try{
            Thread.currentThread().sleep(150);
          }catch(Exception e){
          }//end catch
        }//end if


      }//end wihle
    }//end run
  }// end class



}

⌨️ 快捷键说明

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