📄 fishgamecanvas.java
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class FishGameCanvas extends GameCanvas implements Runnable {
public static final Random RANDOM = new Random();
private static final int MILLISECS_PER_FRAME = 70;
private Thread m_thread;
private long m_startTime;
public boolean m_isStopped;
public boolean m_isPaused;
public int m_width, m_height;
private Graphics m_gOffscreen;
private static final int MANTA_BUMP = 2;
private static final int MANTA_MARGIN = 20;
private static final int MAX_STARS = 6;
private Vector m_stars = new Vector(MAX_STARS);
private Background m_background;
private LayerManager m_layerManager;
//Initialization
public FishGameCanvas() {
//Suppress events for game action keys
super(true);
setFullScreenMode(true);
m_width = getWidth();
m_height = getHeight();
m_gOffscreen = getGraphics();
try {
m_background = new Background();
m_layerManager = new LayerManager();
}
catch (Exception e) {
System.err.println("ERROR creating game object.");
}
}
public boolean start() {
if ( m_background == null || m_layerManager == null) {
return false;
}
setCommandListener (FishMidlet.m_midlet);
m_layerManager.append(m_background);
//launch the game loop thread
m_thread = new Thread(this);
m_thread.start();
m_startTime = System.currentTimeMillis();
return true;
}
public void stop() {
m_isStopped = true;
m_thread = null;
}
public void exit() {
stop();
}
//Runnable - the main game loop
public void run () {
while (!m_isStopped) {
if (!m_isPaused) {
readInput();
simulateWorld();
draw();
}
delayUntilNextFrame();
}
}
private void readInput() {
int keyStates = getKeyStates();
}
private void simulateWorld() {
m_background.animate();
}
private void eraseBackground(Graphics g) {
g.setColor(0x0000ff);
g.fillRect(0, 0, getWidth(), getHeight());
}
private void draw() {
eraseBackground(m_gOffscreen);
m_layerManager.paint(m_gOffscreen,0,0);
flushGraphics();
}
//-------------------------------------------------------
private void delayUntilNextFrame() {
long elapsed = m_startTime;
m_startTime = System.currentTimeMillis();
elapsed = m_startTime - elapsed;
if (elapsed < MILLISECS_PER_FRAME) {
try {
m_thread.sleep(MILLISECS_PER_FRAME - elapsed);
}
catch (Exception e) {
}
}
else {
//We consumed all our allotted time so don抰 sleep, but before
//exiting, yield to allow another thread a chance to process.
m_thread.yield();
}
} // end-of-delayUntilNextFrame()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -