In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java how to achieve the classic game bubble hall". In the daily operation, I believe that many people have doubts about how to achieve the classic game bubble hall in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve the classic game bubble hall in Java". Next, please follow the editor to study!
Main design
Design the game interface and implement it with swing
Draw the game start interface, end interface, map, protagonist, props
Realize bubble explosion
Realize the competition between two protagonists (integral system)
Realize prop drop and corresponding attribute bonus
Achieve game sound effects and background music
Function screenshot
Game launch interface:
Game start interface:
Move effect:
Release bubbles
Bubble explosion effect:
The code implements the game startup class public class GameStart {public static void main (String [] args) {/ / the entry of the entire program to launch StartFrame startFrame = new StartFrame (); startFrame.setVisible (true);}} Core listener class public class GameListener implements KeyListener {private List list @ Override public void keyTyped (KeyEvent e) {/ / TODO Auto-generated method stub} / / Press left 37 right 39 bottom 40 up 38 w87 a65 s83 D68 space 32 enter10 @ Override public void keyPressed (KeyEvent e) {/ / TODO Auto-generated method stub / / System.out.println ("keypressed" + e.getKeyCode ()) List = ElementManager.getInstance (). GetElementList ("play"); Player oneplayer = (Player) list.get (0); Player twoPlayer = (Player) list.get (1); switch (e.getKeyCode ()) {case 65: oneplayer.setLEFT (true) / / oneplayer.setStop (false); break; case 87: oneplayer.setUP (true); / / oneplayer.setStop (false); break; case 68: oneplayer.setRIGHT (true) / / oneplayer.setStop (false); break; case 83: oneplayer.setDOWN (true); / / oneplayer.setStop (false); break; case 32: oneplayer.setPk (true) Break; case 37: twoPlayer.setLEFT (true); break; case 38: twoPlayer.setUP (true); break; case 39: twoPlayer.setRIGHT (true) Break; case 40: twoPlayer.setDOWN (true); break; case 10: twoPlayer.setPk (true); break }} / / release @ Override public void keyReleased (KeyEvent e) {/ / TODO Auto-generated method stub / / System.out.println ("keyreleased" + e.getKeyCode ()); list = ElementManager.getInstance () .getElementList ("play"); Player oneplayer = (Player) list.get (0) Player twoPlayer = (Player) list.get (1); switch (e.getKeyCode ()) {case 65 false / if (oneplayer.isLEFT ()) {/ / oneplayer.setStop (true); / /} oneplayer.setLEFT (false) Break; case 87 true / if (oneplayer.isUP ()) {/ / oneplayer.setStop (true); / /} oneplayer.setUP (false); break Case 68 true / if (oneplayer.isRIGHT ()) {/ / oneplayer.setStop (true); / /} oneplayer.setRIGHT (false); break Case 83 true / if (oneplayer.isDOWN ()) {/ / oneplayer.setStop (true); / /} oneplayer.setDOWN (false); break Case 32: oneplayer.setPk (false); break; case 37: twoPlayer.setLEFT (false); break; case 38: twoPlayer.setUP (false) Break; case 39: twoPlayer.setRIGHT (false); break; case 40: twoPlayer.setDOWN (false); break Case 10: twoPlayer.setPk (false); break;}} Core Thread Class public class GameThread extends Thread {/ / timing data private static int time; private boolean flag=true / / ReFactor the old project @ Override public void run () {/ / this loop controls the overall progress of the game / / while (flag) {/ / endless loop state variables to control / / 1. Load map character loadElement (); / / 2. Show map people (process automation (movement, collision) time = 0; loadBGM (); runGame (); / / 3. End the map try {TimeUnit.MILLISECONDS.sleep (150);} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace () } / /} / / Control the progress but as a control, please do not contact load. You can only access the element public void loadElement () {ElementManager.getInstance () .load () through the element manager. } public void runGame () {/ / this loop controls the state of play in each level map ElementManager manager = ElementManager.getInstance (); while (flag) {Map map = manager.getMap (); Set set = map.keySet () List temp = new ArrayList (); temp.addAll (set); / / the elements in the iterator cannot be changed (increased or decreased) for (int i=temp.size ()-1; I > = 0) during traversal List list -) {List list = map.get (temp.get (I)); for (int j = 0; j)
< list.size(); j++) { SuperElement superElement = list.get(j); superElement.update(); if (!superElement.isVisible()) { manager.removeElementByPx(superElement.getY(), superElement.getX()); list.remove(j); } } } //使用一个独立的方法来进行判定 PK(); //游戏的流程控制 linkGame(); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //死亡 通关状态 结束runGame方法 overGame(); time++; //一秒钟增加10 } } public void PK() { // TODO Auto-generated method stub List players = ElementManager.getInstance().getElementList("play"); List enemys = ElementManager.getInstance().getElementList("enemylist"); //进行比较 listPK(players, enemys); } public void listPK(List list1,List list2){ for (int i = 0; i < list1.size(); i++) { for (int j = 0; j < list2.size(); j++) { if (list1.get(i).gamePK(list2.get(j))) { list2.get(j).setVisible(false); } } } } public void overGame(){ Player player1 = (Player)(ElementManager.getInstance().getElementList("play").get(0)); Player player2 = (Player)(ElementManager.getInstance().getElementList("play").get(1)); if(player1.getNum()>= 1000 | | player2.getNum () > = 1000) {flag = false; new Thread () {public void run () {new audioPlay (Audio.OVER) .player () }. Start ();}} / / flow control of the game public void linkGame () {/ / Map
< String , List >Map = / / ElementManager.getInstance () .getMap (); / / List enemys = map.get ("enemylist"); / add an enemy plane / / if (time = = 0) {/ / enemys.add (Enemy.createEnemy (")) in a second / /} ElementManager.getInstance () .linkGame (time);} public static int getTime () {return time;} public static void setTime (int time) {GameThread.time = time } private void loadBGM () {new Thread () {public void run () {while (flag) {audioPlay play = new audioPlay (Audio.BGM); play.player () If (! flag) {play.stop ();}. Start () } / / the creation of enemy planes} at this point, the study on "how to realize the classic game Bubble Hall in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.