📄 maincanvas.java
字号:
package popo;import java.util.Vector;import javax.microedition.lcdui.*;//import java.util.Random;import javax.microedition.rms.*;import com.nokia.mid.ui.*;import java.util.*;class MainCanvas extends FullCanvas implements Runnable{ Display display; StartCanvas Start; PoPo popoEx; private Thread thread; /** Vector of images to display */ private Vector images; private int level;//关数 private int map[][] = new int[7][6]; //地图,从上到下 /*X and Y*/ private int recX, recY; //方块坐标基准 private int chooserX, chooserY;//选择框坐标基准 /** Width and height */ private int frameWidth, frameHeight;//屏幕大小 private int recWidth, recHeight;//方块大小 /*索引*/ //red: 0-2; yellow: 3-5; blue: 6-8; green: 9-11; Purple: 12-14; orange: 15-17 private int indexGround, indexLost, indexClear, indexChooser; //其它图片索引 private boolean isPause;//游戏暂停标志 private boolean isDowning,isArranging,isBombed;//是否有方块下落中,若是则不能下落新的方块;是否在爆炸 private boolean isClear, isLost;//是否过关,是否game over /*Flags(决定位置)*/ private int wFlagChooser;//选择框水平位置标志(0,1,2,3,4,5)(left -> right) private int hFlagChooser;//选择框垂直位置标志(0,1,2,3,4,5)(up -> down) private int hFlagMap;//地图上现有方块的最大高度 //时间 private int time,score,tempScore; private Timer aTimer; private GameTimerTask TimerTask; //字体 private Font font; //爆炸效果 private int bomb[][] = new int[7][6]; //纪录爆炸方块 private int countFlash ;//爆炸闪烁计数 private int indexThrow;//发射方块数组的索引 private int flagThrow;// 标志使用的颜色数组 private boolean color[] = new boolean[4];//每一关所有颜色的使用状况,true:地图上有,false:地图上没有 //bomb private int bombCount;//当前炸弹数量 private int scoreCount;//加分系数 private int[] Color = new int[2];//下落方块颜色标志 private int[] colorTemp = new int[4];//正在使用的方块颜色 private int[] colorInit = new int[4];//每一关所有的方块颜色 public MainCanvas(StartCanvas Start){ this.Start = Start; this.display = Start.display; popoEx = new PoPo(); frameWidth = getWidth(); frameHeight = getHeight(); font = Font.getFont(Font.FACE_PROPORTIONAL,1,Font.SIZE_LARGE); //关卡 level = 1; } public void init(int level){ isPause = false; isClear = false; isLost = false; isBombed = false;isDowning = false; isArranging = false; //读本关地图及其他参数(根据level值) popoEx.game_init(level); map = popoEx.map; wFlagChooser = 0; hFlagChooser = popoEx.hFlagchooser; hFlagMap = popoEx.hFlagMap; //初始化参数 time = -1; score = 0; tempScore = 0; aTimer = new Timer(); TimerTask = new GameTimerTask(); countFlash = 0; if(level == 3) flagThrow = 4; else flagThrow = 3; indexThrow = 0; //初始化爆炸标志数组 for(int i=0; i<7; i++){ for(int j=0; j<6; j++){ bomb[i][j] = -1; } } bombCount = 0; scoreCount = 0; for(int i=0; i<4; i++){ if(i<popoEx.flagThrow) color[i] = true; else color[i] = false; colorTemp[i] = -1; colorInit[i] = popoEx.color[i]; } Color[0] = -1; Color[1] = -1; } public void setImages(Vector images) { this.images = images; indexGround = 18; indexClear = 19; indexLost = 20; indexChooser = 21; recX = 11; recY = 13; if (images.size() > 0) { //得到彩色方块大小 Image image = (Image)images.elementAt(0);//得到彩色方块大小 recWidth = image.getWidth(); recHeight = image.getHeight(); image = (Image)images.elementAt(indexChooser);//得到选择框大小 } else { recWidth = 0; recHeight = 0; } chooserX = recX - 2; chooserY = recY - 4; } protected void showNotify() { if (images != null && images.size() > 1) { init(level); thread = new Thread(this); aTimer.scheduleAtFixedRate(TimerTask,0,1000); thread.start(); } } protected void keyPressed(int keyCode) { int chooseAction = getGameAction(keyCode); switch (chooseAction) { case UP: //pause if(isLost != true && isClear != true){ if(isPause == false){//pause thread = null; isPause = true; TimerTask.cancel(); } else{//resume isPause = false; time --; thread = new Thread(this); TimerTask = new GameTimerTask(); aTimer.scheduleAtFixedRate(TimerTask,0,1000); thread.start(); } } break; case DOWN://释放方块 if(isPause == false && isDowning == false){ if(will_down()){ isDowning = true; //下落一格 map[hFlagChooser+1][wFlagChooser] = map[hFlagChooser][wFlagChooser]; map[hFlagChooser+1][wFlagChooser+1] = map[hFlagChooser][wFlagChooser+1]; //纪录下落方块颜色,可能会导致颜色增加,后面再判断 Color[0] = map[hFlagChooser][wFlagChooser]/3; Color[1] = map[hFlagChooser][wFlagChooser+1]/3; if(map[hFlagChooser+1][wFlagChooser]%3 >0) bombCount ++; if(map[hFlagChooser+1][wFlagChooser+1]%3 >0) bombCount ++; //trace System.out.println("indexThrow:------ "+indexThrow);/////////// System.out.println("bombCount: "+bombCount);/////////// //取队列中下一个方块 switch(popoEx.flagThrow){ case 4: map[hFlagChooser][wFlagChooser] = popoEx.thrower4[indexThrow][0]; map[hFlagChooser][wFlagChooser+1] = popoEx.thrower4[indexThrow][1]; indexThrow++; if(indexThrow >= 6) indexThrow = 0; break; case 3: map[hFlagChooser][wFlagChooser] = popoEx.thrower3[indexThrow][0]; map[hFlagChooser][wFlagChooser+1] = popoEx.thrower3[indexThrow][1]; indexThrow++; System.out.println("popoEx.thrower3: "+popoEx.thrower3[indexThrow-1][0]+" "+popoEx.thrower3[indexThrow-1][1]);/////////// if(indexThrow >= 6) indexThrow = 0; break; case 2: map[hFlagChooser][wFlagChooser] = popoEx.thrower2[indexThrow][0]; map[hFlagChooser][wFlagChooser+1] = popoEx.thrower2[indexThrow][1]; indexThrow++; System.out.println("popoEx.thrower2: "+popoEx.thrower2[indexThrow][0]+" "+popoEx.thrower2[indexThrow][1]);/////////// if(indexThrow >= 9) indexThrow = 0; break; case 1: map[hFlagChooser][wFlagChooser] = popoEx.thrower1[indexThrow][0]; map[hFlagChooser][wFlagChooser+1] = popoEx.thrower1[indexThrow][1]; System.out.println("popoEx.thrower1: "+popoEx.thrower1[indexThrow][0]+" "+popoEx.thrower1[indexThrow][1]);/////////// indexThrow++; if(indexThrow >= 3) indexThrow = 0; break; default: break; } repaint(chooserX,chooserY,recWidth*7,recHeight*8); } } break; case FIRE://方块旋转 if(isClear == true){//开始下一关 showNotify() ; }else if(isLost == true){//游戏失败,退到菜单界面,参数保持这一关 thread = null; aTimer.cancel(); display.setCurrent(this.Start); } else if(isPause == false){//方块旋转 int temp = map[hFlagChooser][wFlagChooser]; map[hFlagChooser][wFlagChooser] = map[hFlagChooser][wFlagChooser+1]; map[hFlagChooser][wFlagChooser+1] = temp; repaint(chooserX,chooserY+hFlagChooser*recHeight,frameWidth-chooserX,chooserY+(hFlagChooser+1)*recHeight); } break; case LEFT: //选择框左移 if(wFlagChooser > 0 && isPause == false){ wFlagChooser --; } break; case RIGHT://选择框右移 if(wFlagChooser < 4 && isPause == false){ wFlagChooser ++; } break; default: break; } } //发射行上下移动 public void chooser_change(){ if(hFlagMap - 2 >= 0 && hFlagMap - 2 < 7 ){ for(int i =0; i< 6; i++){ map[hFlagMap - 2][i] = map[hFlagChooser][i]; map[hFlagChooser][i] = -1; } hFlagChooser = hFlagMap - 2; } } //方块能否下落 public boolean will_down(){ if(hFlagChooser == 0){ if(map[1][wFlagChooser] == -1 && map[1][wFlagChooser+1] == -1) return true; else return false; }else return true; } //判断是否能够爆炸 public boolean will_bomb(int x, int y){ int flag = map[x][y] /3; for (int i=-1; i<2; i++) { if (x+i<hFlagChooser+1) continue; if (i==0) continue; if (x+i>=7) continue; if(flag>0){//非红色 if(map[x+i][y]/3 == flag) //down--up return true; }else{//红色 if((map[x+i][y]>-1) && (map[x+i][y]<3)) //down--up return true; } } for (int j=-1; j<2; j++) { if (y+j<0) continue; if (j==0) continue; if (y+j>=6) continue; if(flag>0){//非红色 if(flag == map[x][y+j]/3) //left--right return true; }else{//红色 if((map[x][y+j]>-1)&&(map[x][y+j]<3)) //lef--right return true; } } return false; } //控制爆炸 public void do_bomb(int x, int y, int flag){ //注意flag==0的情况,即红色方块的判断 //加分 tempScore += 20; //设置爆炸效果 if(map[x][y] % 3 > 0 )//如果是炸弹,炸弹数递减 bombCount --; bomb[x][y] = 3*flag + 2; map[x][y] = -1; for (int i=-1; i<2; i++) { if (x+i<hFlagChooser+1) continue; if (i==0) continue; if (x+i>=7) continue; if(flag>0){//非红色 if(map[x+i][y]/3 == flag) //down--up do_bomb(x+i,y,flag); }else{//红色 if((map[x+i][y]>-1) && (map[x+i][y]<3)) //down--up do_bomb(x+i,y,flag); } } for (int j=-1; j<2; j++) { if (y+j<0) continue; if (j==0) continue; if (y+j>=6) continue; if(flag>0){//非红色 if(flag == map[x][y+j]/3) //left--right do_bomb(x,y+j,flag); }else{//红色 if((map[x][y+j]>-1)&&(map[x][y+j]<3)) //lef--right do_bomb(x,y+j,flag); } } } //控制方块下落,并且触发炸弹 public void do_down(){ int height = 6; int count = 2; //方块下落 for(int j = wFlagChooser; j <= wFlagChooser+1; j++){ //列 for(int i = hFlagChooser+1; i<6; i++){ //行 if(map[i][j] >= 0 && map[i+1][j] == -1){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -