最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)flappy Bird小游戲

 更新時(shí)間:2018年12月24日 08:47:52   作者:Chenny丶  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)flappy Bird小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)flappy Bird游戲的具體代碼,供大家參考,具體內(nèi)容如下

整個(gè)游戲由3個(gè)類(lèi)構(gòu)成。Bird類(lèi),Pipe類(lèi),stage類(lèi)

第一步:首先寫(xiě)一個(gè)Bird類(lèi)

//鳥(niǎo)類(lèi)
public class Bird {
 private int flyHeight;//飛行高度
 private int xpos;//距離y軸(窗口左邊緣)的位置,
 public static int Up=1;//向上飛
 public static int Down=-1;//向下飛
 public Bird()
 {
 flyHeight=200;
 xpos=30;
 }
 public void fly(int direction)
 {
 if(direction==Bird.Up)
 flyHeight-=20;//每次向上飛20m
 if(direction==Bird.Down)
 flyHeight+=20;//每次向下飛20m
 }
 public int getFlyHeight()//獲得當(dāng)前飛行高度
 {
 return flyHeight;
 }
 public int getXpos()//獲得當(dāng)前鳥(niǎo)的水平位置
 {
 return xpos;
 }
 public Boolean hit(Pipe pipe[])//檢測(cè)是否碰到管道。只有在鳥(niǎo)經(jīng)過(guò)管道的過(guò)程才有可能相撞
 {
 for(int i=0;i<pipe.length;i++)//遍歷管道進(jìn)行檢測(cè),是否相撞
 {
 if(getXpos()+20>=pipe[i].getXpos()&&getXpos()<=pipe[i].getXpos()+20)//鳥(niǎo)經(jīng)過(guò)管道
 if(flyHeight<pipe[i].getUpHeight()||flyHeight>pipe[i].getDownHeight())//鳥(niǎo)與管道相撞
  return true;
 }
 return false;
 }
}

第二步:寫(xiě)一個(gè)Pipe類(lèi),Pipe類(lèi) 里有3個(gè)成員,upHeight表示頂管道端的高度,downHeight表示底端管道段的高度,同樣要記錄管道的水平位置。

public class Pipe {
 private int upHeight;//表示頂管道端的高度
 private int downHeight;//表示底端管道段的高度
 private int xpos;
 public Pipe()
 {
 upHeight=0;
 downHeight=0;
 xpos=0;
 }
 public Pipe(int maxHeight,int xpos)//給管道一個(gè)最大總長(zhǎng)度(maxHeight)=upHeight+downHeight。還有管道的水平位置
 {
 double num;
 num=Math.random();
 while(num==0)
 num=Math.random();
 upHeight=(int) (maxHeight*num);//隨機(jī)產(chǎn)生一個(gè)頂端管道段高度(<maxHeight)
 downHeight=maxHeight-upHeight;//用總長(zhǎng)度減去upHeight
 this.xpos=xpos;
 }
 public void setXpos(int xpos)
 {
 this.xpos=xpos;
 }
 public int getXpos()
 {
 return xpos;
 }
 public int getUpHeight()
 {
 return upHeight;
 }
 public int getDownHeight()
 {
 return downHeight;
 }
 public String toSting()
 {
 return "("+upHeight+","+downHeight+")";
 }
}

