飞机大战java源代码.docx

上传人:b****8 文档编号:11947856 上传时间:2023-06-03 格式:DOCX 页数:29 大小:19.46KB
下载 相关 举报
飞机大战java源代码.docx_第1页
第1页 / 共29页
飞机大战java源代码.docx_第2页
第2页 / 共29页
飞机大战java源代码.docx_第3页
第3页 / 共29页
飞机大战java源代码.docx_第4页
第4页 / 共29页
飞机大战java源代码.docx_第5页
第5页 / 共29页
飞机大战java源代码.docx_第6页
第6页 / 共29页
飞机大战java源代码.docx_第7页
第7页 / 共29页
飞机大战java源代码.docx_第8页
第8页 / 共29页
飞机大战java源代码.docx_第9页
第9页 / 共29页
飞机大战java源代码.docx_第10页
第10页 / 共29页
飞机大战java源代码.docx_第11页
第11页 / 共29页
飞机大战java源代码.docx_第12页
第12页 / 共29页
飞机大战java源代码.docx_第13页
第13页 / 共29页
飞机大战java源代码.docx_第14页
第14页 / 共29页
飞机大战java源代码.docx_第15页
第15页 / 共29页
飞机大战java源代码.docx_第16页
第16页 / 共29页
飞机大战java源代码.docx_第17页
第17页 / 共29页
飞机大战java源代码.docx_第18页
第18页 / 共29页
飞机大战java源代码.docx_第19页
第19页 / 共29页
飞机大战java源代码.docx_第20页
第20页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

飞机大战java源代码.docx

《飞机大战java源代码.docx》由会员分享,可在线阅读,更多相关《飞机大战java源代码.docx(29页珍藏版)》请在冰点文库上搜索。

飞机大战java源代码.docx

飞机大战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(lifeCount>0){

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();

//战机子弹

Listmzds=newArrayList();

//敌机

Listdijis=newArrayList();

//敌机子弹

//Listdijizidans=newArrayList();

//血瓶

Listgifts=newArrayList();

Listpowers=newArrayList();

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;i

MyZiDanmyZidan=mzds.get(i);

if(myZidan.x>800){

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;i

MyZiDanmyZidan=mzds.get(i);

myZidan.draw(g);

}

//清理敌机

for(inti=0;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;i

Giftgift=gifts.get(i);

gift.draw(g);

}

//

//抽奖:

添加power

if((int)(Math.random()*1000)==5){

powers.add(newPower());

}

for(inti=0;i

Powerpower=powers.get(i);

power.draw(g);

}

//画敌机

for(inti=0;i

Dijidj=dijis.get(i);

dj.draw(g);

}

//画飞机

plane.draw(g);

//判断战机相撞

for(inti=0;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.lifeCount>0)

flag=false;

if(plane.lifeCount<1){PlaneStatus.playStatus=-1;}

}

}

//判断战机和血瓶相撞

for(inti=0;i

//先得到每一个血瓶

Giftgift=gifts.get(i);

Rectangler1=gift.getRectangle();

Rectangler2=plane.getRectangle();

if(r1.intersects(r2)){

gifts.remove(i);

if(plane.lifeCount<5){

plane.lifeCount=plane.lifeCount+1;

}

}

}

////判断战机和Power相撞

for(inti=0;i

//先得到每一个power

Powerpower=powers.get(i);

Rectangler1=power.getRectangle();

Rectangler2=plane.getRectangle();

if(r1.intersects(r2)){

powers.remove(i);

flag=true;

}

}

//判断敌机在中弹

for(inti=0;i

//得到每个战机子弹

MyZiDanmyzidan=mzds.get(i);

Rectangler1=myzidan.getRectangle();

for(intj=0;j

//每一个敌机

Dijidiji=dijis.get(j);

Rectangler2=diji.getRectangle();

if(r1.intersects(r2)){

mzds.remove(i);

dijis.remove(j);

count++;

}

}

}

}

booleanisFire1=false;

booleanisFire2=false;

publicvoidkeyPressed(KeyEvente){

plane.keyPressed(e);

if(e.getKeyCode()==KeyEvent.VK_J){

isFire1=true;

}

/*if(e.getKeyCode()==KeyEvent.VK_K){

isFire2=true;

}*/

}

publicvoidkeyReleased(KeyEvente){

plane.keyReleased(e);

if(e.getKeyCode()==KeyEvent.VK_J){

isFire1=false;

fireTime=0;

}

/*if(e.getKeyCode()==KeyEvent.VK_K){

isFire2=false;

fireTime=0;

}*/

if(e.getKeyCode()==KeyEvent.VK_P){

for(inti=0;i

dijis.remove(i);

}

}

}

 

packagecom;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Graphics;

importjava.awt.Image;

importjava.awt.event.KeyEvent;

importjava.io.IOException;

importjavax.imageio.ImageIO;

//游戏结束

publicclassOver{

Imageobg=null;

Imageng=null;

publicOver(){

try{

obg=ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/obg.jpg"));

ng=ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/ng.png"));

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddraw(Graphicsg){

g.drawImage(obg,0,0,null);

g.drawImage(ng,120,100,null);

g.setColor(Color.black);

g.setFont(newFont("幼圆",Font.BOLD,40));

g.drawString("游戏结束啦!

",200,600);

}

publicvoidkeyPressed(KeyEvente){

}

publicvoidkeyReleased(KeyEvente){

}

}

 

packagecom;

importjava.awt.Color;

importjava.awt.Graphics;

importjava.awt.Image;

importjava.awt.event.KeyAdapter;

importjava.awt.event.KeyEvent;

importjavax.swing.JFrame;

publicclassMainFrameextendsJFrame{

Beforebefore=newBefore();

Play01play01=newPlay01();

Overover=newOver();

publicMainFrame(){

//设置标题

this.setTitle("让子弹飞一会~");

//设置大小

this.setSize(600,800);

//定位居中

this.setLocationRelativeTo(null);

//添加关闭操作

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//禁止重置大小z

this.setResizable(false);

//添加键盘监听器

//KeyListener-->KeyAdapter

this.addKeyListener(newKeyAdapter(){

@Override

publicvoidkeyPressed(KeyEvente){

if(PlaneStatus.playStatus==0){

before.keyPressed(e);

}elseif(PlaneStatus.playStatus==1){

play01.keyPressed(e);

}elseif(PlaneStatus.playStatus==-1){

over.keyPressed(e)

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2