java大作业的打猎游戏_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); }}

详细说明:http://java.662p.com/thread-3575-1-1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值