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

📄 gameframe.java.bak

📁 打地鼠游戏,简单的java图形应用,初学者实现的不好请大家原谅
💻 BAK
字号:
package src;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import java.awt.image.CropImageFilter;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.image.BufferedImage;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;

/**GameMap用来载入背景图片-----------*/
class GameMap extends Component
{
	Image mapPic;//图片缓存

	//ID 是图片序号 , width是需要的宽度 ,height是需要的高度
	GameMap(int ID,int Width,int Height,String temp){
		String path = temp;//得到当前路径!!!!有问题
		String  = "";
		try{
			File f = new File(path,"info");//图片信息资料!!!!可扩展功能
			FileReader in = new FileReader(f);
			BufferedReader bufferIn = new BufferedReader(in);
			int count = 1;
			while((s=bufferIn.readLine())!=null){
				if(count == ID){//ID实际上代表读取文件中的第几行
					break;
				}
				else count++;
			}
			bufferIn.close();
			in.close();
		}
		catch(IOException e){System.out.println("file:info read error");return;}
		String[] result = s.split(",");
//!!!!这里还可以优化
		mapPic = Toolkit.getDefaultToolkit().getImage(path + "\\pic\\" + result[1]);//得到原是图片
		mapPic = mapPic.getScaledInstance(Width,Height,Image.SCALE_SMOOTH);//图片缩放
		MediaTracker tracker = new MediaTracker(this); 
		tracker.addImage(mapPic,0); 
		try{tracker.waitForID(0);tracker.removeImage(mapPic);}catch(InterruptedException exception){System.out.println("pic get error");return;} 
	}
};

/**CreateMouse用来建立地鼠出现的新线程*/
class CreateMouse extends Thread
{
	myItem mouse;
	CreateMouse(myItem temp){
		mouse = temp;
	}
	public void run(){
		mouse.run();
	}
		
};

/**游戏的主要界面*/
public class GameFrame extends JPanel
{
	Image _background;//背景图片
	int Width,Height;
	String test;//测试用

	myItem[] mice;//地鼠
	CreateMouse[] miceThread;
	int miceNum;//地鼠的数量

	environment fa;//主环境!!!!可以不要紧耦合么?
	int inited = 0,running = 0;
	
	public GameFrame(environment temp) 
	{ 
		fa = temp;
		setLayout(null);
	} 

	public void run(){//游戏正式开始,主要任务是添加地鼠,并且建立新的线程让他们活动
		running = 1;
		for(int i = 0 ;i < miceNum+2;i++){
			add(mice[i]);
			miceThread[i] = new CreateMouse(mice[i]);
			miceThread[i].start();
		}
	}

	public void init(){//游戏的初始化,设置地鼠的数量,背景图片
		Width = getSize().width;Height = getSize().height;
		_background = new GameMap(fa.picNum,Width,Height,fa.path).mapPic;
		miceNum = fa.miceNum;
		mice = new myItem[miceNum+2];
		miceThread = new CreateMouse[miceNum+2];
		int i = 0;
		for(;i < miceNum;i++){
			mice[i] = new shrewmouseEmerge(this,i,fa.path);
		}
		mice[i] = new enemy(this,i,fa.path);
		i+=1;
		mice[i] = new reward(this,i,fa.path);
		repaint();

		inited = 1;
	}

	public void paintComponent(Graphics g){//绘制背景
		super.paintComponent(g);
		if(_background !=null){
			g.drawImage(_background,0,0,null);
		}
	}
	public void gameEnd(){
		try{
			for(int i = 0 ;i < miceNum;i++){
				miceThread[i].interrupt();
				remove(mice[i]);
				repaint();
				System.out.println("finish here");
			}
		}
		catch(Exception ex){
			System.out.println("cut thread error:" + fa.getStatus());
		}
		
		running = 0 ;
	}
};

/**基础类*/
class myItem extends JButton  implements MouseMotionListener,ActionListener{
	int ID;
	Rectangle rect;
	Point position;//每个地鼠有一个位置
	String iconURL;//地鼠的外观
	GameFrame fa;//!!!!可以去掉么?
	int width , height;
	Image off_screen_buf;//用来实现双缓冲
	Graphics off_screen_gc;//用来实现双缓冲
	
	myItem(GameFrame temp,int id,String path){
		super("");
		ID = id;
		fa = temp;
		iconURL = path+"\\pic\\";
		
		addMouseMotionListener(this);
		addActionListener(this);
		setOpaque(false);
		setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
	}
//	判断是否和其他地鼠冲撞
	public int testIntersect(){
		for(int i = 0 ;i <fa.miceNum+2;i++){
			myItem temp = fa.mice[i];
			try{if(temp !=null && ID != temp.ID && rect.intersects(temp.rect) )
				return 1;
			}
			catch(Exception e){System.out.println("error at mouse:" + ID);}
		}
		return 0;
	}

	//初始化地鼠的位置!!!!包括testIntersect函数的算法还可以优化?
	public void emergeLocation(){
		Random ra = new Random();
		while(position == null || testIntersect()==1){
			position = new Point(ra.nextInt(500)+width,ra.nextInt(500)+height);
			rect = new Rectangle(position.x,position.y-height,width,height);
		}
	}
	
	public void run(){
	}
	
	public void init(){
		setVisible(false);
		fa.fa._status.message = "读取完毕,可以开始游戏";
	}
	
	public int checkRandom(int rate){
		Random ra = new Random();
		int num = ra.nextInt(100);
		if(num>rate){
			return 1;
		}
		return 0;
	}
	
	public void doChange(){}
	
	public void actionPerformed(ActionEvent e){
		init();
		doChange();
		fa.fa.update();
		try{Thread.sleep(400);}catch(Exception ex){}//鼠标点击的延迟时间
	}
	
	public void mouseMoved(MouseEvent e){
		setCursor(new Cursor(Cursor.HAND_CURSOR));//可以使用自定义!!!!
	}
	public void mouseDragged(MouseEvent e){}
}

class shrewmouseEmerge extends myItem //JButton implements MouseMotionListener,ActionListener
{

	static Image iconPic;//外观
	//Image croppedImage;//动态显示时的外观
	int displayHeight;//动态显示时的高度
	int running=0,index;

	static Image[] croppedImage;
	
	
	
	String iconName;
	
	//初始化地鼠的外观
	public void getMyIcon(){
		if(iconPic == null){
			setMyIconName();
			iconURL = iconURL + iconName;
			iconPic = Toolkit.getDefaultToolkit().getImage(iconURL);
			MediaTracker tracker = new MediaTracker(this); 
			tracker.addImage(iconPic,0); 
			try{tracker.waitForID(0);tracker.removeImage(iconPic);}   
			catch(InterruptedException exception){} 
		}
	}

	public void setMyIconName(){
		iconName = "0001.gif";
	}
	//地鼠的初始化
	public void init(){
		getcroppedImage();
		position = null;
		rect = new Rectangle();
		displayHeight = height/20;//修正新的初始化变量
		
		emergeLocation();//初始化位置
		setLocation(position.x,position.y);
		setSize(width,displayHeight);
		super.init();
	}

	public void getcroppedImage(){
		if(croppedImage == null){
			croppedImage = new Image[20];
			for(int i= 0 ;i < 20;i++){
				ImageFilter cropFilter = new CropImageFilter(0,0,width,i*height/20);
				croppedImage[i] = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(iconPic.getSource(),cropFilter));
				MediaTracker tracker = new MediaTracker(this); 
				tracker.addImage(croppedImage[i],0); 
				try{tracker.waitForID(0);tracker.removeImage(croppedImage[i]);} 
				catch(InterruptedException exception){}
			}
		}
	}
	shrewmouseEmerge(GameFrame temp,int id,String path){
		super(temp,id,path);
		width = 99;height = 102;
		
		getMyIcon();//得到外观
		getcroppedImage();
		init();//构造时初始化
	}

	public void paint(Graphics g){
		if(running == 0||index >=20){
			g.drawImage(iconPic,0,0,null);
			return;
		}
		g.drawImage(off_screen_buf,0,0,null);
	}
	
	public void doChange(){
		fa.fa.score++;
		fa.fa._status.message = "打死一个敌人,分数加1";
	}
	public void run(){
		running =1;
		//出现的算法可以优化或是重载?
		for(index = 0 ;index  <20 ;index++){
				displayHeight += height/20;
				setLocation(position.x,position.y-displayHeight);
				setSize(width,displayHeight);
				off_screen_buf = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
				off_screen_gc = off_screen_buf.getGraphics();
				off_screen_gc.drawImage(croppedImage[index],0,0,null);
				try{Thread.sleep(1);}catch(Exception e){}//动画显示延迟
				repaint();
		}
		repaint();
		try{Thread.sleep(800);}catch(Exception e){}//全部出来之后的停留
		setVisible(false);
		try{Thread.sleep(300);}catch(Exception e){}//重新出现的时间间隔
		init();
		setVisible(true);
		if(fa.running == 0){
			running = 0;
			return;
		}
		run();
	}
}

class enemy extends myItem{

	static Image iconPic;//外观
	//Image croppedImage;//动态显示时的外观
	
	int running=0;
	
	String iconName;

	enemy(GameFrame temp,int id,String path){
		super(temp,id,path);
		width=200;height=133;
		addMouseMotionListener(this);
		addActionListener(this);
		setOpaque(false);
		setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		
		getMyIcon();//得到外观
		init();//构造时初始化
		fa.fa._status.message = "读取完毕,可以开始游戏";
	}
	
	public void init(){
		position = null;
		rect = new Rectangle();
		
		emergeLocation();//初始化位置
		setLocation(position.x,position.y);
		setSize(width,height);
		super.init();
	}
	
	public void paint(Graphics g){
		g.drawImage(off_screen_buf,0,0,null);
	}
	
	public void getMyIcon(){
		if(iconPic == null){
			setMyIconName();
			iconURL = iconURL + iconName;
			iconPic = Toolkit.getDefaultToolkit().getImage(iconURL);
			MediaTracker tracker = new MediaTracker(this); 
			tracker.addImage(iconPic,0); 
			try{tracker.waitForID(0);tracker.removeImage(iconPic);}   
			catch(InterruptedException exception){} 
		}
	}
	
	public void run(){
		if(checkRandom(80)==1&&fa.fa.score>=5){
			running = 1;
			off_screen_buf = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
			off_screen_gc = off_screen_buf.getGraphics();
			off_screen_gc.drawImage(iconPic,0,0,null);
			repaint();
			try{Thread.sleep(5000);}catch(Exception e){}//全部出来之后的停留
			setVisible(false);
			try{Thread.sleep(5000);}catch(Exception e){}//重新出现的时间间隔
			init();
			setVisible(true);
		}
		else{
			try{Thread.sleep(1000);}catch(Exception e){}//重新出现的时间间隔
		}
		if(fa.running == 0){
			running = 0;
			return;
		}
		run();
	}
	
	public void setMyIconName(){
		iconName = "0002.gif";
	}
	
	public void doChange(){
		fa.fa.score-=2;
		fa.fa.nowTime += 3;
		fa.fa._status.message = "你打错了";
	}
}

class reward extends myItem{

	static Image iconPic;//外观
	
	int running = 0;
	
	String iconName;

	reward(GameFrame temp,int id,String path){
		super(temp,id,path);
		width=45;height=66;
		
		getMyIcon();//得到外观
		init();//构造时初始化
	}
	
	public void init(){
		position = null;
		rect = new Rectangle();
		
		emergeLocation();//初始化位置
		setLocation(position.x,position.y);
		setSize(width,height);
		super.init();
	}
	
	public void paint(Graphics g){
		g.drawImage(off_screen_buf,0,0,null);
	}
	
	public void getMyIcon(){
		if(iconPic == null){
			setMyIconName();
			iconURL = iconURL + iconName;
			iconPic = Toolkit.getDefaultToolkit().getImage(iconURL);
			MediaTracker tracker = new MediaTracker(this); 
			tracker.addImage(iconPic,0); 
			try{tracker.waitForID(0);tracker.removeImage(iconPic);}   
			catch(InterruptedException exception){} 
		}
	}
	
	public void run(){
		if(checkRandom(80)==1&&fa.fa.nowTime>=30){
			running = 1;
			off_screen_buf = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB);
			off_screen_gc = off_screen_buf.getGraphics();
			off_screen_gc.drawImage(iconPic,0,0,null);
			repaint();
			try{Thread.sleep(2000);}catch(Exception e){}//全部出来之后的停留
			setVisible(false);
			try{Thread.sleep(5000);}catch(Exception e){}//重新出现的时间间隔
			init();
			setVisible(true);
			
		}
		else{
			try{Thread.sleep(1000);}catch(Exception e){}//重新出现的时间间隔
		}
		if(fa.running == 0){
			running = 0;
			return;
		}
		run();
	}
	
	public void setMyIconName(){
		iconName = "0005.gif";
	}
	
	public void doChange(){
		fa.fa.score+=5;
		fa.fa.nowTime -= 1;
		fa.fa._status.message = "打到一个宝物,分数加5,时间减1";
	}
}

⌨️ 快捷键说明

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