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

📄 mycanvas.java

📁 参考了网上资源后优化改进的五子棋手机游戏
💻 JAVA
字号:
package game;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * @author Administrator
 * 实现类
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class MyCanvas extends GameCanvas implements Runnable, CommandListener {
  protected MyMIDlet mymidlet; //和mylet一样,对象变量mymidlet只在该文件中起作用
  private Graphics g;
  int empty; //游戏界面到屏幕边缘的留空
  int canvasW, canvasH; //画布的长和宽
  int chessLength; //棋子的直径
  int chessMapLength, chessMapGrid, chessGridLength; //棋盘的边长,棋盘一边格子数,每格宽度
  int chessMapX, chessMapY; //棋盘左上角x,y坐标
  int selectedX, selectedY; //选择框在棋盘格局上的x,y位置
  boolean isPlayer1; //是否是玩家1
  Chesses[][] chesses; //棋子数组
  boolean newGame; //是否是新的游戏
  boolean isplay; //是否游戏进行中
  private Command exitCommand;
  private Command startCommand;
  int player1win, player2win;
 // private Display display;

  public MyCanvas() {
  super(true);
    newGame = true;
    empty = 10;
    canvasW = getWidth() - empty;
    canvasH = getHeight() - empty;
    chessMapGrid = 15;
    chesses = new Chesses[chessMapGrid + 1][chessMapGrid + 1]; //因x个格子有x+1个交点
    if (canvasW > canvasH) { //根据手机屏幕设定边长为chessMapLength的正方形棋盘
      chessMapLength = canvasH - canvasH % chessMapGrid;
      chessMapX = (canvasW - chessMapLength) / 2 + empty / 2;
      chessMapY = (canvasH % chessMapGrid) / 2 + empty / 2;
    }
    else {
      chessMapLength = canvasW - canvasW % chessMapGrid;
      chessMapX = (canvasW % chessMapGrid) / 2 + empty / 2;
      chessMapY = (canvasH - chessMapLength) / 2 + empty / 2;
    }
    chessGridLength = chessMapLength / chessMapGrid;
    chessLength = chessGridLength - 1;
    selectedX = selectedY = chessMapGrid / 2;
    isPlayer1 = true; 
    exitCommand = new Command("退出", Command.EXIT, 1);
    startCommand = new Command("重新开始", Command.SCREEN, 1);
    addCommand(startCommand);
    addCommand(exitCommand);
    setCommandListener(this);
  }


  public void startup() {
  	isplay = true;
  	    Thread thread = new Thread(this);
  	    thread.start();
  	  }
  
     protected void paintMap(Graphics g) { //画棋盘,一格格画
  	   for (int i = 0; i < chessMapGrid; i++) {
  	      for (int j = 0; j < chessMapGrid; j++) {
  	        g.setColor(50, 128, 100); //浅绿的格子
  	        g.drawRect(chessMapX + j * chessGridLength, //画矩形框
  	                   chessMapY + i * chessGridLength,
  	                   chessGridLength, chessGridLength);
  	        g.setColor(0, 50, 0); //绿色填充格子
	        g.fillRect(chessMapX + 1 + j * chessGridLength, 
	                   chessMapY + 1 + i * chessGridLength,
	                   chessGridLength - 1, chessGridLength - 1);
  	        if (chesses[i][j] != null) {
  	          if (chesses[i][j].isPlayer1) {
  	   g.setColor(255, 255, 255); //Player1的棋子是白色的
  	          }
  	          else {
  	            g.setColor(255, 0, 0); //红色
  	          }
  	        g.fillArc(chessMapX + j * chessGridLength - chessLength / 2, //画棋子
  	        	  	chessMapY + i * chessGridLength - chessLength / 2,
  	        	  	                    chessLength, chessLength, 0, 360);
  	        }
  	      }
  	    }
  	  }

     protected void paintSelected(Graphics g) { //画选择框
	   g.setColor(0, 150, 255); //蓝色
	      g.drawRect(chessMapX + selectedX * chessGridLength - chessGridLength / 2,
	               chessMapY + selectedY * chessGridLength - chessGridLength / 2,
	               chessGridLength, chessGridLength);
	  } 
  
     protected void paintPlayer(Graphics g, boolean isPlayer1) { 
  		if (isPlayer1) {  //用白色大框表示轮到Player1
  		      g.setColor(255, 255, 255);
  		    }
  		    else {
  		      g.setColor(255, 0, 0);
  		    }
  		    g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
  		    g.drawRect(2, 2, getWidth() - 4, getHeight() - 4);
  		    g.drawRect(3, 3, getWidth() - 6, getHeight() - 6);
  		  }

  public void paint(Graphics g) {
  		    g.setColor(0x00000000);
  		    g.fillRect(0, 0, getWidth(), getHeight());
  		    paintPlayer(g, isPlayer1); 		    
  		    paintMap(g);
  		    paintSelected(g); //后画的在最上层
  		    flushGraphics();
  		  }

  		private void init() {
  			if (newGame) {
  			      chesses = new Chesses[chessMapGrid + 1][chessMapGrid + 1];
  			      isPlayer1 = true;
  			      selectedX = selectedY = chessMapGrid / 2;
  			    }
  			  }

  			  public void run() {
  			    g = getGraphics();
  			    while (isplay) {
  			      try {
  			        input(g);
  			        paint(g);
  			        Thread.sleep(50);
  			      }
  			      catch (Exception e) {
  			        e.printStackTrace();
  			      }
  			    }
  			  }

  	     public void input(Graphics g) {
  			  int keyStates = getKeyStates();
  			    if ( (keyStates & LEFT_PRESSED) != 0) {
  			      selectedX = (--selectedX + chessMapGrid + 1) % (chessMapGrid + 1);
  			    }  //之所以要取模是为了让选择框能循环地走
  			    else if ( (keyStates & RIGHT_PRESSED) != 0) {
  			      selectedX = (++selectedX) % (chessMapGrid + 1);
  			    }
  			    else if ( (keyStates & UP_PRESSED) != 0) {
  			      selectedY = (--selectedY + chessMapGrid + 1) % (chessMapGrid + 1);
  			    }
  			    else if ( (keyStates & DOWN_PRESSED) != 0) {
  			      selectedY = (++selectedY) % (chessMapGrid + 1);
  			    }
  			    
  			    else if ( (keyStates & FIRE_PRESSED) != 0) {
  			    if (chesses[selectedY][selectedX] == null) {
  			        chesses[selectedY][selectedX] = new Chesses(this.isPlayer1);
  			        if (checkWin()) {
  			          String winner;
  			          if (isPlayer1) {
  			            winner = "白方胜利";
  			            player1win++;
  			          }
  			          else {
  			            winner = "红方胜利";
  			            player2win++;
  			          }
  			          Alert winAlert = new Alert("",
  			winner + "\n白方  " + player1win + " : " +
  			                                     player2win + "  红方",
  			                                     null, AlertType.INFO);
  			          winAlert.setTimeout(Alert.FOREVER);
  			          Display.getDisplay(MyMIDlet.instance).setCurrent(winAlert); //显示最终结果
  			          init();
  			          flushGraphics();
  			          // repaint();
  			        }
  			        this.isPlayer1 = !this.isPlayer1; //切换下棋方
  			      }
  			    }
  			    flushGraphics();
  			    // repaint();
  			  }

    
   private boolean checkWin() {
    	int num = 1;
    	if (num < 5) { 
    	      num = 1;
    	      for (int i = 1; i <= 4; i++) {
    	        if (isPlayer1(selectedX - i, selectedY)) { //左边排成横线的
    	          num++; }
    	        else if(isPlayer1(selectedX + i, selectedY)) { //右边排成横线的
    	          num++; }
    	        else{ break;}
    	                                     }
    	             }
    	    if (num < 5) {
    	      num = 1;
    	      for (int i = 1; i <= 4; i++) {
    	        if (isPlayer1(selectedX, selectedY - i)) { //上方排成竖线的
    	          num++; }
    	        else if(isPlayer1(selectedX, selectedY + i)) { //下方排成竖线的
    	          num++; }
    	        else { break;}
    	                                     }
    	                  }
    	    if (num < 5) {
    	      num = 1;
    	      for (int i = 1; i <= 4; i++) {
    	        if (isPlayer1(selectedX - i, selectedY - i)) { //左斜上方排成直线
    	          num++;}
    	        else if (isPlayer1(selectedX + i, selectedY + i)) { //右斜下方排成直线
    	          num++;}
    	        else { break;}
    	                                     }
    	                  }
    	    if (num < 5) {
    	      num = 1;
    	      for (int i = 1; i <= 4; i++) {
    	        if (isPlayer1(selectedX + i, selectedY - i)) { //右斜上方排成直线
    	          num++; }
    	        else if(isPlayer1(selectedX - i, selectedY + i)) { //左斜下方排成直线
    	          num++; }
    	        else { break;}
    	                                     }
    	                 }
    	    if (num >= 5) { return true; }
    	    else { return false; }
    	  }

  private boolean isPlayer1(int y, int x) {
    if (x <= 15 && x >= 0 && y <= 15 && y >= 0 && chesses[x][y] != null) {
      if (chesses[x][y].isPlayer1 == this.isPlayer1) { return true; }
      else { return false; }
    }
    else { return false; }
  }

  public void commandAction(Command c, Displayable dpa) {
	if (c.equals(startCommand)) {
	      init();
	      startup();
	    }
	    else if (c.equals(exitCommand)) {
	      MyMIDlet.instance.notifyDestroyed(); //这里把整个程序都关掉了
	    //display.setCurrent(MyMIDlet.getForm());
	    }
	  }
	}

⌨️ 快捷键说明

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