LinezCanvas.java
上传用户:jinxueyang
上传日期:2016-05-15
资源大小:104k
文件大小:20k
- package game;
- import java.util.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.media.Manager;
- public class LinezCanvas extends Canvas
- {
- //Display对象
- Display display;
- //屏幕的宽度与高度
- private int scrWidth = 0;
- private int scrHeight = 0;
- //声明次画面
- private Graphics offG = null;
- private Image image = null;
- private int grid[][] = new int[9][9];
- private int position[][] = new int[81][2];
-
- //格子与顶部、左端的距离
- private int spaceTop = 0;
- private int spaceLeft = 0;
-
- //格子的宽度与高度
- private int gridWidth = 0;
- private int gridHeight = 0;
- //格子的x、y坐标
- private int gridX = 0;
- private int gridY = 0;
- //已经选择标记
- private boolean clickFlag = false;
- //选择框初始位置
- private int initRow = 4;
- private int initCol = 4;
- //球被选择的位置
- private int choseRow = 0;
- private int choseCol = 0;
-
- //声明图片对象
- private Image img[] = new Image[3+(ColorLinezMIDlet.gameGrade-1)*2];
- //声明随机数对象
- private Random random = null;
- //球的下标
- private int chSphere = 0;
-
- //球的颜色数
- private int colorTotal = 0;
-
- //消失球数
- private int delNum = 5;
- //球的总数
- private int sphereTotal = 0;
- //新球位置
- private int newPos = 0;
-
- //存储球位置一维下标
- private int posRow = 0;
-
- //游戏状态 0代表进行中 1游戏暂停 2游戏结束
- private int gameFlag = 0;
-
- //分数
- private int score = 0;
-
- //时间分钟数
- private int minute = 0;
- //秒数
- private int second = 0;
-
- //定时器
- private Timer timer = null;
-
- //球消失时闪烁的次数
- private int disCount = 0;
-
- //球是否正在消失
- private boolean deleting = false;
-
- //球色相同所在的行数
- private int rows[] = new int[6];
- //球色相同所在的列数
- private int cols[] = new int[6];
- //球色相同所在的行数
- private int same[] = new int[6];
-
- //球消失的方向
- private int delDir[] = { 0, 1, 2, 2, 3, 3 };
- private boolean isUper[] = new boolean[6];
-
- //出现球的球数
- private int count = 0;
- private int total = 0;
-
- //暂停选项索引
- private int pauseIndex = 0;
- //暂停图片
- private Image pauseImage = null;
- //游戏结束选项索引
- private int overIndex = 0;
- //游戏结束图片
- private Image overImage = null;
-
- /**
- * 构造方法
- * */
- public LinezCanvas(Display display)
- {
- //获得Display对象
- this.display = display;
- //设屏全屏
- this.setFullScreenMode(true);
- //得到屏幕的宽度与高度
- scrWidth = this.getWidth();
- scrHeight = this.getHeight();
- //计算格子的宽度与高度
- gridWidth = ( scrWidth - 6 ) / grid[0].length;
- gridHeight = gridWidth;
-
- //设置格子与顶部、左端的距离
- spaceTop = (scrHeight - gridHeight*9)/2 + 6;
- spaceLeft = (scrWidth - gridWidth*9)/2;
- //获得次画笔对象
- image = Image.createImage(scrWidth, scrHeight);
- offG = image.getGraphics();
- //创建Random对象
- random = new Random();
- //游戏初始化
- gameInit();
-
- //导入图片
- for ( int i = 0; i < colorTotal; i++ )
- {
- try
- {
- img[i] = Image.createImage("/res/0"+i+".png");
- }
- catch(Exception e)
- {}
- }
-
- //创建Timer对象
- timer = new Timer();
- //启动时间线程
- timer.scheduleAtFixedRate(new TimeTimerTask(),100,1000);
- }
-
- //游戏初始化
- public void gameInit()
- {
- //游戏进行中
- gameFlag = 0;
- //球的颜色数
- colorTotal = 3+(ColorLinezMIDlet.gameGrade-1)*2;
- //球的总数
- sphereTotal = 0;
- //选择框初始位置
- initRow = 4;
- initCol = 4;
- //分数
- score = 0;
- //时间分钟数
- minute = 0;
- //秒数
- second = 0;
- for ( int i = 0, m = 0; i < grid.length; i++ )
- {
- for ( int j = 0; j < grid[i].length; j++, m++ )
- {
- grid[i][j] = 0;
- //新出现球的位置
- position[m][0] = i;
- position[m][1] = j;
- }
- }
- //游戏开始新出现5个小球
- createSphere(5);
- }
-
- protected void paint(Graphics g)
- {
- //清屏
- offG.setColor(0xFFFFFF);
- offG.fillRect(0, 0, scrWidth, scrHeight);
- offG.setColor(0x000000);
- //0代表游戏进行中 1游戏暂停 2游戏结束
- switch ( gameFlag )
- {
- case 0:
- drawGrid(offG);
- break;
- case 1:
- pauseGame(offG);
- break;
- case 2:
- gameOver(offG);
- break;
- }
- g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
- }
-
- /**
- * 启动线程
- * */
- public void start()
- {
- if ( timer == null )
- {
- timer = new Timer();
- timer.schedule(new TimeTimerTask(),100,1000);
- }
- }
- /**
- * 停止线程
- * */
- public void stop()
- {
- if(timer != null)
- {
- timer.cancel();
- timer = null;
- }
- }
-
- /**
- * 退出游戏
- * */
- public void quitGame()
- {
- stop();
- for ( int i = 0; i < colorTotal; i++ )
- {
- img[i] = null;
- }
- if ( pauseImage != null )
- {
- pauseImage = null;
- }
- if ( overImage != null )
- {
- overImage = null;
- }
- }
-
- protected void hideNotify()
- {
- stop();
- }
-
- protected void showNotify()
- {
- start();
- }
- protected void keyPressed(int keyCode)
- {
- //球在消失中立即返回
- if ( deleting )
- {
- return;
- }
- //游戏进行中,进行上下左右移动选择
- if ( gameFlag == 0 )
- {
- int action = this.getGameAction(keyCode);
- switch (action)
- {
- case Canvas.UP:
- moveUp();
- break;
- case Canvas.DOWN:
- moveDown();
- break;
- case Canvas.LEFT:
- moveLeft();
- break;
- case Canvas.RIGHT:
- moveRight();
- break;
- case Canvas.FIRE:
- commitPosition();
- break;
- default:
- if ( keyCode == -7 )
- {
- gameFlag = 1;
- }
- break;
- }
- }
- //游戏暂停
- else if ( gameFlag == 1 )
- {
- switch ( keyCode )
- {
- case -1:
- pauseIndex--;
- if ( pauseIndex < 0 )
- {
- pauseIndex = 3;
- }
- break;
- case -2:
- pauseIndex++;
- if ( pauseIndex > 3 )
- {
- pauseIndex = 0;
- }
- break;
- case -5:
- switch ( pauseIndex )
- {
- case 0:
- gameFlag = 0;
- start();
- break;
- case 1:
- gameInit();
- start();
- break;
- case 2:
- quitGame();
- display.setCurrent(new MainMenuCanvas(display));
- break;
- case 3:
- quitGame();
- ColorLinezMIDlet.quitApp();
- break;
- }
- }
- }
- //游戏结束
- else if ( gameFlag == 2 )
- {
- switch ( keyCode )
- {
- case -1:
- overIndex--;
- if ( overIndex < 0 )
- {
- overIndex = 2;
- }
- break;
- case -2:
- overIndex++;
- if ( overIndex > 2 )
- {
- overIndex = 0;
- }
- break;
- case -5:
- switch ( overIndex )
- {
- case 0:
- gameInit();
- start();
- break;
- case 1:
- quitGame();
- display.setCurrent(new MainMenuCanvas(display));
- break;
- case 2:
- quitGame();
- ColorLinezMIDlet.quitApp();
- break;
- }
- }
- }
-
- if ( ColorLinezMIDlet.isPlaySound )
- {
- playSound();
- }
- repaint();
- }
-
- //向上移
- private void moveUp()
- {
- if ( initRow > 0 )
- {
- if ( clickFlag )
- {
- if ( (grid[initRow-1][initCol] == 0) | ((choseRow == initRow - 1) & choseCol == initCol ) )
- {
- initRow -= 1;
- }
- }
- else
- {
- initRow -= 1;
- }
- }
- }
-
- //向下移
- private void moveDown()
- {
- if ( initRow < 8 )
- {
- if ( clickFlag )
- {
- if ( (grid[initRow+1][initCol] == 0) | ((choseRow == initRow + 1) & choseCol == initCol ) )
- {
- initRow += 1;
- }
- }
- else
- {
- initRow += 1;
- }
- }
- }
- //向左移
- private void moveLeft()
- {
- if ( initCol > 0 )
- {
- if ( clickFlag )
- {
- if ( grid[initRow][initCol-1] == 0 | ((choseCol == initCol - 1) & choseRow == initRow ) )
- {
- initCol -= 1;
- }
- }
- else
- {
- initCol -= 1;
- }
- }
- }
-
- //向右移
- private void moveRight()
- {
- if ( initCol < 8 )
- {
- if ( clickFlag )
- {
- if ( grid[initRow][initCol+1] == 0 | ((choseCol == initCol + 1) & choseRow == initRow ))
- {
- initCol += 1;
- }
- }
- else
- {
- initCol += 1;
- }
- }
- }
-
- //确定移动球
- private void commitPosition()
- {
- //选择要移动的球
- if ( grid[initRow][initCol] > 0 )
- {
- //球是否已经被选择
- if ( !clickFlag )
- {
- //格子的位置
- choseRow = initRow;
- choseCol = initCol;
- //球的位置
- chSphere = grid[initRow][initCol];
- clickFlag = true;
- }
- else
- {
- choseRow = -1;
- choseCol = -1;
- clickFlag = false;
- }
- }
- //选择要移动到的位置
- else
- {
- if ( clickFlag )
- {
- //球的下标
- grid[initRow][initCol] = chSphere;
- grid[choseRow][choseCol] = 0;
- for ( int i = 0; i < position.length; i++ )
- {
- if ( position[i][0] == initRow && position[i][1] == initCol )
- {
- position[i][0] = choseRow;
- position[i][1] = choseCol;
- break;
- }
- }
- clickFlag = false;
- //删除球
- deleteSphere();
- //产生新球
- createSphere(3);
- }
- }
- }
-
-
- private void pauseGame(Graphics g)
- {
- stop();
- try
- {
- if ( pauseImage == null )
- {
- pauseImage = Image.createImage("/res/pausegame.png");
- }
- }
- catch(Exception e)
- {}
- g.drawImage(pauseImage, 0, 0, Graphics.TOP | Graphics.LEFT);
- g.drawRect(46, 72 + pauseIndex * 25, 80, 20);
- }
-
- private void gameOver(Graphics g)
- {
- stop();
- try
- {
- if ( overImage == null )
- {
- overImage = Image.createImage("/res/gameover.png");
- }
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- g.drawImage(overImage, 0, 0, Graphics.TOP | Graphics.LEFT);
- HighScoreRecordStore.saveHighScore(((minute>9)?"":"0") + minute + ":" + ((second>9)?"":"0") + second, score);
- g.drawRect(46, 75 + overIndex * 25, 80, 20);
- }
-
- private void drawGrid(Graphics g)
- {
- //显示分数
- displayScore(g);
- //显示时间
- displayTime(g);
- count = 0;
- //画格子和球
- for ( int i = 0; i < grid.length; i++ )
- {
- for ( int j = 0; j < grid[i].length; j++ )
- {
- //格子的坐标
- gridX = j*gridWidth+spaceLeft;
- gridY = i*gridHeight+spaceTop;
- g.drawRect(gridX, gridY, gridWidth, gridHeight);
- //画已经选择框
- if ( j == choseCol && i == choseRow && clickFlag )
- {
- g.setColor(0xFF0000);
- g.drawRect(gridX + 2, gridY + 2, gridWidth-4, gridHeight - 4);
- g.setColor(0x000000);
- }
-
- //画选择框
- if ( j == initCol && i == initRow )
- {
- g.setColor(0x0055bb);
- g.drawRect(gridX + 1, gridY + 1, gridWidth-2, gridHeight - 2);
- g.setColor(0x000000);
- }
- //画球
- if ( grid[i][j] > 0 )
- {
- g.drawImage(img[grid[i][j] - 1], gridX + (gridWidth - 8) /2 , gridY + (gridHeight - 8) /2, Graphics.TOP | Graphics.LEFT);
- count++;
- }
- }
- }
- if ( !deleting )
- {
- total = count;
- }
- g.drawString("球数:" + String.valueOf(total), spaceLeft + 62, spaceTop - 15,Graphics.TOP | Graphics.LEFT);
- }
- /**
- * 显示分数
- * */
- private void displayScore(Graphics g)
- {
- g.drawString("分数:" + score, spaceLeft, spaceTop - 15, Graphics.TOP | Graphics.LEFT);
- }
-
- /**
- * 显示时间
- * */
- private void displayTime(Graphics g)
- {
- g.drawString("时间:" + ((minute>9)?"":"0") + minute + ":" + ((second>9)?"":"0") + second, scrWidth - spaceLeft - 53, spaceTop - 15, Graphics.TOP | Graphics.LEFT);
- }
-
- /**
- * 产生新球
- * */
- private void createSphere(int mach)
- {
- if ( deleting )
- {
- return;
- }
- //产生新球
- for ( int i = 0; i < mach; i++ )
- {
- if ( sphereTotal > 80 )
- {
- gameFlag = 2;
- break;
- }
- if ( sphereTotal < 81 )
- {
- if ( (81 - sphereTotal)/2 > 0 )
- {
- newPos = random.nextInt()%((81 - sphereTotal)/2) + (81 - sphereTotal)/2+1;
- }
- else
- {
- newPos = random.nextInt()%((81 - sphereTotal)/2+1) + (81 - sphereTotal)/2;
- if ( newPos == 0 )
- {
- newPos = 1;
- }
- }
- }
- posRow = 80-sphereTotal;
- grid[position[newPos-1][0]][position[newPos-1][1]] = random.nextInt()%ColorLinezMIDlet.gameGrade + ColorLinezMIDlet.gameGrade;
- position[newPos-1][0] = position[posRow][0];
- position[newPos-1][1] = position[posRow][1];
- position[posRow][0] = 0;
- position[posRow][1] = 0;
- sphereTotal++;
- if ( sphereTotal > 80 )
- {
- gameFlag = 2;
- break;
- }
- }
- }
-
- /**
- * 删除球
- * */
- public void deleteSphere()
- {
- for ( int i = 0; i < grid.length; i++ )
- {
- for ( int n = 0; n < 6; n++ )
- {
- same[n] = 1;
- rows[n] = -1;
- cols[n] = -1;
- isUper[n] = false;
- }
-
- for ( int j = 0; j < grid[i].length; j ++ )
- {
- //横行上出现大于或等于5个颜色相同并且连续的小球
- if ( (grid[i][j] > 0) && (j < 8) )
- {
- if ( (grid[i][j] == grid[i][j+1]) & !isUper[0] )
- {
- same[0]++;
- rows[0] = i;
- cols[0] = j+1;
- }
- else if ( same[0] < delNum )
- {
- same[0] = 1;
- }
- else if ( same[0] > delNum - 1 )
- {
- isUper[0] = true;
- }
- }
- //竖列出现大于或等于5个颜色相同并且连续的小球
- if ( (grid[j][i] > 0) && (j < 8) )
- {
- if ( (grid[j][i] == grid[j+1][i]) & !isUper[1] )
- {
- same[1]++;
- rows[1] = j+1;
- cols[1] = i;
- }
- else if ( same[1] < delNum )
- {
- same[1] = 1;
- }
- else if ( same[1] > delNum - 1 )
- {
- isUper[1] = true;
- }
- }
- }
-
- for ( int j = 0, n = i; j < i; j++, n-- )
- {
- //左斜1
- if ( grid[n][j] > 0 & (grid[n][j] == grid[n-1][j+1]) & !isUper[2] )
- {
- same[2]++;
- cols[2] = j + 1;
- rows[2] = n - 1;
- }
- else if ( same[2] < delNum )
- {
- same[2] = 1;
- }
- else if ( same[2] > delNum - 1 )
- {
- isUper[2] = true;
- }
-
- //左斜2
- if ( (grid[8-j][8-n] > 0) & (grid[8-j][8-n] == grid[8-j-1][8-n+1]) & !isUper[3] )
- {
- same[3]++;
- cols[3] = 8 - n + 1;
- rows[3] = 8 - j - 1;
- }
- else if ( same[3] < delNum )
- {
- same[3] = 1;
- }
- else if ( same[3] > delNum - 1 )
- {
- isUper[3] = true;
- }
-
- //右斜1
- if ( (grid[j][8-n] > 0) & (grid[j][8-n] == grid[j+1][8-n+1]) & !isUper[4] )
- {
- same[4]++;
- cols[4] = 8 - n + 1;
- rows[4] = j + 1;
- }
- else if ( same[4] < delNum )
- {
- same[4] = 1;
- }
- else if ( same[4] > delNum - 1 )
- {
- isUper[4] = true;
- }
-
- //右斜2
- if ( (grid[8-n][j] > 0) & (grid[8-n][j] == grid[8-n+1][j+1]) & !isUper[5] )
- {
- same[5]++;
- cols[5] = j + 1;
- rows[5] = 8 - n + 1;
- }
- else if ( same[5] < delNum )
- {
- same[5] = 1;
- }
- else if ( same[5] > delNum - 1 )
- {
- isUper[5] = true;
- }
- }
-
- for ( int n = 0; n < 6; n++ )
- {
- //启动球消失的线程
- if ( same[n] > (delNum - 1) && !deleting )
- {
- DeleteTimerTask delTask = new DeleteTimerTask( rows[n], cols[n], same[n], delDir[n]);
- timer.schedule( delTask, 100, 500);
- deleting = true;
- score += same[n];
- break;
- }
- }
- }
- }
-
- /**
- * 播放声音
- */
- public void playSound()
- {
- try
- {
- Manager.playTone(80, 200, 60);
- }catch(Exception e){}
- }
-
- public class TimeTimerTask extends TimerTask
- {
- public void run()
- {
- second++;
- //如果秒数大于59分钟数加1
- if( second > 59 )
- {
- minute++;
- second = 0;
- }
- displayTime(offG);
- //重新画时间
- repaint(scrWidth - spaceLeft - 53, spaceTop - 15, 66, 12);
- System.gc();
- }
- }
-
- public class DeleteTimerTask extends TimerTask
- {
- private int delCol = 0;
- private int delRow = 0;
- private int delTotal = 0;
- private int oper = 0;
- private int color = 0;
-
- private int tempCol = 0;
- private int tempRow = 0;
- private int tempCount = 0;
- private int tempColor = 0;
- public DeleteTimerTask( int row, int col, int total, int oper)
- {
- this.delCol = col;
- this.delRow = row;
- this.delTotal = total;
- this.oper = oper;
- this.color = grid[row][col];
- }
-
- public void run()
- {
- tempCol = delCol;
- tempRow = delRow;
- tempCount = delTotal;
- tempColor = color;
- if ( grid[delRow][delCol] > 0 )
- {
- tempColor = 0;
- }
- while ( tempCount > 0 )
- {
- grid[tempRow][tempCol] = tempColor;
-
- if ( disCount == 0 )
- {
- posRow = 80 - sphereTotal + 1;
- position[posRow][0] = tempRow;
- position[posRow][1] = tempCol;
- sphereTotal--;
- }
-
- switch ( oper )
- {
- //横
- case 0: tempCol--; break;
- //竖
- case 1: tempRow--; break;
- //左斜
- case 2:
- tempCol--;
- tempRow++;
- break;
- //右斜
- case 3:
- tempCol--;
- tempRow--;
- }
- tempCount--;
- }
- repaint();
- if ( disCount++ > 3 )
- {
- disCount = 0;
- deleting = false;
- createSphere(3);
- repaint();
- this.cancel();
- }
- }
- }
- }