📄
字号:
/**
* Title: 吃豆子<p>
* Description: 游戏入口<p>
* Copyright: Copyright (c) Nothing<p>
* Company: Raindrop<p>
* @author Nothing
* @version 1.0
*/
package eatbean;
import eatbean.util.DataTool;
import eatbean.conf.SysParam;
import eatbean.event.*;
//
import java.awt.*;
import java.awt.event.*;
public class Game extends Panel {
private static final int MAP_X = 1;
private static final int MAP_Y = 1;
private Mediator mediator = null;
private Station station = new Station();
private GameListener gameListener = new GameListenerImpl();
private ScreenListenerImpl screenListener = new ScreenListenerImpl();
private ScreenListenerImplHelp screenListenerHelp =
new ScreenListenerImplHelp();
private ScreenListenerImplPause screenListenerPause =
new ScreenListenerImplPause();
private ScreenListenerImplAllMisnDone screenListenerImplAllMisnDone =
new ScreenListenerImplAllMisnDone();
private ScreenListenerImplGameOver screenListenerImplGameOver =
new ScreenListenerImplGameOver();
//public Panel panel = new Panel(null);
private int score = 0;
private int life = 0;
private boolean flgGameOver = true;
public Game() {
super(null);
//this.setBackground(Color.red);
this.setBounds(new Rectangle(1, 1, SysParam.GAME_SCREEN_WIDTH, SysParam.GA
ME_SCREEN_HEIGHT));
//this.init();
}
/** 封装游戏的大部分标准操作 */
class Station {
public final int MAX_STATION = 4;
public final int MIN_STATION = 1;
public final int LIFE = 4;
private int stationNum = MIN_STATION - 1;
private DataTool dataTool = new DataTool();
//public boolean ready = false;
/** 每一关的地图文件 */
private String[] mapPaths = {
"map1.txt", //Demo数据
"map1.txt",
"map2.txt",
"map3.txt",
"map4.txt",
};
/** 每一关的精灵个数 */
private int[] spriteNum = {
1, //demo数据
2,
2,
3,
4,
};
public int getStationNum() { return stationNum; }
public void firstStation() throws Exception {
life = LIFE;
score = 0;
stationNum = MIN_STATION;
mediator.removeScreenListener();
mediator.addScreenListener(screenListener);
initStation(stationNum);
life--;
pause();
}
public boolean nextStation() throws Exception {
if(stationNum >= MAX_STATION) return false;
stationNum++;
initStation(stationNum);
pause();
return true;
}
private void initStation(int sn) throws Exception {
mediator.killAll();
mediator.createMap(dataTool.getMapData(mapPaths[sn]), MAP_X, MAP_Y);
mediator.createFairy();
for(int i = 0; i < spriteNum[sn]; i++)
mediator.createSprite();
}
public void demo() throws Exception {
onlyShowHelp();
mediator.killAll();
mediator.createMap(dataTool.getMapData(mapPaths[0]), MAP_X, MAP_Y);
for(int i = 0; i < spriteNum[0]; i++)
mediator.createSprite();
mediator.setPaused(false);
}
public void continuePlay() {
//pause();
mediator.resetAllActor();
mediator.createFairy();
for(int i = 0; i < spriteNum[stationNum]; i++)
mediator.createSprite();
life--;
}
public void gameOver() {
//Game Over
mediator.setPaused(true);
mediator.removeScreenListener();
mediator.addScreenListener(screenListenerImplGameOver);
flgGameOver = true;
}
public void pause() {
if(flgGameOver) return;
if(mediator.getPaused()) {
ScreenListener tmp = mediator.removeScreenListener();
if(tmp == null || tmp instanceof ScreenListenerImpl)
mediator.addScreenListener(screenListenerPause);
else
mediator.addScreenListener(tmp);
} else {
mediator.setPaused(true);
mediator.removeScreenListener();
mediator.addScreenListener(screenListenerPause);
}
}
public void unPause() {
if(flgGameOver || !mediator.getPaused()) return;
mediator.removeScreenListener();
mediator.repaintBg();
mediator.addScreenListener(screenListener);
mediator.setPaused(false);
}
public void pauseAndShowHelp() {
if(flgGameOver) return;
mediator.setPaused(true);
mediator.removeScreenListener();
mediator.repaintBg();
mediator.addScreenListener(screenListenerHelp);
}
public void onlyShowHelp() {
mediator.removeScreenListener();
mediator.repaintBg();
mediator.addScreenListener(screenListenerHelp);
}
public void allMissionDone() {
mediator.setPaused(true);
mediator.removeScreenListener();
mediator.addScreenListener(screenListenerImplAllMisnDone);
flgGameOver = true;
}
}
public void init() {
System.out.println("Game.init");
mediator = new Mediator();
mediator.createOffScreen(this);
mediator.addGameListener(gameListener);
try {
station.demo();
} catch(Exception ex) {
Debug.println(ex.toString());
ex.printStackTrace();
return;
}
toolsInit();
}
private void start() {
if(!flgGameOver) return;
flgGameOver = false;
try {
station.firstStation();
} catch(Exception ex) {
Debug.println(ex.toString());
ex.printStackTrace();
return;
}
}
/** 结束游戏,回收资源 */
public void exit() {
mediator.setPaused(true);
mediator.removeGameListener();
mediator.removeScreenListener();
mediator.killAll();
}
/** 空方法,阻止系统重画 */
public void paint(Graphics g) {
}
/** 空方法,阻止系统清除背景 */
public void update(Graphics g) {
}
/** 响应键盘事件 */
private void toolsInit() {
this.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT :
station.unPause();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -