请各位大虾帮忙该一个程序
使用低级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.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
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 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();
}
}
----------------解决方案--------------------------------------------------------
哇
千里冰封哥哥
你真是太强了啊~~~~~~~~`
才几分钟就帮偶改好了
真是太神奇了~~~~~~~~~~~`
谢谢你哈````````
----------------解决方案--------------------------------------------------------
这是一个很简单的问题好不?
我晕,你学J2SE没有啊! ----------------解决方案--------------------------------------------------------