中间人 发表于 2009-1-16 09:43:18

给大家共享点关于J2ME的东西

学习J2ME有一个星期了,收获不小!   开发出自己的手机游戏来,感觉很不错!就想到了和大家分享一下子,怎样,够意思吧!   撞球游戏,这个不是最终的,因为最终的在我的笔记本里,这是U盘里的!   学过J2ME 的人都知道J2ME的代码不是很好移动的!
    package com.li;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    public class test extends MIDlet implements CommandListener
    {
   private TestCanvas tc;
      private Display display;
      private Command cmd1;
      private Command cmd2;
      private Command aOK;
      private Command aEXIT;
      private boolean isRun;
      private boolean isOver = false,isOver2 = true;
      private Form form = null;
      private Alert alert =null;
      private TestCanvas _tc = new TestCanvas();
      public test()
      {
         alert = new Alert("游戏说明","这是一个小小游戏,用档板去接小球,每接一次就是10分" "\n" "每100分就进入下一关卡.操作说明:数字4,6或者方向键左右!",null,AlertType.INFO);
         form = new Form("小游戏");
            isRun = true;
            cmd1 = new Command("开始", 4, 1);
            cmd2 = new Command("退出", 7, 2);
            aOK = new Command("重新开始", 4, 1);
            aEXIT = new Command("退出", 7, 1);
      }
      public void nofifystart()
      {
      }
      protected void destroyApp(boolean flag1)
            throws MIDletStateChangeException
      {
      }
      protected void pauseApp()
      {
      }
      protected void startApp()
            throws MIDletStateChangeException
      {
         alert.addCommand(cmd1);
         alert.addCommand(cmd2);
         alert.setCommandListener(this);
            display = Display.getDisplay(this);
            display.setCurrent(alert,form);
      }
      class TestCanvas extends Canvas implements Runnable,CommandListener
      {
         private Command isOK;
         private Command isEXIT,isHelp;
            private int x;
            private int y;
            private int xSign;
            private int ySign;
            private int w;
            private int h;
            private int _x;
            private float score;
            public TestCanvas()
            {
             isOK = new Command("开始游戏",Command.OK,1);
             isEXIT = new Command("暂停游戏游戏",Command.EXIT,1);
             isHelp = new Command("帮助",Command.HELP,1);
                score = 0.0F;
                _x = 0;
                w = 10;
                h = 10;
                xSign = 0;
                ySign = 0;
                this.Menu();
            }
            public void Menu()
            {
             this.addCommand(isOK);
                this.addCommand(isEXIT);
                this.addCommand(isHelp);
                this.setCommandListener(this);
                this.setFullScreenMode(true);
            }
            protected void paint(Graphics g)
            {
                g.setColor(0, 215, 10);
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(100, 125, 150);
                g.fillArc(x, y, w, h, 0, 360);
                g.setColor(0xffffff);
                g.fillRect(_x   100, 310, 50, 319);
                g.setColor(255, 255, 102);
                g.setFont(Font.getFont(32, 1, 16));
                g.drawString("你的积分是:"   score, 50, 0, 20);
                g = null;
                System.gc();
            }
   
               
            public void run()
            {
                do
                {
                  repaint();
                  isOver2 = false;
                  x= xSign;
                  y= ySign;
                  if(x >= getWidth() - w)
                  {
                        x = getWidth() - w;
                        xSign = -1;
                  }
                  if(y >= getHeight() - h)
                  {
                        y = getHeight() - h;
                        ySign = -1;
                  }
                  if(x <= 0)
                  {
                        x = 0;
                        xSign = 1;
                  }
                  if(y <= 0)
                  {
                        y = 0;
                        ySign = 1;
                  }
                  if(y == 300)
                        if(x >= _x   100 && x <= _x   100   50)
                        {
                            ySign = -1;
                            score= 10F;
                        } else
                        {
                            String info = "恭喜您! 你成为本年度最笨的人,你的积分为:";
                            String info2 = "努力,加油! 再接再厉!!!   你的积分为:";
                            Form form = new Form("游戏结束!");
                            isOver = true;
                            form.addCommand(aOK);
                            form.addCommand(aEXIT);
                            form.setCommandListener(this);
                            if(score <= 100F)
                              form.append(info   score);
                            else
                              form.append(info2   score);
                                 display.setCurrent(form);
                                 isRun = false;
                        }
                  try
                  {
                        Thread.sleep(10L);
                  }
                  catch(Exception e)
                  {
                        e.printStackTrace();
                  }
                } while(isRun);
            }
            protected void keyPressed(int keyCode)
            {
             //System.out.println(getKeyName(keyCode));
                if(getKeyName(keyCode).equals("4")||getKeyName(keyCode).equals("Left Arrow"))
                {
                  if(_x   100 > 0)
                        _x -= 20;
                } else
                if(getKeyName(keyCode).equals("6") && _x < 90||getKeyName(keyCode).equals("Right Arrow")&& _x < 90)
                  _x= 20;
            }
            public void commandAction(Command c, Displayable arg1)
            {
                if(c == aOK)
                {
               display.setCurrent(_tc);
                  new Thread(_tc).start();
                }
                if(c == aEXIT)
                  notifyDestroyed();
                if(c == isOK){
               if(isOver == false&&isOver2 == true)
               {
                  isRun = true;
                        display.setCurrent(tc);
                        new Thread(tc).start();
               }
                }
                if(c == isEXIT)
                {
               isRun = false;
                }
            }
      }
   public void commandAction(Command c, Displayable arg1) {
      // TODO 自动生成方法存根
      if(c == cmd1)
      {
       tc = new TestCanvas();
       display.setCurrent(tc);
      }
   }
    }

   上一页
页: [1]
查看完整版本: 给大家共享点关于J2ME的东西