📄 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 LayerManager layers;
private int xView, yView;
private TiledLayer backgroundLayer;
private TiledLayer barrierLayer;
private int waterDelay, waterTile;
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 and barrier tiled layers
try {
backgroundLayer = new TiledLayer(16, 16, Image.createImage("/Background.png"), 48, 48);
barrierLayer = new TiledLayer(16, 16, Image.createImage("/Barrier.png"), 48, 48);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Setup the background tiled layer map
int[] backgroundMap = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 5, 15, 15, 15, 15, 15, 15, 6, 2, 2, 0,
0, 2, 2, 2, 2, 7, 10, 1, 1, 1, 1, 1, 16, 2, 2, 0,
0, 2, 2, 2, 2, 2, 14, 1, 1, 1, 1, 1, 16, 2, 2, 0,
0, 2, 2, 2, 2, 2, 7, 10, 1, 1, 1, 1, 16, 2, 2, 0,
0, 2, 2, 2, 2, 2, 2, 14, 1, 1, 1, 1, 16, 2, 2, 0,
0, 2, 2, 2, 2, 2, 2, 14, 1, 9, 10, 1, 16, 2, 2, 0,
0, 2, 2, 5, 15, 6, 2, 14, 1, 11, 12, 1, 16, 2, 2, 0,
0, 2, 2, 14, 1, 16, 2, 7, 13, 13, 13, 13, 8, 2, 2, 0,
0, 2, 2, 7, 13, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
for (int i = 0; i < backgroundMap.length; i++) {
int column = i % 16;
int row = (i - column) / 16;
backgroundLayer.setCell(column, row, backgroundMap[i]);
}
// Setup the barrier tiled layer map
barrierLayer.createAnimatedTile(1);
int[] barrierMap = {
-1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1,
-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1,
1, 21, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 22, 1,
1, 18, 0, 5, 5, 5, 5, 8, 0, 0, 8, 0, 0, 0, 20, -1,
1, 18, 0, 0, 0, 0, 0, 0, 0, 16, 8, 0, 0, 0, 20, 1,
-1, 18, 7, 7, 7, 11, 7, 8, 0, 0, 10, 5, 0, 0, 20, -1,
1, 18, 0, 11, 0, 0, 11, 7, 7, 12, 0, 0, 0, 0, 20, -1,
-1, 18, 0, 7, 7, 7, 0, 11, 12, 8, 0, 0, 0, 0, 20, 1,
1, 18, 0, 11, 12, 0, 15, 10, 0, 8, 0, 0, 0, 0, 20, 1,
1, 18, 0, 0, 13, 0, 10, 5, 5, 9, 0, 0, 0, 0, 20, -1,
-1, 18, 7, 10, 5, 9, 0, 0, 0, 0, 0, 0, 0, 0, 20, 1,
-1, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, -1,
1, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 1,
1, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 1,
-1, 23, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 24, -1,
-1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, 1, -1
};
for (int i = 0; i < barrierMap.length; i++) {
int column = i % 16;
int row = (i - column) / 16;
barrierLayer.setCell(column, row, barrierMap[i]);
}
// Initialize the animated water delay and tile
waterDelay = 0;
waterTile = 1;
// Initialize the person sprite
try {
personSprite = new Sprite(Image.createImage("/Person.png"), 20, 24);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Create the layer manager
layers = new LayerManager();
layers.append(personSprite);
layers.append(barrierLayer);
layers.append(backgroundLayer);
xView = (backgroundLayer.getWidth() - getWidth()) / 2;
yView = (backgroundLayer.getHeight() - getHeight()) / 2;
layers.setViewWindow(xView, yView, getWidth(), getHeight());
personSprite.setPosition(xView + (getWidth() - personSprite.getWidth()) / 2,
yView + (getHeight() - personSprite.getHeight()) / 2);
// 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();
int xMove = 0, yMove = 0;
if ((keyState & LEFT_PRESSED) != 0)
xMove = -12;
else if ((keyState & RIGHT_PRESSED) != 0)
xMove = 12;
if ((keyState & UP_PRESSED) != 0)
yMove = -12;
else if ((keyState & DOWN_PRESSED) != 0)
yMove = 12;
if (xMove != 0 || yMove != 0) {
layers.setViewWindow(xView + xMove, yView + yMove, getWidth(), getHeight());
personSprite.move(xMove, yMove);
personSprite.nextFrame();
}
// Check for a collision with the person and the barrier tiled layer
if (personSprite.collidesWith(barrierLayer, true)) {
// Play a collision sound
try {
Manager.playTone(ToneControl.C4 + 12, 100, 100);
}
catch (Exception e) {
}
// Restore the original view window and person sprite positions
layers.setViewWindow(xView, yView, getWidth(), getHeight());
personSprite.move(-xMove, -yMove);
}
else {
// If there is no collision, commit the changes to the view window position
xView += xMove;
yView += yMove;
}
// Update the animated water tiles
if (++waterDelay > 2) {
if (++waterTile > 4)
waterTile = 1;
barrierLayer.setAnimatedTile(-1, waterTile);
waterDelay = 0;
}
// Reset the input delay
inputDelay = 0;
}
}
private void draw(Graphics g) {
// Draw the layers
layers.paint(g, 0, 0);
// Flush the offscreen graphics buffer
flushGraphics();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -