📄 gamescreen.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class GameScreen extends GameCanvas implements Runnable, CommandListener {
private static final int MILLIS_PER_TICK = 50;
private Eliminator midlet; // Hold the Main Midlet
private Settings settings; // Hold Game Settings
private Score score; // Hold Game Score
private Command backCommand = new Command("Back", Command.BACK,1);
private GameMap gameMap;
private boolean isPlay; // Game Loop runs when isPlay is true
private int width; // To hold screen width
private int height; // To hold screen height
private int scnViewWidth; // Hold Width Screen View Port
private int scnViewHeight; // Hold Height Screen View Port
private Thread gameThread = null;
// Layer Manager to manager background (terrain)
private LayerManager layerManager;
// TiledLayer - Terrain
private TiledLayer terrain;
private int terrainScroll; // Hold Y position for scrolling
// Constructor and initialization
public GameScreen(Eliminator midlet,Settings settings,Score score) throws Exception {
super(true);
this.midlet = midlet;
addCommand(backCommand);
setCommandListener(this);
width = getWidth(); // get screen width
height = getHeight(); // get screen height
scnViewWidth = width; // Set View Port width to screen width
scnViewHeight = height; // Set View Port height to screen height
isPlay = true;
gameMap = new GameMap(scnViewHeight);
terrain = gameMap.getTerrain();
layerManager = new LayerManager();
layerManager.append(terrain);
}
// Start thread for game loop
public void start() {
gameThread = new Thread(this);
gameThread.start();
}
// Stop thread for game loop
public void stop() {
gameThread = null;
}
// Main Game Loop
public void run() {
Graphics g = getGraphics();
Thread currentThread = Thread.currentThread();
try {
while (currentThread == gameThread) {
long startTime = System.currentTimeMillis();
if (isShown()) {
if (isPlay) {
tick();
}
render(g);
}
long timeTake = System.currentTimeMillis() - startTime;
if (timeTake < MILLIS_PER_TICK) {
synchronized (this) {
wait(MILLIS_PER_TICK - timeTake);
}
} else {
currentThread.yield();
}
}
} catch (InterruptedException ex) {
// won't be thrown
}
}
// Handle dynamic changes to game including user input
public void tick() {
// Scroll Terrain
gameMap.scrollTerrain();
}
public void commandAction(Command c, Displayable d) {
if (c == backCommand) {
midlet.mainMenuScreenShow(null);
}
}
// Method to Display Graphics
private void render(Graphics g) {
// Set Background color to beige
//g.setColor(0xF8DDBE);
g.setColor(gameMap.getGroundColor());
g.fillRect(0,0,width,height);
g.setColor(0x0000ff);
// Get Current Map
terrain = gameMap.getTerrain();
// LayerManager Paint Graphics
layerManager.paint(g,0,0);
flushGraphics();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -