程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java基於Swing完成的狩獵射擊游戲代碼

Java基於Swing完成的狩獵射擊游戲代碼

編輯:關於JAVA

Java基於Swing完成的狩獵射擊游戲代碼。本站提示廣大學習愛好者:(Java基於Swing完成的狩獵射擊游戲代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是Java基於Swing完成的狩獵射擊游戲代碼正文


本文實例講述了Java基於Swing完成的狩獵射擊游戲代碼。分享給年夜家供年夜家參考。

詳細完成代碼以下:


package Game;

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

public class BackgroundPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        private Image image;// 配景圖片

        public BackgroundPanel() {
                setOpaque(false);
                setLayout(null);
        }

        public void setImage(Image image) {
                this.image = image;
        }

        /**
         * 畫出配景
         */
        protected void paintComponent(Graphics g) {
                if (image != null) {
                        // 圖片寬度
                        int width = getWidth();
                        // 圖片高度
                        int height = getHeight();
                        // 畫出圖片
                        g.drawImage(image, 0, 0, width, height, this);
                }
                super.paintComponent(g);
        }
}


package Game;

import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;

public class BirdLabel extends JLabel implements Runnable {
        private static final long serialVersionUID = 1L;
        // 隨機生成線程的休眠時光,即掌握小鳥挪動速度
        private int sleepTime = (int) (Math.random() * 300) + 5;
        private int y = 100;
        private Thread thread;// 將線程作為成員變量
        private Container parent;
        private int score = 15;// 該類腳色對應的分數

        /**
         * 結構辦法
         */
        public BirdLabel() {
                super();
                // 創立小鳥圖標對象
                ImageIcon icon = new ImageIcon(getClass().getResource("bird.gif"));
                setIcon(icon);// 設置控件圖標
                addMouseListener(new MouseAction());// 添加鼠標事宜監聽器
                // 添加控件事宜監聽器
                addComponentListener(new ComponentAction());
                thread = new Thread(this);// 創立線程對象
        }

        /**
         * 控件的控件事宜監聽器
         */
        private final class ComponentAction extends ComponentAdapter {
                public void componentResized(final ComponentEvent e) {
                        thread.start();// 線程啟動
                }
        }

        /**
         * 控件的鼠標事宜監聽器
         */
        private final class MouseAction extends MouseAdapter {
                public void mousePressed(final MouseEvent e) {
                        if (!MainFrame.readyAmmo())// 假如槍彈沒有預備好
                                return;// 甚麼也不做
                        MainFrame.useAmmo();// 消費槍彈
                        appScore();// 加分
                        destory();// 燒毀本組件
                }
        }

        public void run() {
                parent = null;
                int width = 0;
                try {
                        while (width <= 0 || parent == null) {
                                if (parent == null) {
                                        parent = getParent();// 獲得父容器
                                } else {
                                        width = parent.getWidth();// 獲得父容器的寬度
                                }
                                Thread.sleep(10);
                        }
                        for (int i = width; i > 0 && parent != null; i -= 8) {
                                setLocation(i, y);// 從右向左挪動本組件地位
                                Thread.sleep(sleepTime);// 休眠少焉
                        }
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                if (parent != null) {
                        MainFrame.appScore(-score * 10); // 天然燒毀將扣分
                }
                destory();// 挪動終了,燒毀本組件
        }

        /**
         * 自在器移除本組件的辦法
         */
        public void destory() {
                if (parent == null)
                        return;
                parent.remove(this);// 從父容器中移除本逐步
                parent.repaint();
                parent = null; // 經由過程該語句終止線程輪回
        }

        /**
         * 加分的辦法
         */
        private void appScore() {
                System.out.println("小鳥被擊中");
                MainFrame.appScore(15);
        }
}


package Game;

import java.awt.Container;
import java.awt.event.*;

import javax.swing.*;

public class PigLabel extends JLabel implements Runnable {
        private static final long serialVersionUID = 1L;
        // 隨機生成休眠時光,即野豬挪動速度
        private int sleepTime = (int) (Math.random() * 300) + 30;
        private int y = 260;// 控件的垂直坐標
        private int score = 10;// 該腳色對應的分數
        private Thread thread;// 內置線程對象
        private Container parent;// 控件的父容器對象

        /**
         * 結構辦法
         */
        public PigLabel() {
                super();
                ImageIcon icon = new ImageIcon(getClass().getResource("pig.gif"));// 加載野豬圖片
                setIcon(icon);// 設置本組件的圖標
                // 添加鼠標事宜適配器
                addMouseListener(new MouseAdapter() {
                        // 按下鼠標按鍵的處置辦法
                        public void mousePressed(final MouseEvent e) {
                                if (!MainFrame.readyAmmo())
                                        return;
                                MainFrame.useAmmo();// 消費槍彈
                                appScore();// 給游戲加分
                                destory();// 燒毀本組件
                        }
                });
                // 添加組件事宜適配器
                addComponentListener(new ComponentAdapter() {
                        // 調劑組件年夜小時
                        public void componentResized(final ComponentEvent e) {
                                thread.start();// 啟動線程
                        }
                });
                // 初始化線程對象
                thread = new Thread(this);
        }

        public void run() {
                parent = null;
                int width = 0;
                while (width <= 0 || parent == null) {// 獲得父容器寬度
                        if (parent == null)
                                parent = getParent();
                        else
                                width = parent.getWidth();
                }
                // 從左向右挪動本組件
                for (int i = 0; i < width && parent != null; i += 8) {
                        setLocation(i, y);
                        try {
                                Thread.sleep(sleepTime);// 休眠少焉
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
                if (parent != null) {
                        MainFrame.appScore(-score * 10); // 天然燒毀將扣分
                }
                destory();
        }

        /**
         * 自在器移除本組件的辦法
         */
        public void destory() {
                if (parent == null)
                        return;
                parent.remove(this);
                parent.repaint();
                parent = null; // 經由過程該語句終止線程輪回
        }

        /**
         * 加分的辦法
         */
        private void appScore() {
                System.out.println("野豬被擊中");
                MainFrame.appScore(10);
        }
}


package Game;

import static java.lang.Math.random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainFrame extends JFrame {
        private static final long serialVersionUID = 1L;
        private static long score = 0;// 分數
        private static Integer ammoNum = 5;// 槍彈數目
        private static JLabel scoreLabel;// 分數
        private BackgroundPanel backgroundPanel;
        private static JLabel ammoLabel;
        private static JPanel infoPane;

        /**
         * 結構辦法
         */
        public MainFrame() {
                super();
                setResizable(false);// 進制調劑窗體年夜小
                setTitle("狩獵游戲");
                infoPane = (JPanel) getGlassPane();// 獲得玻璃面板
                JLabel label = new JLabel("裝載槍彈……");// 創立提醒標簽組件
                label.setHorizontalAlignment(SwingConstants.CENTER);
                label.setFont(new Font("楷體", Font.BOLD, 32));
                label.setForeground(Color.RED);
                infoPane.setLayout(new BorderLayout());
                infoPane.add(label);// 添加提醒標簽組件到玻璃面板

                setAlwaysOnTop(true);// 是窗體堅持在最頂層
                setBounds(100, 100, 573, 411);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                backgroundPanel = new BackgroundPanel();// 創立帶配景的面板
                backgroundPanel.setImage(new ImageIcon(getClass().getResource(
                                "background.jpg")).getImage());// 設置配景圖片
                getContentPane().add(backgroundPanel, BorderLayout.CENTER);
                // 添加鼠標事宜適配器
                addMouseListener(new FrameMouseListener());
                scoreLabel = new JLabel();// 顯示分數的標簽組件
                scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
                scoreLabel.setForeground(Color.ORANGE);
                scoreLabel.setText("分數:");
                scoreLabel.setBounds(25, 15, 120, 18);
                backgroundPanel.add(scoreLabel);
                ammoLabel = new JLabel();// 顯示主動數目的標簽組件
                ammoLabel.setForeground(Color.ORANGE);
                ammoLabel.setHorizontalAlignment(SwingConstants.RIGHT);
                ammoLabel.setText("槍彈數目:" + ammoNum);
                ammoLabel.setBounds(422, 15, 93, 18);
                backgroundPanel.add(ammoLabel);
        }

        /**
         * 加分辦法
         */
        public synchronized static void appScore(int num) {
                score += num;
                scoreLabel.setText("分數:" + score);
        }

        /**
         * 消費槍彈的辦法
         */
        public synchronized static void useAmmo() {
                synchronized (ammoNum) {
                        ammoNum--;// 槍彈數目遞加
                        ammoLabel.setText("槍彈數目:" + ammoNum);
                        if (ammoNum <= 0) {// 斷定槍彈能否小於0
                                new Thread(new Runnable() {
                                        public void run() {
                                                // 顯示提醒信息面板
                                                infoPane.setVisible(true);
                                                try {
                                                        // 1秒鐘裝載槍彈的時光
                                                        Thread.sleep(1000);
                                                } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                }
                                                ammoNum = 5;// 恢復槍彈數目
                                                // 修正槍彈數目標簽的文本
                                                ammoLabel.setText("槍彈數目:" + ammoNum);
                                                infoPane.setVisible(false);// 隱蔽提醒信息面板
                                        }
                                }).start();
                        }
                }
        }

        /**
         * 斷定槍彈能否夠用
         *
         */
        public synchronized static boolean readyAmmo() {
                synchronized (ammoNum) {
                        return ammoNum > 0;
                }
        }

        public static void main(String args[]) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        MainFrame frame = new MainFrame();
                                        frame.setVisible(true);
                                        frame.start();
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * 啟動游戲的辦法
         */
        public void start() {
                new PigThread().start();
                new BirdThread().start();
        }

        /**
         * 窗體的鼠標事宜監聽器
         *
         */
        private final class FrameMouseListener extends MouseAdapter {
                public void mousePressed(final MouseEvent e) {
                        Component at = backgroundPanel.getComponentAt(e.getPoint());
                        if (at instanceof BackgroundPanel) {// 假如點到面板也扣除槍彈
                                MainFrame.useAmmo();// 消費槍彈
                        }
                        /*
                         * if (at instanceof BirdLabel) {// 假如點到小鳥 MainFrame.appScore(32);//
                         * 加分 } if (at instanceof PigLabel) {// 假如點到野豬
                         * MainFrame.appScore(11);// 加分 }
                         */
                }
        }

        /**
         * 生成豬腳色的線程
         *
         */
        class PigThread extends Thread {
                @Override
                public void run() {
                        while (true) {
                                // 創立代表野豬的標簽控件
                                PigLabel pig = new PigLabel();
                                pig.setSize(120, 80);// 設置控件初始年夜小
                                backgroundPanel.add(pig);// 添加控件到配景面板
                                try {
                                        // 線程隨機休眠一段時光
                                        sleep((long) (random() * 3000) + 500);
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }

        /**
         * 生成鳥腳色的線程
         *
         */
        class BirdThread extends Thread {
                @Override
                public void run() {
                        while (true) {
                                // 創立代表小鳥的標簽控件
                                BirdLabel bird = new BirdLabel();
                                bird.setSize(50, 50);// 設置控件初始年夜小
                                backgroundPanel.add(bird);// 添加控件到配景面板
                                try {
                                        // 線程隨機休眠一段時光
                                        sleep((long) (Math.random() * 3000) + 500);
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                        }
                }
        }
}

願望本文所述對年夜家的Java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved