package game;
import javax.swing.JFrame;
public class Ball_JF extends JFrame {
public Ball_JF(){
this.setTitle("逻辑功能图");
this.setBounds(100,100,800,600); //窗体大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗体停止运行
Ball_JP jp=new Ball_JP();
jp.run_run(); //开始运行多线程
this.add(jp); //画布添加到窗体
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Ball_JF();
}
}
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JPanel;
public class Ball_JP extends JPanel implements MouseMotionListener{ //实现监听鼠标的接口
List balls =new ArrayList(); //存放所有的鱼
public Ball_JP(){
addMouseMotionListener(this); //添加监听
for(int i=0;i<50;i++)
add_ball();
}
void add_ball(){ //随机数产生鱼的初始坐标,大小,方向,颜色等
int x=(int) (Math.random()*700);
int y=(int) (Math.random()*500);
int dir=(int) (Math.random()*4);
int d=(int) (Math.random()*30);
int r=(int) (Math.random()*255);
int g=(int) (Math.random()*255);
int b=(int) (Math.random()*255);
int sp=(int) (Math.random()*1+1);
Color col=new Color(r,g,b);
balls.add(new Ball(x,y,dir,d,sp,col));
}
public void paint(Graphics g) {
super.paint(g);
setBackground(Color.white); //隐含this 设置画布的背景颜色
for(int i=0;i<balls.size();i++){ //把所有的鱼画出来
Ball b=balls.get(i);
b.Draw(g);
}
}
public void run_run(){
new Thread(){
public void run(){
while(true){
for(int i=1;i<balls.size();i++){ //所有鱼运动一次
Ball b=balls.get(i);
b.move();
}
for(int i=0;i<balls.size()-1;i++){ //两个for循环判断所有小球是否碰撞
Ball b1=balls.get(i);
for(int j=i+1;j<balls.size();j++){
Ball b2=balls.get(j);
BallandBall bab=new BallandBall();
if(bab.balldestroy(b1, b2)==1){ //碰到了
if(b1.d>=b2.d){
b1.d+=b2.d/4;
balls.remove(j);
break;
}
else if(b1.d<b2.d){
b2.d+=b1.d/4;
balls.remove(i);
break;
}
}
}
}
repaint(); //再次调用paint()
try {
Thread.sleep(30); //休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
Ball mb=balls.get(0); //偷懒了 控制的只是第0号小球
mb.x=e.getX()-mb.d/2; //获得鼠标的坐标
mb.y=e.getY()-mb.d/2;
}
}
package game;
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
int x,y,dir,d;
int speed;
Color col;
static final int left_up=0,right_up=1,left_down=2,right_down=3;
public Ball(int x,int y,int dir,int d,int speed,Color col){
super();
this.x=x;
this.y=y;
this.dir=dir;
this.d=d;
this.speed=speed;
this.col=col;
}
void Draw(Graphics g){ //画出这个小球
g.setColor(col);
g.fillOval(x, y, d, d);
}
void move(){ //小球运动
switch (this.dir) {
case left_up:
x-=speed;y-=speed;
if(x<=0)dir=right_up;
else if(y<=0)dir=left_down;
break;
case right_up:
x+=speed;y-=speed;
if(x>=800-d)dir=left_up;
else if(y<=0)dir=right_down;
break;
case left_down:
x-=speed;y+=speed;
if(x<=0)dir=right_down;
else if(y>=600-d)dir=left_up;
break;
case right_down:
x+=speed;y+=speed;
if(x>=800-d)dir=left_down;
else if(y>=600-d)dir=right_up;
break;
}
}
}
package game;
public class BallandBall {
static final int left_up=0,right_up=1,left_down=2,right_down=3;
public void collision(Ball a,Ball b){ //两球相碰反弹 并没有调用它
int x1=a.x+a.d/2;
int y1=a.y+a.d/2;
int x2=b.x+b.d/2;
int y2=b.y+b.d/2;
double dis= Math.sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if(dis<=a.d/2+b.d/2){
switch (a.dir) {
case left_up:
a.dir=right_down;
break;
case left_down:
a.dir=right_up;
break;
case right_up:
a.dir=left_down;
break;
case right_down:
a.dir=left_up;
break;
}
switch (b.dir) {
case left_up:
b.dir=right_down;
break;
case left_down:
b.dir=right_up;
break;
case right_up:
b.dir=left_down;
break;
case right_down:
b.dir=left_up;
break;
}
}
}
public int balldestroy(Ball a,Ball b){ //判断两球是否相碰
int ret=0;
int x1=a.x+a.d/2;
int y1=a.y+a.d/2;
int x2=b.x+b.d/2;
int y2=b.y+b.d/2;
double dis= Math.sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if(dis<=a.d/2+b.d/2)ret=1;
return ret;
}
}