GameWorld.java
资源名称:PaoPao.rar [点击查看]
上传用户:sh2222
上传日期:2009-12-31
资源大小:84k
文件大小:32k
源码类别:
J2ME
开发平台:
Java
- package paopao;
- import javax.microedition.lcdui.*;
- import java.util.Hashtable;
- import java.io.InputStream;
- import com.nokia.mid.ui.*;
- public class GameWorld extends FullCanvas implements Runnable{
- final int screenW = getWidth();
- final int screenH = getHeight();
- final int paintW = 128;
- final int paintH = 208 - 3;
- final int orgX = 7;
- final int orgY = 3;
- final int midX = paintW / 2;
- final int midY = paintH / 2;
- //score
- int score;
- //Thread
- boolean killThread = false;
- boolean pauseThread = false;
- boolean overThread = false;
- int overCount;
- Thread thread = null;
- int interval = 50;
- //hardlevel setting
- int hardLevel;
- final int EASY = 9;
- final int MIDDLE = 8;
- final int HARD = 6;
- int shakeAt; //when ball shooted number equals or more then it shakes
- boolean shaking = false;
- int shakeOffX[];
- // top press
- int pressCount; //round intervals per top down count
- int shakeCount;
- int nowTop;
- //angle
- int angle = 90;
- int _angle = 6;
- int angleSinValue;
- int angleCosValue;
- //paopao
- int nextOneColor;
- int toBeShootColor;
- int isMovingColor;
- boolean isMoving = false; //击出的球是否处于运动过程中
- int shootAngle;
- int v = 10;
- int timeCount;
- final int effectiveR = 8;
- final int localStand = 6;
- int startX;
- int startY;
- int nowX;
- int nowY;
- boolean hitted = false;
- int hittedI;
- int hittedJ;
- //falling
- int fallingNum ;
- int[][] fallingPara;
- int fallingV = 8;
- //time
- //round
- int[][] roundPara;
- int roundCount; //read roundPara from map.txt & count
- boolean roundStart = true;
- int roundStartCount;
- int round = 1;
- //
- int disappearCount;
- /** Constructor */
- public GameWorld(int hardLevel) {
- this.hardLevel = hardLevel;
- try {
- jbInit();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- protected void show()
- {
- if(thread == null)
- thread = new Thread(this);
- thread.start();
- }
- /**Component initialization*/
- private void jbInit() throws Exception {
- roundPara = new int[11][8];
- this.readroundPara();
- switch(hardLevel)
- {
- case EASY:
- shakeAt = 6;
- break;
- case MIDDLE:
- shakeAt = 5;
- break;
- case HARD:
- shakeAt = 4;
- break;
- default:
- break;
- }
- toBeShootColor = 1 + Math.abs(Resource.random.nextInt() % 8);
- nextOneColor = 1 + Math.abs(Resource.random.nextInt() % 8);
- fallingPara = new int[50][3];
- shakeOffX = new int[]{1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1};
- // Set up this Displayable to listen to command events
- // add the Exit command
- }
- private void readroundPara()
- {
- InputStream is = null;
- try{
- is = getClass().getResourceAsStream("/round.txt");
- }
- catch(Exception e)
- {}
- int symbol;
- try{
- while((symbol = is.read()) != -1)
- {
- if(symbol == '&')
- {
- roundCount ++;
- if(roundCount == round)
- {
- for(int i = 0;i < 5;i ++)
- is.read();
- int symbol2;
- int rowCount = 0;
- int lineCount = 0;
- while((symbol2 = is.read()) != '&')
- {
- if(symbol2 == 'r')
- {
- rowCount ++;
- lineCount = 0;
- is.read();
- }
- else roundPara[rowCount][lineCount ++] = (int)symbol2 - 48;
- }
- break;
- }
- }
- }
- }
- catch(Exception ex)
- {
- }
- }
- private void nextPaoPao()
- {
- isMovingColor = toBeShootColor;
- toBeShootColor = nextOneColor;
- nextOneColor = 1 + Math.abs(Resource.random.nextInt() % 8);
- }
- protected void keyPressed(int keyCode)
- {
- if(!pauseThread)
- {
- if(keyCode == -6)
- PaoPao.quitApp();
- else if((keyCode == -5 || keyCode == 53) && isMoving == false)
- shoot();
- else if(keyCode == -3)
- {
- if(angle < 174)
- angle += 6;
- }
- else if(keyCode == -4)
- {
- if(angle > 6)
- angle -= 6;
- }
- else if(keyCode == -1)
- {
- if(angle == 90)
- return;
- angle = (angle > 90 ? angle - 6 : angle + 6);
- }
- } //if(!pauseThread)
- }
- /**Handle command events*/
- synchronized private void shoot()
- {
- //pressCount ++;
- //pressCount %= hardLevel;
- //if(pressCount >= shakeAt)
- //shaking = true;
- //if(pressCount == 0)
- //topDown();
- startX = 71 - orgX;
- startY = 170 - orgY;
- nowX = startX;
- nowY = startY;
- shootAngle = angle;
- angleSinValue = Resource.getSinAngleValue(shootAngle);
- angleCosValue = Resource.getCosAngleValue(shootAngle);
- isMoving = true;
- timeCount = 0;
- nextPaoPao();
- }
- private void topDown()
- {
- nowTop ++;
- shaking = false;
- }
- public void run()
- {
- while(!killThread)
- {
- if(!pauseThread)
- {
- if(!overThread)
- {
- if(roundStart)
- {
- roundStartCount ++;
- if(roundStartCount >= 48)
- {
- roundStartCount = 0;
- roundStart = false;
- }
- }
- if(isMoving) //被击出的球处于运动状态中
- {
- paoPaoMoving();
- }
- Falling();
- if(disappearCount > 0)
- {
- disappearCount --;
- if(disappearCount == 0)
- for(int i = 0;i < 10;i ++)
- for(int j = 0;j < (i % 2 == 0 ? 8 : 7);j ++)
- {
- if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- roundPara[i][j] = 0;
- }
- }
- if(shaking)
- {
- shakeCount ++;
- shakeCount %= 16;
- }
- try
- {
- repaint();
- thread.sleep(interval);
- }
- catch(Exception ex){}
- } //if(!overThread)
- //overCount ++;
- // if(overCount >= paintH - nowTop * 14)
- // killThread = true;
- }
- } //while(!killThread)
- //System.out.println("killThread = "+ killThread);
- this.serviceRepaints();
- System.gc();
- PaoPao.shiftCanvas(PaoPao.MENU_ID,0);
- }
- private void Falling()
- {
- if(fallingNum > 0)
- {
- for(int i = 0;i < fallingNum;i ++)
- {
- fallingPara[i][2] += fallingV;
- }
- }
- for(int i = 0;i < fallingNum;i ++)
- {
- if(fallingPara[i][2] > paintH + 8)
- {
- for(int j = i;j < fallingNum - 1;j ++)
- {
- fallingPara[j][0] = fallingPara[j + 1][0];
- fallingPara[j][1] = fallingPara[j + 1][1];
- fallingPara[j][2] = fallingPara[j + 1][2];
- }
- fallingNum--;
- }
- }
- }
- private void paoPaoMoving()
- {
- int posX = 0;
- int posY = 0;
- int preX;
- int preY;
- int pre_x;
- int pre_y;
- preX = nowX;
- preY = nowY;
- pre_x = nowX;
- pre_y = nowY;
- if(hitted)
- {
- hitted = !hitted;
- dealWithHit(hittedI,hittedJ);
- return;
- }
- timeCount ++;
- posX = startX + (timeCount * v) * angleCosValue / 100000;
- posY = startY - (timeCount * v) * angleSinValue / 100000;
- while(true)
- {
- if(posX - 8 < 0)
- {
- startX = 8;
- startY = nowY;
- nowX = 8;
- timeCount = 0;
- angleCosValue = 0 - angleCosValue;
- //timeCount ++;
- break;
- }
- else if(posX + 8 > paintW)
- {
- startX = paintW - 8;
- startY = nowY;
- nowX = paintW - 8;
- timeCount = 0;
- angleCosValue = 0 - angleCosValue;
- //timeCount ++;
- break;
- }
- else if(posY - 8 <= 0) //reach the top
- {
- this.roundPara[0][posX / 16] = isMovingColor;
- nowX = posX;
- nowY = 8;
- checkConnect(0,nowX / 16,isMovingColor);
- if(checkDisappear())
- checkFall();
- isMoving = false;
- return;
- }
- else
- {
- nowX = posX;
- nowY = posY;
- break;
- }
- }
- if(nowY == 8)
- return;
- for(int i = 0;i < 10; i ++)
- {
- if(i % 2 == 0)
- {
- for(int j = 0;j < 8;j ++)
- {
- if(isCollided(nowX,nowY,j * 16 + 8,i * 14 + 8) && roundPara[i][j] > 0 && roundPara[i][j] < 9)
- {
- for(int m = 1;m <= v;m ++)
- {
- preX = startX + ((timeCount - 1) * v + m) * angleCosValue / 100000;
- preY = startY - ((timeCount - 1) * v + m) * angleSinValue / 100000;
- if(isCollided(preX,preY,j * 16 + 8,i * 14 + 8))
- {
- hitted = true;
- nowX = preX;
- nowY = preY;
- hittedI = i;
- hittedJ = j;
- break;
- }
- }
- return;
- }
- }
- }
- else if(i % 2 == 1)
- {
- for(int j = 0;j < 7; j ++)
- {
- if(isCollided(nowX,nowY,8 + j * 16 + 8,i * 14 + 8) && roundPara[i][j] > 0 && roundPara[i][j] < 9)
- {
- for(int m = 1;m <= v;m ++)
- {
- preX = startX + ((timeCount - 1) * v + m) * angleCosValue / 100000;
- preY = startY - ((timeCount - 1) * v + m) * angleSinValue / 100000;
- if(isCollided(preX,preY,8 + j * 16 + 8,i * 14 + 8))
- {
- hitted = true;
- nowX = preX;
- nowY = preY;
- hittedI = i;
- hittedJ = j;
- break;
- }
- }
- return;
- }
- }
- }
- } //for(int i = 0;i < 10; i ++)
- }
- private boolean checkDisappear()
- {
- int sameColorCount = 0;
- for(int i = 0;i < 10;i ++)
- if(i % 2 == 0)
- {
- for(int j = 0 ;j < 8 ;j ++)
- if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- sameColorCount ++;
- }
- else if(i % 2 == 1)
- {
- for(int j = 0;j < 7;j ++)
- if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- sameColorCount ++;
- }
- if(sameColorCount >= 3)
- {
- score += sameColorCount;
- disappearCount = 5;
- /*
- for(int i = 0;i < 10;i ++)
- {
- for(int j = 0;j < (i % 2 == 0 ? 8 : 7);j ++)
- {
- if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- roundPara[i][j] = 0;
- }
- }
- */
- return true;
- }
- for(int i = 0;i < 10;i ++)
- {
- for(int j = 0;j < (i % 2 == 0 ? 8 : 7);j ++)
- {
- if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- roundPara[i][j] -= 8;
- }
- }
- return false;
- }
- private void checkConnect(int x,int y,int color) //let all round have the same color disappear
- {
- if(x == 10 && haveNoSameRound(x,y,color))
- {
- overThread = true;
- return;
- }
- if(roundPara[x][y] == color)
- roundPara[x][y] += 8;
- if(haveNoSameRound(x,y,color))
- return;
- else
- {
- if(x == 9)
- {
- if(y == 0)
- {
- if(roundPara[9][1] == color)
- checkConnect(9,1,color);
- if(roundPara[8][1] == color)
- checkConnect(8,1,color);
- if(roundPara[8][2] == color)
- checkConnect(8,2,color);
- }
- else if(y == 6)
- {
- if(roundPara[9][7] == color)
- checkConnect(10,6,color);
- if(roundPara[8][6] == color)
- checkConnect(8,6,color);
- }
- else
- {
- if(roundPara[8][y] == color)
- checkConnect(8,y,color);
- if(roundPara[8][y + 1] == color)
- checkConnect(8,y + 1,color);
- if(roundPara[9][y - 1] == color)
- checkConnect(9,y - 1,color);
- if(roundPara[9][y + 1] == color)
- checkConnect(9,y + 1,color);
- if(roundPara[10][y] == color)
- checkConnect(10,y,color);
- if(roundPara[10][y + 1] == color)
- checkConnect(10,y + 1,color);
- }
- } //if(x == 10)
- else if(x == 0) //the first row
- {
- if(y == 0)
- {
- if(roundPara[0][1] == color)
- checkConnect(0,1,color);
- if(roundPara[1][0] == color)
- checkConnect(1,0,color);
- }
- else if(y == 7)
- {
- if(roundPara[0][6] == color)
- checkConnect(0,6,color);
- if(roundPara[1][6] == color)
- checkConnect(1,6,color);
- }
- else
- {
- if(roundPara[x][y - 1] == color)
- checkConnect(x,y - 1,color);
- if(roundPara[x][y + 1] == color)
- checkConnect(x,y + 1,color);
- if(roundPara[x + 1][y - 1] == color)
- checkConnect(x + 1,y - 1,color);
- if(roundPara[x + 1][y] == color)
- checkConnect(x + 1,y,color);
- }
- }
- else if(y == 0)
- {
- if(x % 2 == 1)
- {
- if(roundPara[x - 1][y] == color)
- checkConnect(x - 1,y,color);
- if(roundPara[x - 1][y + 1] == color)
- checkConnect(x- 1,y + 1,color);
- if(roundPara[x][y + 1] == color)
- checkConnect(x,y + 1,color);
- if(roundPara[x + 1][y] == color)
- checkConnect(x + 1,y,color);
- if(roundPara[x + 1][y + 1] == color)
- checkConnect(x + 1,y + 1,color);
- }
- else if(x % 2 == 0)
- {
- if(roundPara[x - 1][y] == color)
- checkConnect(x - 1,y,color);
- if(roundPara[x][y + 1] == color)
- checkConnect(x, y + 1,color);
- if(roundPara[x + 1][y] == color)
- checkConnect(x + 1,y ,color);
- }
- }
- else if(y == 6 && x % 2 == 1)
- {
- if(roundPara[x - 1][y] == color)
- checkConnect(x - 1,y,color);
- if(roundPara[x - 1][y + 1] == color)
- checkConnect(x - 1,y + 1,color);
- if(roundPara[x][y - 1] == color)
- checkConnect(x,y - 1,color);
- if(roundPara[x + 1][y] == color)
- checkConnect(x + 1,y,color);
- if(roundPara[x + 1][y + 1] == color)
- checkConnect(x + 1,y + 1,color);
- }
- else if(y == 7 && x % 2 == 0)
- {
- if(roundPara[x - 1][y - 1] == color)
- checkConnect(x - 1,y - 1,color);
- if(roundPara[x][y - 1] == color)
- checkConnect(x,y - 1,color);
- if(roundPara[x + 1][y - 1] == color)
- checkConnect(x + 1,y - 1,color);
- }
- else
- {
- if(x % 2 == 0)
- {
- if(roundPara[x - 1][y] == color)
- checkConnect(x - 1,y,color);
- if(roundPara[x - 1][y - 1] == color)
- checkConnect(x - 1,y - 1,color);
- if(roundPara[x][y - 1] == color)
- checkConnect(x,y - 1,color);
- if(roundPara[x][y + 1] == color)
- checkConnect(x,y + 1,color);
- if(roundPara[x + 1][y] == color)
- checkConnect(x + 1,y,color);
- if(roundPara[x + 1][y - 1] == color)
- checkConnect(x + 1,y - 1,color);
- }
- else if(x % 2 == 1)
- {
- if(roundPara[x - 1][y] == color)
- checkConnect(x - 1,y,color);
- if(roundPara[x - 1][y + 1] == color)
- checkConnect(x - 1,y + 1,color);
- if(roundPara[x][y - 1] == color)
- checkConnect(x,y - 1,color);
- if(roundPara[x][y + 1] == color)
- checkConnect(x,y + 1,color);
- if(roundPara[x + 1][y] == color)
- checkConnect(x + 1,y,color);
- if(roundPara[x + 1][y + 1] == color)
- checkConnect(x + 1,y + 1,color);
- }
- }
- }
- //
- }
- private void checkFall()
- {
- int color;
- for(int i = 0;i < 10;i ++) //have no connectting with the top wall
- {
- if(i % 2 == 0)
- {
- for(int j = 0;j < 8 ;j ++)
- {
- color = roundPara[i][j];
- if(roundPara[i][j] > 0 && roundPara[i][j] < 9 && !connectWithTopWall(i,j))
- {
- fallingNum ++;
- score ++;
- roundPara[i][j] = 0;
- fallingPara[fallingNum - 1][0] = color;
- fallingPara[fallingNum - 1][1] = 16 * j + 8;
- fallingPara[fallingNum - 1][2] = 14 * i + 8;
- }
- for(int x = 0;x < 10;x ++)
- {
- for(int y = 0;y < (x % 2 == 0 ? 8 : 7);y ++)
- if(roundPara[x][y] > 16 && roundPara[x][y] < 25)
- roundPara[x][y] = roundPara[x][y] - 16;
- }
- }
- }
- else if(i % 2 == 1)
- {
- for(int j = 0;j < 7;j ++)
- {
- color = roundPara[i][j];
- if(roundPara[i][j] > 0 && roundPara[i][j] < 9 && !connectWithTopWall(i,j))
- {
- fallingNum ++;
- score ++;
- roundPara[i][j] = 0;
- fallingPara[fallingNum - 1][0] = color;
- fallingPara[fallingNum - 1][1] = 16 * j + 8;
- fallingPara[fallingNum - 1][2] = 14 * i + 8;
- }
- for(int x = 0;x < 10;x ++)
- {
- for(int y = 0;y < (x % 2 == 0 ? 8 : 7);y ++)
- if(roundPara[x][y] > 16 && roundPara[x][y] < 25)
- roundPara[x][y] = roundPara[x][y] - 16;
- }
- }
- }
- } //for(int i = 1;i < 10;i ++)
- for(int m = 0;m < 10;m ++)
- {
- for(int n = 0;n < (m % 2 == 0 ? 8 : 7);n ++)
- System.out.print(roundPara[m][n]);
- }
- }
- private boolean connectWithTopWall(int i,int j)
- {
- if(i == 0 && roundPara[i][j] > 0 && roundPara[i][j] < 9)
- return true;
- else if(roundPara[i][j] > 0 && roundPara[i][j] < 9)
- {
- roundPara[i][j] += 16;
- if(i == 9)
- {
- if(j == 0)
- {
- if(connectWithTopWall(8,0) || connectWithTopWall(8,1) || connectWithTopWall(9,1))
- return true;
- }
- else if(j == 6)
- {
- if(connectWithTopWall(8,6) || connectWithTopWall(8,7) || connectWithTopWall(9,5))
- return true;
- }
- else if(connectWithTopWall(9,j - 1) || connectWithTopWall(9,j + 1)
- ||connectWithTopWall(8,j) || connectWithTopWall(8,j + 1))
- return true;
- }
- else if(i == 0)
- {
- }
- else if(j == 0)
- {
- if(i % 2 == 1)
- {
- if(connectWithTopWall(i - 1,j + 1) || connectWithTopWall(i - 1,j)
- ||connectWithTopWall(i,j + 1)
- ||connectWithTopWall(i + 1,j + 1) || connectWithTopWall(i + 1,j))
- return true;
- }
- else if(i % 2 == 0)
- {
- if(connectWithTopWall(i - 1,j)
- || connectWithTopWall(i,j + 1) ||
- connectWithTopWall(i + 1,j))
- return true;
- }
- }
- else if(j == 6 && i % 2 == 1)
- {
- if(connectWithTopWall(i - 1,j) || connectWithTopWall(i - 1,j + 1)
- || connectWithTopWall(i,j - 1)
- || connectWithTopWall(i + 1,j) || connectWithTopWall(i + 1,j + 1))
- return true;
- }
- else if(j == 7 && i % 2 == 0)
- {
- if(connectWithTopWall(i,j - 1)
- || connectWithTopWall(i - 1,j - 1)
- || connectWithTopWall(i + 1,j - 1))
- return true;
- }
- else
- {
- if(i % 2 == 0)
- {
- if(connectWithTopWall(i - 1,j - 1)|| connectWithTopWall(i - 1,j)
- || connectWithTopWall(i, j - 1) || connectWithTopWall(i, j + 1)
- || connectWithTopWall(i + 1,j - 1) || connectWithTopWall(i + 1, j))
- return true;
- }
- else if(i % 2 == 1)
- {
- if(connectWithTopWall(i - 1,j) || connectWithTopWall(i - 1,j + 1)
- || connectWithTopWall(i, j - 1) || connectWithTopWall(i,j + 1)
- || connectWithTopWall(i + 1,j) || connectWithTopWall(i + 1,j + 1))
- return true;
- }
- }
- return false;
- }
- else return false;
- }
- private boolean haveNoSameRound(int x,int y,int color)
- {
- if(x == 10)
- {
- if(roundPara[9][y - 1] == color || roundPara[9][y] == color)
- return false;
- }
- else if(x == 0) //the first row
- {
- if(y == 0)
- {
- if(roundPara[0][1] == color || roundPara[1][0] == color)
- return false;
- }
- else if(y == 7)
- {
- if(roundPara[0][6] == color || roundPara[1][6] == color)
- return false;
- }
- else if(roundPara[0][y - 1] == color || roundPara[0][y + 1] == color
- || roundPara[1][y] == color || roundPara[1][y - 1] == color)
- return false;
- }
- else if(y == 0)
- {
- if(x % 2 == 1)
- {
- if(roundPara[x - 1][y] == color || roundPara[x - 1][y + 1] == color || roundPara[x][y + 1] == color
- ||roundPara[x + 1][y] == color || roundPara[x + 1][y + 1] == color)
- return false;
- }
- else if(x % 2 == 0)
- {
- if(roundPara[x - 1][y] == color || roundPara[x][y + 1] == color || roundPara[x + 1][y] == color)
- return false;
- }
- }
- else if(y == 6 && x % 2 == 1)
- {
- if(roundPara[x][y - 1] == color || roundPara[x - 1][y] == color || roundPara[x - 1][y + 1] == color
- ||roundPara[x + 1][y] == color || roundPara[x + 1][y + 1] == color)
- return false;
- }
- else if(y == 7 && x % 2 == 0)
- {
- if(roundPara[x][y - 1] == color || roundPara[x - 1][y - 1] == color || roundPara[x + 1][y - 1] == color)
- return false;
- }
- else
- {
- if(x % 2 == 0)
- {
- if(roundPara[x - 1][y - 1] == color || roundPara[x - 1][y] == color || roundPara[x][ y - 1] == color
- ||roundPara[x][y + 1] == color || roundPara[x + 1][y - 1] == color || roundPara[x + 1][y] == color)
- return false;
- }
- else if(x % 2 == 1)
- {
- if(roundPara[x - 1][y] == color || roundPara[x - 1][y + 1] == color || roundPara[x][y - 1] == color
- ||roundPara[x][y + 1] == color || roundPara[x + 1][y] == color || roundPara[x + 1][y + 1] == color)
- return false;
- }
- } //else
- return true;
- }
- private void dealWithHit(int i,int j)
- {
- int a = 8 << 1;
- if(nowX == paintW - 8)
- {
- if(i % 2 == 0)
- {
- roundPara[i + 1][j - 1] = isMovingColor;
- checkConnect(i + 1,j - 1,isMovingColor);
- }
- else
- {
- roundPara[i + 1][j + 1] = isMovingColor;
- checkConnect(i + 1,j + 1,isMovingColor);
- }
- }
- else if(nowX == 8)
- {
- roundPara[i + 1][0] = isMovingColor;
- checkConnect(i + 1,0,isMovingColor);
- }
- else if(i % 2 == 0)
- {
- if(nowX < 8 + j * 16) //left
- {
- if(j >= 1 && Math.abs(nowY - (i * 14 + 8)) < localStand && roundPara[i][j - 1] == 0)
- {
- roundPara[i][j - 1] = isMovingColor;
- checkConnect(i,j - 1,isMovingColor);
- }
- else if(roundPara[i + 1][j - 1] == 0)
- {
- roundPara[i + 1][j - 1] = isMovingColor;
- checkConnect(i + 1,j - 1,isMovingColor);
- }
- else {
- //if(roundPara[i][j - 1] == 0)
- // {
- // roundPara[i][j - 1] = isMovingColor;
- //checkConnect(i,j - 1,isMovingColor);
- //}
- //else
- {
- roundPara[i + 1][j] = isMovingColor;
- checkConnect(i + 1,j,isMovingColor);
- }
- }
- }
- else if(nowX >= 8 + j * 16)
- {
- if(j <= 6 && Math.abs(nowY - (i * 14 + 8)) < localStand && roundPara[i][j + 1] == 0)
- {
- roundPara[i][j + 1] = isMovingColor;
- checkConnect(i,j + 1,isMovingColor);
- }
- else if(roundPara[i + 1][j] == 0) {
- roundPara[i + 1][j] = isMovingColor;
- checkConnect(i + 1,j,isMovingColor);
- }
- else{
- roundPara[i + 1][j - 1] = isMovingColor;
- checkConnect(i + 1,j - 1,isMovingColor);
- }
- }
- }
- else if(i % 2 == 1)
- {
- if(nowX < 8 + j * 16 + 8) //left
- {
- if(j >= 1 && Math.abs(nowY - (i * 14 + 8)) < localStand && roundPara[i][j - 1] == 0)
- {
- roundPara[i][j - 1] = isMovingColor;
- checkConnect(i,j - 1,isMovingColor);
- }
- else if(j >= 0 && roundPara[i + 1][j] == 0)
- {
- roundPara[i + 1][j] = isMovingColor;
- checkConnect(i + 1,j,isMovingColor);
- }
- else
- {
- roundPara[i + 1][j + 1] = isMovingColor;
- checkConnect(i + 1,j + 1,isMovingColor);
- }
- }
- else if(nowX >= 8 + j * 16 + 8)
- {
- if(j <= 5 && Math.abs(nowY - (i * 14 + 8)) < localStand && roundPara[i][j + 1] == 0) //down
- {
- roundPara[i][j + 1] = isMovingColor;
- checkConnect(i,j + 1,isMovingColor);
- }
- else if(roundPara[i + 1][j + 1] == 0)
- {
- roundPara[i + 1][j + 1] = isMovingColor;
- checkConnect(i + 1,j + 1,isMovingColor);
- }
- else
- {
- roundPara[i + 1][j] = isMovingColor;
- checkConnect(i + 1,j,isMovingColor);
- }
- }
- }
- for(int x = 0;x < 10;x ++)
- {
- for(int y = 0;y < (x % 2 == 0 ? 8 : 7);y ++)
- System.out.print(roundPara[x][y]);
- }
- if(checkDisappear()) // if this round three or more ball disappeared
- checkFall();
- isMoving = false;
- } //dealWithHit()
- private boolean gameOver()
- {
- for(int i = 11 - nowTop;i < 12;i ++)
- {
- for(int j = 0;j < (i % 2 == 0 ? 8 : 7);j ++)
- if(roundPara[i][j] > 0 && roundPara[i][j] < 9)
- return true;
- }
- return false;
- }
- private boolean isCollided(int x1,int y1,int x2,int y2)
- {
- if((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) <= (8 + effectiveR) * (8 + effectiveR))
- return true;
- else return false;
- }
- /** Required paint implementation */
- protected void paint(Graphics g)
- {
- g.setColor(0,0,0);
- g.fillRect(0,0,screenW,screenH);
- drawBackGround(g);
- g.translate(7,3);
- g.setClip(0,0,paintW,paintH);
- DirectGraphics dg = DirectUtils.getDirectGraphics(g);
- drawForeGround(g,dg);
- /** @todo Add paint codes */
- }
- private void addBalls(int floors)
- {
- for(int i = 0;i < 8;i ++) //往最底层加上一层泡泡
- {
- for(int j = 10;j > 0;j --)
- {
- if(roundPara[i][j] != 0)
- {
- if(i % 2 == 0)
- {
- if(roundPara[i][j + 1] == 0)
- addOneBall(i,j + 1);
- break;
- }
- else if(i % 2 == 1) // i % 2 == 1
- {
- roundPara[i + 1][j - 1] = roundPara[i + 1][j - 1] == 0 ? 1 + (((Resource.random.nextInt() % 8)<<1)>>1) : roundPara[i + 1][j - 1];
- roundPara[i + 1][j] = roundPara[i + 1][j] == 0 ? 1 + (((Resource.random.nextInt() % 8) << 1)>>1) : roundPara[i + 1][j] ;
- }
- }
- }
- }
- }
- private void addOneBall(int x,int y)
- {
- if(
- (x % 2 == 0 && y < 8 && roundPara[x][y] == 0) ||
- (x % 2 == 1 && y < 7 && roundPara[x][y] == 0)
- )
- roundPara[x][y] = 1 + Math.abs(Resource.random.nextInt() % 8);
- }
- private void drawScore(Graphics g)
- {
- g.setClip(102,192,74,16);
- g.setColor(100,255,100);
- g.drawString("score:"+ score,104,194,Graphics.LEFT | Graphics.TOP);
- g.setClip(0,0,176,208);
- }
- private void drawPaoPao(Graphics g)
- {
- for(int i = 0;i < 11;i ++)
- {
- if(i % 2 == 0)
- for(int j = 0;j < 8;j ++)
- {
- if(roundPara[i][j] > 0 && roundPara[i][j] < 9)
- {
- g.setClip(16 * j,14 * i,16,16);
- g.drawImage(Resource.getImage(Resource.BALL),
- j * 16 - (roundPara[i][j] - 1) * 16 +(shaking ? shakeOffX[shakeCount]:0),
- i * 14 ,Graphics.LEFT | Graphics.TOP);
- g.setClip(0,0,paintW,paintH);
- }
- else if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- {
- g.setClip(16 * j,14 * i,16,16);
- switch(disappearCount)
- {
- case 5:
- g.drawImage(Resource.getImage(Resource.BALL),
- j * 16 - (roundPara[i][j] - 8 - 1) * 16 +(shaking ? shakeOffX[shakeCount]:0),
- i * 14 ,Graphics.LEFT | Graphics.TOP);
- break;
- case 4:
- case 3:
- case 2:
- case 1:
- g.drawImage(Resource.getImage(Resource.BALL),
- j * 16 - (15 - disappearCount / 3) * 16 +(shaking ? shakeOffX[shakeCount]:0),
- i * 14 ,Graphics.LEFT | Graphics.TOP);
- break;
- default:
- break;
- }
- g.setClip(0,0,paintW,paintH);
- }
- }
- else if(i % 2 == 1)
- for( int j = 0;j < 7 ; j ++)
- {
- if(roundPara[i][j] > 0 && roundPara[i][j] < 9)
- {
- g.setClip(8 + 16 * j,14 * i,16,16);
- g.drawImage(Resource.getImage(Resource.BALL),
- 8 + 16 * j - (roundPara[i][j] - 1) * 16 + (shaking ? shakeOffX[shakeCount]:0),
- i * 14 ,Graphics.TOP | Graphics.LEFT);
- g.setClip(0,0,paintW,paintH);
- }
- else if(roundPara[i][j] > 8 && roundPara[i][j] < 17)
- {
- g.setClip(16 * j + 8,14 * i,16,16);
- switch(disappearCount / 3)
- {
- case 5:
- g.drawImage(Resource.getImage(Resource.BALL),
- j * 16 + 8 - (roundPara[i][j] - 8 - 1) * 16 +(shaking ? shakeOffX[shakeCount]:0),
- i * 14 ,Graphics.LEFT | Graphics.TOP);
- break;
- case 4:
- case 3:
- case 2:
- case 1:
- g.drawImage(Resource.getImage(Resource.BALL),
- j * 16 + 8 - (15 - disappearCount / 3) * 16 +(shaking ? shakeOffX[shakeCount]:0),
- i * 14 ,Graphics.LEFT | Graphics.TOP);
- break;
- default:
- break;
- }
- g.setClip(0,0,paintW,paintH);
- }
- }
- }
- }
- private void drawForeGround(Graphics g,DirectGraphics dg)
- {
- //drawTop(g);
- drawToBeShoot(g);
- drawNextOne(g);
- drawIsMoving(g);
- drawPointer(dg);
- drawPaoPao(g);
- drawFalling(g);
- if(roundStart)
- {;}
- else
- {
- if(overThread)
- {
- g.setClip(0,paintH - overCount,paintW,overCount);
- for(int i = 0;i < 12;i ++)
- for(int j = 0;j < (i % 2 == 0 ? 8 : 7);j ++)
- if(roundPara[i][j] != 0)
- g.drawImage(Resource.getImage(Resource.BBALL),j * 16 + 8 + (i % 2 == 0 ? 0 : 8),
- i * 14,Graphics.VCENTER |Graphics.HCENTER);
- g.setClip(0,0,paintW,paintH);
- }
- else
- {
- }
- }
- if(roundStart)
- drawRoundStart(dg);
- }
- private void drawBackGround(Graphics g)
- {
- g.drawImage(Resource.getImage(Resource.BG),0,0,Graphics.LEFT | Graphics.TOP);
- drawScore(g);
- }
- private void drawTop(Graphics g)
- {
- }
- private void drawPointer(DirectGraphics dg)
- {
- if(angle <= 90)
- dg.drawImage(Resource.getImage(angle / 6),71 - orgX,170 - orgY,
- Graphics.VCENTER | Graphics.HCENTER,0);
- else if(angle >= 90)
- dg.drawImage(Resource.getImage((180 - angle) / 6),71 - orgX,170 - orgY,
- Graphics.VCENTER | Graphics.HCENTER,
- DirectGraphics.FLIP_HORIZONTAL);
- }
- private void drawIsMoving(Graphics g)
- {
- if(isMoving)
- {
- g.setClip(nowX - 8,nowY - 8,16,16);
- g.drawImage(Resource.getImage(Resource.BALL),nowX - 8 - (isMovingColor - 1) * 16,nowY - 8,Graphics.LEFT | Graphics.TOP);
- g.setClip(0,0,paintW,paintH);
- }
- }
- private void drawToBeShoot(Graphics g)
- {
- g.setClip(71 - orgX - 8,170 - orgY - 8,16,16);
- g.drawImage(Resource.getImage(Resource.BALL),71 - orgX - 8 - (toBeShootColor - 1) * 16,
- 170 - orgY - 8,Graphics.LEFT | Graphics.TOP);
- g.setClip(0,0,paintW,paintH);
- }
- private void drawNextOne(Graphics g)
- {
- g.setClip(54 - orgX - 8,190 - orgY - 16,16,16);
- g.drawImage(Resource.getImage(Resource.BALL),54 - orgX - 8 - (nextOneColor - 1) * 16,
- 190 - orgY - 16,Graphics.TOP | Graphics.LEFT);
- g.setClip(0,0,paintW,paintH);
- }
- private void drawFalling(Graphics g)
- {
- if(fallingNum > 0)
- {
- for(int i = 0;i < fallingNum;i ++)
- {
- g.setClip(fallingPara[i][1] - 8,fallingPara[i][2] - 8,16,16);
- g.drawImage(Resource.getImage(Resource.BALL),
- fallingPara[i][1] - 8 - 16 * (fallingPara[i][0] - 1),
- fallingPara[i][2] - 8,Graphics.LEFT | Graphics.TOP);
- g.setClip(0,0,paintW,paintH);
- }
- }
- }
- public static void drawRoundStart(Graphics g)
- {
- }
- private void drawRoundStart(DirectGraphics dg)
- {
- if(roundStartCount <= 16)
- {
- }
- else if(roundStartCount <= 32)
- {
- if(roundStartCount >= 48)
- {
- }
- }
- else
- {
- }
- }
- private void drawGameOver(Graphics g)
- {
- }
- }