SVN8.COM - SVN中文技术网

投递文章 投稿指南 SVN中文技术网公告:技术交流诚聘优秀版主最新公告
搜索: 您的位置主页>JAVA技术>语言基础>最近在学习J2ME,传点东西来共享!

最近在学习J2ME,传点东西来共享!

SVN技术网 www.svn8.com 2008-06-04 07:09:36   来源:   作者:  评论:0 点击:
学习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);
  }
 }
}



技术交流 录入:SVN中文技术网[www.svn8.com]
Tags:  
责任编辑:
  • 请文明参与讨论,禁止漫骂攻击。 用户名:新注册) 密码: 匿名:
    评论总数:0 [ 查看全部 ] 网友评论
    关于我们 - 联系我们 - 广告服务 - RSS订阅 - 网站地图 - 返回顶部