交互性
前面已经完成了全屏幕,全屏幕下的动画与全屏幕下的动画移动。下面来看交互性问题。用键盘控制Fly的移动。Fly类见上一篇。在这之前先写一个辅助类,CoreSupport。这个类主要完成显示设置和运动动画loop,在开发的类可以再这个类的基础之上,直接继承这个类。
/**
*
*/
package com.jsheng.game.util;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Window;
import javax.swing.ImageIcon;
/** function:
* company: jsheng
* @author wanghn [email protected]
*/
public abstract class CoreSupport {
protected static final int FONT_SIZE = 24;
private static final DisplayMode POSSIBLE_MODES[] = {
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0)
};
private boolean isRunning;
protected MyScreenMgr screen;
public void stop() {
isRunning = false;
}
public void run() {
try {
init();
gameLoop();
}
finally {
screen.reScreen();
}
}
public void init() {
screen = new MyScreenMgr();
DisplayMode displayMode =
screen.findFirstCompatibleMode(POSSIBLE_MODES);
screen.setFullScreen(displayMode);
Window window = screen.getFullScreenWindow();
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
window.setBackground(Color.black);
window.setForeground(Color.white);
isRunning = true;
}
public Image loadImage(String fileName) {
return new ImageIcon(fileName).getImage();
}
public void gameLoop() {
long startTime = System.currentTimeMillis();
long currTime = startTime;
while (isRunning) {
long elapsedTime =
System.currentTimeMillis() - currTime;
currTime += elapsedTime;
update(elapsedTime);
Graphics2D g = screen.getGraphics();
draw(g);
g.dispose();
screen.update();
try {
Thread.sleep(20);
}
catch (InterruptedException ex) { }
}
}
public void update(long elapsedTime) {
}
public abstract void draw(Graphics2D g);
}
其中的init方法初始化图片和变量
update方法在子类中实现
draw方法由子类完成
下面完成用户操作,用ESC退出程序,并能显示按下的键盘按键
package com.jsheng.game.test1;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import com.jsheng.game.util.CoreSupport;
import com.jsheng.game.util.GameCore;
/** function:
* company: jsheng
* @author wanghn [email protected]
*/
public class KeyTest extends CoreSupport implements KeyListener {
public static void main(String[] args) {
new KeyTest().run();
}
private LinkedList<String> messages = new LinkedList<String>();
public void init() {
super.init();
Window window = screen.getFullScreenWindow();
window.setFocusTraversalKeysEnabled(false);
window.addKeyListener(this);
addMessage("键盘测试,按下ESC退出");
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ESCAPE) {
stop();
}
else {
addMessage("Pressed: " +
KeyEvent.getKeyText(keyCode));
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
addMessage("Released: " + KeyEvent.getKeyText(keyCode));
}
public void keyTyped(KeyEvent e) {
}
public synchronized void addMessage(String message) {
messages.add(message);
if (messages.size() >= screen.getHeight() / FONT_SIZE) {
messages.remove(0);
}
}
public synchronized void draw(Graphics2D g) {
Window window = screen.getFullScreenWindow();
g.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(window.getBackground());
g.fillRect(0, 0, screen.getWidth(), screen.getHeight());
g.setColor(window.getForeground());
int y = FONT_SIZE;
for (int i=0; i<messages.size(); i++) {
g.drawString(messages.get(i), 5, y);
y+=FONT_SIZE;
}
}
}