最后一步,寫(xiě)好舞臺(tái)類(lèi),即操作游戲的類(lèi)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.Array;
import java.util.Timer;
import java.util.TimerTask;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class stage extends JPanel{
 private Pipe pipe[];//管道數(shù)組
 private Bird bird;//鳥(niǎo)
 private int space;//上下管道之間的間隔
 public JLabel scoreBoard;//計(jì)分面板
 private int score;//計(jì)分
 public stage()
 {
 space=150;//<span style="font-family: Arial, Helvetica, sans-serif;">上下管道之間的間隔為150</span>
 score=0;
 scoreBoard=new JLabel("得分:"+score);
 pipe=new Pipe[5];//總共5跟根
 for(int i=0;i<pipe.length;i++)
 {
 pipe[i]=new Pipe(400-space,i*130+110);//柱子每隔110m放一根
 //System.out.println(pipe[i].toSting());
 }
 bird=new Bird();
 }
 public void play()
 {
 Timer timer=new Timer();//定時(shí)任務(wù),即使不操作也能動(dòng)
 timer.schedule(new TimerTask()
 {
  public void run()
  {
  
  if(bird.hit(pipe))//碰到,重置所有數(shù)據(jù)成員
  {
  //System.out.println("碰到了");
  score=0;
  scoreBoard.setText("得分:"+score);
  pipe=new Pipe[10];
  for(int x=0;x<pipe.length;x++)
  pipe[x]=new Pipe(400-space,x*130+110);
  bird=new Bird();
  }
  else{//沒(méi)碰到
  //System.out.println("沒(méi)碰到");
  bird.fly(Bird.Down);//鳥(niǎo)默認(rèn)向下飛
  for(int x=0;x<pipe.length;x++)//管道每次往前移動(dòng)10m,造成鳥(niǎo)向右移動(dòng)的效果
  {
   pipe[x].setXpos(pipe[x].getXpos()-10);
  }
  score=score+10;
  scoreBoard.setText("得分:"+score);
  }
  repaint();
  }
 }, 0, 1000);//在不操作的情況下,每一秒飛一次
 this.requestFocus();//獲取”輸入“焦點(diǎn)
 this.addKeyListener(new KeyAdapter() {//加入鍵盤(pán)時(shí)間
 public void keyPressed(KeyEvent e)
 {
 if(e.getKeyCode()==38)
 <span style="white-space:pre"> </span>action(Bird.Up);
 else if(e.getKeyCode()==40)
 action(Bird.Down);
 });
 }
 public void action(int direction)//按下上下方向鍵后執(zhí)行的函數(shù)
 {
 
 if(bird.hit(pipe))
 {
 //System.out.println("碰到了");
 score=0;
 scoreBoard.setText("得分:"+score);
 pipe=new Pipe[10];
 for(int x=0;x<pipe.length;x++)
 pipe[x]=new Pipe(400-space,x*130+110);
 bird=new Bird();
 }
 else{
 //System.out.println("沒(méi)碰到");
 if(direction==Bird.Up)
 {
 bird.fly(Bird.Up);
 }
 else if(direction==Bird.Down)
 {
 bird.fly(Bird.Down);
 }
 for(int x=0;x<pipe.length;x++)//管道每次往前移動(dòng)10m,造成鳥(niǎo)向右移動(dòng)的效果
 {
 pipe[x].setXpos(pipe[x].getXpos()-10);
 }
 score=score+10;
 scoreBoard.setText("得分:"+score);
 }
 repaint();
 }
 public void paint(Graphics g)
 {
 g.setColor(g.getColor());
 g.fillRect(0, 0, getWidth(), getHeight());//用默認(rèn)顏色清屏
 g.setColor(Color.red);
 g.fill3DRect(bird.getXpos(), bird.getFlyHeight(), 20, 20, true);//紅色畫(huà)鳥(niǎo)
 g.setColor(Color.green);
 for(int i=0;i<pipe.length;i++)//綠色畫(huà)管道
 {
 g.fill3DRect(pipe[i].getXpos(), 0, 20, pipe[i].getUpHeight(), true);
 g.fill3DRect(pipe[i].getXpos(),pipe[i].getUpHeight()+space, 20, pipe[i].getDownHeight(), true);
 }
 if(pipe[0].getXpos()+20<=0)//如果第一根管道出界,就刪掉第一根管道,把后面的往前挪,再新創(chuàng)建一根管道
 {
 for(int i=1;i<pipe.length;i++)
  pipe[i-1]=pipe[i];
 pipe[pipe.length-1]=new Pipe(400-space,(pipe.length-1)*130+110);
 //System.out.println(pipe[pipe.length-1].toSting());
 }
 }
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 JFrame jf=new JFrame("flappyBird");
 stage app=new stage();
 jf.setLayout(null);
 app.setBounds(0,20,600,400);
 app.scoreBoard.setBounds(0, 0, 100, 20);
 jf.add(app);
 jf.add(app.scoreBoard);
 jf.setSize(610, 460);
 jf.setVisible(true);
 app.play();//游戲開(kāi)始
 }
 
}

