欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java基于Swing实现的打猎射击游戏代码

程序员文章站 2024-03-02 09:05:55
本文实例讲述了java基于swing实现的打猎射击游戏代码。分享给大家供大家参考。 具体实现代码如下: 复制代码 代码如下: package game; impor...

本文实例讲述了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程序设计有所帮助。