一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - java實現flappy Bird小游戲

java實現flappy Bird小游戲

2021-06-24 10:06Chenny丶 Java教程

這篇文章主要為大家詳細介紹了java實現flappy Bird小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現flappy bird游戲的具體代碼,供大家參考,具體內容如下

整個游戲由3個類構成。bird類,pipe類,stage類

第一步:首先寫一個bird類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//鳥類
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()//獲得當前飛行高度
 {
 return flyheight;
 }
 public int getxpos()//獲得當前鳥的水平位置
 {
 return xpos;
 }
 public boolean hit(pipe pipe[])//檢測是否碰到管道。只有在鳥經過管道的過程才有可能相撞
 {
 for(int i=0;i<pipe.length;i++)//遍歷管道進行檢測,是否相撞
 {
 if(getxpos()+20>=pipe[i].getxpos()&&getxpos()<=pipe[i].getxpos()+20)//鳥經過管道
 if(flyheight<pipe[i].getupheight()||flyheight>pipe[i].getdownheight())//鳥與管道相撞
  return true;
 }
 return false;
 }
}

第二步:寫一個pipe類,pipe類 里有3個成員,upheight表示頂管道端的高度,downheight表示底端管道段的高度,同樣要記錄管道的水平位置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)//給管道一個最大總長度(maxheight)=upheight+downheight。還有管道的水平位置
 {
 double num;
 num=math.random();
 while(num==0)
 num=math.random();
 upheight=(int) (maxheight*num);//隨機產生一個頂端管道段高度(<maxheight)
 downheight=maxheight-upheight;//用總長度減去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+")";
 }
}

最后一步,寫好舞臺類,即操作游戲的類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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[];//管道數組
 private bird bird;//鳥
 private int space;//上下管道之間的間隔
 public jlabel scoreboard;//計分面板
 private int score;//計分
 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();//定時任務,即使不操作也能動
 timer.schedule(new timertask()
 {
  public void run()
  {
  
  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("沒碰到");
  bird.fly(bird.down);//鳥默認向下飛
  for(int x=0;x<pipe.length;x++)//管道每次往前移動10m,造成鳥向右移動的效果
  {
   pipe[x].setxpos(pipe[x].getxpos()-10);
  }
  score=score+10;
  scoreboard.settext("得分:"+score);
  }
  repaint();
  }
 }, 0, 1000);//在不操作的情況下,每一秒飛一次
 this.requestfocus();//獲取”輸入“焦點
 this.addkeylistener(new keyadapter() {//加入鍵盤時間
 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)//按下上下方向鍵后執行的函數
 {
 
 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("沒碰到");
 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++)//管道每次往前移動10m,造成鳥向右移動的效果
 {
 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());//用默認顏色清屏
 g.setcolor(color.red);
 g.fill3drect(bird.getxpos(), bird.getflyheight(), 20, 20, true);//紅色畫鳥
 g.setcolor(color.green);
 for(int i=0;i<pipe.length;i++)//綠色畫管道
 {
 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)//如果第一根管道出界,就刪掉第一根管道,把后面的往前挪,再新創建一根管道
 {
 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();//游戲開始
 }
 
}

程序截圖:

java實現flappy Bird小游戲

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_1017097573/article/details/51707098

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美在线播放一区二区 | 久久视频精品3线视频在线观看 | 闺蜜调教我做她的脚奴 | 日韩理论片在线看免费观看 | www.一区| 99ri国产在线 | 男神插曲女生动漫完整版动漫 | 女教师系列三上悠亚在线观看 | 久久毛片视频 | 77久久| 麻豆在线传煤 | 国产99久久久国产精品成人 | 好爽好粗 | 驯服有夫之妇HD中字日本 | a4yy欧美一区二区三区 | 国产亚洲精品美女久久久 | 国产欧美日韩精品在线 | 国产亚洲欧美在线中文bt天堂网 | 舔到喷水 | 午夜无码国产理论在线 | 国产成人精品午夜在线播放 | 欧美jjvideo | 高清视频大片免费观看 | 黑帮少爷爱上我第8集在线观看 | 无敌秦墨漫画免费阅读 | 成人免费视频播放 | 黑人双渗透 | 91肥熟国产老肥熟在线 | 青青视频国产依人在线 | 亚洲天堂中文 | 美国videos | 国产视频福利 | 九九99九九精彩网站 | 国产精品久久久久久网站 | 青青青手机在线观看 | 香蕉久久一区二区三区 | 久久一本综合 | 精品视频日本 | 经典欧美gifxxoo动态图暗网 | 王者荣耀瑶白色液体 | 腿交hd|