飞机大战-java--源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

packagecom;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.event.KeyEvent;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassPlane{ImagefeijiImage=null;intx=300;inty=700;intlifeCount=5;publicPlane(){try{feijiImage=ImageIO.read(Plane.class.getClassLoader().getResourceAsStream(images/feiji.png));}catch(IOExceptione){e.printStackTrace();}}publicvoiddraw(Graphicsg){//画飞机图片g.drawImage(feijiImage,x,y,null);//飞机移动this.move();//血条if(lifeCount0){g.setColor(Color.WHITE);g.fillRect(20,80,100,10);g.setColor(Color.red);g.fillRect(20,80,(100/5)*lifeCount,10);g.setColor(Color.blue);g.setFont(newFont(幼圆,Font.BOLD,30));g.drawString(Score:+Play01.count,20,60);}}publicvoidmove(){if(isUP&&!isDown&&!isLeft&&!isRight){//上y=y-5;}elseif(!isUP&&isDown&&!isLeft&&!isRight){//下y=y+5;}elseif(!isUP&&!isDown&&isLeft&&!isRight){//左x=x-5;}elseif(!isUP&&!isDown&&!isLeft&&isRight){//右x=x+5;}elseif(isUP&&!isDown&&isLeft&&!isRight){//左上x=x-5;y=y-5;}elseif(!isUP&&isDown&&isLeft&&!isRight){//左下x=x-5;y=y+5;}elseif(isUP&&!isDown&&!isLeft&&isRight){//右上x=x+5;y=y-5;}elseif(!isUP&&isDown&&!isLeft&&isRight){//右下x=x+5;y=y+5;}}booleanisUP=false;booleanisDown=false;booleanisLeft=false;booleanisRight=false;//摁下publicvoidkeyPressed(KeyEvente){intkeyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D){isRight=true;}elseif(keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A){isLeft=true;}elseif(keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W){isUP=true;}elseif(keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S){isDown=true;}}//放开publicvoidkeyReleased(KeyEvente){intkeyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D){isRight=false;}elseif(keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A){isLeft=false;}elseif(keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W){isUP=false;}elseif(keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S){isDown=false;}}publicRectanglegetRectangle(){returnnewRectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null));}}packagecom;publicclassPlaneStatus{publicstaticintroleNum=1;publicstaticintplayStatus=0;//0游戏开始前1第一关-1游戏结束}packagecom;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.event.KeyEvent;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassPower{ImagePowerImage=null;intx=20;inty=0;publicPower(){this.x=(int)(Math.random()*540)+20;try{PowerImage=ImageIO.read(Gift.class.getClassLoader().getResourceAsStream(images/power.png));}catch(IOExceptione){e.printStackTrace();}}publicvoiddraw(Graphicsg){//画血瓶图片g.drawImage(PowerImage,x,y,null);//血瓶移动y++;}publicRectanglegetRectangle(){returnnewRectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null));}}packagecom;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.event.KeyEvent;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjavax.imageio.ImageIO;//第一关publicclassPlay01{staticintcount=0;ImagebgImage=null;//战机Planeplane=newPlane();//战机子弹ListMyZiDanmzds=newArrayListMyZiDan();//敌机ListDijidijis=newArrayListDiji();//敌机子弹//ListDijiZiDandijizidans=newArrayListDijiZiDan();//血瓶ListGiftgifts=newArrayListGift();ListPowerpowers=newArrayListPower();publicPlay01(){try{bgImage=ImageIO.read(Play01.class.getClassLoader().getResourceAsStream(images/bg_01.jpg));}catch(IOExceptione){e.printStackTrace();}}intbgY1=0;intbgY2=-600;intfireTime=0;booleanflag=false;publicvoiddraw(Graphicsg){//画背景图片g.drawImage(bgImage,0,bgY1,null);bgY1+=5;if(bgY1=600){bgY1=0;}g.drawImage(bgImage,0,bgY2,null);bgY2+=5;if(bgY2=0){bgY2=-600;}//清理战机子弹for(inti=0;imzds.size();i++){MyZiDanmyZidan=mzds.get(i);if(myZidan.x800){mzds.remove(i);}}//添加子弹if(isFire1==true&&flag==false){if(fireTime%8==0){mzds.add(newMyZiDan(plane.x+25,plane.y+0));}fireTime++;}if(isFire1==true&&flag==true){if(fireTime%4==0){mzds.add(newMyZiDan(plane.x+8,plane.y+0));mzds.add(newMyZiDan(plane.x+52,plane.y+0));}fireTime++;}//画战机子弹for(inti=0;imzds.size();i++){MyZiDanmyZidan=mzds.get(i);myZidan.draw(g);}//清理敌机for(inti=0;idijis.size();i++){Dijidj=dijis.get(i);if(dj.x-100){dijis.remove(i);}}//抽奖:添加敌机if((int)(Math.random()*20)==5){dijis.add(newDiji());}//抽奖:添加血瓶if((int)(Math.random()*1000)==5){gifts.add(newGift());}for(inti=0;igifts.size();i++){Giftgift=gifts.get(i);gift.draw(g);}////抽奖:添加powerif((int)(Math.random()*1000)==5){powers.add(newPower());}for(inti=0;ipowers.size();i++){Powerpower=powers.get(i);power.draw(g);}//画敌机for(inti=0;idijis.size();i++){Dijidj=dijis.get(i);dj.draw(g);}//画飞机plane.draw(g);//判断战机相撞for(inti=0;idijis.size();i++){//先得到每一个敌机Dijidj=dijis.get(i);Rectangler1=dj.getRectangle();Rectangler2=plane.getRectangle();if(r1.intersects(r2)){dijis.remove(i);plane.lifeCount=plane.lifeCount-1;if(plane.lifeCount0)flag=false;if(plane.lifeCount1){PlaneStatus.playStatus=-1;}}}//判断战机和血瓶相撞for(inti=0;igifts.size();i++){//先得到每一个血瓶Giftgift=gifts.get(i);Rectangler1=gift.getRectangle();R

1 / 19
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功