📄 gameapplet.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* A time-managed version of the Applet class.
*/
public abstract class GameApplet extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
//定义的一个静态成员,决定处理事件的时间间隔
protected static long REFRESH_RATE_MILLIS = 25;
//内存缓存图片,以克服抖动
private Graphics gBack;
private Image iBack;
//保存当前的屏幕对象
private GameScreen currentScreen;
//保存当前的上下文对象
private GameContext currentContext;
//保存游戏媒体加载器对象
private GameMedia currentMedia;
//保存SUN音频管理器对象
private SunAudioManager sunAudio;
//保存游戏的宽度与高度
private int gameWidth, gameHeight;
//标志游戏是否在运行,子线程是否在运行
private boolean gameRunning, threadRunning;
//保存按键事件列表
private int[] listKeys;
//保存鼠标的位置
private Point mousePosition;
//保存鼠标左键的状态
private boolean leftMouseButtonState;
//保存鼠标右键的状态
private boolean rightMouseButtonState;
//标识游戏的运行状态
private boolean isApplication = false;
//初始化Applet
public void init()
{
//得到Applet的宽度与高度
gameWidth = this.getSize().width;
gameHeight = this.getSize().height;
//创建与Applet同大小的内存缓存图片对象
iBack = this.createImage(gameWidth, gameHeight);
gBack = iBack.getGraphics();
//创建游戏上下文对象
currentContext = new GameContext();
//创建游戏媒体加载器对象
currentMedia = new GameMedia(this);
try
{ //创建SUN音频管理器对象
sunAudio = new SunAudioManager(this);
}
catch(Exception e)
{
System.err.println("Package sun.audio is not available");
}
//调用派生类中的gameInit(),初始化游戏资源
gameInit();
//设置游戏当前的屏幕
setCurrentScreen(this.getInitialScreen());
listKeys = new int[0];
mousePosition = new Point(0, 0);
//注册键盘按键事件处理器
if (needsKeyEvents())
{
addKeyListener(this);
}
//注册鼠标事件处理器
if (needsMouseEvents())
{
addMouseListener(this);
addMouseMotionListener(this);
}
}
//抽象函数,用于得到初始的游戏屏幕
public abstract GameScreen getInitialScreen();
//抽象函数,用于初始化游戏
public abstract void gameInit();
// 调协游戏为应用程序方式运行
public void setAsApplication()
{
this.isApplication = true;
}
//得到游戏运行的方式
public boolean isApplication()
{
return this.isApplication;
}
//得到游戏声音管理器对象
public SunAudioManager getSunAudioManager()
{
return sunAudio;
}
//得到是否需要注册键盘事件处理器
public boolean needsKeyEvents()
{
return true;
}
//得到是否需要注册鼠标事件处理器
public boolean needsMouseEvents()
{
return true;
}
//得到游戏的宽度
public final int getWidth()
{
return gameWidth;
}
//得到游戏的高度
public final int getHeight()
{
return gameHeight;
}
//得到游戏的内存缓存图片
public final Image getBackgroundImage()
{
return iBack;
}
//得到游戏的内存缓存图片对应的画笔
public final Graphics getBackgroundGraphics()
{
return gBack;
}
//得到游戏的上下文对象
public final GameContext getGameContext()
{
return currentContext;
}
//得到游戏的媒体加载器对象
public final GameMedia getGameMedia()
{
return currentMedia;
}
//设置游戏的当前屏幕
public final void setCurrentScreen(GameScreen newCurrentScreen)
{
currentScreen = newCurrentScreen;
currentScreen.initBackground();
}
//启动线程
public final void start()
{
while (threadRunning);
gameRunning = true;
new Thread(this).start();
}
//停止线程
public final void stop()
{
gameRunning = false;
}
//这是子线程的代码
public final void run() //Applet主类是用来捕获用户的输入的,并且每隔25毫移就处理一次事件
{
long time;
threadRunning = true;
while(gameRunning)
{
//得到事件处理前一刻的时间
time = System.currentTimeMillis();
//调用当前屏幕的play进行事件处理
currentScreen.play(listKeys, mousePosition, leftMouseButtonState, rightMouseButtonState);
//从内存缓存图片中清除所有显示的精灵
currentScreen.refreshAll();
//重新显示所有的精灵
currentScreen.paintAll();
//得到处理事件消耗的时差
time = System.currentTimeMillis()-time;
//保证最少隔25毫秒才处理一次事件
if (time < this.REFRESH_RATE_MILLIS)
{
try
{
Thread.sleep(this.REFRESH_RATE_MILLIS - time);
}
catch(Exception e) {
System.err.println("[Thread] - Unable to sleep");
}
}
if (gameRunning) {
paint(this.getGraphics());
}
}
threadRunning = false;
}
//显示游戏
public final void paint(Graphics g)
{
if (iBack != null)
{
g.drawImage(iBack, 0, 0, this);
}
}
//处理键盘按下事件
public final void keyPressed(KeyEvent e)
{
int currentKey = e.getKeyCode();
if (currentKey == 18)
{
return;
}
//将按键的键码保存到数组中
if (listKeys.length != 0)
{
if (listKeys[0] != currentKey)
{
int[] newListKeys = new int[listKeys.length+1];
newListKeys[0] = currentKey;
for (int i=0 ; i<listKeys.length ; i++)
{
newListKeys[i+1] = listKeys[i];
}
listKeys = newListKeys;
}
}
else
{
listKeys = new int[1];
listKeys[0] = currentKey;
}
}
//处理键盘释放事件
public final void keyReleased(KeyEvent e)
{
int currentKey = e.getKeyCode();
//从数组中删除按键的键码
if (listKeys.length != 0)
{
int[] newListKeys = new int[listKeys.length-1];
boolean found = false;
for (int i=0 ; i<listKeys.length ; i++)
{
if (found)
{
newListKeys[i-1] = listKeys[i];
}
else
{
if (i != listKeys.length - 1)
{
newListKeys[i] = listKeys[i];
}
}
if (listKeys[i] == currentKey)
{
found = true;
}
if (i == listKeys.length-1 && !found)
{
return;
}
}
listKeys = newListKeys;
}
}
public final void keyTyped(KeyEvent e) {}
public final void mouseClicked(MouseEvent e) {}
//处理鼠标进入事件
public final void mouseEntered(MouseEvent e)
{
mousePosition = e.getPoint();
paint(this.getGraphics());
}
//处理鼠标退出事件
public final void mouseExited(MouseEvent e)
{
mousePosition = e.getPoint();
paint(this.getGraphics());
}
//处理鼠标按下事件
public final void mousePressed(MouseEvent e)
{
mousePosition = e.getPoint();
int buttons = e.getModifiers();//System.out.println("x:"+mousePosition.getX()+"\ty:"+mousePosition.getY());
if ((buttons & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) {
leftMouseButtonState = !leftMouseButtonState;
}
if ((buttons & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK) {
rightMouseButtonState = !rightMouseButtonState;
}
paint(this.getGraphics());
}
//处理鼠标释放事件
public final void mouseReleased(MouseEvent e)
{
mousePosition = e.getPoint();
int buttons = e.getModifiers();
if ((buttons & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) {
leftMouseButtonState = !leftMouseButtonState;
}
if ((buttons & MouseEvent.BUTTON3_MASK) == MouseEvent.BUTTON3_MASK) {
rightMouseButtonState = !rightMouseButtonState;
}
paint(this.getGraphics());
}
//处理鼠标拖动事件
public final void mouseDragged(MouseEvent e)
{
mousePosition = e.getPoint();
paint(this.getGraphics());
}
//处理鼠标移动事件
public final void mouseMoved(MouseEvent e)
{
mousePosition = e.getPoint();
paint(this.getGraphics());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -