日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java魔兽猎人_Java基于Swing实现的打猎射击游戏代码

發布時間:2023/12/3 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java魔兽猎人_Java基于Swing实现的打猎射击游戏代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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魔兽猎人_Java基于Swing实现的打猎射击游戏代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。