?? 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 + -