flappy bird游戏代码,flappy bird是什么类型的游戏

  flappy bird游戏代码,flappy bird是什么类型的游戏

  

目录

前言主要设计功能截图代码实现游戏启动类核心类工具类总结

 

  

前言

《布谷鸟闯关-简单版》 是一个基于爪哇岛的布谷鸟闯关游戏,摁上键控制鸟的位置穿过管道间的缝隙,需要做碰撞检测,监听键盘事件,背景图片的切换,障碍物管道产生时英语字母表的第25个字母轴上需要随机位置。

 

  

主要设计

设计游戏界面,用摇摆实现设计背景设计移动墙设计布谷鸟设计障碍物设计背景音乐和音效由几个关键的布尔类型变量开始,崩溃,结束是产生键键盘事件时用来控制界面显示的弹框的操作:空格键开始游戏,输入键取消"游戏结束"弹框,摁空格键继续,上键(向上)是对鸟的控制工具类图像管理器专用于读取图片

 

  

功能截图

游戏开始:

 

  控制布谷鸟

  飞过障碍物

  撞到障碍物,游戏结束

  

代码实现

 

  

游戏启动类

public class Main { public static void Main(String[]args){ Cuckoo frame=new Cuckoo();while(true){ try { thread。睡眠(100);如果(!框架。崩溃){帧。repaint();} } catch(中断异常e){ e . printstacktrace();} } }}

 

  

核心类