程序截圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析

    Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析

    這篇文章主要為大家介紹了Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • java獲取用戶(hù)輸入的字符串方法

    java獲取用戶(hù)輸入的字符串方法

    今天小編就為大家分享一篇java獲取用戶(hù)輸入的字符串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 如何解決redis的NOAUTH Authentication required異常

    如何解決redis的NOAUTH Authentication required異常

    這篇文章主要介紹了Jedis異常解決:NOAUTH Authentication required,,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值
    2019-07-07
  • Java 切割字符串的幾種方式集合

    Java 切割字符串的幾種方式集合

    這篇文章主要介紹了Java 切割字符串的幾種方式集合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 使用JAXBContext 設(shè)置xml節(jié)點(diǎn)屬性

    使用JAXBContext 設(shè)置xml節(jié)點(diǎn)屬性

    這篇文章主要介紹了使用JAXBContext 設(shè)置xml節(jié)點(diǎn)屬性的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 淺談Java并發(fā)之同步器設(shè)計(jì)

    淺談Java并發(fā)之同步器設(shè)計(jì)

    這篇文章主要介紹Java并發(fā)之同步器設(shè)計(jì),本文以記錄方式并發(fā)編程中同步器設(shè)計(jì)的一些共性特征。并簡(jiǎn)單介紹了Java中的AQS,需要的朋友可以參考一下文章的詳細(xì)內(nèi)容
    2021-10-10
  • 手把手教你排查解決Java編譯報(bào)錯(cuò):找不到符號(hào)

    手把手教你排查解決Java編譯報(bào)錯(cuò):找不到符號(hào)

    這篇文章主要介紹了手把手教你排查解決Java編譯報(bào)錯(cuò):找不到符號(hào)的相關(guān)資料,提供了排查步驟和解決方法,通過(guò)這些步驟,開(kāi)發(fā)者可以有效地找到并解決編譯器拋出的找不到符號(hào)錯(cuò)誤,從而提高開(kāi)發(fā)效率,需要的朋友可以參考下
    2025-04-04
  • Spring Boot 與 Vue.js 整合流程

    Spring Boot 與 Vue.js 整合流程

    本文重點(diǎn)介紹我在Spring Boot 與 Vue.js 整合實(shí)踐過(guò)程中的基本流程,以及遇到的問(wèn)題,感興趣的朋友跟隨小編一起看看吧
    2018-09-09
  • Spring注解配置AOP導(dǎo)致通知執(zhí)行順序紊亂解決方案

    Spring注解配置AOP導(dǎo)致通知執(zhí)行順序紊亂解決方案

    這篇文章主要介紹了Spring注解配置AOP導(dǎo)致通知執(zhí)行順序紊亂解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • spring boot中的properties參數(shù)配置詳解

    spring boot中的properties參數(shù)配置詳解

    這篇文章主要介紹了spring boot中的properties參數(shù)配置,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

乌兰县| 韩城市| 芮城县| 保亭| 长海县| 松江区| 新泰市| 柳江县| 婺源县| 勃利县| 乌苏市| 南丹县| 阿坝| 上林县| 偏关县| 扶沟县| 寻乌县| 靖州| 怀远县| 班戈县| 防城港市| 普宁市| 永宁县| 龙南县| 万州区| 辰溪县| 江安县| 新沂市| 遂平县| 合阳县| 易门县| 张家口市| 南木林县| 南靖县| 海兴县| 安多县| 浦城县| 天水市| 康乐县| 济阳县| 综艺|