大数据学习第七天

碰碰球

package day07;
/**

  • 需求分析
  • 分析小球的属性:坐标,大小(直径),颜色,方向,速度
  • 抽象类:Ball
  • 设计类:BallMain–创建窗体
  •     BallJPanel--画小球
    
  •     BallAndBall--处理小球与小球之间的关系
    
  • 流程:小球的绘制
  •    产生小球,让一个小球进行运动,多个小球的运动
    
  •    小球进行碰撞
    
  •    实现大球吃小球
    
  • @author Administrator

*/
import java.awt.Color;
import java.awt.Graphics;

public class Ball {
//小球基本属性
int x,y;//定义x,y坐标
int d;//直径
Color ballcolor;//颜色
int speed;//速度
int position;//运动方向

//小球运动方
public static final int LEFT_UP=0;
public static final int RIGHT_UP=1;
public static final int LEFT_DOWN=2;
public static final int RIGHT_DOWN=3;
//构造方法
public Ball(int x,int y ,int d,Color ballcolor,int speed,int position){
	this.x=x;
	this.y=y;
	this.d=d;
	this.ballcolor=ballcolor;
	this.speed=speed;
	this.position=position;
}
//画小球
public void drawBall(Graphics g) {
	g.setColor(ballcolor);
	g.fillOval(x, y, d, d);
}

/**小球的运动方向*/
public void ballMove(){
	switch(this.position){
	case LEFT_UP:
		x-=speed;
		y-=speed;
		if(x<=0){
			this.position=RIGHT_UP;
		}else if(y<=0){
			this.position=LEFT_DOWN;
		}
		
		break;
	case RIGHT_UP:
		x+=speed;
		y-=speed;
		if(x>=(750-d)){
			this.position=LEFT_UP;
		}else if (y<=0) {
			this.position=RIGHT_DOWN;
			
		}
		
		break;
	case LEFT_DOWN:
		x-=speed;
		y+=speed;
		if (x<=0) {
			this.position=RIGHT_DOWN;
		}else if (y>=(550-d)) {
			this.position=LEFT_UP;
			
		}
		
		break;
	case RIGHT_DOWN:
		x+=speed;
		y+=speed;
		if (x<=(750-d)) {
			this.position=LEFT_DOWN;
			
		}else if (y<=(550-d)) {
			this.position=RIGHT_UP;
			
		}
		
		break;
	}

}

}

package day07;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class BallMain extends JFrame {

//窗体宽高设置
public static final int SCREEN_WIDTH=750;
public static final int SCREEN_HEIGHT=550;
//屏幕大小
Dimension d =Toolkit.getDefaultToolkit().getScreenSize();
int width= (int)d.getWidth();
int height= (int)d.getHeight();
//构造方法
public BallMain(){
	this.setTitle("V1.8");
	//设置位置
	this.setBounds(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);

	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	//添加小球到窗体上
	BallJPanel bj=new BallJPanel();
	this.add(bj);
	//显示窗体
	this.setVisible(true);
	bj.startBalls();
}
public static void main(String[] args){
	new BallMain();
}

}

package day07;

import java.awt.Color;

import java.awt.Graphics;
import java.util.List;
import java.util.ArrayList;

import javax.swing.JPanel;

public class BallJPanel extends JPanel{

List<Ball> ballList=new ArrayList<Ball>();
//小球数量
private int ballNumber=30;
//构造方法
public BallJPanel(){
	addBall();
}

/**产生小球的方法*/
public void addBall() {
	
	for(int i=0;i<ballNumber;i++){
		//随机产生100个小球
		int x=(int)(Math.random()*BallMain.SCREEN_WIDTH);
		int y=(int)(Math.random()*BallMain.SCREEN_HEIGHT);
		int position=(int)(Math.random()*4);//0,1,2,3
		int d=(int)(Math.random()*50+1);
		int speed=(int)(Math.random()*50+1);
		//颜色
		int red=(int)(Math.random()*255+1);
		int green=(int)(Math.random()*255+1);
		int blue=(int)(Math.random()*255+1);
		Color ballcolor=new Color(red,green,blue);
		Ball b=new Ball(x, y, d, ballcolor, speed, position);

		//将小球添加到集合中
		ballList.add(b);
	}
		
	}

@Override
public void paint(Graphics g){
	super.paint(g);
	for (int i=0;i<ballList.size();i++){
		Ball ball=ballList.get(i);
		ball.drawBall(g);
	}
	
	
}
/**小球运动*/
public void startBalls() {
	//启动线程 ------匿名内部类
	new Thread() {
		
		public void run() {
			
			while (true) {
				//1.遍历小球集合
				for (int i = 0; i < ballList.size(); i++) {
					//2.取出每一个小球
					Ball b = ballList.get(i);
					//3.让每一个小球进行移动
					b.ballMove();
				}
				
				for (int i = 0; i < ballList.size(); i++) {
					//先取第一个小球
					Ball b1 = ballList.get(i);
					for (int j = i + 1; j < ballList.size(); j++) {
						Ball b2 = ballList.get(j);
						BallAndBall bab = new BallAndBall();
						//bab.ballCrach(b1, b2);//小球的碰撞
						
						if (bab.isBallCrach(b1, b2)) {
							if (b1.d >= b2.d) {
								b1.d += b2.d/3;
								ballList.remove(b2);
								break;
							}else if(b2.d >= b1.d) {
								b2.d += b1.d/3;
								ballList.remove(b1);
								break;
							}
						}
						
						
					}
				}
				
				repaint();//重绘
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		};
		
	}.start();
}

}

package day07;
/**

  • 两球碰撞检测
  • @author Administrator

*/

public class BallAndBall {
//两小球碰撞
public void ballCrach(Ball b1,Ball b2){
int x1 = b1.x+b1.d/2;
int y1 = b1.y+b1.d/2;
int x2 = b1.x+b2.d/2;
int y2 = b1.y+b2.d/2;
//计算圆心距
double e = Math.sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2));

	//如果碰撞上了
	if(e<=b1.d/2 + b2.d/2){
		//b1小球
		switch(b1.position){
		case Ball.LEFT_UP:
			b1.position = Ball.RIGHT_DOWN;
			break;
		case Ball.RIGHT_UP:
			b1.position = Ball.LEFT_DOWN;
			break;
		case Ball.LEFT_DOWN:
			b1.position = Ball.RIGHT_UP;
			break;
		case Ball.RIGHT_DOWN:
			b1.position = Ball.LEFT_UP;
			break;
		}
		//b2小球
		switch(b2.position){
		case Ball.LEFT_UP:
			b2.position = Ball.RIGHT_DOWN;
			break;
		case Ball.RIGHT_UP:
			b2.position = Ball.LEFT_DOWN;
			break;
		case Ball.LEFT_DOWN:
			b2.position = Ball.RIGHT_UP;
			break;
		case Ball.RIGHT_DOWN:
			b2.position = Ball.LEFT_UP;
			break;
		}
	}
}
//检测是否碰撞上
	public boolean isBallCrach(Ball b1, Ball b2) {
		boolean flag = false;
		int x1 = b1.x + b1.d/2;
		int y1 = b1.y + b1.d/2;
		int x2 = b2.x + b2.d/2;
		int y2 = b2.y + b2.d/2;
		
		//计算圆心距
		double e = Math.sqrt((x1 - x2) * (x1 - x2) 
				+ (y1 - y2)*(y1 - y2));
		
		if (e <= b1.d/2 + b2.d/2) {
			return true;
		}
		return false;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值