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

📄 gamecanvas.java

📁 魔术方块,好玩手机游戏范例,以j2me撰写
💻 JAVA
字号:
// unnamed package
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class gameCanvas extends Canvas
{
    private final mobileTetris midletCanvas;
    private Image handImage, backgroundImage, firstImage, wordImage;
    private int viewTetris, moveX, moveY, maxisX, maxisY, maxisX2, maxisY2, MAXType1, MAXType2, rotation, countY;
    private int scoreTotal, blockScore, playGame;
    private boolean showWord, viewMap, againGame, cutBlock, GameOver, showScore;
    private valueMatrix randoms, moveXY;
    private goStart gostart;
    private showStart showstart;
    private int[][] gameMatrix, keepMatrix;
    private int[][][] tetrisMAX={
         {{-1,-1,-1,0,0,0,1,0},{1,-1,0,-1,0,0,0,1},{1,1,1,0,0,0,-1,0},{-1,1,0,1,0,0,0,-1}},
	 {{-1,0,0,0,1,-1,1,0},{0,-1,0,0,1,1,0,1},{1,0,0,0,-1,1,-1,0},{0,1,0,0,-1,-1,0,-1}},
	 {{-1,0,0,-1,0,0,1,0},{0,-1,1,0,0,0,0,1},{1,0,0,1,0,0,-1,0},{0,1,-1,0,0,0,0,-1}},
	 {{-1,0,0,0,1,0,2,0},{0,-1,0,0,0,1,0,2}},
         {{-1,0,0,0,0,-1,1,-1},{0,-1,0,0,1,0,1,1}},
	 {{-1,-1,0,-1,0,0,1,0},{1,-1,1,0,0,0,0,1}},
	 {{0,-1,1,-1,0,0,1,0}}
    };

    private int[] colors={0x00EEC900,0x00ff00ff,0x0000ff00,0x000000ff,0x008B008B,0x00800000,0x0000ffff,0x009932CC};

    public gameCanvas(mobileTetris midletCanvas)
    {
       this.midletCanvas=midletCanvas;
       randoms=new valueMatrix();
       moveXY=new valueMatrix();
       gameMatrix=new int[12][18];
       keepMatrix=new int[12][18];
       try
       {
         wordImage=Image.createImage("/word.png");
         handImage=Image.createImage("/hand.png");
         backgroundImage=Image.createImage("/background.png");
         firstImage=Image.createImage("/first.png");
       }catch(Exception ex){}
       MAXType1=randoms.randMatrix();
       MAXType2=randoms.randMatrix();
       showstart=new showStart();
       showstart.start();
    }

    protected void keyPressed(int keyCode)
    {
      int gameKey=getGameAction(keyCode);
      if(viewTetris==0)
        switch(gameKey)
        {
          default:
             showstart=null;
             startgames();
             gostart=new goStart();
             gostart.start();                
        }
      
      if(!againGame && viewTetris==10) 
        switch(gameKey)
        {
          case UP:              
               break;
          case DOWN:        
               moveY++;
               if(moveY>18) moveY=18;
               viewMap=randoms.mapYN(tetrisMAX[MAXType1][rotation], keepMatrix, moveX, moveY, "y");
               if(viewMap && moveY<18)  
                 repaint();
               else
                 moveY--;  
               break;
          case LEFT:
               moveX--;           
               viewMap=randoms.mapYN(tetrisMAX[MAXType1][rotation], keepMatrix, moveX, moveY, "x");
               if(viewMap)
                 repaint();
               else
                 moveX++; 
               break;
          case RIGHT:
               moveX++;
               viewMap=randoms.mapYN(tetrisMAX[MAXType1][rotation], keepMatrix, moveX, moveY, "x");
               if(viewMap)
                 repaint();
               else
                 moveX--;
               break;
          case FIRE:
               rotation++;
               if(rotation>(tetrisMAX[MAXType1].length-1))
                  rotation=0;
               viewMap=randoms.mapYN(tetrisMAX[MAXType1][rotation], keepMatrix, moveX, moveY, "xy"); 
               if(viewMap)
                 repaint();
               else
                 rotation--;
               break;
        }

      if(viewTetris==20)
        switch(gameKey)
        {
           case UP:
                playGame=0;
                repaint();
                break;
           case DOWN:
                playGame=1;
                repaint();
                break;
           case FIRE:
                if(playGame==0)
                  againgames();
                else
                  midletCanvas.listExit();
                break;             
        }
    }

   public void paint(Graphics g)
   {
      if(viewTetris==0)
        paintView0(g);
      else if(viewTetris==10)
        paintView10(g);
      if(viewTetris==20)
        paintView20(g);
   }

   private void paintView0(Graphics g)
   {
       g.drawImage(backgroundImage,0,0, Graphics.TOP|Graphics.LEFT);
       if(showWord)
          g.drawImage(wordImage, 15, 125, Graphics.TOP|Graphics.LEFT);
   }

   private void paintView10(Graphics g)
   {
      init();
      g.drawImage(firstImage,0,0, Graphics.TOP|Graphics.LEFT);
      g.setColor(0xff0000);
      g.fillRect(0, 0, 2, 154);
      g.fillRect(0, 0, 98, 5);
      g.fillRect(98, 0, 2, 154);
      g.fillRect(0, 154, 100, 5);
         
      for(int i=0;i<(tetrisMAX[MAXType1][rotation].length/2);i++)
      {
        g.setColor(colors[MAXType1+1]);
        g.fillRect(2+8*(tetrisMAX[MAXType1][rotation][maxisX]+moveX),8*(tetrisMAX[MAXType1][rotation][maxisY]+moveY),8,8);
        g.setColor(0);
        g.drawRect(2+8*(tetrisMAX[MAXType1][rotation][maxisX]+moveX),8*(tetrisMAX[MAXType1][rotation][maxisY]+moveY),8,8);
        gameMatrix[tetrisMAX[MAXType1][rotation][maxisX]+moveX][tetrisMAX[MAXType1][rotation][maxisY]+moveY-1]=MAXType1+1;
        maxisX=maxisX+2;
        maxisY=maxisY+2;
      }

      for(int i=0;i<(tetrisMAX[MAXType2][0].length/2);i++)
      {
        g.setColor(colors[MAXType2+1]);
        g.fillRect(2+8*(tetrisMAX[MAXType2][0][maxisX2]+16),8*(tetrisMAX[MAXType2][0][maxisY2]+2),8,8);
        g.setColor(0);
        g.drawRect(2+8*(tetrisMAX[MAXType2][0][maxisX2]+16),8*(tetrisMAX[MAXType2][0][maxisY2]+2),8,8);
        maxisX2=maxisX2+2;
        maxisY2=maxisY2+2;
      }

      for(int j=0;j<12;j++)
        for(int k=0;k<18;k++)          
          if(keepMatrix[j][k]!=0)
          {
            g.setColor(colors[keepMatrix[j][k]]);
            g.fillRect(2+8*j, 8*(k+1), 8, 8);
            g.setColor(0);
            g.drawRect(2+8*j, 8*(k+1), 8, 8); 
          }

      g.setColor(0x0000ff);
      if(showScore)
      {
         g.drawString(String.valueOf(blockScore), 40, 80, Graphics.TOP|Graphics.LEFT);
         scoreEnd();
      } 
      g.drawString("羆だ:", 120, 60, Graphics.TOP|Graphics.LEFT); 
      g.drawString(String.valueOf(scoreTotal), 170, 80, Graphics.TOP|Graphics.RIGHT);
      checkBlock();
   }

   private void paintView20(Graphics g)
   {
      init();
      g.drawImage(firstImage,0,0, Graphics.TOP|Graphics.LEFT);
      g.setColor(0xff0000);
      g.fillRect(0, 0, 2, 154);
      g.fillRect(0, 0, 98, 5);
      g.fillRect(98, 0, 2, 154);
      g.fillRect(0, 154, 100, 5);
      for(int i=0;i<(tetrisMAX[MAXType2][0].length/2);i++)
      {
        g.setColor(colors[MAXType2+1]);
        g.fillRect(2+8*(tetrisMAX[MAXType2][0][maxisX2]+16),8*(tetrisMAX[MAXType2][0][maxisY2]+2),8,8);
        g.setColor(0);
        g.drawRect(2+8*(tetrisMAX[MAXType2][0][maxisX2]+16),8*(tetrisMAX[MAXType2][0][maxisY2]+2),8,8);
        maxisX2=maxisX2+2;
        maxisY2=maxisY2+2;
      }

      for(int j=0;j<12;j++)
        for(int k=0;k<18;k++)          
          if(keepMatrix[j][k]!=0)
          {
            g.setColor(colors[keepMatrix[j][k]]);
            g.fillRect(2+8*j, 8*(k+1), 8, 8);
            g.setColor(0);
            g.drawRect(2+8*j, 8*(k+1), 8, 8); 
          }
      
      g.drawString("羆だ:", 120, 60, Graphics.TOP|Graphics.LEFT); 
      g.drawString(String.valueOf(scoreTotal), 170, 80, Graphics.TOP|Graphics.RIGHT);
      g.setColor(0x800080);
      g.drawString("笴栏挡

⌨️ 快捷键说明

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