├── .gitignore ├── LICENSE ├── README.md ├── images ├── 0.png ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── bg.png ├── column.png ├── gameover.png ├── ground.png └── start.png └── src └── org └── wf └── game └── flappybird └── FalappyBirdGame.java /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /out/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 synchronized 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FishGame 2 | Java游戏,飞翔的小鸟 3 | 4 | ## 运行截图 5 | 6 | ![截图](https://s2.ax1x.com/2019/04/02/Ay07FS.png) 7 | 8 | ## 推荐 9 | **EasyWeb管系统模板**
10 |  一个开箱即用的后台模板,使用简单,模板丰富,包含传统ifram版、spa单页面路由版,[前往查看](https://easyweb.vip)。 -------------------------------------------------------------------------------- /images/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/0.png -------------------------------------------------------------------------------- /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/3.png -------------------------------------------------------------------------------- /images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/4.png -------------------------------------------------------------------------------- /images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/5.png -------------------------------------------------------------------------------- /images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/6.png -------------------------------------------------------------------------------- /images/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/7.png -------------------------------------------------------------------------------- /images/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/bg.png -------------------------------------------------------------------------------- /images/column.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/column.png -------------------------------------------------------------------------------- /images/gameover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/gameover.png -------------------------------------------------------------------------------- /images/ground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/ground.png -------------------------------------------------------------------------------- /images/start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andy521/FlappyBirdGame/ac174e9aaab837fe08e73119418ff5e0b5a0c1d5/images/start.png -------------------------------------------------------------------------------- /src/org/wf/game/flappybird/FalappyBirdGame.java: -------------------------------------------------------------------------------- 1 | package org.wf.game.flappybird; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | import java.awt.Graphics; 6 | import java.awt.Graphics2D; 7 | import java.awt.event.KeyAdapter; 8 | import java.awt.event.KeyEvent; 9 | import java.awt.event.MouseAdapter; 10 | import java.awt.event.MouseEvent; 11 | import java.awt.image.BufferedImage; 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.util.Random; 15 | 16 | import javax.imageio.ImageIO; 17 | import javax.swing.JFrame; 18 | import javax.swing.JPanel; 19 | 20 | public class FalappyBirdGame { 21 | public static void main(String[] args) { 22 | // 定义画框 23 | JFrame jf = new JFrame("bird_game"); 24 | jf.setSize(432, 674); 25 | jf.setAlwaysOnTop(false); 26 | jf.setLocationRelativeTo(null); 27 | jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 | jf.setResizable(false); 29 | Sky sky = new Sky(); 30 | jf.add(sky); 31 | // 显示画框 32 | jf.setVisible(true); 33 | sky.action(); 34 | } 35 | } 36 | 37 | // 天空类 38 | class Sky extends JPanel { 39 | private static final long serialVersionUID = 1L; 40 | BufferedImage bgBufferImage; // 背景图片 41 | Ground ground = new Ground(); // 地面 42 | Column column = new Column(350); // 钢管 43 | Column column2 = new Column(600); // 钢管 44 | static Bird bird = new Bird(); // 小鸟 45 | int score = 0; // 游戏得分 46 | BufferedImage startBufferImage; // 开始准备界面 47 | boolean isStrat; // 是否开始游戏 48 | BufferedImage overBufferImage; // 游戏结束界面 49 | boolean isOver; // 游戏是否结束 50 | 51 | public Sky() { 52 | super(); 53 | // 读取图片 54 | File bgImage = new File("images/bg.png"); 55 | File starImage = new File("images/start.png"); 56 | File overImage = new File("images/gameover.png"); 57 | try { 58 | bgBufferImage = ImageIO.read(bgImage); 59 | startBufferImage = ImageIO.read(starImage); 60 | overBufferImage = ImageIO.read(overImage); 61 | } catch (IOException e) { 62 | e.printStackTrace(); 63 | } 64 | } 65 | 66 | // 绘制界面方法 67 | @Override 68 | public void paint(Graphics graphics) { 69 | // 画背景 70 | graphics.drawImage(bgBufferImage, 0, 0, null); 71 | // 获取新的画笔对象 72 | Graphics2D gg = (Graphics2D) graphics; 73 | gg.rotate(-bird.ratation, bird.bird_x, bird.bird_y); 74 | // 画小鸟 75 | graphics.drawImage(bird.biBufferImage, bird.bird_x - bird.bird_width 76 | / 2, bird.bird_y - bird.bird_height / 2, null); 77 | gg.rotate(bird.ratation, bird.bird_x, bird.bird_y); 78 | // 画钢管 79 | graphics.drawImage(column.coBufferImage, column.column_x - column.width 80 | / 2, column.column_y - column.height / 2, null); 81 | graphics.drawImage(column2.coBufferImage, column2.column_x 82 | - column2.width / 2, column2.column_y - column2.height / 2, 83 | null); 84 | // 画地面 85 | graphics.drawImage(ground.grBufferImage, ground.ground_x, 86 | ground.ground_y, null); 87 | // 画文字 88 | graphics.setColor(Color.BLUE); 89 | graphics.setFont(new Font("楷体", Font.ITALIC, 30)); 90 | graphics.drawString("分数:" + score, 100, 600); 91 | // 画开始准备图片 92 | if (!isStrat && !isOver) { 93 | graphics.drawImage(startBufferImage, 0, 0, null); 94 | } 95 | // 画结束界面 96 | if (isOver) { 97 | graphics.drawImage(overBufferImage, 0, 0, null); 98 | } 99 | } 100 | 101 | // 游戏启动逻辑 102 | public void action() { 103 | // 添加鼠标监听器 104 | MouseAdapter adapter = new MouseAdapter() { 105 | @Override 106 | public void mousePressed(MouseEvent e) { 107 | // System.out.println("点击了鼠标"); 108 | /* 109 | * 若游戏结束重新开始游戏,游戏恢复初始状态 110 | * 若未结束:鸟飞起来 111 | */ 112 | if (isOver) { 113 | bird = new Bird(); 114 | ground = new Ground(); 115 | column = new Column(350); 116 | column2 = new Column(600); 117 | score = 0; 118 | isOver = false; 119 | isStrat = false; 120 | } else { 121 | bird.refly(); 122 | isStrat = true; 123 | } 124 | } 125 | }; 126 | this.addMouseListener(adapter); 127 | // 添加键盘监听器 128 | KeyAdapter keyAdapter = new KeyAdapter() { 129 | @Override 130 | public void keyPressed(KeyEvent e) { 131 | char charA = e.getKeyChar(); 132 | if (charA == 'w') { 133 | if (bird.bird_y > 20) { 134 | bird.bird_y -= 20; 135 | } 136 | } else if (charA == 's') { 137 | if (bird.bird_y < 465) { 138 | bird.bird_y += 20; 139 | } 140 | } else if (charA == 'a') { 141 | if (bird.bird_x > 20) { 142 | bird.bird_x -= 20; 143 | } 144 | } else if (charA == 'd') { 145 | if (bird.bird_x < 395) { 146 | bird.bird_x += 20; 147 | } 148 | } else if (charA == ' ') { 149 | /* 150 | * 若游戏结束重新开始游戏,游戏恢复初始状态 151 | * 若未结束:鸟飞起来 152 | */ 153 | if (isOver) { 154 | bird = new Bird(); 155 | ground = new Ground(); 156 | column = new Column(350); 157 | column2 = new Column(600); 158 | score = 0; 159 | isOver = false; 160 | isStrat = false; 161 | } else { 162 | bird.refly(); 163 | isStrat = true; 164 | } 165 | } 166 | super.keyPressed(e); 167 | } 168 | }; 169 | this.addKeyListener(keyAdapter); 170 | this.requestFocus(); 171 | 172 | while (true) { 173 | // 判断游戏是否开始 174 | if (isStrat && !isOver) { 175 | ground.move(); 176 | column.move(); 177 | column2.move(); 178 | bird.change(); 179 | bird.move_go(); 180 | } 181 | // 判断撞击障碍 182 | if (bird.bird_x - bird.bird_width / 2 == column.column_x 183 | + column.width / 2 184 | || bird.bird_x - bird.bird_width / 2 == column2.column_x 185 | + column2.width / 2) { 186 | score++; 187 | } 188 | if (bird.hit(ground) || bird.hit(column) || bird.hit(column2)) { 189 | isStrat = false; 190 | isOver = true; 191 | } 192 | try { 193 | Thread.sleep(20); 194 | } catch (InterruptedException e) { 195 | e.printStackTrace(); 196 | } 197 | repaint(); 198 | } 199 | } 200 | } 201 | 202 | // 地面类 203 | class Ground { 204 | int ground_x, ground_y; // 地面的坐标 205 | BufferedImage grBufferImage; // 地面图片 206 | 207 | public Ground() { 208 | super(); 209 | File grImage = new File("images/ground.png"); 210 | try { 211 | grBufferImage = ImageIO.read(grImage); 212 | } catch (IOException e) { 213 | e.printStackTrace(); 214 | } 215 | ground_y = 500; 216 | } 217 | 218 | // 地面动画方法 219 | public void move() { 220 | ground_x--; 221 | if (ground_x < -110) { 222 | ground_x = 0; 223 | } 224 | } 225 | } 226 | 227 | // 钢管类 228 | class Column { 229 | int column_x, column_y; // 钢管的中心坐标 230 | int width, height; // 宽度高度 231 | int gap = 140; // 钢管的空隙 232 | Random random = new Random(); 233 | ; // 随机坐标 234 | BufferedImage coBufferImage; // 钢管图片 235 | 236 | public Column(int x) { 237 | super(); 238 | File coImage = new File("images/column.png"); 239 | try { 240 | coBufferImage = ImageIO.read(coImage); 241 | } catch (IOException e) { 242 | e.printStackTrace(); 243 | } 244 | column_x = x; 245 | column_y = random.nextInt(180) + 150; 246 | width = coBufferImage.getWidth(); 247 | height = coBufferImage.getHeight(); 248 | } 249 | 250 | // 钢管动画方法 251 | public void move() { 252 | column_x--; 253 | if (column_x < -width / 2) { 254 | column_y = random.nextInt(180) + 150; 255 | column_x = 432 + width / 2; 256 | } 257 | } 258 | } 259 | 260 | // 鸟类 261 | class Bird { 262 | int bird_x = 60, bird_y = 300; // 鸟的中心点坐标 263 | int bird_width, bird_height; // 鸟的宽度,高度 264 | double speed = 20; // 速度 265 | double g = 4; // 加速度 266 | double s; // 运动距离 267 | double t = 0.3; // 运动时间 268 | BufferedImage biBufferImage; // 鸟图片 269 | BufferedImage[] images = new BufferedImage[8]; 270 | int bird_icon = 0; 271 | 272 | public Bird() { 273 | super(); 274 | for (int i = 0; i < images.length; i++) { 275 | File biImage = new File("images/" + i + ".png"); 276 | try { 277 | images[i] = ImageIO.read(biImage); 278 | } catch (IOException e) { 279 | e.printStackTrace(); 280 | } 281 | } 282 | biBufferImage = images[0]; 283 | bird_width = biBufferImage.getWidth(); 284 | bird_height = biBufferImage.getHeight(); 285 | } 286 | 287 | // 小鸟展翅动画方法 288 | int index = 0; 289 | 290 | public void change() { 291 | index++; 292 | biBufferImage = images[index / 3 % 8]; 293 | } 294 | 295 | // 小鸟移动的方法 296 | double ratation; // 倾斜角度 297 | 298 | public void move_go() { 299 | double v0 = speed; 300 | s = v0 * t - 0.5 * g * t * t; 301 | double vt = v0 - g * t; 302 | speed = vt; 303 | bird_y = bird_y - (int) s; 304 | ratation = s / 16; 305 | if (bird_y <= bird_height / 2) { 306 | bird_y = bird_height / 2; 307 | } 308 | } 309 | 310 | // 重新飞翔 311 | public void refly() { 312 | speed = 20; 313 | } 314 | 315 | // 撞击地面 316 | public boolean hit(Ground ground) { 317 | return bird_y + bird_height / 2 >= ground.ground_y; 318 | } 319 | 320 | // 撞击钢管 321 | public boolean hit(Column column) { 322 | int left_x = column.column_x - column.width / 2 - bird_width / 2; 323 | int right_x = column.column_x + column.width / 2 + bird_width / 2; 324 | int top_y = column.column_y - column.gap / 2 + bird_height / 2 - 5; 325 | int down_y = column.column_y + column.gap / 2 - bird_height / 2 + 5; 326 | if (bird_x > left_x && bird_x < right_x) { 327 | if (bird_y > top_y && bird_y < down_y) { 328 | return false; 329 | } else { 330 | return true; 331 | } 332 | } else { 333 | return false; 334 | } 335 | } 336 | } --------------------------------------------------------------------------------