| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1181 人关注过本帖
标题:请各位大虾帮忙该一个程序
只看楼主 加入收藏
fangfangff
Rank: 1
等 级:新手上路
威 望:2
帖 子:479
专家分:0
注 册:2006-12-22
收藏
 问题点数:0 回复次数:3 
请各位大虾帮忙该一个程序
使用低级UI知识,设计一个小程序,实现一个小球在屏幕上运动的效果,要求如下:
?    初始状态下小球就处于运动状态,
?    当按下上下左右键时小球会向着对应的方向运动,
?    当小球撞到屏幕边框的时候停止运动。

  我写的代码如下:

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;


public class Gamecanvas extends GameCanvas implements Runnable {
    private boolean isPlay;    //isPlay为真时游戏线程开始运行
    private long delay;         //线程保持的时间
    private int currentX,currentY;   //当前的坐标
    private int width;          //屏幕宽度
    private int height;          //屏幕高度

    public Gamecanvas() {
        // TODO 自动生成构造函数存根
        super(true);               //抑制键盘
        width = getWidth();         //获取屏幕宽度
        height = getHeight();       //获取屏幕高度
        currentX = width / 2;       //设置球的当前位置X
        currentY = height / 2;      //设置球的当前位置Y
        delay = 20;
    }
   
    public void start()
    {
        isPlay = true;
        Thread t = new Thread(this);      //创建一个线程实例
        t.start();                         //启动线程
    }
    
    public void stop()
    {
        isPlay = false;
    }
    
    public void run()                //游戏循环
    {
        Graphics g = getGraphics();    //获取对象
        while (isPlay == true)
        {
            input();                   //键盘输入
            draw(g);                  //绘制画布
            try{
                Thread.sleep(delay);
            }
            catch(InterruptedException ie){}
        }
    }
    
    private void input()
    {
        int keyStates = getKeyStates();      //获取键盘状态
        if((keyStates & LEFT_PRESSED)!=0)         //按下左键
            currentX = Math.max(10, currentX-1);   
        if((keyStates & RIGHT_PRESSED)!=0)        //按下右键
            if(currentX + 5 < width)
                currentX = Math.min(width-10, currentX + 1);
        if((keyStates & UP_PRESSED)!=0)           //按下上键
            currentY = Math.max(10, currentY-1);
        if((keyStates & DOWN_PRESSED)!=0)         //按下下键
            if(currentY + 10 < height)
                currentY = Math.min(height-10, currentY + 1);
    }
    
    private void draw(Graphics g)
    {
        g.setColor(0xffffff);                 //设置颜色为白色
        g.fillRect(0, 0, width, height);       //填充整个屏幕
        g.setColor(0xff00ff);                 //颜色设置为紫色
        g.fillArc(currentX-10, currentY -10, 20, 20, 0, 360);   //绘制小球
        flushGraphics();
    }
    

}


import javax.microedition.
import javax.microedition.
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class GameCanvasMIDlet extends MIDlet implements CommandListener {
    private Gamecanvas gameCanvas;        //声明游戏画布对象
    private Command exitCmd = new Command("Exit",Command.SCREEN,1); //退出

    public GameCanvasMIDlet() {
        // TODO 自动生成构造函数存根
        gameCanvas = new Gamecanvas();    //创建游戏画布对象
        gameCanvas.addCommand(exitCmd);   //添加退出键
        gameCanvas.setCommandListener(this);  //监听事件
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO 自动生成方法存根

    }

    protected void pauseApp() {
        // TODO 自动生成方法存根

    }

    protected void startApp() throws MIDletStateChangeException {
        gameCanvas.start();          //启动游戏线程
        Display.getDisplay(this).setCurrent(gameCanvas);  //显示画布
        // TODO 自动生成方法存根
        
    }

    public void commandAction(Command cmd, Displayable disp) {
        // TODO 自动生成方法存根
        if(cmd == exitCmd)       //退出的判断
        {
            System.gc();
            try {
                destroyApp(false);
            } catch (MIDletStateChangeException e) {
                // TODO 自动生成 catch 块
                e.printStackTrace();
            }
            notifyDestroyed();
        }

    }

}


偶写的代码只是可以通过方向键来控制球的运动方向而已
不能实现刚运行时小球就开始运动,也不能实现碰到屏幕边框
时暂停运动,麻烦各位大虾帮忙改一下好吗???
千里冰封哥哥,帮忙改一下好吗??
要你帮偶写一个也可以,谢谢哈~~~~~~~~~~~~~~~``
谢谢各位哈```````
搜索更多相关主题的帖子: 大虾 import 小球 javax 
2008-05-02 19:14
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
帮你改好了,开始是四个方向里面随机一个方向,然后会一直往一个方向走,直到你按别的方向键为止.
停止了以后,你按别的方向的键一样会继续动起来.
import java.util.Random;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;

public class Gamecanvas extends GameCanvas implements Runnable {

    private boolean isPlay;    //isPlay为真时游戏线程开始运行
    private int currentX,  currentY;   //当前的坐标
    private int width;          //屏幕宽度
    private int height;          //屏幕高度
    //----增加的变量
    private int direction;//方向
    public static final int D_LEFT = 0;
    public static final int D_RIGHT = 1;
    public static final int D_UP = 2;
    public static final int D_DOWN = 3;
    private static final int BALL_WIDTH = 20;//球的宽度
    private static final int ADDS = 2;//一次增加的象素
    private static final int SLEEP_TIME = 20;//睡眠的时间

    public Gamecanvas() {
        // TODO 自动生成构造函数存根
        super(true);               //抑制键盘
        width = getWidth();         //获取屏幕宽度
        height = getHeight();       //获取屏幕高度
        currentX = (width - BALL_WIDTH) / 2;       //设置球的当前位置X
        currentY = (height - BALL_WIDTH) / 2;      //设置球的当前位置Y
        direction = new Random().nextInt(4);
    }

    public void start() {
        isPlay = true;
        Thread t = new Thread(this);      //创建一个线程实例
        t.start();                         //启动线程

    }

    public void stop() {
        isPlay = false;
    }

    public void run() //游戏循环
    {
        Graphics g = getGraphics();    //获取对象
        while (isPlay == true) {
            input();                   //键盘输入
            calculateXY();//计算座标
            draw(g);                  //绘制画布
            try {
                Thread.sleep(SLEEP_TIME);
            } catch (InterruptedException ie) {
            }
        }
    }

    private void input() {
        int keyStates = getKeyStates();      //获取键盘状态
        if ((keyStates & LEFT_PRESSED) != 0) //按下左键
        {
            direction = D_LEFT;
        }
        if ((keyStates & RIGHT_PRESSED) != 0) //按下右键
        {
            direction = D_RIGHT;
        }
        if ((keyStates & UP_PRESSED) != 0) //按下上键
        {
            direction = D_UP;
        }
        if ((keyStates & DOWN_PRESSED) != 0) //按下下键
        {
            direction = D_DOWN;
        }
    }

    /**
     * 计算小球的XY的座标
     */
    private void calculateXY() {

        switch (direction) {
            case D_LEFT:
                currentX -= ADDS;
                break;
            case D_RIGHT:
                currentX += ADDS;
                break;
            case D_UP:
                currentY -= ADDS;
                break;
            case D_DOWN:
                currentY += ADDS;
        }
        //计算边缘
        if (currentX < 0) {
            currentX = 0;
        } else if (currentX + BALL_WIDTH > width) {
            currentX = width - BALL_WIDTH;
        }
        if (currentY < 0) {
            currentY = 0;
        } else if (currentY + BALL_WIDTH > height) {
            currentY = height - BALL_WIDTH;
        }
    }

    private void draw(Graphics g) {
        g.setColor(0xffffff);                 //设置颜色为白色
        g.fillRect(0, 0, width, height);       //填充整个屏幕
        g.setColor(0xff00ff);                 //颜色设置为紫色
        g.fillArc(currentX, currentY, BALL_WIDTH, BALL_WIDTH, 0, 360);   //绘制小球
        flushGraphics();
    }
}

可惜不是你,陪我到最后
2008-05-04 22:00
fangfangff
Rank: 1
等 级:新手上路
威 望:2
帖 子:479
专家分:0
注 册:2006-12-22
收藏
得分:0 

千里冰封哥哥
你真是太强了啊~~~~~~~~`
才几分钟就帮偶改好了
真是太神奇了~~~~~~~~~~~`
谢谢你哈````````

千里冰封---My Love 尽管相隔千里 , 依然拥有冰封
2008-05-05 16:56
lh15871815717
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-5-26
收藏
得分:0 
这是一个很简单的问题好不?
我晕,你学J2SE没有啊!
2008-05-26 20:56
快速回复:请各位大虾帮忙该一个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047286 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved