java泡泡龙游戏,
00-1010前言主要设计功能截图代码实现游戏启动类核心监视器类核心线程类总结
00-1010《泡泡堂II》是一款基于java的自制游戏,采用MVC模式,模型、视图、控制器分离,项目结构清晰,易于扩展,使用配置文件设置游戏基本配置,扩展地图人物道具等。同时,在编程过程中使用了单体模式、工厂模式、模板模式等设计模式。为了游戏的可玩性,特别设计了平滑碰撞和机器人。
00-1010设计游戏界面,使用swing绘制游戏开始界面、结束界面、地图、主角、道具,实现泡泡爆炸。为了尽可能的恢复泡泡大厅游戏,初步实现了机器人功能。该机器人可以判断障碍物以释放炸弹、躲避炸弹和攻击玩家。实现道具掉落和相应的属性加成,实现游戏音频和背景音乐的流畅碰撞。3360个字符在转角处移动时往往不只是对齐,程序会判定玩家撞上了障碍物,所以玩家不能转弯。所以我们在处理这种情况的时候,会让玩家移动流畅,让玩家仿佛滑了进去,增强玩家的游戏体验,设计了单/双人模式。
00-1010游戏启动界面:
正确描述:
游戏开始:
释放炸弹:
爆炸效果:
目录
前言
公共类GameStart {私有静态GameFrame gameFrame//游戏启动入口public static void main(string[]args){//资源加载try { element loader . getelementloader()。readgamepro();ElementLoader.getElementLoader()。readImagePro();ElementLoader.getElementLoader()。readCharactorsPro();ElementLoader.getElementLoader()。readBubblePro();ElementLoader.getElementLoader()。readSquarePro();} catch(io exception e){ system . out . println(资源加载失败);e . printstacktrace();}//初始化game frame=new game frame();//界面显示game frame . set visible(true);//Music Play游戏音乐播放器音乐播放器=新游戏音乐播放器();music player . start();}/* * *接口开关* @param panelName接口名称*/public static void change jpanel(string panel name){ if(panel name== game ){ game controller . setgamerrunning(true);game frame . addlistener();} else { game controller . setgamerrunning(false);game frame . remove listener();} game frame . change panel(panel name);//strong制刷新,否则监控无效game frame . set visible(false);game frame . set visible(true);} public static void startnew game(){ game controller . setgamerunning(true);game frame . start game();changeJPanel( game );}}
主要设计
公共类GameThread扩展线程{私有布尔运行;//指示当前级别是否处于私有布尔值over=false//表示游戏是否结束。结束后返回开始菜单private static int sleepTime=20//runGame刷新时间//倒计时变量private static int all time=600 * 1000;//10分钟@Overridepublic void run() {while(!结束){跑步
= true;//当前关卡正在进行//加载元素loadElement();//显示人物,流程,自动化runGame();//结束当前关overGame(over);}GameStart.changeJPanel("over");}//加载元素private void loadElement() {ElementManager.getManager().loadMap();//加载地图及其元素}/** * 关卡结束 * 如果over为真则游戏失败返回界面,否则进入下一关 * @param over */private void overGame(Boolean over) {ElementManager.getManager().overGame(over);}//显示人物,游戏流程,自动化private void runGame() {allTime = 600*1000;while(running) {Map<String, List<SuperElement>> map = ElementManager.getManager().getMap();Set<String> set = map.keySet();for(String key:set) {List<SuperElement> list = map.get(key);for(int i=list.size()-1; i>=0; i--) {list.get(i).update();if(!list.get(i).isAlive())list.remove(i);}}//添加游戏的流程控制linkGame()?//玩家与炸弹碰撞死亡playerBoom();//可破坏物与炸弹碰撞fragilityBoom();//电脑与炸弹碰撞死亡npcBoom();//电脑与道具碰撞效果,暂时不开启//npcMagicBox();//玩家与道具碰撞效果playerMagicBox();//检测是否玩家全部死亡defeat();//控制runGame进程allTime = allTime - sleepTime;try {sleep(20);} catch (InterruptedException e) {// TODO: handle exceptione.printStackTrace();}}}private void defeat() {boolean allDead = true;int surviveP = 0;int winner = 2;//0为玩家1,1为玩家2,2为电脑获胜List<SuperElement> playerList = ElementManager.getManager().getElementList("player");List<SuperElement> npcList = ElementManager.getManager().getElementList("npc");for(SuperElement se:playerList) {if(!((Player)se).isDead()) {surviveP++;}}for(SuperElement npc:npcList) {if(!((Npc)npc).isDead()) {allDead = false;}}//玩家失败if(surviveP==0(allTime<=0 && !allDead)) {running = false;over = true;OverJPanel.getResult().setText("defeated");}//玩家胜利if(allDead&&surviveP==1) {running = false;over = true;for(SuperElement se:playerList) {if(!((Player)se).isDead()) {surviveP++;winner = ((Player)se).getPlayerNum();}}OverJPanel.getResult().setText("player "+(winner+1)+" win");}//时间到,两个玩家都活着if(allTime<=0&&surviveP==2&&allDead) {running = false;over = true;int score1 = ((Player)playerList.get(0)).score;int score2 = ((Player)playerList.get(0)).score;if(score1==score2) {OverJPanel.getResult().setText("defeated");}else if(score1>score2){OverJPanel.getResult().setText("player 1 win");}else {OverJPanel.getResult().setText("player 2 win");}}}//玩家与炸弹碰撞判断private void playerBoom() {List<SuperElement> playerList = ElementManager.getManager().getElementList("player");List<SuperElement> explodeList = ElementManager.getManager().getElementList("explode");for(int i=0; i<playerList.size(); i++) {for(int j=0; j<explodeList.size(); j++) {if(explodeList.get(j).crash(playerList.get(i))){Player player = (Player) playerList.get(i);player.setHealthPoint(-1);//生命值-1}}}}//npc与炸弹碰撞判断private void npcBoom() {List<SuperElement> playerList = ElementManager.getManager().getElementList("player");List<SuperElement> npcList = ElementManager.getManager().getElementList("npc");List<SuperElement> explodeList = ElementManager.getManager().getElementList("explode");for(int i=0; i<npcList.size(); i++) {for(int j=0; j<explodeList.size(); j++) {if(explodeList.get(j).crash(npcList.get(i))){Npc npc = (Npc) npcList.get(i);npc.setDead(true);npc.setX(-100);npc.setY(-100);BubbleExplode e = (BubbleExplode)explodeList.get(j);if(e.getPlayerNum()<2)//目前只有玩家计分((Player)playerList.get(e.getPlayerNum())).setScore(((Player)playerList.get(e.getPlayerNum())).getScore()+50);}}}}//障碍物与炸弹碰撞判断private void fragilityBoom() {List<SuperElement> playerList = ElementManager.getManager().getElementList("player");List<SuperElement> explodes = ElementManager.getManager().getElementList("explode");List<SuperElement> fragility = ElementManager.getManager().getElementList("fragility");for(int i=0; i<fragility.size(); i++) {for(int j=0; j<explodes.size(); j++) {if(explodes.get(j).crash(fragility.get(i))) {MapFragility mapFragility = (MapFragility)fragility.get(i);mapFragility.setDestoried(true);BubbleExplode e = (BubbleExplode)explodes.get(j);if(e.getPlayerNum()<2)//目前只有玩家计分((Player)playerList.get(e.getPlayerNum())).setScore(((Player)playerList.get(e.getPlayerNum())).getScore()+10);}}}}//玩家与道具碰撞判断private void playerMagicBox() {List<SuperElement> playerList = ElementManager.getManager().getElementList("player");List<SuperElement> magicBoxList = ElementManager.getManager().getElementList("magicBox");for(int i=0; i<playerList.size(); i++) {for(int j=magicBoxList.size()-1; j>=0; j--) {if(magicBoxList.get(j).crash(playerList.get(i))){MagicBox magicBox = (MagicBox) magicBoxList.get(j);magicBox.setCharacterIndex(i);//谁吃方块magicBox.setEaten(true);//方块被吃((Player)playerList.get(i)).setScore(((Player)playerList.get(i)).getScore()+30);}}}}//玩家与道具碰撞判断private void npcMagicBox() {List<SuperElement> npcList = ElementManager.getManager().getElementList("npc");List<SuperElement> magicBoxList = ElementManager.getManager().getElementList("magicBox");for(int i=0; i<npcList.size(); i++) {for(int j=magicBoxList.size()-1; j>=0; j--) {if(magicBoxList.get(j).crash(npcList.get(i))){MagicBox magicBox = (MagicBox) magicBoxList.get(j);magicBox.setCharacterIndex(i+2);//谁吃方块magicBox.setEaten(true);//方块被吃}}}}//runGame调用,加入拓展public void linkGame() {}public static int getAllTime() {return allTime;}}
核心线程类
public class GameKeyListener implements KeyListener{/** * 用栈来解决按键冲突 * 每个栈用来存放不同用户的按键,通过判断按键的code来设置移动方向或者攻击 * */private List<?> list;private Stack<Integer> p1PressStack = new Stack<>();private Stack<Integer> p2PressStack = new Stack<>();@Overridepublic void keyPressed(KeyEvent e) {list = ElementManager.getManager().getElementList("player");Player player1 = (Player) list.get(0);int code = e.getKeyCode();switch (code) {case 10://炸弹键if(player1.isKeepAttack())//不允许一直按着炸弹键,每次只能放一个炸弹player1.setAttack(false);else {player1.setKeepAttack(true);player1.setAttack(true);}break;case 37://左右上下case 38:case 39:case 40:if(!p1PressStack.contains(code)) {p1PressStack.push(code);}player1.setMoveType(MoveTypeEnum.codeToMoveType(code));break;default://其它按键无视break;}if(GameController.isTwoPlayer()) {Player player2 = (Player) list.get(1);switch (code) {case 32:if(player2.isKeepAttack())player2.setAttack(false);else {player2.setKeepAttack(true);player2.setAttack(true);}break;case 65:case 87:case 68:case 83:if(!p2PressStack.contains(code)) {p2PressStack.push(code);}player2.setMoveType(MoveTypeEnum.codeToMoveType(code));break;default:break;}}}@Overridepublic void keyReleased(KeyEvent e) {List<?> list = ElementManager.getManager().getElementList("player");int code = e.getKeyCode();Player player1 = (Player) list.get(0);if(!player1.isDead()) {switch (code) {case 10:player1.setAttack(false);player1.setKeepAttack(false);break;case 37:case 38:case 39:case 40:if(p1PressStack.peek()!=code) {p1PressStack.remove(new Integer(code));} else {p1PressStack.pop();if(p1PressStack.size()==0) {player1.setMoveType(MoveTypeEnum.STOP);} else {player1.setMoveType(MoveTypeEnum.codeToMoveType(p1PressStack.peek()));}}break;default:break;}}if(GameController.isTwoPlayer()) {Player player2 = (Player) list.get(1);if(!player2.isDead()) {switch (code) {case 32:player2.setAttack(false);player2.setKeepAttack(false);break;case 65:case 87:case 68:case 83:if(p2PressStack.peek()!=code) {p2PressStack.remove(new Integer(code));} else {p2PressStack.pop();if(p2PressStack.size()==0) {player2.setMoveType(MoveTypeEnum.STOP);} else {player2.setMoveType(MoveTypeEnum.codeToMoveType(p2PressStack.peek()));}}break;default:break;}}}}@Overridepublic void keyTyped(KeyEvent arg0) {// TODO 自动生成的方法存根}public void clearKeyStatcks() {p1PressStack.clear();p2PressStack.clear();}}
总结
通过此次的《泡泡堂对战版》实现,让我对JAVA的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。
java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。
以上就是Java实现泡泡堂对战版游戏的示例代码的详细内容,更多关于Java泡泡堂的资料请关注盛行IT其它相关文章!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。