*新手求助*J2ME的UFO手机游戏改进
毕业设计做的题目就是 J2ME手机游戏设计与开发——UFO看书看了几星期了,能在一些简单例子上做修改,但是所修改的只是皮毛。
脑袋里有一些想法,在程序里面改来改去始终不能成功编译。急哦!
求业界高手指点迷津!
我的构思如下
一、游戏一开始就计时。游戏的行星基础数为3,每一段时间增加一个。
二、直到玩家控制的UFO碰到行星,游戏结束。游戏结束时,屏幕显示游戏结束并显示玩家坚持的时间。
感激不尽!!!
源程序代码如下:import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import *;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class UFOCanvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private Random rand;
private Sprite ufoSprite;
private int ufoXSpeed, ufoYSpeed;
private Sprite[] roidSprite = new Sprite[4];
private Player tonePlayer;
public UFOCanvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 33;}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Initialize the random number generator
rand = new Random();
// Initialize the UFO and roids sprites
ufoXSpeed = ufoYSpeed = 0;
try {
ufoSprite = new Sprite(Image.createImage("/Saucer.png"));
ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
(getHeight() - ufoSprite.getHeight()) / 2);
Image img = Image.createImage("/Roid.png");
roidSprite[0] = new Sprite(img, 42, 35);
roidSprite[1] = new Sprite(img, 42, 35);
roidSprite[2] = new Sprite(img, 42, 35);
roidSprite[3] = new Sprite(img, 42, 35);}
catch (IOException e) {
System.err.println("Failed loading images!");}
// Initialize the tone tune and play it once
initTune();
playTune();
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();}
public void stop() {
// Clean up the tone tune
cleanupTune();
// Stop the animation
sleeping = true;}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
update();
draw(g);
try {
Thread.sleep(frameDelay); }
catch (InterruptedException ie) {}}}
private void update() {
// Randomly play the "encounters" tone tune
if (rand.nextInt() % 500 == 0) playTune();
// Process user input to control the UFO speed
byte G4 = (byte)(ToneControl.C4 + 7);
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
// Play a sound to indicate movement
try { Manager.playTone(G4, 100, 50);}
catch (Exception e) {}
ufoXSpeed--;}
else if ((keyState & RIGHT_PRESSED) != 0) {
// Play a sound to indicate movement
try {Manager.playTone(G4, 100, 50);}
catch (Exception e) {}
ufoXSpeed++;}
if ((keyState & UP_PRESSED) != 0) {
// Play a sound to indicate movement
try { Manager.playTone(G4, 100, 50); }
catch (Exception e) { }
ufoYSpeed--;}
else if ((keyState & DOWN_PRESSED) != 0) {
// Play a sound to indicate movement
try {Manager.playTone(G4, 100, 50);}
catch (Exception e) { }
ufoYSpeed++; }
ufoXSpeed = Math.min(Math.max(ufoXSpeed, -8), 8);
ufoYSpeed = Math.min(Math.max(ufoYSpeed, -8), 8);
// Move the UFO sprite
ufoSprite.move(ufoXSpeed, ufoYSpeed);
checkBounds(ufoSprite);
// Update the roid sprites
for (int i = 0; i < 4; i++) {
// Move the roid sprites
roidSprite[i].move(i + 1, 1 - i);
checkBounds(roidSprite[i]);
// Increment the frames of the roid sprites
if (i == 1)
roidSprite[i].prevFrame();
else
roidSprite[i].nextFrame();
// Check for a collision between the UFO and roids
if (ufoSprite.collidesWith(roidSprite[i], true)) {
// Play a collision sound
try {Manager.playTone(ToneControl.C4 - 12, 500, 100);}
catch (Exception e) {}
// Reset the sprite positions and speeds
ufoSprite.setPosition((getWidth() - ufoSprite.getWidth()) / 2,
(getHeight() - ufoSprite.getHeight()) / 2);
ufoXSpeed = ufoYSpeed = 0;
for (int j = 0; j < 4; j++)
roidSprite[j].setPosition(0, 0);
// No need to continue updating the roid sprites
break; }}}
private void draw(Graphics g) {
// Clear the display
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
// Draw the UFO sprite
ufoSprite.paint(g);
// Draw the roid sprites
for (int i = 0; i < 4; i++)
roidSprite[i].paint(g);
// Flush the offscreen graphics buffer
flushGraphics(); }
private void checkBounds(Sprite sprite) {
// Wrap the sprite around the screen if necessary
if (sprite.getX() < -sprite.getWidth())
sprite.setPosition(getWidth(), sprite.getY());
else if (sprite.getX() > getWidth())
sprite.setPosition(-sprite.getWidth(), sprite.getY());
if (sprite.getY() < -sprite.getHeight())
sprite.setPosition(sprite.getX(), getHeight());
else if (sprite.getY() > getHeight())
sprite.setPosition(sprite.getX(), -sprite.getHeight()); }
private void initTune() {
byte tempo = 30; // 120bpm
byte d4 = 16; // 1/4 note
byte d2 = 32; // 1/2 note
byte C4 = ToneControl.C4;
byte A6 = (byte)(C4 + 21);
byte B6 = (byte)(C4 + 23);
byte G5 = (byte)(C4 + 19);
byte G4 = (byte)(C4 + 7);
byte D5 = (byte)(C4 + 14);
byte rest = ToneControl.SILENCE;
byte[] encountersSequence = {
ToneControl.VERSION, 1,
ToneControl.TEMPO, tempo,
ToneControl.BLOCK_START, 0,
A6,d4, B6,d4, G5,d4, G4,d4, D5,d2, rest,d2,
ToneControl.BLOCK_END, 0,
ToneControl.PLAY_BLOCK, 0,
ToneControl.PLAY_BLOCK, 0, };
try {
// Create the tone player
tonePlayer = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
tonePlayer.realize();
// Create the tone control and set the tone sequence
ToneControl toneControl = (ToneControl)tonePlayer.getControl("ToneControl");
toneControl.setSequence(encountersSequence);}
catch (IOException ioe) {}
catch (MediaException me) {}}
private void playTune() {
try {
// Play the tone sequence
tonePlayer.start(); }
catch (MediaException me) { }}
private void cleanupTune() {
// Close the tone player
tonePlayer.close();}}