package game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import tool.Sound;
public class FlyppyBird extends JFrame implements Runnable{
private BackGround backGround;
public FlyppyBird() {
intiaView();
}
public void intiaView(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
backGround = new BackGround();
this.setBounds(100,100,400,600);
this.add(backGround);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void run() {
while (true) {
backGround.bird.fly();
backGround.ground.moveStep();
backGround.repaint();
}
}
public static void main(String[] args) {
new FlyppyBird();
}
}
class BackGround extends JPanel implements Runnable{
int score = 0;
BufferedImage startImage;
BufferedImage overImage;
Sound start;
Sound hit;
Ground ground;
Bird bird;
Pipe pipe;
int flag = 1;//flag对应三种状态,游戏开始界面 游戏界面 游戏结束界面
public BackGround() {
try {
startImage = ImageIO.read(new File("img/start.png"));
overImage = ImageIO.read(new File("img/gameover.png"));
start = new Sound("sound/score.wav");
hit = new Sound("sound/hit.wav");
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ground = new Ground();
bird = new Bird();
pipe = new Pipe();
new Thread(this).start();
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (flag == 1) {
flag = 2;
// return;
}
if (flag == 2) {
bird.flyUp();
try {
new Sound("sound/flappy.wav").play();
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (flag == 3) {
score = 0;
flag = 2;
bird.reset();
pipe.reset();
}
}
});
}
public boolean touchPipe(Pipe pipe){
boolean isTouch = false;
//判断是否触碰到上面的管道
if (bird.x + bird.width >= pipe.x &&
bird.x <= pipe.x + pipe.width
&&bird.y <= pipe.y + 524) {
isTouch = true;
}
if (bird.x + bird.width >= pipe.x &&
bird.x<= pipe.x + pipe.width&&
bird.y+bird.height>=pipe.y+ 678 ) {
isTouch = true;
}
//判断是否触碰到下面的管道
return isTouch;
}
public boolean touchGround(){
boolean isTouch = false;
if (bird.y + bird.height >= 600 - ground.height || bird.y + 10 <= 0) {
isTouch = true;
}
return isTouch;
}
@Override
public void paint(Graphics g) {
g.drawImage(ground.bg, 0, 0, ground.bg.getWidth(),ground.bg.getHeight(), null);
if (flag == 1 ) {
g.drawImage(startImage,0, 0,null);
}
if (flag == 3) {
g.setFont(new Font("宋体",Font.BOLD,20));
g.setColor(Color.red);
g.drawString("Score:"+ score, 25, 25);
g.drawImage(overImage,0, 0,null);
}
if (flag == 2) {
g.drawImage(pipe.pipe, pipe.x, pipe.y,null);
g.drawImage(bird.bird, bird.x, bird.y, bird.bird.getWidth(),bird.bird.getHeight(), null);
g.setFont(new Font("宋体",Font.BOLD,20));
g.setColor(Color.red);
g.drawString("Score:"+ score, 25, 25);
}
g.drawImage(ground.img, ground.x, ground.y, ground.width, ground.height, null);
}
@Override
public void run() {
while (true) {
if (touchGround() || touchPipe(pipe)) {
flag = 3;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (flag == 2) {
if (score % 1000 == 0) {
try {
new Sound("sound/score.wav").play();
} catch (UnsupportedAudioFileException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
score += 1;
bird.fly();
pipe.move();
}
ground.moveStep();
repaint();
}
}
class Pipe{
int distance;
int x;
int y;
int width;
int height;
BufferedImage pipe;
Random random = new Random();
public Pipe() {
try {
pipe = ImageIO.read(new File("img/column.png"));
width = pipe.getWidth();
height = pipe.getHeight();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
distance = random.nextInt(100) + 400;
x = 400 + pipe.getWidth();
y = random.nextInt(100) - 400;
}
public void move(){
if (x >= - pipe.getWidth()) {
x -= 5;
}else{
x = 400 ;
y = random.nextInt(100) - 400;
}
}
public void reset(){
x = 400 + pipe.getWidth();
y = random.nextInt(100) - 400;
}
}
class Bird{
int height;
int width;
double t = 0.05;
double v1 = -100;
double v2;
double g = 180;
double speed;
int x;
int y;
int i = 0;
BufferedImage bird;
public Bird() {
x = 150;
y = 180;
try {
bird = ImageIO.read(new File("img/0.png"));
height = bird.getHeight();
width = bird.getWidth();
} catch (IOException e) {
e.printStackTrace();
}
}
public void reset(){
x = 150;
y = 180;
v1 = - 100;
}
public void flyUp(){
v1 = -100;
}
public void fly(){
if (i > 6) {
i = 0;
}
try {
bird = ImageIO.read(new File("img/"+i +".png"));
i++;
} catch (IOException e) {
e.printStackTrace();
}
//计算小鸟的坐标
int s = (int) (v1 * t + 0.5 * g * t * t);
y = y + s;
v1 = v1 + g * t;
}
}
class Ground{
BufferedImage bg;
BufferedImage img;
int width;
int height;
int x;
int y;
public Ground() {//img/ground.png
try {
img = ImageIO.read(new File("img/ground.png"));
bg = ImageIO.read(new File("img/bg.png"));
} catch (IOException e) {
e.printStackTrace();
}
width = img.getWidth();
height = img.getHeight();
x = 0;
y = 600 - height;
}
public void moveStep(){
if (x >= -width + 400) {
x -= 15;
}
else {
x = 0;
}
}
}
//class Food{
// Random random;
// int x;
// int y;
// BufferedImage apple;
// int width;
// int height;
// public Food() {
// random = new Random();
// try {
// apple = ImageIO.read(new File("img/apple.png"));
// x = 400;
// y = random.nextInt(400)+100;
// width = apple.getWidth();
// height = apple.getHeight();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
// public void move(){
// x -= 15;
// }
//
// public void reset(){
// x = 400;
// y = random.nextInt(400)+100;
// }
//
//
//}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java实现的flyppybird游戏
共32个文件
png:14个
class:8个
wav:4个
需积分: 10 12 下载量 129 浏览量
2017-06-23
10:08:44
上传
评论
收藏 131KB RAR 举报
温馨提示
使用java实现的flyppybird游戏,内涵flyppybird需要的图片,音频资源
资源推荐
资源详情
资源评论
收起资源包目录
FlyppyBird.rar (32个子文件)
FlyppyBird
.project 386B
bin
test
Main.class 767B
tool
Sound.class 2KB
game
FlyppyBird.class 1KB
BackGround$1.class 1KB
BackGround$Bird.class 2KB
BackGround$Ground.class 1KB
BackGround$Pipe.class 1KB
BackGround.class 4KB
.settings
org.eclipse.jdt.core.prefs 598B
sound
hit.wav 3KB
score.wav 77KB
start.wav 3KB
flappy.wav 25KB
src
test
Main.java 389B
tool
Sound.java 2KB
game
FlyppyBird.java 7KB
img
5.png 3KB
bg.png 9KB
apple.png 3KB
start.png 9KB
3.png 3KB
column.png 5KB
0.png 3KB
4.png 3KB
gameover.png 8KB
7.png 3KB
ground.png 3KB
6.png 3KB
1.png 3KB
2.png 3KB
.classpath 301B
共 32 条
- 1
资源评论
qq_31281189
- 粉丝: 21
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- TG-2024-04-26-183849310.mp4
- 汇编语言的概要介绍与分析
- 个人博客系统设计与开发.zip
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- htmlzwbjq_downyi.com.zip
- 无头单向非循环链表的实现(Test.c)
- 无头单向非循环链表的实现(SList.c)
- 浏览器重定向插件更新文件
- SSA-BP麻雀算法优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功