📄 game.java
字号:
}
}
if (playerSprite.collidesWith(monster1Sprite[i], true)) {
// 播放一个乐音表示撞到怪
if (volume) {
try {
Manager.playTone(ToneControl.C4, 250, 100);
}
catch (MediaException me) {
}
}
if (ring == 3) {
ring1--;
}
else {
energy -= 4;
}
}
}
bossSprite.update();
if (playerSprite.collidesWith(bossSprite, true)) {
if (ring == 3) {
ring1--;
}
else {
energy -= 25;
}
}
// 检测游戏结束
if (energy <= 0 || goodsSaved == 2 || time <= 0) {
// 停止音乐
if (volume) {
try {
musicPlayer.stop();
}
catch (MediaException me) {
}
}
// 当玩家沉没时播放声音
try {
if (volume) {
if (energy <= 0 || time <= 0) {
gameoverPlayer.start();
}
else {
winPlayer.start();
}
}
}
catch (MediaException me) {
}
playerSprite.setVisible(false); // 隐藏玩家
if (goodsSaved == 3) {
pass++;
}
if (energy <= 0 || time <= 0 || (goodsSaved == 2 && pass == 5)) {
updateHiScores(); //更新最高得分
}
if (energy <= 0 || time <= 0) {
pass = 0; //重新初始化得分
}
gameOver = true;
}
}
}
private void draw(Graphics g) {
// 绘制包含能量和获救物品数目的信息栏
g.drawImage(infoBar, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setColor(0, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.setColor(255, 0, 0); // blue
g.fillRect(30, 3, energy, 12); //绘制生命进度条
layers.paint(g, 0, infoBar.getHeight()); // 绘制地图
g.setColor(250, 200, 0);
g.fillRect(0, SCREEN_HEIGHT - 8, SCREEN_WIDTH * time / 60000, SCREEN_HEIGHT); //绘制时间进度条
g.setColor(0, 0, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.drawString("生命:", 2, 1, Graphics.TOP | Graphics.LEFT);
g.drawString("物品获得: " + goodsSaved, 80, 1, Graphics.TOP | Graphics.LEFT);
g.drawString("时间", 0, SCREEN_HEIGHT - 14, Graphics.TOP | Graphics.LEFT);
//绘制跑动文字
if (positionX <= -300) {
rightforward = false;
}
if (positionX >= SCREEN_WIDTH) {
rightforward = true;
}
if (rightforward) {
positionX -= 5;
}
else {
positionX = SCREEN_WIDTH;
}
//显示文字
g.setColor(0, 0, 0);
g.drawString("第四届齐鲁软件大赛参赛作品 ~~~YTX设计团队~~~", positionX, positionY,
Graphics.LEFT | Graphics.TOP);
//失败
if (energy <= 0 || time <= 0) {
g.drawImage(death, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 30,
Graphics.VCENTER | Graphics.HCENTER);
}
//结束
g.setColor(255, 0, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_MEDIUM));
if (energy <= 0 || time <= 0 || (goodsSaved == 2 && pass == 5)) {
// 绘制游戏结束信息和分数
g.drawImage(over, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2,
Graphics.VCENTER | Graphics.HCENTER);
g.drawString("你的分数: " + score, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 30,
Graphics.TOP | Graphics.HCENTER);
}
//过关
if (goodsSaved == 2 && pass != 5) {
// 绘制游戏结束信息和分数
g.drawImage(win, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2,
Graphics.VCENTER | Graphics.HCENTER);
g.drawString("你的分数: " + score, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 30,
Graphics.TOP | Graphics.HCENTER);
}
//通关
if (goodsSaved == 2 && pass == 5) {
// 绘制游戏结束信息和分数
g.drawImage(sucess, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 20,
Graphics.VCENTER | Graphics.HCENTER);
g.drawString("快去挑战下一个模式吧 ", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 10,
Graphics.TOP | Graphics.HCENTER);
}
flushGraphics(); // 刷新屏幕外图形缓冲区
}
private void placeSprite(Sprite sprite, TiledLayer barrier) {
//初次尝试一个随机位置
randX = Math.abs(rand.nextInt() % barrier.getWidth()) - sprite.getWidth();
randY = Math.abs(rand.nextInt() % barrier.getHeight()) - sprite.getHeight();
sprite.setPosition(randX, randY);
// 重新置位直到没有冲突
while (sprite.collidesWith(barrier, true)) {
randX = Math.abs(rand.nextInt() % barrier.getWidth()) - sprite.getWidth();
randY = Math.abs(rand.nextInt() % barrier.getHeight()) - sprite.getHeight();
sprite.setPosition(randX, randY);
}
}
//用最近的游戏得分来更新最高得分列表
private void updateHiScores() {
// 察看当前得分是否需要进入最高得分列表
int i;
for (i = 0; i < 5; i++) {
if (score > hiScores[i]) {
break;
}
}
// 把当前得分插入到最高得分列表中
if (i < 5) {
for (int j = 4; j > i; j--) {
hiScores[j] = hiScores[j - 1];
}
hiScores[i] = score;
}
}
//把整数数组hiScores写入到HidScores记录存储中
private void writeHiScores() {
try {
RecordStore.deleteRecordStore("HiScores"); //删除前面的Hiscores记录存储
}
catch (Exception e) {
}
// 创建一个新的HiScores记录存储
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HiScores", true);
}
catch (Exception e) {
System.err.println("Failed creating hi score record store!");
}
// 写入得分
for (int i = 0; i < 5; i++) {
byte[] recordData = Integer.toString(hiScores[i]).getBytes(); // 格式化每个得分以便写入
try {
// 把得分作为记录写入
rs.addRecord(recordData, 0, recordData.length);
}
catch (Exception e) {
System.err.println("Failed writing hi scores!");
}
}
// 关闭记录存储
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("Failed closing hi score record store!");
}
}
private void readHiScores() {
// Open the hi scores record store
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HiScores", false);
}
catch (Exception e) {
}
if (rs != null) {
// Read the hi score records
try {
int len;
byte[] recordData = new byte[8];
for (int i = 1; i <= rs.getNumRecords(); i++) {
// Re-allocate record holder if necessary
if (rs.getRecordSize(i) > recordData.length) {
recordData = new byte[rs.getRecordSize(i)];
}
// Read the score and store it in the hi score array
len = rs.getRecord(i, recordData, 0);
hiScores[i - 1] = (Integer.parseInt(new String(recordData, 0, len)));
}
}
catch (Exception e) {
System.err.println("Failed reading hi scores!");
}
// Close the record store
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("Failed closing hi score record store!");
}
}
else {
// The record store doesn't exist, so initialize the scores to 0
for (int i = 0; i < 5; i++) {
hiScores[i] = 0;
}
}
}
//存档
private void saveRecord(int scores, int energy, int playerX, int playerY,
int goodSaved, int dif, int time, int pass, int xView,
int yView) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("save", true);
}
catch (Exception e) {}
saveMessage[0] = scores;
saveMessage[1] = energy;
saveMessage[2] = playerX;
saveMessage[3] = playerY;
saveMessage[4] = goodSaved;
saveMessage[5] = dif;
saveMessage[6] = time;
saveMessage[7] = pass;
saveMessage[8] = xView;
saveMessage[9] = yView;
int num = 0;
try {
num = rs.getNumRecords();
}
catch (Exception e) {
System.err.println("未保存记录!");
}
for (int i = 0; i < N; i++) {
byte[] recordData = Integer.toString(saveMessage[i]).getBytes();
if (num == 0) {
try {
rs.addRecord(recordData, 0, recordData.length);
}
catch (Exception e) {
System.err.println("failed save!");
}
}
else {
try {
rs.setRecord(i + 1, recordData, 0, recordData.length);
}
catch (Exception e) {
System.err.println("failed save!");
}
}
}
try {
rs.closeRecordStore();
}
catch (Exception e) {}
}
public void readRecord(int[] loadGame) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("save", false);
}
catch (Exception e) {}
for (int i = 0; i < N; i++) {
if (rs != null) {
try {
int len;
byte[] recordData = new byte[rs.getRecordSize(i + 1)];
len = rs.getRecord(i + 1, recordData, 0);
loadGame[i] = (Integer.parseInt(new String(recordData, 0, len)));
}
catch (Exception e) {
System.err.println("Failed reading hi scores!");
}
}
}
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.err.println("failed close RecordStore!");
}
}
public void commandAction(Command c, Displayable s) {
Navigator.flow(c.getLabel());
if (c.getLabel().equals("返回")) {
stop();
}
if (c.getLabel().equals("保存")) {
saveRecord(score, energy, playerX, playerY, goodsSaved, dif, time, pass,
xView, yView);
}
if (c.getLabel().equals("暂停")) {
removeCommand(pauseCommand);
addCommand(playCommand);
pause = true;
}
if (c.getLabel().equals("继续")) {
removeCommand(playCommand);
addCommand(pauseCommand);
pause = false;
}
if (c == musicOnCommand) {
try {
removeCommand(musicOnCommand);
addCommand(musicOffCommand);
volume = true;
musicPlayer.start();
}
catch (Exception dfdfe) {}
}
if (c == musicOffCommand) {
try {
removeCommand(musicOffCommand);
addCommand(musicOnCommand);
volume = false;
musicPlayer.stop();
}
catch (Exception dddfe) {}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -