day07java基础的最后一天,每天是架构

day07

练习:球球大作战简易版

package cn.tedu.game;

import java.awt.Color;

import java.awt.Graphics;

/**

  • 小球:

  • 直径、坐标、颜色、速度、方向

  • 1.设计类: 抽象类 —Ball

  • 设计类----BallMain, BallJPanel ,BallAndBall

  • 2.分析:小球的绘制

  • 让一个小球运动、多个运动、小球碰撞、大的把小的吃了

  • @author Administrator
    *
    */
    public class Ball{
    //成员变量— 属性
    int x,y, d,dir;//x坐标,y坐标,直径,方向
    int speed;//速度
    Color ballColor;//小球的颜色
    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 dir, int d, int speed, Color ballColor){
    this.x = x;
    this.y = y;
    this.dir = dir;
    this.d = d;
    this.speed = speed;
    this.ballColor = ballColor;
    }
    /绘制小球/
    public void drawBall(Graphics g){
    g.setColor(ballColor);
    g.fillOval(x, y, d, d);

    }
    //小球移动方向的判断
    public void moveBall(){

    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( y <= 0){
    		dir = RIGHT_DOWN;
    	}else if(x >= BallMain.WIDTH - d){
    		dir = LEFT_UP;
    	}
    break;
    
    case LEFT_DOWN:
    	
    	x -= speed;
    	y += speed;
    	if(x <= 0){
    		dir = RIGHT_DOWN;
    	}else if(y >= BallMain.HEIGHT - d){
    		dir = LEFT_UP;
    	}
    	break;
    	
    case RIGHT_DOWN:
    	x += speed;
    	y += speed;
    	if(x >= BallMain.WIDTH - d){
    		dir = LEFT_DOWN;
    	}else if(y >= BallMain.HEIGHT - d){
    		dir = RIGHT_UP;
    	}
    	break;
    }
    
    }
    

}

package cn.tedu.game;

/*

  • 小球碰撞分析
    */
    public class BallAndBall {
    //是否发生碰撞
    public boolean isBallCrach(Ball b1,Ball b2){

    boolean flag = false;
    int x1 = b1.x + b2.d/2;
    int y1 = b1.y + b2.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 flag;
    

    }

    //小球的碰撞分析
    public void ballCrach(Ball b1,Ball b2){
    int x1 = b1.x + b2.d/2;
    int y1 = b1.y + b2.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){
    	//小球1
    	switch(b1.dir){
    	case Ball.LEFT_UP:
    		b1.dir = Ball.RIGHT_DOWN;
    		break;
    	case Ball.RIGHT_UP:
    		b1.dir = Ball.LEFT_DOWN;
    		break;
    	case Ball.LEFT_DOWN:
    		b1.dir = Ball.RIGHT_UP;
    		break;
    	case Ball.RIGHT_DOWN:
    		b1.dir = Ball.LEFT_UP;
    		break;
    	
    	}
    
      
    		}
    //球2的方向处理
    	if(e <= b1.d/2 + b2.d/2){
    		//小球2
    		switch(b2.dir){
    		case Ball.LEFT_UP:
    			b2.dir = Ball.RIGHT_DOWN;
    			break;
    		case Ball.RIGHT_UP:
    			b2.dir = Ball.LEFT_DOWN;
    			break;
    		case Ball.LEFT_DOWN:
    			b2.dir = Ball.RIGHT_UP;
    			break;
    		case Ball.RIGHT_DOWN:
    			b2.dir = Ball.LEFT_UP;
    			break;
            }
    
        }
    

    }
    }
    package cn.tedu.game;
    import java.awt.Color;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JPanel;
    import com.sun.prism.Graphics;
    /**

    • 画布

    • @author Administrator
      *
      */
      public class BallJPanel extends JPanel{

      /集合------存储小球的/
      List ballList = new ArrayList();
      /小球的数量/
      private int ballNumber = 30;
      int score = 0;
      /构造方法/
      public BallJPanel(){
      //添加小球
      addBalls();
      }
      /添加小球/
      public void addBalls(){

      for (int i = 0; i < ballNumber; i++){
      	int x = (int) (Math.random()*BallMain.WIDTH);
      	int y  = (int) (Math.random()*BallMain.HEIGHT);
      	int d =  (int) (Math.random()*60);
      	int dir  =(int) (Math.random()*4);
      	int speed = (int) (Math.random()*20 + 10);
      	int red  = (int) (Math.random()*256);
      	int green = (int) (Math.random()*256);
      	int blue = (int) (Math.random()*256);
      	
      	Color ballColor = new Color(red,green,blue);
      	
      	//产生小球
      	Ball b  = new Ball(x, y, dir, d, speed, ballColor);
      	ballList.add(b);//将产生的小球添加到集合中
      }
      

      }
      @Override
      public void paint(java.awt.Graphics g){
      super.paint(g);
      //遍历集合中的所有小球
      for (int i = 0; i < ballList.size(); i++){
      ///取出每一个小球进行绘制
      Ball b = ballList.get(i);
      g.setColor(b.ballColor);
      g.fillOval(b.x, b.y,b.d, b.d);
      }
      g.drawString(“分数:”+score,30,45);
      }
      //线程
      public void startBalls(){
      //启动线程
      new Thread(){
      public void run(){

      		//System.out.println("启动线程了");
      	while(true){
      		//遍历所有小球
      		for(int i = 0;i <ballList.size();i++){
      			Ball b = ballList.get(i);
      			b.moveBall();
      		}
      		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){
      						score += 5;
      						b1.d+= b2.d/3;
      						ballList.remove(b2);
      						break;
      					}else if(b1.d <= b2.d){
      						score += 3;
      						b2.d+= b1.d/3;
      						ballList.remove(b1);
      						break;
      					}
      				}
      			}
      		}
      		repaint();
      		try{
      		Thread.sleep(10);
      		}catch(InterruptedException e){
      			e.printStackTrace();
      		}
      	}
      	}
      	
      }.start();
      

      }
      }
      package cn.tedu.game;
      import javax.swing.JFrame;
      /**

      • 小球的主函数类

      • @author Administrator
        *
        */
        public class BallMain extends JFrame{
        public static final int WIDTH = 1400;
        public static final int HEIGHT = 800;
        /小球的构造方法/
        public BallMain(){
        this.setTitle(“球球大作战V简易版”);
        this.setSize(WIDTH,HEIGHT);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);//居中显示

        BallJPanel bj = new BallJPanel();
        this.add(bj);
        
        
        
        this.setVisible(true);
        //加载小球启动
        bj.startBalls();
        

        }
        /*主函数,程序的入口/
        public static void main(String[] args){
        new BallMain();
        }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值