FlappyBird
JavaSE实现的FlappyBird游戏
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 295 2014-02-11 23:29 .classpath
文件 369 2014-02-11 23:29 .project
目录 0 2014-05-08 06:07 .settings
文件 587 2014-02-11 23:29 .settingsorg.eclipse.jdt.core.prefs
目录 0 2014-05-08 06:07 bin
目录 0 2014-05-08 06:07 bincom
目录 0 2014-05-08 06:07 bincomhsj
目录 0 2014-05-08 06:07 bincomhsjird
文件 3157 2014-02-18 17:49 bincomhsjird .png
文件 3167 2014-02-18 17:49 bincomhsjird1.png
文件 3159 2014-02-18 17:49 bincomhsjird2.png
文件 3165 2014-02-18 17:49 bincomhsjird3.png
文件 3157 2014-02-18 17:50 bincomhsjird4.png
文件 3165 2014-02-18 17:50 bincomhsjird5.png
文件 3159 2014-02-18 17:50 bincomhsjird6.png
文件 3167 2014-02-18 17:50 bincomhsjird7.png
文件 9226 2014-02-18 17:32 bincomhsjirdg.png
文件 3494 2014-05-08 06:03 bincomhsjirdBird.class
文件 1522 2014-05-08 06:03 bincomhsjirdColumn.class
文件 5588 2014-02-18 12:09 bincomhsjirdcolumn.png
文件 8684 2014-02-18 12:12 bincomhsjirdgameover.png
文件 1240 2014-05-08 06:06 bincomhsjirdGround.class
文件 3567 2014-02-18 18:14 bincomhsjirdground.png
文件 9600 2014-02-18 18:05 bincomhsjirdstart.png
文件 37888 2014-05-08 06:07 bincomhsjirdThumbs.db
文件 827 2014-05-08 06:04 bincomhsjirdWorld$1.class
文件 891 2014-05-08 06:04 bincomhsjirdWorld$2.class
文件 3804 2014-05-08 06:04 bincomhsjirdWorld.class
文件 46745 2014-02-19 10:07 layout.png
文件 31 2014-05-08 06:07 run.bat
目录 0 2014-05-08 06:07 src
............此处省略20个文件信息
package com.hsj.bird;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Bird {
/** 鸟飞行位置 这个位置是鸟的中心位置 */
int x; int y;
/** 飞行角度 */
double angle;
/** 动画帧 */
BufferedImage[] images;
/** 当前图片 */
BufferedImage image;
/** 当前图片序号 */
int index = 0;
/** 重力加速度 */
final double g;
/** 时间间隔秒 */
final double t;
/** 初始上抛速度 */
final double v0;
/** 当前上抛速度 */
double speed;
/** 移动距离 */
double s;
/** 鸟的范围 鸟的范围是一个正方形区域 中心点是xy */
int size = 40;
public Bird() throws Exception {
this.g = 4; //重力加速度
this.t = 0.25; //每次计算的间隔时间
this.v0 = 20; //初始上抛速度
x = 132; //鸟的初始位置
y = 275; //鸟的初始位置
//加载动画帧
images = new BufferedImage[8];
for(int i=0; i<8; i++){
images[i] = ImageIO.read(getClass().getResource(i+“.png“));
}
image = images[0];
}
/** 飞行一步
* 竖直上抛位移计算
* (1) 上抛速度计算 V=Vo-gt
(2) 上抛距离计算 S=Vot-1/2gt^2
* */
public void step(){
//V0 是本次
double v0 = speed;
//计算垂直上抛运动 经过时间t以后的速度
double v = v0 - g*t;
//作为下次计算的初始速度
speed = v;
//计算垂直上抛运动的运行距离
s = v0*t - 0.5 * g * t * t;
//将计算的距离 换算为 y坐标的变化
y = y - (int)s;
//计算小鸟的仰角
angle = -Math.atan(s/8);
}
public void fly(){
//更换小鸟的动画帧图片 其中/4 是为了调整动画更新的频率
index++;
image = images[(index/8)%images.length];
}
/** 小鸟向上飞扬 */
public void flappy(){
//每次小鸟上抛跳跃 就是将小鸟在当前点重新以初始速度 V0 向上抛
speed = v0;
}
//绘制时并发执行的 要同步避免 旋转坐标系对其他绘制方法的影响
public synchronized void paint(Graphics g){
//g.drawRect(x-size/2 y-size/2 size size);
Graphics2D g2 = (Graphics2D)g;
//旋转绘图坐标系 绘制
g2.rotate(angle this.x this.y);
//以xy 为中心绘制图片
int x = this.x-image.getWidth()/2;
int y = this.y-image.getHeight()/2;
g.drawImage(image x y null);
//旋转回来
g2.rotate(-angle this.x this.y);
}
@Override
public String toString() {
return “Bird [x=“ + x + “ y=“ + y + “ g=“ + g + “ t=“ + t + “ v0=“
+ v0 + “ speed=“ + speed + “s=“+s+“]“;
}
/** 判断鸟是否通过柱子 */
public boolean pass(Column col1 Column col2) {
return col1.x==x || col2.x==x;
}
/** 判断鸟是否与柱子和地发生碰撞 */
public boolean hit(Column column1 Column column2 Ground ground) {
//碰到地面
if(y-size/2 >= ground.y){
return true;
}
//碰到柱子
return hit(column1) || hit(column2);
}
/** 检查当前鸟是否碰到柱子 */
public boolean hit(Column col){
//如果鸟碰到柱子: 鸟的中心点x坐标在 柱子宽度 + 鸟的一半
if( x>col.x-col.width/2-size/2 && x if(y>col.y-col.gap/2+size/2 && y return false;
}
return true;
}
return false;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 295 2014-02-11 23:29 .classpath
文件 369 2014-02-11 23:29 .project
目录 0 2014-05-08 06:07 .settings
文件 587 2014-02-11 23:29 .settingsorg.eclipse.jdt.core.prefs
目录 0 2014-05-08 06:07 bin
目录 0 2014-05-08 06:07 bincom
目录 0 2014-05-08 06:07 bincomhsj
目录 0 2014-05-08 06:07 bincomhsjird
文件 3157 2014-02-18 17:49 bincomhsjird .png
文件 3167 2014-02-18 17:49 bincomhsjird1.png
文件 3159 2014-02-18 17:49 bincomhsjird2.png
文件 3165 2014-02-18 17:49 bincomhsjird3.png
文件 3157 2014-02-18 17:50 bincomhsjird4.png
文件 3165 2014-02-18 17:50 bincomhsjird5.png
文件 3159 2014-02-18 17:50 bincomhsjird6.png
文件 3167 2014-02-18 17:50 bincomhsjird7.png
文件 9226 2014-02-18 17:32 bincomhsjirdg.png
文件 3494 2014-05-08 06:03 bincomhsjirdBird.class
文件 1522 2014-05-08 06:03 bincomhsjirdColumn.class
文件 5588 2014-02-18 12:09 bincomhsjirdcolumn.png
文件 8684 2014-02-18 12:12 bincomhsjirdgameover.png
文件 1240 2014-05-08 06:06 bincomhsjirdGround.class
文件 3567 2014-02-18 18:14 bincomhsjirdground.png
文件 9600 2014-02-18 18:05 bincomhsjirdstart.png
文件 37888 2014-05-08 06:07 bincomhsjirdThumbs.db
文件 827 2014-05-08 06:04 bincomhsjirdWorld$1.class
文件 891 2014-05-08 06:04 bincomhsjirdWorld$2.class
文件 3804 2014-05-08 06:04 bincomhsjirdWorld.class
文件 46745 2014-02-19 10:07 layout.png
文件 31 2014-05-08 06:07 run.bat
目录 0 2014-05-08 06:07 src
............此处省略20个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)