问题描述
主类GameFrame[/u]我做的这是一个台球反弹的动画,我目的很简单,我就是想使窗口内实现好多球有起点出发然后碰壁反弹,现在我只实现了4个球的效果,每增加一个球就要多些好多行代码,请大神帮我解决这个问题,问题点请往下看packagecn.bdqn.lastGame;importjava.awt.Color;importjava.awt.Font;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.util.Random;/***游戏窗口类*@authordell**/publicclassGameFrameextendsFrame{//GUI编程:AWT,swing等Imagemoon_img=GameUtil.getImage("images/moon.png");Randomrd=newRandom();/***加载窗口*/publicvoidlaunchFrame(){setSize(600,600);setLocation(0,100);setVisible(true);setIconImage(moon_img);newPaintThread().start();//启动线程addWindowListener(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEventarg0){//TODOAuto-generatedmethodstubSystem.exit(0);}});}privatedoublexyz=30;privatedoublex0=xyz,y0=xyz;privatedoubledegree0=Math.PI/3;//[0,2pi]privatedoublex1=600-xyz,y1=xyz;privatedoubledegree1=Math.PI/3;//[0,2pi]privatedoublex2=xyz,y2=600-xyz;privatedoubledegree2=Math.PI/3;//[0,2pi]privatedoublex3=600-xyz,y3=600-xyz;privatedoubledegree3=Math.PI/3;//[0,2pi]@Overridepublicvoidpaint(Graphicsg){Colorc=g.getColor();//g.drawLine(100,100,200,200);//g.drawRect(100,100,200,200);//g.drawOval(100,100,200,200);Fontf=newFont("宋体",Font.BOLD,30);g.setFont(f);g.fillRect(0,0,600,600);g.setColor(Color.blue);g.drawString("制作人:北京企鹅",150,60);//g.drawImage(moon_img,(int)x0,(int)y0,null);g.setColor(Color.green);g.fillOval((int)x0,(int)y0,20,20);g.setColor(Color.red);g.fillOval((int)x1,(int)y1,20,20);g.setColor(Color.BLUE);g.fillOval((int)x2,(int)y2,20,20);g.setColor(Color.orange);g.fillOval((int)x3,(int)y3,20,20);g.setColor(c);//x+=3;//y+=3;x0=GamePath.linePath(x0,y0,degree0)[0];y0=GamePath.linePath(x0,y0,degree0)[1];degree0=GamePath.linePath(x0,y0,degree0)[2];x1=GamePath.linePath(x1,y1,degree1)[0];y1=GamePath.linePath(x1,y1,degree1)[1];degree1=GamePath.linePath(x1,y1,degree1)[2];x2=GamePath.linePath(x2,y2,degree2)[0];y2=GamePath.linePath(x2,y2,degree2)[1];degree2=GamePath.linePath(x2,y2,degree2)[2];x3=GamePath.linePath(x3,y3,degree3)[0];y3=GamePath.linePath(x3,y3,degree3)[1];degree3=GamePath.linePath(x3,y3,degree3)[2];}/***定义一个重画窗口的线程类,是一个内部类*@author昭**/classPaintThreadextendsThread{publicvoidrun(){while(true){xyz=30;repaint();xyz=100;repaint();xyz=250;repaint();try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}}}}publicstaticvoidmain(String[]aar){GameFramegf=newGameFrame();gf.launchFrame();}}
在内部类里我想改一次xyz的值球的初始位置就改变了因此航线也变了,然后调用一次repaint方法就有4个球再跳;再改一次xyz的值然后再调用一次repaint方法,依次类推,这样按道理就可以出现好多球,然后没循环一次球就跑一步,然后就是好多球乱跳,结果并不是我想的那样,求解?其他两个工具类如下想看可以看下:GamePath类packagecn.bdqn.lastGame;publicclassGamePath{publicstaticdouble[]linePath(doublex,doubley,doubledegree){x+=3*Math.cos(degree);y+=3*Math.sin(degree);if(y>600-20||y<30){degree=-degree;}if(x<0||x>600-30){degree=Math.PI-degree;}returnnewdouble[]{x,y,degree};}}
GameUtil类packagecn.bdqn.lastGame;importjava.awt.Image;importjava.awt.image.BufferedImage;importjava.io.IOException;importjava.net.URL;importjavax.imageio.ImageIO;/***游戏开发中常用的工具类(比如:加载图片等方法)*@author昭**/publicclassGameUtil{publicstaticImagegetImage(Stringpath){URLu=GameUtil.class.getClassLoader().getResource(path);BufferedImageimg=null;try{img=ImageIO.read(u);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnimg;}}
解决方案
本帖最后由 sinat_23447647 于 2014-12-30 17:12:06 编辑