⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameworld.java

📁 手机游戏 泡泡龙 J2ME源代码+详细注释
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
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))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -