JAVA代码flappy Bird飞翔的小鸟附图.docx

上传人:b****5 文档编号:29808935 上传时间:2023-07-27 格式:DOCX 页数:14 大小:124.11KB
下载 相关 举报
JAVA代码flappy Bird飞翔的小鸟附图.docx_第1页
第1页 / 共14页
JAVA代码flappy Bird飞翔的小鸟附图.docx_第2页
第2页 / 共14页
JAVA代码flappy Bird飞翔的小鸟附图.docx_第3页
第3页 / 共14页
JAVA代码flappy Bird飞翔的小鸟附图.docx_第4页
第4页 / 共14页
JAVA代码flappy Bird飞翔的小鸟附图.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

JAVA代码flappy Bird飞翔的小鸟附图.docx

《JAVA代码flappy Bird飞翔的小鸟附图.docx》由会员分享,可在线阅读,更多相关《JAVA代码flappy Bird飞翔的小鸟附图.docx(14页珍藏版)》请在冰豆网上搜索。

JAVA代码flappy Bird飞翔的小鸟附图.docx

JAVA代码flappyBird飞翔的小鸟附图

packageflappyBrid01;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Graphics;

importjava.awt.Graphics2D;

importjava.awt.event.MouseAdapter;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.awt.image.BufferedImage;

importjavax.imageio.ImageIO;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

publicclassBirdGameextendsJPanel{

Birdbird;

Columncolumn1,column2;

Groundground;

BufferedImagebackGround;

//分数

intscore;

//标志游戏是否结束

//booleangameOver;

BufferedImagegameOverImage;

BufferedImagestartImage;

//游戏状态

intstate;

publicstaticfinalintSTART=0;//常量名字大写

publicstaticfinalintRUNNING=1;

publicstaticfinalintGAMEOVER=2;

publicBirdGame()throwsException{

score=0;

bird=newBird();

column1=newColumn

(1);

column2=newColumn

(2);

ground=newGround();

backGround=ImageIO.read(getClass().getResource("bg.png"));

//gameOver=false;

state=START;

gameOverImage=ImageIO.read(getClass().getResource("gameover.png"));

startImage=ImageIO.read(getClass().getResource("start.png"));

}

//游戏启动方法

publicstaticvoidmain(String[]args)throwsException{

JFrameframe=newJFrame("FlappyBird");

BirdGamegame=newBirdGame();

frame.add(game);

frame.setSize(432,644);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

game.action();

}

//画图

@Override//认定是重写,下面有错就会指出

publicvoidpaint(Graphicsg){

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

g.drawImage(column1.image,column1.x-column1.width/2,column1.y

-column1.height/2,null);

g.drawImage(column2.image,column2.x-column2.width/2,column2.y

-column2.height/2,null);

g.drawImage(ground.image,ground.x,ground.y,null);

//鸟的旋转放在鸟的前面

Graphics2Dg2=(Graphics2D)g;//强转

g2.rotate(-bird.alpha,bird.x,bird.y);//(角度,中心点的x,中心点的一)

g.drawImage(bird.image,bird.x-bird.width/2,bird.y-bird.height/2,null);

//画笔再转回来

g2.rotate(bird.alpha,bird.x,bird.y);

Fontf=newFont(Font.SANS_SERIF,Font.BOLD,40);

g.setFont(f);

g.drawString(""+score,43,63);

g.setColor(newColor(255,255,255));

g.drawString(""+score,40,60);

/*

*if(gameOver){g.drawImage(gameOverImage,0,0,null);}}

*/

switch(state){

caseGAMEOVER:

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

break;

caseSTART:

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

break;

}

}

//动图片移动

publicvoidaction()throwsException{

MouseListenerl=newMouseAdapter(){

@Override

publicvoidmousePressed(MouseEvente){

try{

switch(state){

caseGAMEOVER:

//游戏结束,所有游戏内容重置

bird=newBird();

column1=newColumn

(1);

column2=newColumn

(2);

ground=newGround();

score=0;

//改变状态

state=START;

break;

caseSTART:

state=RUNNING;

caseRUNNING:

//鸟向上飞

bird.fly();

}

}catch(Exceptione1){

e1.printStackTrace();

}

}

};

this.addMouseListener(l);

while(true){

/*

*if(!

gameOver){ground.step();column1.step();column2.step();

*bird.step();bird.flappy();}

*if(bird.hit(ground)||bird.hit(column1)||bird.hit(column2)||bird.hit()){

*gameOver=true;}

*/

switch(state){

caseSTART:

//鸟挥动

bird.flappy();

//地面移动

ground.step();

break;

caseRUNNING:

ground.step();

column1.step();

column2.step();

bird.step();

bird.flappy();

//如果撞了改变状态

if(bird.hit(ground)||bird.hit(column1)||bird.hit(column2)||bird.hit()){

state=GAMEOVER;

}

//计分

if(bird.x==column1.x||bird.x==column2.x){

score++;

}

repaint();//太快,需要线程sleep

Thread.sleep(1000/60);

}

}

}

}

