打波游戏(有瘁)
偷懒写的,基本上可以玩,没写关闭程序,自己用资源管理器关好了。[ 本帖最后由 虾B写 于 2010-10-5 20:09 编辑 ]
搜索更多相关的解决方案:
游戏
----------------解决方案--------------------------------------------------------
回复 楼主 虾B写
----------------解决方案--------------------------------------------------------
请用这个替换ball.java里的代码。
程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class ball extends Canvas implements MouseListener,MouseMotionListener,ActionListener{
private Timer time=new Timer(24,this);
private int mouseX,mouseY;
private int bb[][]={ //两点成一线定义墙,横竖斜都行.
{20,0,285,0},
{315,0,580,0},
{600,20,600,380},
{285,400,20,400},
{580,400,315,400},
{100,380,0,20}
};
private double MoChaLi=0.99;
private double cc[][]={ //定义小球,第一个为母球.
{140,340,3,0,0},//(x,y,color,弧度,力度);
{40,40,0,0,0},
{240,140,1,0,0},
{340,240,2,0,0},
{440,340,0,0,0}
};
private Color color[]={new Color(0x328aef),new Color(0x0f3a00),new Color(0xf32ae1),new Color(0xffffff)};
private int xx[]={-20,-20};
ball(){
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.setColor(new Color(0x3f32e3));
for(int b[] : bb){
g.drawLine(b[0],b[1],b[2],b[3]);
}
for(double b[] : cc){
g.setColor(color[(int)b[2]]);
g.fillOval((int)b[0],(int)b[1],20,20);
}
g.setColor(new Color(0xffffff));
g.drawOval(xx[0],xx[1],20,20);
if(xx[0]!=-20 && xx[1]!=-20){
g.drawLine( (int) cc[0][0] + 10, (int) cc[0][1]+10 , xx[0] + 10 , xx[1] + 10 );
}
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){
if(!time.isRunning()){
int zx=xx[0]- (int) cc[0][0];
int zy=xx[1]- (int) cc[0][1];
cc[0][3]=Math.atan2(zy,zx);
cc[0][4]=10;
clearMouse();
time.start();
}
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){
clearMouse();
repaint();
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){
mouseX=e.getX();
mouseY=e.getY();
if(!time.isRunning()){
xx[0]=mouseX-10;
xx[1]=mouseY-10;
repaint();
}
}
private void clearMouse(){
xx[0]=-20;
xx[1]=-20;
}
public void actionPerformed(ActionEvent e){
boolean t=true;
for(double b[] : cc){
if(b[4]!=0){
b[0]+=b[4]*Math.cos(b[3]);
b[1]+=b[4]*Math.sin(b[3]);
b[4]*=MoChaLi;
b[4]=(b[4]<0.5)?0:b[4];
t=false;
}
}
if(t){
time.stop();
xx[0]=mouseX-10;
xx[1]=mouseY-10;
}else{
double b1,b2;
//撞墙测试开始
for(double b[] : cc){
for(int i[] : bb){
double a1=Math.sqrt( (i[0]-i[2]) * (i[0]-i[2]) + (i[1]-i[3]) * (i[1]-i[3]) );
double a2=Math.sqrt( (i[0]-b[0]-10) * (i[0]-b[0]-10) + (i[1]-b[1]-10) * (i[1]-b[1]-
10) );
double a3=Math.sqrt( (i[2]-b[0]-10) * (i[2]-b[0]-10) + (i[3]-b[1]-10) * (i[3]-b[1]-
10) );
double a31=Math.acos( (a2*a2+a1*a1-a3*a3)/2/a2/a1 )*180/Math.PI;
double a21=Math.acos( (a3*a3+a1*a1-a2*a2)/2/a3/a1 )*180/Math.PI;
if(a21<=90 && a31<=90){
double oo=0;
double q1=a3*Math.cos(a21*Math.PI/180);
double q2=a3*Math.sin(a21*Math.PI/180);
double w1=a2*Math.cos(a31*Math.PI/180);
double w2=a2*Math.sin(a31*Math.PI/180);
if(q1<w1+1 && q1>w1-1){
oo=q1;
}else{oo=q2;}
if(Math.abs(oo)<11){
b[3]=(360-b[3]*180/Math.PI+2*(Math.atan2(i[1]-i[3],i[0]-i[2])*180/Math.PI))*Math.PI/180;
}
}
}
}
//碰撞测试开始
double li,vx,vy,vx1,vy1,vx2,vy2,ox,oy,vv;
for(int i=0;i<cc.length-1;i++){
for(int y=i+1;y<cc.length;y++){
vx=cc[i][0]-cc[y][0];
vy=cc[i][1]-cc[y][1];
li=Math.sqrt(vx*vx+vy*vy);
if(li<21){
vv=Math.atan2(vy,vx);
vx1=cc[i][4]*Math.cos(cc[i][3]);
vy1=cc[i][4]*Math.sin(cc[i][3]);
vx2=cc[y][4]*Math.cos(cc[y][3]);
vy2=cc[y][4]*Math.sin(cc[y][3]);
ox=Math.cos(vv)*vx1+Math.sin(vv)*vy1;
oy=Math.cos(vv)*vy1-Math.sin(vv)*vx1;
vx1=ox;
vy1=oy;
ox=Math.cos(vv)*vx2+Math.sin(vv)*vy2;
oy=Math.cos(vv)*vy2-Math.sin(vv)*vx2;
vx2=vx1;
vy2=oy;
vx1=ox;
ox=Math.cos(vv)*vx1-Math.sin(vv)*vy1;
oy=Math.cos(vv)*vy1+Math.sin(vv)*vx1;
vx1=ox;
vy1=oy;
ox=Math.cos(vv)*vx2-Math.sin(vv)*vy2;
oy=Math.cos(vv)*vy2+Math.sin(vv)*vx2;
vx2=ox;
vy2=oy;
cc[i][4]=Math.sqrt(vx1*vx1+vy1*vy1);
cc[i][3]=Math.atan2(vy1,vx1);
cc[y][4]=Math.sqrt(vx2*vx2+vy2*vy2);
cc[y][3]=Math.atan2(vy2,vx2);
}
}
}
}
if(cc[0][0]<-10 || cc[0][0]>610 || cc[0][1]<-10 || cc[0][1]>410){
cc[0][0]=100;
cc[0][1]=100;
cc[0][3]=0;
cc[0][4]=0;
}
repaint();
}
}
import java.awt.event.*;
import javax.swing.Timer;
public class ball extends Canvas implements MouseListener,MouseMotionListener,ActionListener{
private Timer time=new Timer(24,this);
private int mouseX,mouseY;
private int bb[][]={ //两点成一线定义墙,横竖斜都行.
{20,0,285,0},
{315,0,580,0},
{600,20,600,380},
{285,400,20,400},
{580,400,315,400},
{100,380,0,20}
};
private double MoChaLi=0.99;
private double cc[][]={ //定义小球,第一个为母球.
{140,340,3,0,0},//(x,y,color,弧度,力度);
{40,40,0,0,0},
{240,140,1,0,0},
{340,240,2,0,0},
{440,340,0,0,0}
};
private Color color[]={new Color(0x328aef),new Color(0x0f3a00),new Color(0xf32ae1),new Color(0xffffff)};
private int xx[]={-20,-20};
ball(){
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.setColor(new Color(0x3f32e3));
for(int b[] : bb){
g.drawLine(b[0],b[1],b[2],b[3]);
}
for(double b[] : cc){
g.setColor(color[(int)b[2]]);
g.fillOval((int)b[0],(int)b[1],20,20);
}
g.setColor(new Color(0xffffff));
g.drawOval(xx[0],xx[1],20,20);
if(xx[0]!=-20 && xx[1]!=-20){
g.drawLine( (int) cc[0][0] + 10, (int) cc[0][1]+10 , xx[0] + 10 , xx[1] + 10 );
}
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){
if(!time.isRunning()){
int zx=xx[0]- (int) cc[0][0];
int zy=xx[1]- (int) cc[0][1];
cc[0][3]=Math.atan2(zy,zx);
cc[0][4]=10;
clearMouse();
time.start();
}
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){
clearMouse();
repaint();
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){
mouseX=e.getX();
mouseY=e.getY();
if(!time.isRunning()){
xx[0]=mouseX-10;
xx[1]=mouseY-10;
repaint();
}
}
private void clearMouse(){
xx[0]=-20;
xx[1]=-20;
}
public void actionPerformed(ActionEvent e){
boolean t=true;
for(double b[] : cc){
if(b[4]!=0){
b[0]+=b[4]*Math.cos(b[3]);
b[1]+=b[4]*Math.sin(b[3]);
b[4]*=MoChaLi;
b[4]=(b[4]<0.5)?0:b[4];
t=false;
}
}
if(t){
time.stop();
xx[0]=mouseX-10;
xx[1]=mouseY-10;
}else{
double b1,b2;
//撞墙测试开始
for(double b[] : cc){
for(int i[] : bb){
double a1=Math.sqrt( (i[0]-i[2]) * (i[0]-i[2]) + (i[1]-i[3]) * (i[1]-i[3]) );
double a2=Math.sqrt( (i[0]-b[0]-10) * (i[0]-b[0]-10) + (i[1]-b[1]-10) * (i[1]-b[1]-
10) );
double a3=Math.sqrt( (i[2]-b[0]-10) * (i[2]-b[0]-10) + (i[3]-b[1]-10) * (i[3]-b[1]-
10) );
double a31=Math.acos( (a2*a2+a1*a1-a3*a3)/2/a2/a1 )*180/Math.PI;
double a21=Math.acos( (a3*a3+a1*a1-a2*a2)/2/a3/a1 )*180/Math.PI;
if(a21<=90 && a31<=90){
double oo=0;
double q1=a3*Math.cos(a21*Math.PI/180);
double q2=a3*Math.sin(a21*Math.PI/180);
double w1=a2*Math.cos(a31*Math.PI/180);
double w2=a2*Math.sin(a31*Math.PI/180);
if(q1<w1+1 && q1>w1-1){
oo=q1;
}else{oo=q2;}
if(Math.abs(oo)<11){
b[3]=(360-b[3]*180/Math.PI+2*(Math.atan2(i[1]-i[3],i[0]-i[2])*180/Math.PI))*Math.PI/180;
}
}
}
}
//碰撞测试开始
double li,vx,vy,vx1,vy1,vx2,vy2,ox,oy,vv;
for(int i=0;i<cc.length-1;i++){
for(int y=i+1;y<cc.length;y++){
vx=cc[i][0]-cc[y][0];
vy=cc[i][1]-cc[y][1];
li=Math.sqrt(vx*vx+vy*vy);
if(li<21){
vv=Math.atan2(vy,vx);
vx1=cc[i][4]*Math.cos(cc[i][3]);
vy1=cc[i][4]*Math.sin(cc[i][3]);
vx2=cc[y][4]*Math.cos(cc[y][3]);
vy2=cc[y][4]*Math.sin(cc[y][3]);
ox=Math.cos(vv)*vx1+Math.sin(vv)*vy1;
oy=Math.cos(vv)*vy1-Math.sin(vv)*vx1;
vx1=ox;
vy1=oy;
ox=Math.cos(vv)*vx2+Math.sin(vv)*vy2;
oy=Math.cos(vv)*vy2-Math.sin(vv)*vx2;
vx2=vx1;
vy2=oy;
vx1=ox;
ox=Math.cos(vv)*vx1-Math.sin(vv)*vy1;
oy=Math.cos(vv)*vy1+Math.sin(vv)*vx1;
vx1=ox;
vy1=oy;
ox=Math.cos(vv)*vx2-Math.sin(vv)*vy2;
oy=Math.cos(vv)*vy2+Math.sin(vv)*vx2;
vx2=ox;
vy2=oy;
cc[i][4]=Math.sqrt(vx1*vx1+vy1*vy1);
cc[i][3]=Math.atan2(vy1,vx1);
cc[y][4]=Math.sqrt(vx2*vx2+vy2*vy2);
cc[y][3]=Math.atan2(vy2,vx2);
}
}
}
}
if(cc[0][0]<-10 || cc[0][0]>610 || cc[0][1]<-10 || cc[0][1]>410){
cc[0][0]=100;
cc[0][1]=100;
cc[0][3]=0;
cc[0][4]=0;
}
repaint();
}
}
----------------解决方案--------------------------------------------------------
不错不错…
----------------解决方案--------------------------------------------------------