Java 小球碰撞
2017-05-31 本文已影响0人
木栈桥上
小球碰撞的关键是 判断小球是否碰撞过:
1.当两小球间距小于小球直径时发生碰撞。
2.当小球碰到边缘时发生碰撞。
import static org.junit.Assert.*;
import javax.swing.*
import org.junit.Test;
import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Draw {
static int length; //小球数量
static int[][] BallList; //小球属性数组 0,1为x,y 3,4为X,Y的标识符
public static void main(String[] args){
Draw draw=new Draw();
System.out.println("输入小球个数");
Scanner sc=new Scanner(System.in);
length=sc.nextInt();
BallList=new int[length][5];
draw.addBall(length);
JFrame frame=new JFrame();
frame.setSize(1024, 768);
frame.getContentPane().setBackground(Color.white);
Draw.Ball w=new Draw.Ball();
frame.add(w);
Thread t=new Thread(w);
t.start();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addBall(int length1){ //随机位置生成小球
for(int i=0;i<length1;i++){
BallList[i][0]=(int)(Math.random()*800+100);
BallList[i][1]=(int)(Math.random()*500+200);
BallList[i][3]=1;
BallList[i][4]=1;
}
}
public static class Ball extends Panel implements Runnable{
int a,b,c; //管理颜色的变量
ArrayList<Color> colo=new ArrayList<Color>(length); //管理颜色的数组
int[][] juge=new int[length][2]; //判断每个小球是否碰撞
public Ball(){ //给判断变量和颜色变量赋初值
for(int i=0;i<length;i++){
juge[i][0]=1;
juge[i][1]=1;
a=(int)(Math.random()*100);
b=(int)(Math.random()*100);
c=(int)(Math.random()*100);
colo.add(i, new Color(a,b,c)); //给每个小球上初色
}
}
public void paint(Graphics g){
for(int i=0;i<length;i++){
if(BallList[i][3]!=juge[i][0]||BallList[i][4]!=juge[i][1]) //当小球碰撞后改变颜色
{
a=(int)(Math.random()*100);
b=(int)(Math.random()*100);
c=(int)(Math.random()*100);
colo.set(i, new Color(a,b,c));
}
juge[i][0]=BallList[i][3]; //改变颜色后将判断变量的值设为小球的标识符
juge[i][1]=BallList[i][4];
g.setColor(colo.get(i));
//g.setFont(new Font("",0,30));
g.fillOval(BallList[i][0], BallList[i][1], 20, 20);
}
}
public void run()
{
while(true)
{
for(int i=0;i<length;i++){ //判断小球行驶轨迹 标识符为1时向右(向上)行驶
if(BallList[i][3]==1&&BallList[i][4]==1){
BallList[i][0]++;BallList[i][1]++;
if(BallList[i][0]>1000) BallList[i][3]=0; //当小球撞墙时标识符变化
if(BallList[i][1]>720) BallList[i][4]=0;
try{
Thread.sleep(1);
}catch(Exception ex){}
repaint();
}
else if(BallList[i][3]!=1&&BallList[i][4]==1)
{
BallList[i][0]--;BallList[i][1]++;
if(BallList[i][0]<10) BallList[i][3]=1;
if(BallList[i][1]>720) BallList[i][4]=0;
try{
Thread.sleep(1);
}catch(Exception ex){}
repaint();
}
else if(BallList[i][3]==1&&BallList[i][4]!=1){
BallList[i][0]++;
BallList[i][1]--;
if(BallList[i][0]>1000) BallList[i][3]=0;
if(BallList[i][1]<10) BallList[i][4]=1;
try{
Thread.sleep(1);
}catch(Exception ex){}
repaint();
}
else{
BallList[i][0]--;
BallList[i][1]--;
if(BallList[i][0]<10) BallList[i][3]=1;
if(BallList[i][1]<10) BallList[i][4]=1;
try{
Thread.sleep(1);
}catch(Exception ex){}
repaint();
}
for(int p=0;p<length;p++){ //小球两两判断是否相撞
for(int q=p+1;q<length;q++){
if(Math.abs(BallList[p][0]-BallList[q][0])<=20&&Math.abs(BallList[p][1]-BallList[q][1])<=20){ //当两小球球心间距小于等于20时判定相撞
//相撞后改变标识符
if(BallList[p][3]==1) BallList[p][3]=0;
else BallList[p][3]=1;
if(BallList[p][4]==1) BallList[p][4]=0;
else BallList[p][4]=1;
if(BallList[q][3]==1) BallList[q][3]=0;
else BallList[q][3]=1;
if(BallList[q][4]==1) BallList[q][4]=0;
else BallList[q][4]=1;
try{
Thread.sleep(1);
}catch(Exception ex){}
repaint();
}
}
}
}
}
}
}
}