packageflappyBrid01;

importjava.awt.image.BufferedImage;

importjavax.imageio.ImageIO;

//鸟类

publicclassBird{

BufferedImageimage;

intx,y;

intwidth,height;

intsize;//鸟的大小。

用于做碰撞检测。

//增加属性计算鸟的位置

doubleg;//重力加速度

doublet;//两次位置的时间

doublev0;//初始速度

doublespeed;//经过t时间的速度。

鸟当前的速度。

doubles;//经过时间t之后的鸟的位移

doublealpha;//鸟的倾角弧度单位

//是鸟的动画帧

BufferedImage[]images;

intindex;

publicBird()throwsException{

image=ImageIO.read(getClass().getResource("0.png"));

width=image.getWidth();

height=image.getHeight();

x=132;

y=280;

size=40;

g=4;

v0=20;

t=0.25;

speed=v0;

s=0;

alpha=0;

images=newBufferedImage[8];

for(inti=0;i

images[i]=ImageIO.read(getClass().getResource(i+".png"));

}

index=0;

}

publicvoidflappy(){

index++;

image=images[(index/10)%8];

//除10的意义,index地面移动10个像素鸟动一下。

余8的意义,图片只有8张。

}

publicvoidstep(){

doublev=speed;

s=v*t+g*t*t/2;

y=y-(int)s;

speed=v-g*t;

alpha=Math.atan(s/8);

}

publicvoidfly(){

//重新设置初始速度,重新向上飞

speed=v0;

}

publicbooleanhit(){

booleanhit=y-size/2<0;

returnhit;

}

publicbooleanhit(Groundground){

booleanhit=y+size/2>ground.y;

if(hit){

//把鸟放在地上

y=ground.y-size/2;

alpha=-Math.PI/2;

}

returnhit;

}

publicbooleanhit(Columnc){

//鸟飞进柱子范围

if(x>c.x-size/2-c.width/2

&&x

//是否在间隙内

if(y>=c.y-c.gap/2+size/2

&&y<=c.y+c.gap/2-size/2){

returnfalse;

}

returntrue;

}

returnfalse;

}

}

packageflappyBrid01;

importjava.awt.image.BufferedImage;

importjava.util.Random;

importjavax.imageio.ImageIO;

//柱类

publicclassColumn{

BufferedImageimage;

intx,y;//附中心位置

intwidth,height;

//柱子间距.144

intgap;

//两根柱子之间的距离.245

intdistance;

Randomran=newRandom();

//构造柱子对象,初始化属性值

publicColumn(intn)throwsException{//n是第几根柱子/抛所有

image=ImageIO.read(getClass().getResource("column.png"));

width=image.getWidth();

height=image.getHeight();

gap=144;

distance=245;

x=550+(n-1)*distance;//550,第一根柱子与y轴的距离

y=ran.nextInt(218)+132;//最高中心点218到最低中心点350。

}

publicvoidstep(){

x--;

if(x+width/2<=0){//if(x==-width/2)

x=distance*2-width/2;//x=432+width/2;

y=ran.nextInt(218)+132;

}

}

}

packageflappyBrid01;

importjava.awt.image.BufferedImage;

importjavax.imageio.ImageIO;

//地类

publicclassGround{

BufferedImageimage;

intx,y;

intwidth;

intheight;

publicGround()throwsException{

image=ImageIO.read(getClass().getResource("ground.png"));

width=image.getWidth();

height=image.getHeight();

x=0;

y=500;

}

publicvoidstep(){

x--;

if(x==-109){

x=0;

}

}

}

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

当前位置:首页 > 求职职场 > 自我管理与提升

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1