?? wcanvas.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 WCanvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private int inputDelay;
private TiledLayer backgroundLayer;
private Sprite personSprite;
private Player musicPlayer;
public WCanvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 33;
// Clear the input delay
inputDelay = 0;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Create the background tiled layer
try {
backgroundLayer = new TiledLayer(16, 16, Image.createImage("/Background.png"), 48, 48);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Setup the background tiled layer map
int[] layerMap = {
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 21, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 22, 3,
3, 18, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 3,
3, 18, 2, 2, 2, 5, 15, 15, 15, 15, 15, 15, 6, 2, 20, 3,
3, 18, 2, 2, 2, 7, 10, 1, 1, 1, 1, 1, 16, 2, 20, 3,
3, 18, 2, 2, 2, 2, 14, 1, 1, 1, 1, 1, 16, 2, 20, 3,
3, 18, 2, 2, 2, 2, 7, 10, 1, 1, 1, 1, 16, 2, 20, 3,
3, 18, 2, 2, 2, 2, 2, 14, 1, 1, 1, 1, 16, 2, 20, 3,
3, 18, 2, 2, 2, 2, 2, 14, 1, 9, 10, 1, 16, 2, 20, 3,
3, 18, 2, 5, 15, 6, 2, 14, 1, 11, 12, 1, 16, 2, 20, 3,
3, 18, 2, 14, 1, 16, 2, 7, 13, 13, 13, 13, 8, 2, 20, 3,
3, 18, 2, 7, 13, 8, 2, 2, 2, 2, 2, 2, 2, 2, 20, 3,
3, 18, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 3,
3, 23, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 24, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
};
for (int i = 0; i < layerMap.length; i++) {
int column = i % 16;
int row = (i - column) / 16;
backgroundLayer.setCell(column, row, layerMap[i]);
}
backgroundLayer.setPosition((getWidth() - backgroundLayer.getWidth()) / 2,
(getHeight() - backgroundLayer.getHeight()) / 2);
// Initialize the person sprite
try {
personSprite = new Sprite(Image.createImage("/Person.png"), 20, 24);
personSprite.setPosition((getWidth() - personSprite.getWidth()) / 2,
(getHeight() - personSprite.getHeight()) / 2);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Initialize and start the music player
try {
InputStream is = getClass().getResourceAsStream("Music.mid");
musicPlayer = Manager.createPlayer(is, "audio/midi");
musicPlayer.prefetch();
musicPlayer.setLoopCount(-1);
musicPlayer.start();
}
catch (IOException ioe) {
}
catch (MediaException me) {
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Close the music player
musicPlayer.close();
// 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() {
// Process user input to move the background layer and animate the person
if (++inputDelay > 2) {
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
backgroundLayer.move(12, 0);
personSprite.nextFrame();
}
else if ((keyState & RIGHT_PRESSED) != 0) {
backgroundLayer.move(-12, 0);
personSprite.nextFrame();
}
if ((keyState & UP_PRESSED) != 0) {
backgroundLayer.move(0, 12);
personSprite.nextFrame();
}
else if ((keyState & DOWN_PRESSED) != 0) {
backgroundLayer.move(0, -12);
personSprite.nextFrame();
}
checkBackgroundBounds(backgroundLayer);
// Reset the input delay
inputDelay = 0;
}
}
private void draw(Graphics g) {
// Draw the background tiled layer
backgroundLayer.paint(g);
// Draw the person sprite
personSprite.paint(g);
// Flush the offscreen graphics buffer
flushGraphics();
}
private void checkBackgroundBounds(TiledLayer background) {
// Stop the background if necessary
if (background.getX() > -15)
background.setPosition(-15, background.getY());
else if (background.getX() < -572)
background.setPosition(-572, background.getY());
if (background.getY() > -25)
background.setPosition(background.getX(), -25);
else if (background.getY() < -572)
background.setPosition(background.getX(), -572);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -