Java游戏《疯狂女医生》代码

在手机玩过这种游戏

玩着还不错,

所以自己就写了一个程序

 

具体玩法,自己玩一次就会懂的

稳定版的Java游戏《疯狂女医生》

 

 

   Java游戏《疯狂女医生》代码_第1张图片

 

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Hufan extends JFrame{
	HuPanel hp;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Hufan();
	}
	public Hufan(){
		hp=new HuPanel();
		this.add(hp);
		new Thread(hp).start();
		this.setTitle("疯狂女医生");
		this.setSize(400, 400);
		this.setLocation(400, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}
class HuPanel extends JPanel implements MouseListener,Runnable{
	
	public static ArrayList<Balloon> al=new ArrayList<Balloon>();//存放圆
	public static ArrayList<Bomb> alBomb=new ArrayList<Bomb>();//存放子弹
	Balloon ball;
	Bomb bomb;
	Random r=new Random();
	
	public HuPanel(){
		this.addMouseListener(this);
		//初始化每个圆,并且随机大小傎
		for(int i=0;i<7;i++){
			for(int j=0;j<7;j++){
				ball=new Balloon(i, j, r.nextInt(5));
				al.add(ball);
			}
		}
	}
	
	public void paint(Graphics g){
		super.paint(g);
		//画格子
		for(int i=0;i<8;i++){
			g.drawLine(0, i*40, 280, i*40);
			g.drawLine(i*40, 0, i*40, 280);
		}
		//画球
		for(int i=0;i<al.size();i++){
			Balloon b=al.get(i); 
			if (b != null) {
				switch (b.life) {
				case 0:
					g.fillOval(b.x * 40 + 15, b.y * 40 + 10, 10, 15);
					break;
				case 1:
					g.fillOval(b.x * 40 + 10, b.y * 40 + 10, 19, 14);
					break;
				case 2:
					g.fillOval(b.x * 40 + 10, b.y * 40 + 10, 17, 22);
					break;
				case 3:
					g.fillOval(b.x * 40 + 8, b.y * 40 + 10, 25, 20);
					break;
				case 4:
					g.fillOval(b.x * 40 + 5, b.y * 40 + 5, 30, 30);
					break;
				}
			}
		}
		//画炸弹
		for(int i=0;i<alBomb.size();i++){
			
			Bomb bomb=alBomb.get(i);
			if(bomb!=null){
				g.fillOval(bomb.x, bomb.y, 10, 10);
			}
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		int x=e.getX()/40;
		int y=e.getY()/40;
		if(alBomb.size()==0){//子弹集合为0时,就是没有子弹了
			if(al.contains(new Balloon(x,y))){//如果集合里存在此对象
				for(int i=0;i<al.size();i++){
					Balloon b=al.get(i); 
					if(b.x==x && b.y==y){
						b.life++;
						if(b.life==5){
							al.remove(b);
							for(int j=0;j<4;j++){
								bomb=new Bomb(b.x*40+15, b.y*40+15, j);
								alBomb.add(bomb);
								new Thread(bomb).start();
							}
						}
						break;
					}
				}
			}else{
				ball=new Balloon(x, y, 0);
				al.add(ball);
			}
		}
	}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.repaint();
		}
	}
}
class Balloon{//圆类
	int x;
	int y;
	int life;//圆的生命历程,到5时就会爆
	public Balloon(int x, int y) {
		this.x = x;
		this.y = y;
	}
	public Balloon(int x, int y, int life) {
		this.x = x;
		this.y = y;
		this.life = life;
	}

	public boolean equals(Object obj) {
		Balloon u = (Balloon) obj;
		
		if(x==u.x && y==u.y){
			return true;
		}else{
			return false;
		}
	}
}
class Bomb implements Runnable{//子弹类
	int x;
	int y;
	int speed=5;
	int direct;
	Bomb bomb;
	boolean isStop=true;
	public Bomb(int x, int y, int direct) {
		this.x = x;
		this.y = y;
		this.direct = direct;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			switch(direct){
			case 0:
				y-=speed;
				break;
			case 1:
				x-=speed;
				break;
			case 2:
				y+=speed;
				break;
			case 3:
				x+=speed;
				break;
			}
			if(x<0||x>280||y<0||y>280){
				HuPanel.alBomb.remove(this);
			}
			for(int i=0;i<HuPanel.al.size();i++){
				Balloon b=HuPanel.al.get(i);
				//子弹碰到集合里的球时
				if(x>b.x*40+10&&x<b.x*40+20&&y>b.y*40+10&&y<b.y*40+20){
					
					HuPanel.alBomb.remove(this);//从子弹集合里删除此子弹
					
					b.life++;//圆的生命值加1
					if(b.life==5){//如果圆的生命值为5时,就要爆,并再生成四个子弹
						
						for(int j=0;j<4;j++){
							bomb=new Bomb(b.x*40+15, b.y*40+15, j);
							HuPanel.alBomb.add(bomb);
							new Thread(bomb).start();
						}
						HuPanel.al.remove(b);
					}
					isStop=false;
					HuPanel.alBomb.remove(this);//从子弹集合里删除此子弹
					break;
				}
			}
			if(isStop==false){//退出线程
				break;
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


你可能感兴趣的:(Java游戏《疯狂女医生》代码)