最后,感谢老师的指导和同学们对我的帮助。
六、附录
以下是调试后的相关算法实现:
A:
public boolean isPeng(int xNow,int yNow,int mxNow,int myNow){//利用两点之间的距离算法,判断是否撞球 int x1 = xNow+15,y1 = yNow+15; int x2 = mxNow+20,y2 = myNow+20; int mulx = x1-x2,muly = y2 -y1; double sum = Math.pow(muly, 2)+Math.pow(mulx, 2); if(sum B: public void paint(Graphics g){ if(!this.getIsOver()) //在此判断游戏是否结束; { super.paint(g);//清除原先的图像 g.setColor(Color.RED); g.setFont(new Font(\宋体\ g.drawString(\你已坚持:\ for(int i = 0;i g.setColor(groupThread.get(i).getColor()); g.fillOval(xNow,yNow,30,30); if(xNow>(getWidth()-23)||xNow<0){//碰到左右边界; groupThread.get(i).dx = -groupThread.get(i).dx; } if(yNow>(getHeight()-23)||yNow<0){//碰到上下边界; groupThread.get(i).dy = -groupThread.get(i).dy; } groupThread.get(i).setxNow(xNow+groupThread.get(i).getdx()); groupThread.get(i).setyNow(yNow+groupThread.get(i).getdy()); } } if(isMouse){ g.drawImage(new ImageIcon(\myNow, 40,40, this); } }//end paint(); C : (1)public void mousePressed(MouseEvent e) {//单击启动鼠标事件; if(e.getButton()==MouseEvent.BUTTON1){ if(isMouseThread){//鼠标线程是否活着; mouse.stop(); isMouseThread = false; } if(!isTime){ copy.schedule(new task(), 0,1000);//启动定时器; isTime = true; } if(!isGroupThread){ for(int i =0 ;i }//end if(e.getButton()==MouseEvent.BUTTON1); } (2) public void mouseReleased(MouseEvent e) { mxNow, //mouse.setmxNow(e.getX()); // mouse.setmyNow(e.getY());//设置松开点的鼠标位置; mxNow = e.getX(); myNow = e.getY(); if(!isMouseThread){ mouse = new mouseThread(mxNow,myNow,groupThread,this);// 新建鼠标线程;/启动鼠标线程;............................................................................ mouse.start(); isMouseThread = true; } } (3)public void mouseDragged(MouseEvent e) { mxNow = e.getX();// TODO Auto-generated method stub myNow = e.getY(); if(mxNow>(this.getWidth()-30)||mxNow<=0||myNow>(this.getHeight()-30)||myNow<=0){//鼠标是否碰到边界; // executorThread.shutdown(); this.setIsOver(true); copy.cancel(); JOptionPane.showMessageDialog(null, \你撞墙了,下次小心点。。。游戏结束\提示\ this.removeMouseListener(this); this.removeMouseMotionListener(this); } for(int i =0 ;i int xNow =groupThread.get(i).getxNow(); int yNow = groupThread.get(i).getyNow(); if(isPeng(xNow,yNow,mxNow,myNow)){ // executorThread.shutdown(); this.setIsOver(true); copy.cancel(); JOptionPane.showMessageDialog(null, \你撞球了){\挑战失败,游戏结束.}\提示\ //System.exit(0); this.removeMouseListener(this); this.removeMouseMotionListener(this); } //鼠标的上下左右边和 球碰撞的情况, } }
相关推荐: