package com.frame;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import com.dialog.Dialog;
import com.image.GameImage;
import com.wall.Wall;
public class Game extends JFrame implements Runnable {
private int speed;
private int px;
private int py;
private boolean IsNotCollide = true;
private int index = 0;
private int count = 0;
private List<Wall> wall = new ArrayList<Wall>();
public Game() {
super("疯狂的小鸟");
py = (int) this.getSize().getHeight() / 2;
px = 290;
speed = 80;
final Container c = getContentPane();
c.setLayout(null);
setResizable(false);
getContentPane().setLayout(null);
//为窗体添加鼠标事件,控制鸟的移动
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//当获取的來源是窗体,并且未发生碰撞
if (e.getSource() == c && IsNotCollide == true) {
py -= 40;
}
}
});
//初始化图片
GameImage.initialize();
//初始化上下的墙体
this.initaialize();
new Thread(this).start();
this.setSize(800, 500);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//碰撞检测函数
public void collide(Wall obj) {
if (((obj.getLength()-10 > py) || (obj.getLength() + 65 < py))
&& (obj.getPositive().x <= px + 55 && obj.getPositive().x + 60 >= px + 55)) {
IsNotCollide = false;
// 停止墻体的线程
Iterator iter = wall.iterator();
while (iter.hasNext()) {
Wall stop = (Wall) iter.next();
stop.set_isstop(false);
}
if(index<3)index =3;
Dialog d =new Dialog(index - 3,this);
}
}
public void paint(Graphics g) {
//双缓冲技术
BufferedImage image = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = image.getGraphics();
g2.drawImage(GameImage.background_Image, 0, 0,800,500, this); //添加背景
g2.drawImage(GameImage.icon.getImage(), px, py, 80, 60, this);//添加鸟
//添加墙体
Iterator iter = wall.iterator();
while (iter.hasNext()) {
Wall obj = (Wall) iter.next();
g2.drawImage(obj.getImage_Upon(), obj.getPositive().x,//绘制上方的墙体
obj.getPositive().y,60,obj.getLength(), this);
g2.drawImage(obj.getImage_Below(), obj.getPositive().x,//绘制下方的墙体
obj.getPositive().y+obj.getLength()+120,60,500-obj.getLength(), this);
}
g.drawImage(image, 0, 0, this);
}
//上下墙体初始化
public void initaialize() {
for (int i = 0; i < 50; i++) {
Wall obj = null;
obj = new Wall(new Point(850, 0),
new Random().nextInt(250) + 50, GameImage.up_Image,GameImage.down_Image);
wall.add(obj);
}
}
public void start() {
Thread t = new Thread(this);
t.start();
}
//复写run()方法
public void run() {
while (IsNotCollide) {
if (count % 50 == 0) {
Wall obj = wall.get(index);
obj.start();
index++;
if (index >= 50) {
Iterator iter = wall.iterator();
while (iter.hasNext()) {
Wall stop = (Wall) iter.next();
stop.set_isstop(false);
}
IsNotCollide = false;
if(index<3)index =3;
Dialog d = new Dialog(index -3,this);
//index = 0;
}
count =0;
}
//碰撞检测
if(index>=3){
collide(wall.get(index-3));
}
count++;
try {
Thread.sleep(speed);
} catch (Exception e) {
}
py += 10;
this.repaint();
if(py >= 500 || py <= 0){
//停止墙体的线程
Iterator iter = wall.iterator();
while (iter.hasNext()) {
Wall stop = (Wall) iter.next();
stop.set_isstop(false);
}
IsNotCollide = false;
if(index<3)index =3;
Dialog d = new Dialog(index -3,this);
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
这是一款java编写的类似于疯狂的小鸟的游戏.zip (61个子文件)
FlyBird-master
.classpath 295B
.settings
org.eclipse.jdt.core.prefs 587B
org.eclipse.core.resources.prefs 73B
.gitattributes 378B
src
com
dialog
Exit.java 2KB
Dialog.java 2KB
image
start2.png 20KB
exit2.png 20KB
help2.png 20KB
background.jpg 346KB
help_background.png 277KB
start1.png 19KB
GameImage.java 1KB
background2.jpg 19KB
exit1.png 17KB
below.png 12KB
background0.jpg 127KB
help1.png 20KB
bird.gif 148KB
Dialog_background.png 393KB
exit_background.png 152KB
upon.png 12KB
wall
Wall.java 1KB
main
App.java 130B
frame
MyJFrame.java 4KB
Game.java 4KB
help
Help.java 2KB
bin
com
dialog
Dialog$1.class 950B
Dialog.class 3KB
Exit.class 2KB
Exit$1.class 933B
Exit$2.class 866B
Dialog$2.class 989B
image
start2.png 20KB
exit2.png 20KB
help2.png 20KB
background.jpg 346KB
help_background.png 277KB
GameImage.class 1KB
start1.png 19KB
background2.jpg 19KB
exit1.png 17KB
below.png 12KB
background0.jpg 127KB
help1.png 20KB
bird.gif 148KB
Dialog_background.png 393KB
exit_background.png 152KB
upon.png 12KB
wall
Wall.class 2KB
main
App.class 409B
frame
MyJFrame$1.class 1KB
Game.class 5KB
MyJFrame.class 3KB
Game$1.class 925B
MyJFrame$2.class 2KB
help
Help.class 3KB
Help$1.class 815B
疯狂的小鸟.jar 1.57MB
.project 366B
.gitignore 574B
共 61 条
- 1
资源评论
马coder
- 粉丝: 1212
- 资源: 6602
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于原生微信小程序实现的课堂考勤系统的设计与实现
- 商道融绿、润灵环球ESG评级数据(2015-2023年).xlsx
- 商道融绿、润灵环球ESG评级数据(2015-2023年).dta
- 基于 GDAL 与 PROJ4 的遥感图像处理软件,使用 Qt 构建课程设计
- 图形化界面采用Easyx编写,实现对哈夫曼树的显示操作
- 使用后端开发框架Spring Boot构建应用程序.pdf
- 基于Boson的计算机网络实验:RIP和IGRP的配置
- 在线教育系统 JAVA+Vue+SpringBoot+MySQL
- 健身房管理系统 JAVA+Vue+SpringBoot+MySQL
- 卓越导师双选系统 JAVA+Vue+SpringBoot+MySQL
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功