📄 gamecanvas.java~19~
字号:
package game;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import java.util.Random;//Canvas是一个抽象类,在进行游戏编程时用到的一个关键类,提供了处理游戏动作、按键事件、指针事件的方法public class GameCanvas extends Canvas implements CommandListener{ private TicTacToeGame midlet; private Command exitCommand; private Command newCommand; private boolean playerIsCircle; private boolean computerIsCircle; private boolean computerIsFirst; private Image boardImage; private Image gameOverImage; private Graphics ig; private int boardCellWidth; private int boardTop; private int boardLeft; private int cursorPosition; private Game1 game; private int computerGamesWonTally=0; private int playerGamesWonTally=0; private int stalemateGameTally=0; private Random aRandom=new Random(); private int BLACK=0x00000000; private int WHITE=0x00FFFFFF; private int RED=0x00FF0000; private int BLUE=0x000000FF; public GameCanvas(TicTacToeGame _midlet,boolean _playerIsCircle,boolean _playerIsFirst) { midlet=_midlet; playerIsCircle=_playerIsCircle; computerIsCircle=!_playerIsCircle; computerIsFirst=!_playerIsFirst; game=new Game1(aRandom); initializeBoard();//初始化背景 exitCommand=new Command("EXIT",Command.SCREEN,1); newCommand=new Command("NEW",Command.SCREEN,1); this.addCommand(exitCommand); this.addCommand(newCommand); setCommandListener(this); initialize(); }public void paint(Graphics g)//paint是Canvas必须实现的方法 { if(game.isGameOver()) { paintGameOver(g); } else { paintGame(g); } }public void paintGame(Graphics g) { if(boardImage!=null) { g.setColor(WHITE); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.drawImage(boardImage,boardLeft,boardTop,Graphics.TOP|Graphics.LEFT); } } public void paintGameOver(Graphics g) { String statusMsg=null; if(game.isComputerWin()) { statusMsg="输了!8-("; computerGamesWonTally++; gameOverImage=loadImage("/lose.png"); } else if(game.isPlayerWin()) { statusMsg="赢了!"; playerGamesWonTally++; gameOverImage=loadImage("/win.png"); } else { statusMsg="平了!"; stalemateGameTally++; gameOverImage=loadImage("/sta.png"); } String tallyMsg="胜:"+ playerGamesWonTally+"平:"+stalemateGameTally+"负:"+computerGamesWonTally; Font aFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM); int strHeight=aFont.getHeight(); int statusMsgWidth=aFont.stringWidth(statusMsg); int tallyMsgWidth=aFont.stringWidth(tallyMsg); int strWidth=tallyMsgWidth; int imgWidth=gameOverImage.getWidth(); int imgHeight=gameOverImage.getHeight(); if(statusMsgWidth>tallyMsgWidth) { strWidth=statusMsgWidth; } int x=(this.getWidth()-strWidth)/2;//为了使字符串显示在屏幕中间 if(x<0) { x=0;//strWidth长,则把头来 } int y=this.getHeight()-imgHeight; if(y<0) { y=0; } int imgX=(this.getWidth()-imgWidth)/2;// if(imgX<0) { imgX=0; } int imgY=0;//????? g.setColor(WHITE); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.drawImage(gameOverImage,imgX,imgY,0); g.setColor(BLACK); g.drawString(statusMsg,x,y,(Graphics.TOP|Graphics.LEFT)); g.drawString(tallyMsg,x,(y+1+strHeight),(Graphics.TOP|Graphics.LEFT)); } public void commandAction(Command c,Displayable d) { if(c==exitCommand) { midlet.exitGame(); } else if(c==newCommand) { initialize(); } } //初始化游戏屏幕 public void initialize() { drawBoard();// game.initialize(); if(computerIsFirst) { int move=game.makeComputerMove(); drawPiece(computerIsCircle,move); } cursorPosition=0; drawPlayerCursor(BLACK); repaint(); } public void initializeBoard() { if(getWidth()>getHeight())//宽大于高 { boardCellWidth=(getHeight()-2)/3;//每个小方格按高来算 boardLeft=(getWidth()-boardCellWidth*3)/2; boardTop=1; } else { boardCellWidth=(getWidth()-2)/3; boardLeft=1; boardTop=(getHeight()-boardCellWidth*3)/2; } boardImage=Image.createImage(((boardCellWidth*3)+2),((boardCellWidth*3)+2)); ig=boardImage.getGraphics(); } //按键事件处理 public void keyPressed(int keyCode) { if(game.isGameOver()) { return; } int gameAction=getGameAction(keyCode);//改函数可以把按键事件映射到抽象键事件,再用catch进行分类 switch(gameAction) { //选择 case FIRE: { doPlayerMove();//当用户按确定键后调用此函数, break; } //向右移 case RIGHT: { doMoveCursor(1,0); break; } //向左移 case LEFT: { doMoveCursor(-1,0); break; } //向下移 case DOWN: { doMoveCursor(0,1); break; } //向上移 case UP: { doMoveCursor(0,-1); break; } default:break; } } //处理player的移动 public void doPlayerMove() { if(game.isFree(cursorPosition)) { game.makePlayerMove(cursorPosition); drawPiece(playerIsCircle,cursorPosition); if(!game.isGameOver()) { int move=game.makeComputerMove(); drawPiece(computerIsCircle,move); } repaint(); } } //处理光标移动 public void doMoveCursor(int dx,int dy) { int newCursorPosition=cursorPosition+dx+3*dy; if((newCursorPosition>=0)&&(newCursorPosition<9)) { drawPlayerCursor(WHITE);//把前一次光标所在的框还原, cursorPosition=newCursorPosition; drawPlayerCursor(BLACK);//把目前光标所在的框加黑, repaint(); } } //绘制图标 public void drawPiece(boolean isCircle,int pos) { int x=((pos%3)*boardCellWidth)+3; int y=((pos/3)*boardCellWidth)+3; if(isCircle) { drawCircle(x,y); } else { drawCross(x,y); }}//在指定位置画蓝色的圈public void drawCircle(int x,int y){ //画两个圆是为了体现圈的概念, ig.setColor(BLUE); ig.fillArc(x,y,boardCellWidth-4,boardCellWidth-4,0,360);//参数x、y是矩形的左顶点,后俩个参数是矩形的高、宽, ig.setColor(WHITE); ig.fillArc(x+4,y+4,boardCellWidth-4-8,boardCellWidth-4-8,0,360);}//在指定位置绘制红色的叉public void drawCross(int x,int y){ ig.setColor(RED); //ig.drawLine(x-boardCellWidth/2,y-boardCellWidth/2,x+boardCellWidth/2,y+boardCellWidth/2); for(int i=0;i<4;i++)//这个for循环为了改变叉线的宽度, { ig.drawLine(x+1+i,y,x+boardCellWidth-4-4+i,y+boardCellWidth-5);//画左线 ig.drawLine(x+1+i,y+boardCellWidth-5,x+boardCellWidth-4-4+i,y);//画右线 }}//绘制光标当前所在位置的方格public void drawPlayerCursor(int color){ ig.setColor(color); ig.drawRect(((cursorPosition%3)*boardCellWidth)+2,((cursorPosition/3)*boardCellWidth)+2,boardCellWidth-3,boardCellWidth-3);//boardCellWidth-3是因为drawRect画的矩形将覆盖width+1,height+1的区域,这样减3就合适了/}//绘制游戏面板public void drawBoard(){ ig.setColor(WHITE); ig.fillRect(0,0,boardImage.getWidth(),boardImage.getHeight()); ig.setColor(BLACK); for(int i=0;i<4;i++)//这个for循环将画出这个游戏界面的框架,即横竖4跳线 { ig.fillRect(0,boardCellWidth*i,(boardCellWidth*3)+2,2); ig.fillRect(boardCellWidth*i,0,2,boardCellWidth*3); }}//建立Iamge对象public Image loadImage(String imageFile){ Image anImage; try { anImage=Image.createImage(imageFile); } catch(Exception e) { anImage=null; } return anImage;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -