运行效果:

代码:

 package bird;

 import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random; import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel; public class Bird extends JPanel {
private final int WIDTH=288;
private int HEIGHT=512+112;
// Image img;
private Image bg,land,pipe_up,pipe_down,bird,score_h,score_t,score_n;
private int y=300,land_x=0,pipe_x=200,current_y=0,time=0,down=0,bg_x=0,t=0,score=0,yt=-1;
private int a = 0, b = 0, c = 0;
private int[] pipe_ys=new int[4];
private Image [] score_number=new Image[10];
private Random random;
Bird(){
getBestScores() ;
Toolkit tool = this.getToolkit();
bg= tool.getImage("images/bg_day.png");
land=tool.getImage("images/land.png");
pipe_up= tool.getImage("images/pipe_up.png");
pipe_down= tool.getImage("images/pipe_down.png");
bird= tool.getImage("images/bird0_01.png");
random = new Random();
for(int i=0;i<=9;i++){
score_number[i]= tool.getImage("images/number_score_0"+i+".png");
}
init();
this.addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
//System.out.print(c); switch (c) {
case KeyEvent.VK_LEFT : break;
case KeyEvent.VK_RIGHT : break;
case KeyEvent.VK_UP :
System.exit(0);
break;
case KeyEvent.VK_DOWN : break;
case KeyEvent.VK_SPACE :
down=0;
if(time<=0)time=16;
break; default :
// System.out.print("hey"); }
}
});
this.setFocusable(true);
}
void init(){ y=300;
land_x=0;
pipe_x=200;
current_y=0;
time=0;
down=0;
bg_x=0;
t=0;
score=0;
yt=-1;
for(int i=0;i<=3;i++){
pipe_ys[i]=-1* Math.abs(random.nextInt()) % 150;
}
}
public int get_Height(){return HEIGHT;}
public int get_Width(){return WIDTH;}
private void move(){
while(true){
moveImg();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
moveBird();
repaint();
check();
t++;
}
}
private void moveBird(){
time--;
if(t%1==0){if(time>0){y-=4;}}
if(time<=-5&&y<=512-37){if(t%3==0){down+=1;y+=down;}}
}
private void moveImg(){
bg_x--;
if(bg_x<-288)bg_x+=288;
land_x-=1;
if(land_x<-20)land_x+=23;
pipe_x-=1;
if(pipe_x<=-52){
pipe_x+=200;
int ran = Math.abs(random.nextInt()) % 200;
ran=-ran;
pipe_ys[current_y]=ran;
current_y++;
current_y%=4;
}
}
private void check(){
int temp=current_y;
for(int i=0;i<=3;i++){
if(80+34>=pipe_x&&80<=pipe_x+52){
if(yt!=current_y){score++;yt=current_y;}
if(y<=pipe_ys[temp]+320||y+25>=pipe_ys[temp]+450){
gameOver();
init();
}
}
}
}
private int gameOver() { if (score > a) {
c = b;
b = a;
a = score;
save();
//System.exit(0);
}
else if (score > b) {
c = b;
b = score;
save();
//System.exit(0);
return 0;
}
else if (score > c) {
c = score;
save();
//System.exit(0);
return 0;
}
JOptionPane.showMessageDialog(null, "GAME OVER!\n\nBEST SCORE:\n"+a+"\n"+b+"\n"+c, "你死了(按任意键再来一次)",
JOptionPane.PLAIN_MESSAGE);
//System.exit(0);
return 0;
}
private void getBestScores() { BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("score.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} try {
a = Integer.parseInt(reader.readLine());
b = Integer.parseInt(reader.readLine());
c = Integer.parseInt(reader.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
/*String bestS = new String("Best Score: " + new Integer(a).toString()
+ " " + new Integer(b).toString() + " "
+ new Integer(c).toString());*/
return ;
}
private int save() { BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("score.txt"));
} catch (IOException e) {
e.printStackTrace();
}
String d = new Integer(a).toString(), e = new Integer(b).toString(),
f = new Integer(c).toString();
try {
writer.write(d);
writer.newLine();
writer.write(e);
writer.newLine();
writer.write(f);
writer.flush();
writer.close();
} catch (NumberFormatException ev) {
ev.printStackTrace();
} catch (IOException ev) {
ev.printStackTrace();
}
return 0;
}
private void showScore(Graphics g){
int score_h=score/100,score_t=score%100/10,score_n=score%10;
if(score_h!=0){g.drawImage(score_number[score_h], 80,20,this);}
g.drawImage(score_number[score_t], 80+20,20,this);
g.drawImage(score_number[score_n], 80+20+20,20,this);
}
public void paint(Graphics g) {//Component
super.paintComponent(g);
//g.drawLine(x,y,200,200);
g.drawImage(bg, bg_x,0,this);
g.drawImage(bg, bg_x+288,0,this);
//g.drawImage(land_1, landx_1, HEIGHT-land_1.getHeight(this), WIDTH,WIDTH*land_1.getHeight(this)/land_1.getWidth(this), this); int temp=current_y;
for(int i=0;i<=3;i++){
g.drawImage(pipe_down,pipe_x+i*200,pipe_ys[temp],this);
g.drawImage(pipe_up,pipe_x+i*200,pipe_ys[temp]+450,this);
g.drawImage(bird,80,y,this);
temp++;
temp%=4;
}
g.drawImage(land, land_x,512,this);
showScore(g);
}
public static void main(String[] args) {
JFrame frame = new JFrame("(按UP键退出)");
Bird b=new Bird();
frame.getContentPane()
.add(b);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(b.get_Width(),b.get_Height());
frame.setLocation(200, 100);
frame.setVisible(true);
b.move();
} }

素材:

bg_day.png

bird0_01.png

land.png

pipe_up.png

pipe_down.png

number_score_00.png~number_score_09.png    (数字均为白色,图片背景为透明。为能够在博客中显示出来,特设置为灰色背景)

程序写于大三上学期。

2016.4.12更新博客。

END

Flappy Bird (Java实现)的更多相关文章

  1. 程序员带你一步步分析AI如何玩Flappy Bird

    以下内容来源于一次部门内部的分享,主要针对AI初学者,介绍包括CNN.Deep Q Network以及TensorFlow平台等内容.由于笔者并非深度学习算法研究者,因此以下更多从应用的角度对整个系统 ...

  2. 教你从头到尾利用DQN自动玩flappy bird(全程命令提示,GPU+CPU版)【转】

    转自:http://blog.csdn.net/v_JULY_v/article/details/52810219?locationNum=3&fps=1 目录(?)[-] 教你从头到尾利用D ...

  3. canvas 制作flappy bird(像素小鸟)全流程

    flappy bird制作全流程: 一.前言 像素小鸟这个简单的游戏于2014年在网络上爆红,游戏上线一段时间内appleStore上的下载量一度达到5000万次,风靡一时, 近年来移动web的普及为 ...

  4. 自己动手写游戏:Flappy Bird

    START:最近闲来无事,看了看一下<C#开发Flappy Bird游戏>的教程,自己也试着做了一下,实现了一个超级简单版(十分简陋)的Flappy Bird,使用的语言是C#,技术采用了 ...

  5. C语言版flappy bird黑白框游戏

    在此记录下本人在大一暑假,2014.6~8这段时间复习C语言,随手编的一个模仿之前很火热的小游戏----flappy bird.代码bug基本被我找光了,如果有哪位兄弟找到其他的就帮我留言下吧,谢谢了 ...

  6. 闲扯游戏编程之html5篇--山寨版《flappy bird》源码

    新年新气象,最近事情不多,继续闲暇学习记点随笔,欢迎拍砖.之前的〈简单游戏学编程语言python篇〉写的比较幼稚和粗糙,且告一段落.开启新的一篇关于javascript+html5的从零开始的学习.仍 ...

  7. 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...

  8. 65行 JavaScript 代码实现 Flappy Bird 游戏

    飞扬的小鸟(Flappy Bird)无疑是2014年全世界最受关注的一款游戏.这款游戏是一位来自越南河内的独立游戏开发者阮哈东开发,形式简易但难度极高的休闲游戏,很容易让人上瘾. 这里给大家分享一篇这 ...

  9. 用Phaser来制作一个html5游戏——flappy bird (二)

    在上一篇教程中我们完成了boot.preload.menu这三个state的制作,下面我们就要进入本游戏最核心的一个state的制作了.play这个state的代码比较多,我不会一一进行说明,只会把一 ...

  10. 用Phaser来制作一个html5游戏——flappy bird (一)

    Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...

随机推荐

  1. 【问题】js 改变鼠标样式,chrome浏览器不能立即更新,暂没有解决办法

    元素的css,cursor可以改变鼠标样式.也就是鼠标放到元素上去时,改变为相应状态. 通过JS改变cursor时,我发现chrome浏览器不能立即更新,需要动一下鼠标才行,试了几个其它浏览器都是立即 ...

  2. 一款实用的viewer.js 图片相册

    Viewer.js 是一款强大的图片相册插件,像SNS交友网站一般都会用到点击缩略图,弹出层大图片,而且弹出层有多个控制按钮,比如放大缩小.旋转.撤回等,底部有缩略图列表可切换. 支持移动设备触摸事件 ...

  3. ArcGIS JS 学习笔记1 用ArcGIS JS 实现仿百度地图的距离量测和面积量测

    一.开篇 在博客注册了三年,今天才决定写第一篇博客,警告自己不要懒!!! 二.关于ArcGIS JS 版本选择 在写这篇博客时ArcGIS JS 4.0正式版已经发布.它和3.x版本的不同是,Map不 ...

  4. SharePoint 2013 初始化Ribbon选中Tab

    SharePoint使用中,经常打开页面会有默认展开的Ribbon选项,有时这又不是我们需要的,所以我们就需要默认选中的项目,下面简单介绍下如何实现. 方法一 1.Dispform.aspx页面默认R ...

  5. Core Animation - 核心动画

    CAAnimation类,是一个抽象类.遵循CAMediaTiming协议和CAAction协议! CAMediaTiming协议 可以调整时间,包括持续时间,速度,重复次数. CAAction协议  ...

  6. Android TextView 高亮字体并添加点击事件

    运行效果 package com.zutil.lib; import android.graphics.Typeface; import android.os.Bundle; import andro ...

  7. java 实现https请求

    java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从 ...

  8. JAVA Web 实现会话跟踪的技术笔记

    1.HTTP协议无状态:客户端的请求与服务器的响应所发生的一系列行为简单的说是客户发送了请求,服务器就给客户端响应,它们彼此之间都没有记录下来.如: 顾客与自动售货机 普通顾客(非会员)与商场 2.c ...

  9. 安卓开发第一步:Android Studio安装配置

    虽然本人是JAVA开发工程师平时主要开发Web App,但因为项目需求需要开发对应的移动端.一时又找不到合适的安卓开发人员,兄弟我只好被项目经理"抓来当壮丁了".俗话说好" ...

  10. 简单的ASP.NET Forms身份认证

    读了几篇牛人的此方面的文章,自己也动手做了一下,就想有必要总结一下.当然我的文章质量自然不能与人家相比,只是写给从没有接触过这个知识点的朋友. 网站的身份认证我以前只知道session,偶然发现一些牛 ...