⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 a java game code for java
💻 JAVA
字号:

/**
 * 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.GAME_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();
							mediator.fairySetNextDirection(Fairy.DIR_WEST);
							break;
					case KeyEvent.VK_RIGHT :
						    station.unPause();
							mediator.fairySetNextDirection(Fairy.DIR_EAST);
							break;
					case KeyEvent.VK_UP :
						    station.unPause();
							mediator.fairySetNextDirection(Fairy.DIR_NORTH);
							break;
					case KeyEvent.VK_DOWN :
						    station.unPause();
							mediator.fairySetNextDirection(Fairy.DIR_SOUTH);
							break;
					case KeyEvent.VK_S:
							start();
							break;
					case KeyEvent.VK_P:
							station.pause();
							break;
					case KeyEvent.VK_H:
						    station.pauseAndShowHelp();
						    break;
					case KeyEvent.VK_N:
						    //for debug
						    System.out.println("VK_N");
						    try {
						        station.nextStation();
							} catch(Exception ex) { ex.printStackTrace(); }
							break;
					default :
				}
            }
			public void keyReleased(KeyEvent e) {
				switch(e.getKeyCode()) {
					case KeyEvent.VK_LEFT :
					case KeyEvent.VK_RIGHT :
					case KeyEvent.VK_UP :
					case KeyEvent.VK_DOWN :
							mediator.fairyResetNextDirection();
							break;
					default :
				}
			}
        });
		//this.paintAll(this.getGraphics());
	}
	/** 游戏事件监听者 */
	class GameListenerImpl extends GameListener {
		private boolean hasLifeInc = false;
		public void objectEat(GameEvent e) {
			//System.out.println("eatbean");
			Object source = e.getSource();
			if(source == null) return;
			if(source instanceof Room)
				//score += ((Room)source).POINT;
				score += 10;
			if((score >= SysParam.SCORE_TO_ADD_LIFE) && !hasLifeInc) {
				mediator.beep();
				life++;  //奖励,生命值加1
				hasLifeInc = true;
			}
		}
		public void actorEat(GameEvent e) {
			Object source = e.getSource();
			if(source == null) return;
			if(source instanceof Fairy) {
			    if(Debug.ON) Debug.println("objectEat() Fairy被杀");
				if(life > 0) {
					station.continuePlay();
					//mediator.setPaused(false);
				} else {
					//Game Over
					if(Debug.ON) Debug.println("Game Over");
					try {
					    //station.demo();
						station.gameOver();
					} catch(Exception ex) {}
				}
			} else if(source instanceof Sprite) {
			    if(Debug.ON) Debug.println("objectEat() Sprite被杀");
			}
		}
		public void stationDone(GameEvent e) {
			//Mission Done
			try {
			    if(!station.nextStation()) {
				    //All Mission Done
					station.allMissionDone();
				}
			} catch(Exception ex) {};
		}
	}
	/** 标准屏幕事件监听者,以实时更新显示分数/生命值等信息 */
	class ScreenListenerImpl implements ScreenListener {
		Color c = Color.white;
		Font font = new Font("宋体", Font.BOLD, 12);
		public void update(ScreenEvent se) {
			OffScreen offScr = (OffScreen)se.getSource();
			int h = mediator.getMapHeight();
			if(offScr != null) {
				updateGeneralInfo(offScr, h);
			}
		}
	}
	/** 显示帮助信息 */
	class ScreenListenerImplHelp implements ScreenListener {
		Color c = Color.yellow;
		Font font = new Font("宋体", Font.BOLD, 13);
		public void update(ScreenEvent se) {
			OffScreen offScr = (OffScreen)se.getSource();
			int h = mediator.getMapHeight();
			if(offScr != null) {
				offScr.setColor(c);
				offScr.setFont(font);
				int baseX = 130, baseY = 80;
				offScr.drawString("吃豆子Ver1.0", baseX, baseY);
				offScr.drawString("\"s\"-开始游戏", baseX-10, baseY=baseY+20);
				offScr.drawString("\"p\"-暂停游戏", baseX-10, baseY=baseY+20);
				offScr.drawString("\"方向键\"-返回游戏,控制精灵", baseX-10, baseY=baseY+20);
				baseX = 80; baseY = 220;
				offScr.setColor(Color.white);
				offScr.drawString("---------------------------------", 50, baseY);
				offScr.drawString("程序: \"nothing\"", baseX, baseY=baseY+15);
				offScr.drawString("美工: \"olive\"", baseX, baseY=baseY+15);
				offScr.drawString("Raindrop Studio 2002.3", baseX+20, baseY=baseY+15);
				offScr.drawString("Email: chinaren_gc@chinaren.com", baseX+20, baseY=baseY+15);
				updateGeneralInfo(offScr, h);
			}
		}
	}
	/** 显示暂停信息 */
	class ScreenListenerImplPause implements ScreenListener {
		Color c = Color.yellow;
		Font font = new Font("宋体", Font.BOLD, 14);
		public void update(ScreenEvent se) {
			OffScreen offScr = (OffScreen)se.getSource();
			int h = mediator.getMapHeight();
			if(offScr != null) {
				offScr.setColor(c);
				offScr.setFont(font);
				offScr.drawString("第" + station.stationNum + "关", 140, 100);
				offScr.drawString("暂     停", 130, 120);
				offScr.drawString("\"方向键\"返回游戏", 130, 140);
				updateGeneralInfo(offScr, h);
			}
		}
	}
	/** 通关时显示相关信息 */
	class ScreenListenerImplAllMisnDone implements ScreenListener {
		Color c = Color.yellow;
		Font font = new Font("宋体", Font.BOLD, 16);
		public void update(ScreenEvent se) {
			OffScreen offScr = (OffScreen)se.getSource();
			int h = mediator.getMapHeight();
			if(offScr != null) {
				offScr.setColor(c);
				offScr.setFont(font);
				offScr.drawString("大侠您通关了!", 100, 120);
				offScr.drawString("签个名吧:)", 120, 160);
				updateGeneralInfo(offScr, h);
			}
		}
	}
	/** GameOver时显示相关信息 */
	class ScreenListenerImplGameOver implements ScreenListener {
		Color c = Color.yellow;
		Font font = new Font("宋体", Font.BOLD, 20);
		public void update(ScreenEvent se) {
			OffScreen offScr = (OffScreen)se.getSource();
			int h = mediator.getMapHeight();
			if(offScr != null) {
				offScr.setColor(c);
				offScr.setFont(font);
				offScr.drawString("Game Over", 130, 120);
				Font font = new Font("宋体", Font.BOLD, 14);
				offScr.setFont(font);
				offScr.drawString("\"s\"-重玩", 140, 160);
				updateGeneralInfo(offScr, h);
			}
		}
	}
	private void updateGeneralInfo(OffScreen offScr, int h) {
		offScr.clearRect(0, h+2,
							offScr.getWidth(), offScr.getHeight() - h);

		h = h + 20;
		offScr.setColor(Color.white);
		offScr.setFont(new Font("宋体", Font.BOLD, 12));
		offScr.drawString("生命: " + Integer.toString(life), 10, h);
		offScr.drawString("得分: " + Integer.toString(score), 120, h);
		offScr.drawString("关卡: " + Integer.toString(station.getStationNum()), 280, h);
		offScr.drawString("\"h\"-帮助  [Raindrop Studio]", 140, h+14);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -