第一范文网 - 专业文章范例文档资料分享平台

Java程序设计实验报告2(弹球游戏)[1]1

来源:用户分享 时间:2025/8/17 11:58:35 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

最后,感谢老师的指导和同学们对我的帮助。

六、附录

以下是调试后的相关算法实现:

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); } //鼠标的上下左右边和 球碰撞的情况,

}

}

Java程序设计实验报告2(弹球游戏)[1]1.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2pwa78ndqp9da6a52j15_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top