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

📄 playcanvas.java

📁 用J2ME写的一个益智类手机游戏。玩家回答问题
💻 JAVA
字号:
package mindsurf;

import java.util.*;
import javax.microedition.lcdui.*;
import com.nokia.mid.ui.FullCanvas;

//游戏画面
public class PlayCanvas extends FullCanvas{
  Display display;
  private Image play[]=new Image[7];
  private int tubindex=0;
  private int yunindex[]=new int[3];
  private int a[]={0,1,2,3,4,5,6,21,10,9,7,8,13,12,11,22,16,17,15,19,14,18,23};
  private int change=0;
  private int counter=0;
  private int count=0;
  private int shaizi=0;
  private int shaiziRandom=0;
  public static int color=0;
  public static int para=0;
  private int wenzicount=0;
  private int i=0;
  private int nextindex=0;
  private String type;
  private boolean yunflag=false;
  private boolean yunstop=true;
  private boolean anjian=true;
  private boolean flag=false;
  private boolean ren=true;
  private boolean b=true;
  private boolean c=true;
  private boolean dati=false;
  private boolean tixing=false;
  private Random random=new Random();
  private Timer timer;

  public PlayCanvas(Display display) {
    this.display=display;
    try{
      play[0]=Image.createImage("/res/school.png");
      play[1]=Image.createImage("/res/tubiao3s.png");
      play[2]=Image.createImage("/res/yun.png");
      play[3]=Image.createImage("/res/fangge.png");
      play[4]=Image.createImage("/res/shaizi.png");
      play[5]=Image.createImage("/res/xscm.png");
      play[6]=Image.createImage("/res/wenzi.png");
   }catch(Exception e){}
   //传递点数总和画题型方格
   change=IqMIDlet.dianshu;
   timer = new Timer();
   timer.schedule(new yunTask(),0,50);
   timer.schedule(new yunchaTask(),0,50);
   timer.schedule(new shaiziTask(),0,60);
   timer.schedule(new dituTask(),0,600);
   timer.schedule(new renTask(),0,300);
   timer.schedule(new nextTask(),0,1000);
  }

  protected void paint(Graphics g) {
    //设字体
    Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    g.setFont(f);
    g.setColor(255,255,255);
    //画背景
    g.drawImage(play[0], 0, 0, Graphics.LEFT | Graphics.TOP);
    g.drawString("xx",90,51,Graphics.LEFT | Graphics.TOP);
    if (change < 7){
      tubindex = 0;
      g.drawString("小学", 103, 50, Graphics.LEFT | Graphics.TOP);
    }
    if (change >= 7 && change < 15){
      tubindex = 1;
      g.drawString("中学", 103, 50, Graphics.LEFT | Graphics.TOP);
    }
    if (change >= 15 && change < 23){
      tubindex = 2;
      g.drawString("大学", 103, 50, Graphics.LEFT | Graphics.TOP);
    }
    for (int m = 0; m < 2; m++) {
        for (int n = 0; n < 4; n++) {
          g.setClip(n*32,64+m*32,32,32);
          g.drawImage(play[1], n * 32-tubindex*32, 64 + m * 32,
                      Graphics.LEFT | Graphics.TOP);
        }
      }

    //云动画
    g.setClip(0,0,128,20);
    g.drawImage(play[2], -128 + yunindex[0], 5, Graphics.LEFT | Graphics.TOP);
    if (yunflag) {
      g.drawImage(play[2], -128 + yunindex[1], 5, Graphics.LEFT | Graphics.TOP);
    }else {
      g.drawImage(play[2], 0 + yunindex[2], 5, Graphics.LEFT | Graphics.TOP);
    }

   //count=0状态拼地图count>0状态地图移动
    for(int i=0;i<a.length;i++){
      if(a[i]==0){
        g.setClip(32 + i * 32 - count * 32 - change * 32,93,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32, 93,
                    Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==1||a[i]==7||a[i]==14){
        g.setClip(32 + i * 32 - count * 32 - change * 32,88,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-32, 88,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==2||a[i]==8||a[i]==15){
        g.setClip(32 + i * 32 - count * 32 - change * 32,93,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-64, 93,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==3||a[i]==9||a[i]==16){
        g.setClip(32 + i * 32 - count * 32 - change * 32,88,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-96, 88,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==4||a[i]==10||a[i]==17){
        g.setClip(32 + i * 32 - count * 32 - change * 32,93,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-128, 93,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==5||a[i]==11){
        g.setClip(32 + i * 32 - count * 32 - change * 32,93,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-160, 93,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==6||a[i]==12||a[i]==18){
        g.setClip(32 + i * 32 - count * 32 - change * 32,88,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-192, 88,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==13||a[i]==19){
        g.setClip(32 + i * 32 - count * 32 - change * 32,88,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-224, 88,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==21||a[i]==22){
        g.setClip(32 + i * 32 - count * 32 - change * 32,93,32,32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32-256, 93,
                     Graphics.LEFT | Graphics.TOP);
      }
      if(a[i]==23){
        g.setClip(32 + i * 32 - count * 32 - change * 32, 93, 32, 32);
        g.drawImage(play[3], 32 + i * 32 - count * 32 - change * 32 - 288, 93,
                    Graphics.LEFT | Graphics.TOP);
        }
      }

      //shaizi动画
        switch(shaiziRandom){
          case 1: g.setClip(56,45,15,18);
                  g.drawImage(play[4],56,45,Graphics.LEFT | Graphics.TOP);
                  break;
          case 2: g.setClip(56,45,15,18);
                  g.drawImage(play[4],41,45,Graphics.LEFT | Graphics.TOP);
                  break;
          case 3: g.setClip(56,45,15,18);
                  g.drawImage(play[4],26,45,Graphics.LEFT | Graphics.TOP);
                  break;
        }

    //人物动画
    g.setClip(32,60,24,50);
    if(ren){
      g.drawImage(play[5], 34, 70, Graphics.LEFT | Graphics.TOP);
    }else{
      g.drawImage(play[5], 34, 60, Graphics.LEFT | Graphics.TOP);
    }

  //文字显示
    if(dati&&tixing==false){
      g.setClip(0,0,128,128);
      g.setColor(0x767676);
      g.fillRoundRect(28, 8, 80, 20, 8, 8);
      g.setColor(255, 255, 255);
      g.fillRoundRect(24, 5, 80, 20, 8, 8);
      if (wenzicount == 1) {
        g.setClip(24, 5, 80, 20);
        g.drawImage(play[6], 24, 5, Graphics.LEFT | Graphics.TOP);
      }
      if (wenzicount == 2) {
        g.setClip(24, 5, 80, 20);
        g.drawImage(play[6], -56, 5, Graphics.LEFT | Graphics.TOP);
      }
    }

    //题型显示
     g.setClip(0,0,128,128);
     if(tixing){
       g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
       g.setColor(255,255,255);
       g.fillRect(2,18,10,10);
       g.drawString(type,15,16,Graphics.LEFT | Graphics.TOP);
       g.setColor(0,0,0);
       g.setFont(Font.getDefaultFont());
     }
  }

  public void keyPressed(int keyCode){
    if (anjian) {
        if (keyCode == Canvas.KEY_NUM5) {
          //产生随机点数
          shaiziRandom = Math.abs(random.nextInt()) % 3 + 1;
          color = shaiziRandom;
          repaint();
          //跳走规则
          if (change < 7 && change > 3) {
            if (shaiziRandom > Math.abs(7 - change)) {
              counter = Math.abs(7 - change);
            }
            else {
              counter = shaiziRandom;
            }
          }
          else if (change < 15 && change > 11) {
            if (shaiziRandom > Math.abs(15 - change)) {
              counter = Math.abs(15 - change);
            }
            else {
              counter = shaiziRandom;
            }
          }
          else if (change < 22 && change > 17) {
            if (shaiziRandom > Math.abs(22 - change)) {
              counter = Math.abs(22 - change);
            }
            else {
              counter = shaiziRandom;
            }
          }
          else {
            counter = shaiziRandom;
          }
          //将点数总和传递给IqMIDlet.dianshu
          IqMIDlet.dianshu = counter + IqMIDlet.dianshu;
          //按键失效
          anjian = !anjian;
          flag = true;
          }
         //题型
          para = a[IqMIDlet.dianshu];
          if(para==1||para==7||para==14)
            type="真假";
          if(para==2||para==8||para==15)
            type="数学";
          if(para==3||para==9||para==16)
            type="音乐";
          if(para==4||para==10||para==17)
            type="社会";
          if(para==5||para==11)
            type="语文";
          if(para==6||para==12||para==18)
            type="课外";
          if(para==13||para==19)
            type="理科";
          if (para == 21 || para== 22)
            wenzicount = 1;
          if (para == 23)
            wenzicount = 2;
          repaint();
        }
  }

  //2张云图轮流在画面中移动
  public class yunTask extends TimerTask{
     public void run() {
     if(yunstop==true){
       //第一张云图走过256祯后在回到第一祯
       if(yunindex[0]==256){
         yunindex[0]=0;
       }else{
         yunindex[0]++;
       }
       repaint();
       //第一张云图移动128祯后第二张云图开始移动
        if(yunflag){
          //第二张云图走过256祯后在回到第一祯
          if (yunindex[1] == 256) {
           yunindex[1] = 0;
         }else {
           yunindex[1]++;
         }
         repaint();
          }
         }
       }
   }

 //2张云图时差128祯
  public class yunchaTask extends TimerTask{
    public void run() {
      if(yunstop==true){
      if (yunflag ==false) {
        if (yunindex[2] == 128) {
          yunflag = true;
        }
        else {
          yunindex[2] ++;
        }
        repaint();
        }
      }
    }
 }

  //shaizi循环变化
  public class shaiziTask extends TimerTask{
    public void run() {
      if (flag == false) {
        if (shaiziRandom == 3) {
          shaiziRandom = 1;
        }
        else {
          shaiziRandom++;
        }
        repaint();
      }
    }
  }

  //方格移动动画,移动counter格
  public class dituTask extends TimerTask{
    public void run(){
      if(flag&&b){
        if(count==counter){
          b=!b;
        }else{
          count++;
        }
        repaint();
    }
   }
  }

 //人物动画
  public class renTask extends TimerTask{
   public void run(){
     if(flag&&c){
       if(i==2*counter){
         c=!c;
         //云动画线程停下
         yunstop=false;
         //IqMIDlet.dianshu == 7 ,15,22显示考试文字
         if (IqMIDlet.dianshu == 7 || IqMIDlet.dianshu == 15 || IqMIDlet.dianshu == 22) {
           dati=!dati;
         }else{
           //显示题型
           tixing=!tixing;
           dati=!dati;
         }
       }else{
         i++;
         ren=!ren;
       }
       repaint();
   }
  }
 }

 //延时后切换画面
  public class nextTask extends TimerTask{
    public void run(){
      if(dati==true){
        if (nextindex == 1){
          play=null;
          timer.cancel();
          timer = null;
          IqMIDlet.questionflag=true;
          display.setCurrent(new LoadingCanvas(display));
          System.gc();
        }else{
          nextindex++;
        }
      }
    }
  }
}

⌨️ 快捷键说明

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