使用Java实现球球大作战

package cn.tedu.day07;

import java.awt.Color;
import java.awt.Graphics;



/**

- 需求分析

- 1.分析小球的属性:

- 坐标、大小(直径)、颜色、方向、速度。

- 2.抽象类:Ball

- 设计类:BallMain---创建窗体

- BallJpanel---画小球

- BallAndBall---处理小球之间的关系

- 3.流程:

- (1)小球的绘制

- (2)产生小球,让一个小球进行运动;多个小球运动

- (3)小球进行碰撞

- (4)实现大球吃小球

- @author Administrator
  *
   */
  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;
  private static final int SCREEN_HEIGHT =550;
  private static final int SCREEN_WIDTH = 750;
  /**构造方法*/
  public Ball(int x,int y,int position,int speed,int d,Color ballcolor){
  	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 LEFT_DOWN:
  	x-=speed;
  	y+=speed;
  	if(x<=0){
  		this.position=RIGHT_DOWN;
  		
  	}else if(y>=(SCREEN_HEIGHT-d)){
  		this.position=LEFT_UP;
  	}
  	break;
  case RIGHT_UP:
  	x+=speed;
  	y-=speed;
  	if(x>=(SCREEN_WIDTH-d)){
  		this.position=LEFT_UP;
  	}else if(y<=0){
  		this.position=RIGHT_DOWN;
  	}
  break;
  case RIGHT_DOWN:
  	x+=speed;
  	y+=speed;
  	if(x>=(SCREEN_WIDTH-d)){
  		this.position=LEFT_DOWN;
  	}else if(y>=(SCREEN_HEIGHT-d)){
  		this.position=RIGHT_UP;
  	}
  	break;
  	default:
  		break;
  
  }
  
  // TODO Auto-generated method stub
  ```

  }

  

}
package cn.tedu.day07;
/**

- 两球的碰撞检测

- @author Administrator
  *
   */
  public class BallAndBall {
  //两小球的碰撞
  public void ballCrash(Ball b1,Ball b2){
  //检测是否碰上

  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){
  	switch (b1.position){
  	case Ball.LEFT_UP:
  		b1.position=Ball.RIGHT_DOWN;
  		break;
  	case Ball.LEFT_DOWN:
  		b1.position=Ball.RIGHT_UP;
  		break;
  	case Ball.RIGHT_UP:
  		b1.position=Ball.LEFT_DOWN;
  		break;
  	case Ball.RIGHT_DOWN:
  		b1.position=Ball.LEFT_UP;
  		break;
  	}
  	switch (b2.position){
  	case Ball.LEFT_UP:
  		b2.position=Ball.RIGHT_DOWN;
  		break;
  	case Ball.LEFT_DOWN:
  		b2.position=Ball.RIGHT_UP;
  		break;
  	case Ball.RIGHT_UP:
  		b2.position=Ball.LEFT_DOWN;
  		break;
  	case Ball.RIGHT_DOWN:
  		b2.position=Ball.LEFT_UP;
  		break;
  	}
  }
  }

public boolean isBallCrash(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 true;
}
```

}
package cn.tedu.day07;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;



/**

- 画小球

- @author Administrator
  *
   */
  public class BallJPanel extends JPanel{
  Listballlist=new ArrayList();
  private int ballNumber=10;
  //构造方法
  public BallJPanel(){
  	addBall();
  }
  public void addBall(){
  	

  ```
  //随机产生100个小球
  		for (int i=0;ib2.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(50);
  			} catch (InterruptedException e) {
  				// TODO: handle exception
  				e.printStackTrace();
  			}
  			
  		}
  	};			
  }.start();
  ```

}
}
  • 
     package cn.tedu.day07;
     
     import java.awt.Dimension;
     	
     	import javax.swing.JFrame;
     	import java.awt.Toolkit;
     	/**
         
     	- 窗体的产生
     	
     	- @author Administrator
     	  *
     	   */
     	  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 heigth=(int)d.getHeight();
     	  //构造方法
     	  public BallMain(){
     	  	this.setTitle("V1.0");
     	  	//设置位置
     	  	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();
        ```
      
        }
        }

你可能感兴趣的:(使用Java实现球球大作战)