资源描述:
《JAVA代码 flappy Bird 飞翔的小鸟 附图.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、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.Im
2、ageIO;importjavax.swing.JFrame;importjavax.swing.JPanel;publicclassBirdGameextendsJPanel{Birdbird;Columncolumn1,column2;Groundground;BufferedImagebackGround;//分数intscore;//标志游戏是否结束//booleangameOver;BufferedImagegameOverImage;BufferedImagestartImage;//游戏状态intstate;publi
3、cstaticfinalintSTART=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"));//g
4、ameOver=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=newBird
5、Game();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.drawIma
6、ge(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.
7、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,25
8、5));g.drawString(""+score,40,60);/**if(gameOver){g.drawImage(gameOverImage,0,0,null);}}*/switch(state){caseGAMEOVER:g