Java版打飞机小游戏
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java版打飞机小游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                放假寫的一個Java端打飛機小游戲。
復習到的知識點有:java圖形界面,多線程,集合框架等。
主要的收獲是知道了處理圖層的方式,即JLayeredPane層次面板,主要思路
如下:
1. ? ? ? ?創建一個JLayeredPanel對象實例layeredPane,聲明大小
2. ? ? ? ?將背景圖片的JPanel 放到圖層layeredPane最底層
3. ? ? ? ?將飛機,子彈的JPanel放到背景圖片之上的圖層
4. ? ? ? ?調用JFrame的setLayeredPane()方法加入圖層面板
可改進方向:
1.排行榜
2.計時計分系統
3.敵機子彈
= =感覺都有思路了就沒搞了2333
游戲截圖:
?
?
?
游戲實現類(含注釋):
public class GameFrame extends JFrame implements KeyListener {// 我的飛機private MyPlane myplane = null;//控制子彈線程private boolean flagBullet = true;//控制敵方飛機線程private boolean flagPlane = true;// 創建一個JLayeredPane用于分層的。JLayeredPane layeredPane;// 創建一個Panel和一個Label用于存放圖片,作為背景。JPanel backgroundJp;JLabel backgroundJLabel;ImageIcon image;public static GameFrame game;//子彈作業ArrayList<Bullet> bullets ;private static int x;private static int y;public GameFrame() {game = this;//實例化子彈隊列bullets = new ArrayList<>();myplane = new MyPlane();myplane.setLocation(633, 550);//創建一個用來分層的layeredPane = new JLayeredPane();image = new ImageIcon("images/background2.jpg");// 隨便找一張圖就可以看到效果。// 創建背景的那些東西backgroundJp = new JPanel();backgroundJp.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());backgroundJLabel = new JLabel(image);backgroundJp.add(backgroundJLabel);// 將jp放到最底層。layeredPane.add(backgroundJp, JLayeredPane.DEFAULT_LAYER);// 將jb放到高一層的地方layeredPane.add(myplane, JLayeredPane.MODAL_LAYER);this.setLayeredPane(layeredPane);this.setBounds(33, 0, 1300, 700); this.addKeyListener(this);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//圖標this.setIconImage(new ImageIcon("images/bfire.png").getImage());//不可放大或縮小this.setResizable(false);this.setVisible(true);//子彈線程launch();// 敵機線程otherPlane();}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {//獲得飛機位置if (myplane!= null) {x = myplane.getX();y = myplane.getY();}//每次移動大小int speed = 20;//根據不同指令執行命令if (e.getKeyChar() == 'W' || e.getKeyChar() == 'w') { // 上移y -= speed;} else if (e.getKeyChar() == 'S' || e.getKeyChar() == 's') { // 下移y += speed;} else if (e.getKeyChar() == 'A' || e.getKeyChar() == 'a') { // 左移x -= speed;} else if (e.getKeyChar() == 'D' || e.getKeyChar() == 'd') { // 右移x += speed;}//設置活動范圍if (x > -5 && x < Constant.windowx*2 - 165 && y > 0&& y < Constant.windowy*2 - 175) {if (myplane!=null) {myplane.setLocation(x, y);}}// repaint();}@Overridepublic void keyReleased(KeyEvent e) {}// 開啟子彈線程private void launch() {new Thread() {@Overridepublic void run() {//如果為true,則啟動線程,否則關閉while (flagBullet) {// 給出現時的坐標加上一個值,調整位置//以飛機坐標為參數,生成一個子彈對象Bullet bullet = new Bullet(myplane.getX() + 52,myplane.getY() + 30, game);// 將子彈加入面板layeredPane.add(bullet, JLayeredPane.MODAL_LAYER);// 添加進子彈列表GameList.bulletList.add(bullet);/*// 開始行動弄個子彈隊列然后開個線程遍歷隊列然后每個子彈的x 和 y都相加 每次重設每個子彈的位置bullets.add(bullet);for (int i = 0; i < bullets.size(); i++) {int by = bullets.get(i).getY();int bx = bullets.get(i).getX();System.out.println("第"+i+"個子彈" + x +"and" + y);bullets.get(i).setY(by-60);bullets.get(i).setLocation(bx, by);//判斷這顆子彈是否打到某個敵機GameList.intersectsBullet(bullets.get(i),game);// 將子彈加入面板layeredPane.add(bullet, JLayeredPane.POPUP_LAYER);// 如果出了界面,就跳出循環,不再執行if (by < 0) {//該顆子彈已出了可見面板,從面板中移除,跳出循環layeredPane.remove(bullet);//將子彈從列表中移除GameList.bulletList.remove(this);break;}}*/new Thread(bullet).start();// 每隔500毫秒發射一顆子彈try {Thread.sleep(300);} catch (Exception e) {e.printStackTrace();}}}}.start();}/** 啟動敵機線程*/private void otherPlane() {new Thread() {@Overridepublic void run() {//獲得第一次出現敵機的時間long firstTime = System.currentTimeMillis();//一次最多存在number數量的敵機int number = 5;while (flagPlane) {//獲得當前時間long currentTime = System.currentTimeMillis();//每10秒多增加一架可以在界面上顯示的敵機if(currentTime - firstTime > 10000){number ++;//交換數據firstTime = currentTime;}int size = GameList.planeList.size();//如果list中的敵機數量小于總共可以在界面上顯示的,則創建對象if (size <= number) {OtherPlane otherPlane = new OtherPlane(GameFrame.this);//需要把該對象放到面板中去顯示layeredPane.add(otherPlane, JLayeredPane.MODAL_LAYER);//加入listGameList.planeList.add(otherPlane);//啟動,可以活動new Thread(otherPlane).start();//沒500毫秒生成一個try {Thread.sleep(500);} catch (Exception e) {e.printStackTrace();}}}}}.start();}// 返回MyPlane對象public MyPlane getMyPlane() {return myplane;}//將我方飛機的數據清除public void setMyPlane(MyPlane myplane){this.myplane = myplane;}// 將子彈或者敵機從面板移除public void remove(Component c) {layeredPane.remove(c);}//停止子彈線程public void setFlagBullet(boolean b) {this.flagBullet = b;}//停止敵機線程public void setFlagPlane(boolean b) {this.flagPlane = b;}}?項目壓縮包:
?
總結
以上是生活随笔為你收集整理的Java版打飞机小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 7.3 BTC行情分析 整体震荡区间走势
 - 下一篇: openfire文件传输