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

📄 gamecanvas.java

📁 一本介绍手机游戏开发的书中的源代码
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;

public class gameCanvas extends Canvas
{
  private final j2meAI j2meai;
  private static int UpDown, LeftRight;
  private int i, j, viewAI, picture, goX, goY, judgeGO, randNum;
  private boolean gameOver; 
  private Random gameRand;
  private runMouse runmouse;
  private finalRoad finalroad;
  private static Image[] AIImage=new Image[6];
  private int[][] gomap={{0,1,0,1,1,0,0,0,0,0},{0,1,0,0,1,1,1,0,1,0},{0,1,1,0,1,0,0,0,1,1},
                         {0,0,0,0,1,0,1,0,0,0},{0,1,0,1,1,0,1,1,1,0},{0,1,0,0,0,0,1,0,1,0},
                         {1,1,0,1,0,1,1,0,1,0},{0,0,0,1,0,0,0,0,0,1},{0,1,1,1,0,1,1,1,0,1},
                         {0,0,0,1,0,0,0,1,0,100}};

  public gameCanvas(j2meAI j2meai)
  {
    this.j2meai=j2meai;
    gameRand=new Random();
    try{
        AIImage[0]=Image.createImage("/wall.png");
        AIImage[1]=Image.createImage("/mouse1.png");
        AIImage[2]=Image.createImage("/mouse2.png");
        AIImage[3]=Image.createImage("/mouse3.png");
        AIImage[4]=Image.createImage("/mouse4.png");
        AIImage[5]=Image.createImage("/exit.png");
    }catch(Exception ex){}
    init();
  }

  public void keyReleased(int keyCode)
  {
       switch(keyCode)
       {
          case 50:  // '2'
               gameCanvas.UpDown=0;
               break;
          case 56:  // '8'
               gameCanvas.UpDown=0;
               break;
          case 52:  // '4'
               gameCanvas.LeftRight=0;
               break;
          case 54:  // '6'
               gameCanvas.LeftRight=0;
               break;
        }
  }

  public void keyPressed(int keyCode)
  {
     int gameKey=getGameAction(keyCode);
     if(viewAI==0)
         switch(gameKey)
         {
           case FIRE:  // '5'
                runmouse=new runMouse();
                runmouse.start();
                runmouse=null;
                break;
          }
     else if(viewAI==10)
         switch(gameKey)
         {
           case FIRE:  // '5'
                viewAI=15;
                init();
                finalroad=new finalRoad();
                finalroad.start();
                finalroad=null;
                break;
          }
  }

  public void paint(Graphics g)
  {
     paintView0(g);
     if(viewAI==0)
       paintView5(g);
     else if(viewAI==10)
       paintView10(g);
     else
       paintView15(g);
  }

  private void paintView0(Graphics g)
  {
     g.setColor(0xffffff);
     g.fillRect(0,0,getWidth(),getHeight());
     g.setColor(0xff0000);
     g.drawRect(4,3,170,170);
     for(i=0;i<10;i++)
       for(j=0;j<10;j++)
         if(gomap[i][j]==1)
            g.drawImage(AIImage[0], 4+j*17, 3+i*17, Graphics.LEFT | Graphics.TOP);
         else if(gomap[i][j]==99){
            g.setColor(0x00ff00);
            g.fillRect(4+17*j,3+17*i,17,17);
         }
     g.drawImage(AIImage[5], 4+9*17, 3+9*17, Graphics.LEFT | Graphics.TOP);
  }

  private void paintView5(Graphics g)
  {
     g.drawImage(AIImage[picture], 4+goX*17, 3+goY*17, Graphics.LEFT | Graphics.TOP);     
  }

  private void paintView10(Graphics g)
  {
     g.drawImage(AIImage[picture], 157, 156, Graphics.LEFT | Graphics.TOP);
     g.setColor(0x0000ff);
     g.fillRect(40,50,100,50);
     g.setColor(0xff0000);
     g.drawString("

⌨️ 快捷键说明

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