/** * 1.窗口* 2.背景* 3.移动墙* 4.布谷鸟* 5.障碍物* 6.背景音乐* 7.音效* 8.欢迎界面* 9 .游戏结束*/公开类疯狂的扩展帧{ private int stepprivate int分数;私有int高度;私有int moveLeft私有内部管道移动布尔崩溃=假;私有布尔over=false私有布尔start=false private static int bird _ y=270私有静态最终整数L _ X=800私有静态最终整数L _ Y=200私有静态最终int SPEED=2;私有静态最终int BIRD _ X=100私有静态最终int GAME _ X _ Y _ 0=0私有静态最终int GAME _ WIDTH=288私服静态最终int GAME _ HEIGHT=私人静态最终int PIPE _ WIDTH=image mgr。烟斗。getwidth();私有静态最终int PIPE _ HEIGHT=image mgr。烟斗。getheight();private static final int READY _ WIDTH=图像管理器。准备好了。getwidth();private static final int READY _ HEIGHT=image mgr。准备好了。getheight();private static final int START _ WIDTH=图像管理器。开始吧。getwidth();private static final int BIRD _ WIDTH=image mgr。鸟[0].getWidth();私有静态最终int BI

 

  RD_HEIGHT = ImageMgr.bird[0].getWidth(); private static final int GROUND_HEIGHT = ImageMgr.ground.getHeight(); private static final int GAME_OVER_WIDTH = ImageMgr.gameOver.getWidth(); private static final int GAME_OVER_HEIGHT = ImageMgr.gameOver.getHeight(); private static final int BACKGROUND_HEIGHT = ImageMgr.background.getHeight(); private static Image offScreenImage; private static Rectangle pipeRectangle1; private static Rectangle pipeRectangle2; private static Rectangle birdRectangle; Cuckoo(){ setVisible(true); setResizable(false); //setTitle("cuckoo"); setBounds(L_X, L_Y,GAME_WIDTH,GAME_HEIGHT); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()){ case KeyEvent.VK_SPACE : start = true;break; case KeyEvent.VK_ENTER : reopen();break; } } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) { bird_y -= 20; } } }); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } @Override public void update(Graphics g) { if(offScreenImage == null){ offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT); } Graphics gOffScreen = offScreenImage.getGraphics(); Color c = gOffScreen.getColor(); gOffScreen.setColor(Color.BLACK); gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT); gOffScreen.setColor(c); paint(gOffScreen); g.drawImage(offScreenImage,0,0,null); } @Override public void paint(Graphics g) { background(g); ground(g); start(g); if(start){ bird(g); pipe(g); colliedWith(); gameOver(g); score(g); } } //如果碰撞后,可以嗯enter键重开 private void reopen(){ if(crash){ score = 0; over = false; crash = false; start = false; moveLeft = 0; pipeMove = 0; //bird_y = 270;//继上次死的位置开始 } } private void score(Graphics g){ Font font = new Font("微软雅黑",Font.BOLD,18); g.setFont(font); Color c = g.getColor(); g.setColor(Color.lightGray); g.drawString("score " + score,20,70); g.setColor(c); } private void background(Graphics g){ g.drawImage(ImageMgr.background, GAME_X_Y_0 - moveLeft,GAME_X_Y_0, GAME_WIDTH - moveLeft,GAME_HEIGHT, GAME_X_Y_0, GAME_X_Y_0, GAME_WIDTH, GAME_HEIGHT, null); g.drawImage(ImageMgr.background, GAME_WIDTH-1 - moveLeft,GAME_X_Y_0, GAME_WIDTH*2 - moveLeft, GAME_HEIGHT, GAME_X_Y_0, GAME_X_Y_0, GAME_WIDTH, GAME_HEIGHT, null); moveLeft += SPEED; //游戏启动计分 if(start && moveLeft % 100 == 0){ score += 1; } if(moveLeft >= GAME_WIDTH){ moveLeft = 0; } } private void ground(Graphics g){ g.drawImage(ImageMgr.ground,0,BACKGROUND_HEIGHT - GROUND_HEIGHT,null); } private void bird(Graphics g){ Color c = g.getColor(); g.setColor(Color.BLUE); g.drawRect(BIRD_X, bird_y,BIRD_WIDTH,BIRD_HEIGHT); g.setColor(c); g.drawImage(ImageMgr.bird[step++], BIRD_X, bird_y,null); if(step >= ImageMgr.bird.length){ step = 0; } if(start){ bird_y += 4; } birdRectangle = new Rectangle(BIRD_X,bird_y,BIRD_WIDTH,BIRD_HEIGHT); } private void pipe(Graphics g){ drawPipe(g); pipeMove += SPEED; if(pipeMove >= GAME_WIDTH + PIPE_WIDTH){ pipeMove = 0; Random ran = new Random(); height = ran.nextInt(10)*20; drawPipe(g); } } private void drawPipe(Graphics g){ g.setColor(Color.RED);// g.drawRect(GAME_WIDTH - pipeMove,GAME_HEIGHT/5*3 - height,PIPE_WIDTH,PIPE_HEIGHT);// g.drawRect(GAME_WIDTH - pipeMove,-GAME_HEIGHT/5*4 - height,PIPE_WIDTH,PIPE_HEIGHT); g.drawImage(ImageMgr.pipe,GAME_WIDTH - pipeMove,GAME_HEIGHT/5*3 - height,null); g.drawImage(ImageMgr.pipe,GAME_WIDTH - pipeMove,-GAME_HEIGHT/5*4 - height,null); pipeRectangle1 = new Rectangle(GAME_WIDTH - pipeMove,GAME_HEIGHT/5*3 - height,PIPE_WIDTH,PIPE_HEIGHT); pipeRectangle2 = new Rectangle(GAME_WIDTH - pipeMove,-GAME_HEIGHT/5*4 - height,PIPE_WIDTH,PIPE_HEIGHT); } private void gameOver(Graphics g){ if(over){ g.drawImage(ImageMgr.gameOver,GAME_WIDTH/2 - GAME_OVER_WIDTH/2, GAME_HEIGHT/2 - GAME_OVER_HEIGHT,null); } } private void start(Graphics g){ if(!start){ g.drawImage(ImageMgr.ready,GAME_WIDTH/2 - READY_WIDTH/2, GAME_HEIGHT/2 - READY_HEIGHT,null); g.drawImage(ImageMgr.start,GAME_WIDTH/2 - START_WIDTH/2, GAME_HEIGHT/2,null); } } private void colliedWith(){ if(birdRectangle.intersects(pipeRectangle1) birdRectangle.intersects(pipeRectangle2)){ crash = true; over = true; //gameOver(g); } }}

 

  

工具类

public class ImageMgr { static BufferedImage background,ground,pipe,gameOver,start,ready; static BufferedImage[] bird = new BufferedImage[3]; private static final ImageMgr INSTANCE = new ImageMgr(); //nothing private ImageMgr(){ initImage(); }; private void initImage(){ try { background = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/bg_light.png"))); ground = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/ground.png"))); pipe = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/pipe.png"))); gameOver = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/gameover.png"))); start = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/start.png"))); ready = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/ready.png"))); for(int i = 0;i < 3;i++){ bird[i] = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/blueBird_" + (i+1) + ".png"))); } } catch (IOException ex) { ex.printStackTrace(); } } public static ImageMgr getInstance(){ return INSTANCE; }}

 

  

总结

通过此次的《布谷鸟闯关-简单版》实现,让我对JAVA的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。

 

  java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。

  以上就是Java实现经典游戏Flappy Bird的示例代码的详细内容,更多关于Java Flappy Bird游戏的资料请关注盛行IT其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: