龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

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

时间:2014-11-14 11:09来源:网络整理 作者:网络 点击:
分享到:
/** * 控件的控件事件监听器 */ private final class ComponentAction extends ComponentAdapter { public void componentResized(final ComponentEvent e) { thread.start();// 线程启动 } } /** * 控

        /**
         * 控件的控件事件监听器
         */
        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();// 移动完毕,销毁本组件
        }

精彩图集

赞助商链接