⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bossframe.java

📁 简单的用Java做的小游戏主要是用了自己的框架来
💻 JAVA
字号:
package frame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;


import javax.swing.JFrame;

import role.BaseRole;
import role.BossHand;
import role.BossHead;
import role.Box;

import assistant.FrameKeyEvent;
import assistant.PublicVar;
import assistant.MapRect;
import assistant.Music;
import assistant.TrackerBossImage;

/**
 * 本类为BOSS窗体类
 * @author Administrator
 *
 */
public class BossFrame extends JFrame implements Runnable{
	
	private static final long serialVersionUID = 2111855677812462037L;

	/**BOSS图片*/
	public static Image bossImage;
	
	/** 取得双缓冲画布 */
	private Image memoryImage;

	/** 产生双缓冲画笔 */
	private Graphics memoryGraphics;
	
	/** 媒体追踪器tracker */
	private MediaTracker tracker;
	
	/**是否胜利*/
	public static boolean isWin;
	
	/**角色产生计数器*/
	private int count;
	
	/**音乐*/
	public static Music playMusic;
	
	/**
	 * 构造方法
	 *
	 */
	public BossFrame(){
			/* 设置窗体大小 */
			this.setSize(650, 550);

			/* 设置窗体可见 */
			this.setVisible(true);

			/* 设置点击关闭,程序结束 */
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			
			/* 设置窗口不可改变 */
			this.setResizable(false);
			
			/*设置窗体居中*/
			this.setLocationRelativeTo(null);
			
			/*媒体追踪器*/
			this.tracker = new MediaTracker(this);
			
			TrackerBossImage.trackerImage(this.tracker,this);
			
			/*过关*/
			PublicVar.isBoss = true;
			
			this.rest();//初始化
			
			/*加入地图区域*/
			PublicVar.rectList.add(new MapRect(0,445,650,20,true));
			
			this.createRole();//创建角色
			
			/*加入松鼠*/
			PublicVar.roleList.add(PublicVar.chip);
			
			/* 创建画布 */
			memoryImage = this.createImage(650, 550);
			
			/* 得到画笔对象 */
			memoryGraphics = memoryImage.getGraphics();
			
			/*注册键盘事件*/
			this.addKeyListener(new FrameKeyEvent());
			
			//播放背景音乐
			playMusic=new Music(Music.PLAY_BOSS);
			
			/*线程启动*/
			Thread thread=new Thread(this);
			thread.start();
	}
	
	/**
	 * 创建角色
	 *
	 */
	private void createRole(){
		PublicVar.roleList.add(new BossHand(100, 235, 74, 60, PublicVar.LEFT));
		PublicVar.roleList.add(new BossHand(450, 140, 64, 50, PublicVar.RIGHT));
		PublicVar.roleList.add(new BossHead(285,105));
		
		PublicVar.roleList.add(new Box(300,415,Box.ISIRON,false,false));
	}
	
	/**
	 * 重写线程run方法完成业务逻辑
	 */
	public void run() {
		while(PublicVar.isStart){
			repaint();//窗体重绘
			
			if(isWin){//胜利
				count++;
				if(count>250){//等待胜利音乐播放完毕
					break;
				}
			}
			
			PublicVar.chipDie();//判断松鼠是否死亡
			
			try {
				Thread.sleep(20);//线程休眠
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		if(isWin){//如果胜利
			this.dispose();
			new GameOverWinFrame();//过关动画窗体
		}
	}
	
	public void update(Graphics g){
		memoryGraphics.drawImage(bossImage, 0,0,this);
		memoryGraphics.setColor(Color.white);

		for(int i=0;i<PublicVar.roleList.size();i++){//循环遍历角色集合
			BaseRole role = PublicVar.roleList.get(i);
			
			role.drawMyself(memoryGraphics, this);//绘制角色
			memoryGraphics.drawRect(role.getRect().x,role.getRect().y,
					role.getRect().width,role.getRect().height);
		}
		
		memoryGraphics.setColor(Color.green);
		
		for(int i=0;i<PublicVar.rectList.size();i++){//循环遍历地图区域集合
			MapRect mapRect=PublicVar.rectList.get(i);
			memoryGraphics.drawRect(mapRect.x,mapRect.y,mapRect.width,mapRect.height);//绘制地图区域
		}
		
		PublicVar.drawInfo(memoryGraphics);
		
		/**将内存中的图像画在画布上*/
		g.drawImage(memoryImage, 0, 0, this);
		
	}

	
	public static void main(String[] args){
		new BossFrame();
	}
	
	/**
	 * 初始化变量
	 *
	 */
	private void rest(){
		PublicVar.isRight = false;
		PublicVar.isDown = false;
		PublicVar.isFire = false;
		PublicVar.isLeft = false;
		PublicVar.isSkip = false;
		PublicVar.isUp = false;
		PublicVar.roleList.clear();
		PublicVar.rectList.clear();
		PublicVar.chip.setX(10);
		PublicVar.chip.box = null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -