站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→java小游戏

java小游戏

发表新主题   回复此主题

第1楼 2007-06-22 01:16 花开花落 写道:

java小游戏

怎样使下面代码里的car能够上下移动(现在这段代码只能实现左右移动) 附从网上下载的源代码
package game;
import java.util.Timer;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;


public class GameCtrl extends Canvas implements CommandListener{
private final Command startCommand;
private final Command quitCommand;
private final RacingMIDlet midlet;
private Graphics graph;
private Timer timer = new Timer();
private NextFrame nextFrame;

// 炸弹图象
public Image bombImage;

// 赛车图象
public Image carImage;

// 屏幕尺寸
public int width = 0;
public int height = 0;


// 赛车水平位置
public int carPos = 50;

// 炸弹位置
public int[] bombPosX = {0, 0, 0, 0};
public int[] bombPosY = {0, 0, 0, 0};

// 炸弹是否出界
public boolean[] bombCanUse = {false, false, false, false};

// 游戏结束标志
public boolean isGameOver = false;

// 游戏开始标志
public boolean isGameRun = false;

// 游戏积分
public int score = 0;


public GameCtrl(RacingMIDlet midlet) {
super();

// 保存MIDlet类对象
this.midlet = midlet;

// 得到屏幕尺寸
width = getWidth();
height = getHeight();

try {
// 装载炸弹图象
bombImage = Image.createImage("/bomb.png");

// 装载赛车图象
carImage = Image.createImage("/car.png");
}catch(Exception e) {}

// 添加命令按键
quitCommand = new Command("退出", Command.EXIT, 2);
addCommand(quitCommand);

startCommand = new Command("开始", Command.OK, 1);
addCommand(startCommand);

// 侦听按键响应
setCommandListener(this);
}


protected void paint(Graphics g) {
graph = g;
if (isGameOver == true && isGameRun == true){
isGameRun = false;

// 显示logo
Alert result = new Alert("本局积分", String.valueOf(score), null, AlertType.INFO);

// 延迟4秒
result.setTimeout(2000);

// 显示闪屏界面
midlet.display.setCurrent(result, this);
return;
}

if (isGameRun == true){
// 白色清空画布
graph.setColor(255, 255, 255);
graph.fillRect(0, 0, width, height);

// 绘制赛车
g.drawImage(carImage, carPos, height - carImage.getHeight(), Graphics.HCENTER | Graphics.VCENTER);

// 绘制炸弹
for (int i = 0; i < 14; i++){
if (bombCanUse[i] == true)
g.drawImage(bombImage, bombPosX[i], bombPosY[i], Graphics.HCENTER | Graphics.VCENTER);
}
}
}


public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == startCommand){
// 用户开始游戏
initialize();
}

if (arg0 == quitCommand){
if (isGameRun == true){
// 结束游戏
isGameOver = true;
isGameRun = false;

// 关闭定时器
nextFrame.cancel();
}

// 用户退出游戏
try{
midlet.quit();
}
catch(MIDletStateChangeExcept

第2楼 2013-08-31 12:44 Robot :

java小游戏 相关


第3楼 2007-06-23 15:00 Kimmy★Piggy 写道:

已经通过邮件答复你了~

发表新主题   回复此主题