java实现捕鱼达人游戏
本文实例为大家分享了java实现捕鱼达人游戏的具体代码,供大家参考,具体内容如下
效果图如下:
源代码分享:
测试类:
packagegame; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjavax.imageio.ImageIO; importjavax.swing.JFrame; /** *测试类 *@authorLenovo * */ publicclassClient{ publicstaticvoidmain(String[]args)throwsIOException{ //创建窗口 JFramegameFrame=newJFrame("捕鱼达人"); //将池塘放入到界面中去 Poolpool=newPool(); gameFrame.setContentPane(pool); //创建窗口图标,绝对路径 BufferedImageicon=ImageIO.read(newFile("E:/New_life/fish_game/resource/images/fish07_03.png")); gameFrame.setIconImage(icon); //设置窗口的尺寸 gameFrame.setSize(800,500); //窗口的位置 gameFrame.setLocation(10,10); //设置窗口不可拖拽 gameFrame.setResizable(false); //设置窗口可以关闭 gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //让窗口显示 gameFrame.setVisible(true); //调用方法 pool.action(); } }
大炮的设置:
packagegame; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjavax.imageio.ImageIO; publicclassCannon{ //大炮的图片 privateBufferedImageimage; //坐标值 privateintx; privateinty; publicCannon()throwsIOException{ this.image=ImageIO.read(newFile("resource/images/barrel.png")); this.x=420; this.y=400; } publicBufferedImagegetImage(){ returnimage; } publicvoidsetImage(BufferedImageimage){ this.image=image; } publicintgetX(){ returnx; } publicvoidsetX(intx){ this.x=x; } publicintgetY(){ returny; } publicvoidsetY(inty){ this.y=y; } }
与鱼塘的设置:
packagegame; importjava.awt.Graphics; importjava.awt.Graphics2D; importjava.awt.event.MouseAdapter; importjava.awt.event.MouseEvent; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjava.util.ArrayList; importjava.util.LinkedList; importjavax.imageio.ImageIO; importjavax.swing.JPanel; publicclassPoolextendsJPanel{ privatestaticfinallongserialVersionUID=1L; /** *背景图片 *海王 *鱼 *大炮 *状态栏 */ //池塘 privateBufferedImagebackgroud; //单条鱼 //privateFishfish; //多条与 privateFish[]fishes; //状态栏 privateBufferedImagestatusImage; //大炮 privateCannoncannon; //鼠标x轴 privateintmouseX; //鼠标Y轴 privateintmouseY; //渔网 privateNetnet; //子弹发射的角度 privatedoubletheta; //子弹 privateLinkedListbullets; //反射原点 publicPool()throwsIOException{ this.backgroud=ImageIO.read(newFile("resource/images/bg.jpg")); //this.fish=newFish("fish08"); //设置10条鱼 this.fishes=newFish[11]; for(inti=0;i<9;i++){ StringfishName="fish0"+(i+1); Fishfish=newFish(fishName); this.fishes[i]=fish; } this.fishes[9]=newFish("fish13"); this.fishes[10]=newFish("fish14"); //初始化状态栏 this.statusImage=ImageIO.read(newFile("resource/images/bottom-bar.png")); //初始化大炮 this.cannon=newCannon(); //调用监听器 this.addListener(); //创建网 this.net=newNet(); //数组定义 this.bullets=newLinkedList (); } privatevoidaddListener(){ //添加监听器 this.addMouseListener(newMouseAdapter(){ @Override publicvoidmouseClicked(MouseEventarg0){ System.out.println("发射子弹!"); try{ //创建子弹 Bulletbullet=newBullet(cannon.getX(),cannon.getY(),theta,Pool.this); //启动线程 bullet.start(); //将对象添加到集合中去 bullets.add(bullet); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } @Override publicvoidmouseEntered(MouseEventarg0){ //进入,让渔网显示 net.setShow(true); } @Override publicvoidmouseExited(MouseEventarg0){ //退出,让渔网消失 net.setShow(false); } }); //鼠标移动监听 this.addMouseMotionListener(newMouseAdapter(){ @Override publicvoidmouseMoved(MouseEvente){ mouseX=e.getX()+20; mouseY=e.getY(); System.out.println("("+mouseX+","+mouseY+")"); //渔网移动 net.move(mouseX,mouseY); } }); } /** *画界面 */ @Override publicvoidpaint(Graphicsarg0){ super.paint(arg0); arg0.drawImage(backgroud,0,0,backgroud.getWidth(),backgroud.getHeight(),null); for(inti=0;i getBullets(){ returnbullets; } publicvoidsetBullets(LinkedList bullets){ this.bullets=bullets; } publicFish[]getFishes(){ returnfishes; } publicvoidsetFishes(Fish[]fishes){ this.fishes=fishes; } }
鱼类的设置:
packagegame; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjava.util.Random; importjavax.imageio.ImageIO; publicclassFishextendsThread{ //宽度 @SuppressWarnings("unused") privateintwidth; @SuppressWarnings("unused") privateintheight; //位置 //x坐标 @SuppressWarnings("unused") privateintx; //y坐标 @SuppressWarnings("unused") privateinty; //图片 @SuppressWarnings("unused") privateBufferedImageimage; //速度 @SuppressWarnings("unused") privateintstep; //是否被抓 @SuppressWarnings("unused") privatebooleanisCatch; //鱼游动的图片数组 @SuppressWarnings("unused") privateBufferedImage[]images; //抓获鱼的图片 privateBufferedImage[]catchImages; //图片的下标 @SuppressWarnings("unused") privateintimagesIndex; /** *鱼的构造方法 *@paramname鱼的图片名称 *@throwsIOException */ publicFish(StringimageName)throwsIOException{ //鱼游动的初始化 this.images=newBufferedImage[10]; for(inti=0;i<10;i++){ StringfishName=imageName+"_0"+i+".png"; BufferedImagetempImage=ImageIO.read(newFile("resource/images/"+fishName)); images[i]=tempImage; } //初始化图片下标 this.imagesIndex=0; this.image=this.images[this.imagesIndex]; //初始化鱼的宽度和高度 this.width=this.image.getWidth(); this.height=this.image.getHeight(); //初始化x和y的坐标 this.x=800; Randomrandom=newRandom(); intnextInt=random.nextInt(400); this.y=nextInt; //初始化速度 this.step=random.nextInt(5); //初始化是否被抓住 this.isCatch=false; this.catchImages=newBufferedImage[2]; this.catchImages[0]=ImageIO.read(newFile("resource/images/"+imageName+"_catch_01.png")); //this.width=image.getWidth(); } /** *鱼的游动 */ publicvoidmove(){ //坐标减去游动的速度 this.x=this.x-this.step; //切换鱼的图片 this.image=this.images[this.imagesIndex++%this.images.length]; //重新游一遍,小于鱼与横坐标则返回 if(this.x<-this.width){ //重置x坐标 this.x=800; //重置y坐标 Randomrandom=newRandom(); this.y=random.nextInt(375); //重置鱼游的速度 this.step=random.nextInt(5)+1; } //休眠 try{ sleep(50); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } /** *被捕获时翻滚 */ publicvoidturnOver(){ //切换鱼被捕获时鱼的图片 for(inti=0;i<6;i++){ this.image=this.catchImages[i%this.catchImages.length]; try{ sleep(50); }catch(InterruptedExceptione){ e.printStackTrace(); } } //重置鱼的属性,坐标,速度,是否被抓 this.x=800; Randomrandom=newRandom(); this.y=random.nextInt(375); this.step=random.nextInt(5)+1; this.isCatch=false; } @Override publicvoidrun(){ while(true){ if(this.isCatch){ turnOver(); }else{ move(); } } } /** *生成了鱼的属性set和get方法 *@return */ publicintgetWidth(){ returnwidth; } publicvoidsetWidth(intwidth){ this.width=width; } publicintgetHeight(){ returnheight; } publicvoidsetHeight(intheight){ this.height=height; } publicintgetX(){ returnx; } publicvoidsetX(intx){ this.x=x; } publicintgetY(){ returny; } publicvoidsetY(inty){ this.y=y; } publicBufferedImagegetImage(){ returnimage; } publicvoidsetImage(BufferedImageimage){ this.image=image; } publicbooleanisCatch(){ returnisCatch; } publicvoidsetCatch(booleanisCatch){ this.isCatch=isCatch; } }
鱼网的设置(这里渔网是静态的,有缺陷):
packagegame; /** *捕鱼网 *@authorLenovo * */ importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjavax.imageio.ImageIO; publicclassNet{ //图片 privateBufferedImageimage; //X坐标 privatedoublex; //Y坐标 privatedoubley; //宽度 privatedoublewidth; //高度 privatedoubleheight; //是否展示 privatebooleanisShow; /** *渔网构造方法 *@throwsIOException */ publicNet()throwsIOException{ //初始化图片 this.image=ImageIO.read(newFile("resource/images/net09.png")); this.x=100; this.y=100; this.width=this.image.getWidth(); this.height=this.image.getHeight(); this.isShow=true; } /** *渔网的移动 *@parammouseX *@parammouseY */ publicvoidmove(doublemouseX,doublemouseY){ //求渔网的中心点 doublecenterX=this.x+this.width/2; doublecenterY=this.y+this.height/2; //中心点与离鼠标的x位置 doublexx=mouseX-centerX; //中心点与离鼠标的y位置 doubleyy=mouseY-centerY; //左上角点平移 this.x=this.x+xx; this.y=this.y+yy; } publicBufferedImagegetImage(){ returnimage; } publicvoidsetImage(BufferedImageimage){ this.image=image; } publicdoublegetX(){ returnx; } publicvoidsetX(doublex){ this.x=x; } publicdoublegetY(){ returny; } publicvoidsetY(doubley){ this.y=y; } publicdoublegetWidth(){ returnwidth; } publicvoidsetWidth(doublewidth){ this.width=width; } publicdoublegetHeight(){ returnheight; } publicvoidsetHeight(doubleheight){ this.height=height; } publicbooleanisShow(){ returnisShow; } publicvoidsetShow(booleanisShow){ this.isShow=isShow; } }
发射的子弹
packagegame; importjava.awt.Point; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjavax.imageio.ImageIO; /** *发射的子弹 *@authorzouzhuo * */ publicclassBulletextendsThread{ //图片 privateBufferedImageimage; //坐标值 privateintx; privateinty; //大小 privateintwidth; privateintheight; //是否活着 privatebooleanisAlive; //速度 privateintstep; //角度 privatedoublethread; //子弹发射的原点 privatePointpoint; //池塘 privatePoolpool; publicBullet(intx,inty,Doublethread,Poolpool)throwsIOException{ this.image=ImageIO.read(newFile("resource/images/bullet1.png")); this.width=this.image.getWidth(); this.height=this.image.getHeight(); this.isAlive=true; this.step=10; this.x=x; this.y=y; this.thread=thread; this.point=newPoint(x,y); //this.point.x=x; //this.point.y=y; this.pool=pool; } /** *子弹移动的速度 */ publicvoidmove(){ this.y=this.y-this.step; //判断出界 intdistance=this.point.y-this.y; //求xx,需要进一步进行强制转换 intxx=(int)(distance*Math.sin(this.thread)); intxxx=this.point.x+xx; //求yy坐标 intyy=(int)(distance*Math.cos(this.thread)); intyyy=this.point.y-yy; //判断是否出界 if(xxx<0||xxx>800||yyy<0){ //将子弹置为死亡 this.isAlive=false; //在数组中删除子弹 this.pool.getBullets().remove(this); } //判断是否击中鱼 Fish[]fishs=pool.getFishes(); for(Fishfish:fishs){ //鱼的x坐标范围 intmaxX=fish.getX()+fish.getWidth(); //鱼的y坐标范围 intmayY=fish.getY()+fish.getHeight(); if(xxx>fish.getX()&&xxx还有一个计分板没有写上,没有开始结束的界面,渔网是静态的,这些功能都还没有实现,日后更新。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。