国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产在线观看免费 | 国产一级二级毛片 | 国产欧美精品区一区二区三区 | 黄色国产网站 | 日韩成人在线播放 | 日韩欧美中文字幕一区二区三区 | av在线免费播放 | 亚洲天堂一区二区 | 精品视频在线播放 | 精品国产乱码久久久久久蜜柚 | 国产韩国精品一区二区三区 | 久久狠狠| 人人澡人人爽 | 欧美国产日韩一区 | 国内激情自拍 | 在线成人小视频 | 天天干夜操 | 国产成人一区二区三区 | 99精品视频在线观看 | 91大神xh98hx在线播放 | 国产午夜精品一区二区三区嫩草 | 黄色片子免费观看 | 国产精品久久久久久久久久久久久 | 最好的2019中文大全在线观看 | 成人国产精品免费观看 | 国产在线1 | 久久久久久久久久久久一区二区 | 在线色网站 | 亚洲va中文字幕 | 国产在线一区二区 | 日b片 | 97理论片 | 青青草免费在线视频 | 中文字幕日韩在线 | 国内精品久久久久久影视8 有码在线 | 亚洲国产高清美女在线观看 | 免费一级毛片电影 | 国产成人精品一区二区 | 91视频导航 | av午夜电影 | 狠狠色综合网站久久久久久久 |