已锁定- -| 回首页 | 2005年索引 | - -[原创]用J2ME编写的一个拼图游戏

[原创]用J2ME编写的可以控制移动的方块

关键词J2ME    TiledLayer    Sprite    GameCanvas    GAME                                          

--sunfruit

    用J2ME编写的可以控制移动的方块

    提供源代码下载

    版本信息
        WTK2.2
        MIDP2.0
        CLDC 1.1
    功能简介:
        可以通过方向键控制移动的方块
    
    欢迎大家提意见,交流

    源代码如下

//////////////////////////////////第一个类//////////////////////////////////////
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;

/**
 *

Title:


 *
 *

Description:


 *
 *

Copyright: Copyright (c) 2005


 *
 *

Company:


 *
 * @author not attributable
 * @version 1.0
 */
public class Draw extends MIDlet {

    private Display display=null;

    private DrawGameCanva drawGameCanva=null;

    //private Thread threadGameCanva=new Thread(drawGameCanva);

    public Draw() {
        display=Display.getDisplay(this);
        drawGameCanva = new DrawGameCanva();
        new Thread(drawGameCanva).start();
        display.setCurrent(drawGameCanva);
    }

    /**
     * destroyApp
     *
     * @param _boolean boolean
     * @throws MIDletStateChangeException
     * @todo Implement this javax.microedition.midlet.MIDlet method
     */
    protected void destroyApp(boolean _boolean) throws
            MIDletStateChangeException {
    }

    /**
     * pauseApp
     *
     * @todo Implement this javax.microedition.midlet.MIDlet method
     */
    protected void pauseApp() {
    }

    /**
     * startApp
     *
     * @throws MIDletStateChangeException
     * @todo Implement this javax.microedition.midlet.MIDlet method
     */
    protected void startApp() throws MIDletStateChangeException {
    }
}

//////////////////////////////////第二个类//////////////////////////////////////

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;

/**
 *

Title:


 *
 *

Description:


 *
 *

Copyright: Copyright (c) 2005


 *
 *

Company:


 *
 * @author not attributable
 * @version 1.0
 */
public class DrawGameCanva extends GameCanvas implements Runnable {
    Graphics g = null;
    int positionX = 0;
    int positionY = 0;
    DrawColor drawColor = null;
    public DrawGameCanva() {
        super(false);
        g = getGraphics();
        drawColor = new DrawColor(g);
    }

    public void run() {
        while (true) {
            int keyState = this.getKeyStates();
            //System.out.println(keyState);
            if ((keyState & LEFT_PRESSED) != 0) {
                positionX--;
                System.out.println(positionX);
            } else if ((keyState & RIGHT_PRESSED) != 0) {
                positionX++;
                System.out.println(positionX);
            } else if ((keyState & DOWN_PRESSED) != 0) {
                positionY++;
                System.out.println(positionY);
            } else if ((keyState & UP_PRESSED) != 0) {
                positionY--;
                System.out.println(positionY);
            }
            drawColor.move(positionX, positionY);
            try {
                Thread.sleep(20);
            } catch (InterruptedException ie) {}
        }
    }

    class DrawColor {
        private Graphics g = null;
        private int i = 10;
        private int positionX = 0;
        private int positionY = 0;
        public DrawColor(Graphics g) {
            this.g = g;
        }

        public void move(int positionX1, int positionY1) {
            g.setColor(255, 255, 255);
            g.fillRect(positionX, positionY, i, i);
            this.positionX = positionX1;
            this.positionY = positionY1;
            g.setColor(0, 0, 0);
            g.fillRect(positionX, positionY, i, i);
            flushGraphics();
        }

    }
}

【作者: sunfruit】【访问统计:】【2005年12月1日 星期四 09:40】【 加入博采】【打印

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=3718475

博客手拉手

[2005-11-25 17:27:54.0]    KXML:J2ME中的XML语法分析利器

[2005-11-25 17:29:44.0]    J2ME Wireless Toolkit 2.2 发行版下载

[2005-11-24 11:05:40.0]    Java 3D手机游戏MetalBeasts被Mobile3DGames.com列在首位!

[2005-11-18 09:38:53.0]    J2ME程序开发全方位讲解汇总

[2005-11-18 09:39:28.0]    J2ME程序开发全方位讲解汇总

回复

评论内容: