需求分析
1、分析小球的属性:
坐标、大小、颜色、方向、速度
2、抽象类:Ball
设计类:BallMain—创建窗体
BallJPanel—画小球
BallAndBall—处理小球之间的关系
3、流程:
1)小球的绘制
2)产生小球,让一个小球进行运动,多个小球的运动
3)小球进行碰撞
4)实现大球吃小球
源代码如下:
Ball.javaimport java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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 Ball(int x, int y, int d, int speed, Color ballColor){
this.x = x;
this.y = y;
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 drawBall2(Graphics g){
g.setColor(ballColor);
g.fillOval(x, y, d, d);
//球加文字
g.setColor(Color.RED);
//设置字体大小
Font font = new Font(Font.DIALOG, Font.BOLD, 14);
g.setFont(font);
g.drawString("^_^", x+d/2, y+d/2);
}
//小球的运动方向
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;
}
}
}
BallMain.javaimportjava.awt.Dimension;
importjava.awt.Toolkit;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjavax.swing.JFrame;
importsun.audio.AudioData;
importsun.audio.AudioPlayer;
importsun.audio.AudioStream;
importsun.audio.ContinuousAudioDataStream;
/*创建窗体*/
publicclassBallMainextendsJFrame{
//窗体的宽高
publicstaticfinalintSCREEN_WIDTH=1360;
publicstaticfinalintSCREEN_HEIGHT=760;
//全屏
Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
intwidth=(int)d.getWidth();
intheight=(int)d.getHeight();
publicBallMain(){
this.setTitle("V1.0");
//设置位置
this.setBounds(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
//添加小球到窗体
BallJPanelbj=newBallJPanel();
this.add(bj);
//添加键盘的监听事件
this.addKeyListener(bj);
/*frame.addKeyListener(tj);
tj.addKeyListener(tj);
*/
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
publicstaticvoidmain(String[]args){
BallMainb=newBallMain();
//添加音乐
try{
FileInputStreamf=newFileInputStream("music/yyyy.wav");
AudioStreamas=newAudioStream(f);
AudioPlayer.player.start(as);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
BallJPanel.javaimportjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importjavax.imageio.ImageIO;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
/*画小球*/
publicclassBallJPanelextendsJPanelimplementsKeyListener{
//存储小球的集合
ListballList=newArrayList();
intx1=450;
inty1=450;
intd1=40;
privateColorwhite;
//玩家球
Ballgame=newBall(x1,y1,d1,50,white);
//小球的数量
privateintballNumber=100;
publicBallJPanel(){
addBall();
startBalls();
}
//产生小球的方法
publicvoidaddBall(){
for(inti=0;i
//随机产生100个小球
intx=(int)(Math.random()*BallMain.SCREEN_WIDTH);
inty=(int)(Math.random()*BallMain.SCREEN_HEIGHT);
intposition=(int)(Math.random()*4);
//intd=(int)(Math.random()*50+1);
intd=20;
intspeed=1;
//颜色三原色RGB
intred=(int)(Math.random()*255+1);
intgreen=(int)(Math.random()*255+1);
intblue=(int)(Math.random()*255+1);
ColorballColor=newColor(red,green,blue);
Ballb=newBall(x,y,position,d,speed,ballColor);
//将小球添加到集合中
ballList.add(b);
}
}
publicvoidpaint(Graphicsg){
super.paint(g);
BufferedImageimg=null;
//添加图片
try{
img=ImageIO.read(newFile("music/timg.png"));
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
g.drawImage(img,0,0,1360,760,this);
this.setBackground(Color.CYAN);
for(inti=0;i
Ballball=ballList.get(i);
ball.drawBall(g);
}
//玩家
game.drawBall2(g);
}
publicvoidstartBalls(){
//启动线程-----匿名内部类
newThread(){
publicvoidrun(){
while(true){
//遍历小球集合
for(inti=0;i
//取出小球
Ballb=ballList.get(i);
//让每一个小球进行移动
b.ballMove();
}
for(inti=0;i
//先取第一个小球
Ballb1=ballList.get(i);
for(intj=i+1;j
Ballb2=ballList.get(j);
BallAndBallbad=newBallAndBall();
//bad.ballCrach(b1,b2);
if(bad.isBallCrach(b1,b2)){
if(b1.d>=b2.d){
b1.d+=b2.d/3;
ballList.remove(b2);
break;
}elseif(b1.d
b2.d+=b1.d/3;
ballList.remove(b1);
break;
}
}
if(bad.isBallCrach(b1,game)){
if(bad.isBallCrach(b1,game)){
if(b1.d>game.d){
System.out.println("GAMEOEVR");
JOptionPane.showMessageDialog(null,"GAMEOVER");
return;
}else{
game.d+=b1.d/3;
ballList.remove(b1);
break;
}
}
}
}
}
repaint();//重绘
try{
Thread.sleep(5);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
};
}.start();
}
@Override
publicvoidkeyTyped(KeyEvente){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidkeyPressed(KeyEvente){
//TODOAuto-generatedmethodstub
if(e.getKeyCode()==KeyEvent.VK_UP){
//System.out.println("点击了上方向键");
game.y-=10;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
//System.out.println("点击了下方向键");
game.y+=10;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
//System.out.println("点击了左方向键");
game.x-=15;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
//System.out.println("点击了右方向键");
game.x+=15;
}
if(e.getKeyCode()==KeyEvent.VK_1){
//System.out.println("点击了右方向键");
game.x+=10;
game.y-=10;
}
if(e.getKeyCode()==KeyEvent.VK_2){
//System.out.println("点击了右方向键");
game.x-=10;
game.y-=10;
}
if(e.getKeyCode()==KeyEvent.VK_3){
//System.out.println("点击了右方向键");
game.x-=10;
game.y+=10;
}
if(e.getKeyCode()==KeyEvent.VK_4){
//System.out.println("点击了右方向键");
game.x+=10;
game.y+=10;
}
repaint();
}
@Override
publicvoidkeyReleased(KeyEvente){
//TODOAuto-generatedmethodstub
}
}
BallAndBall.java/*处理小球之间的关系*/
publicclassBallAndBall{
//两小球碰撞
publicvoidballCrach(Ballb1,Ballb2){
intx1=b1.x+b1.d/2;
inty1=b1.y+b1.d/2;
intx2=b2.x+b2.d/2;
inty2=b2.y+b2.d/2;
doublee=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
//如果碰撞上
if(e<=b1.d/2+b2.d/2){
//b1小球
switch(b1.position){
caseBall.LEFT_UP:
b1.position=Ball.RIGHT_DOWN;
break;
caseBall.RIGHT_UP:
b1.position=Ball.LEFT_DOWN;
break;
caseBall.LEFT_DOWN:
b1.position=Ball.RIGHT_UP;
break;
caseBall.RIGHT_DOWN:
b1.position=Ball.LEFT_UP;
break;
}
//b2小球
switch(b2.position){
caseBall.LEFT_UP:
b2.position=Ball.RIGHT_DOWN;
break;
caseBall.RIGHT_UP:
b2.position=Ball.LEFT_DOWN;
break;
caseBall.LEFT_DOWN:
b2.position=Ball.RIGHT_UP;
break;
caseBall.RIGHT_DOWN:
b2.position=Ball.LEFT_UP;
break;
}
}
}
//检查是否碰撞上
publicbooleanisBallCrach(Ballb1,Ballb2){
booleanflag=false;
intx1=b1.x+b1.d/2;
inty1=b1.y+b1.d/2;
intx2=b2.x+b2.d/2;
inty2=b2.y+b2.d/2;
//计算圆心距
doublee=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if(e<=b1.d/2+b2.d/2){
returntrue;
}
returnfalse;
}
}
其中音乐和图片可自行修改,只需要修改路径即可,音乐格式为wav,下载之后转变格式即可
运行截图如下: