小游戏大球吃小球

首先建立文档:
BallMain类:
package cn.tedu.day07;

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

import javax.swing.JFrame;
import javax.tools.Tool;

/**

  • 窗体的产生
  • @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 height = (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); //显示窗体;调用paint方法
bj.startBalls();
}

public static void main(String[] args) {
new BallMain();
}

}
Ball类:
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;

/*构造方法/ public Ball(int x, int y, int position, int d, int speed, Color ballColor) { this.x = x;
this.y = y;
this.position = position;
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 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 >= BallMain.SCREEN_WIDTH - 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 >= BallMain.SCREEN_HEIGHT - d) {
this.position = LEFT_UP;
}
break;
case RIGHT_DOWN:
x += speed;
y += speed;
if (x >= BallMain.SCREEN_WIDTH - d) {
this.position = LEFT_DOWN;
}else if(y >= BallMain.SCREEN_HEIGHT - d) {
this.position = RIGHT_UP;
}
break;
}
}
}
BallJPanel类:
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{
/*存储小球的集合/
List ballList = new ArrayList();
/*小球的数量/
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);
//System.out.println(“x = :” + x + ",y = " + y);
int position = (int) (Math.random()*4);//0,1,2,3
int d = (int) (Math.random()*60 + 1);
int speed = (int) (Math.random()*100 + 1);;
//颜色的话, 三原色 R G B
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, position, d, speed, ballColor);
//将小球添加到集合中
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();
}
}
BallAndBalll类:
package cn.tedu.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 = 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) {
//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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值