?? ufocanvas.java
字號:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
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[3];
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);
}
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 < 3; 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 < 3; 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 < 3; 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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -