Bird类
public class Bird {
BufferedImage image;
int x,y,width,height;
//鸟的大小,检测碰撞
int size;
//用于计算鸟的位置
double g;//重力加速度
double t;//两次位置的时间间隔,即运动时间
double v0;//初始上抛速度
double speed;//当前的上抛速度
double s;//经过时间t后是位移
double alpha;//鸟的倾角,单位是弧度
//定义一组图片,是鸟的动画帧
BufferedImage[] images;
//动画帧数数组元素的下标位置
int index;
public Bird() throws Exception {
image = ImageIO.read(new File("img/0.png"));
width = image.getWidth();
height = image.getHeight();
x=132;
y=280;
size = 40;
g=4;
v0=20;
t=0.25;
speed=v0;
s=0;
alpha=0;
//创建数组,创建8个元素的数组
images = new BufferedImage[8];
for (int i = 0; i < images.length; i++) {
images[i] = ImageIO.read(new File("img/"+i+".png"));
}
index = 0;
}
//鸟飞翔的方法
public void fly(){
index++;
image = images[(index/12)%8];//之所以除以12是为了使鸟的运行速度减慢,即切换图片
}
public void flappy(){
//重新设置初始速度,重新向上飞
speed = v0;
}
//碰撞的方法
public boolean hit(Ground ground){
boolean hit = y + size / 2 > ground.y;
if(hit){
//将鸟放置到地面上
y = ground.y-size/2;
//使鸟落地时有摔倒效果
alpha = -3.14159265358979323/2;
}
return hit;
}
//检测当前的鸟是否撞到了柱子
public boolean hit(Column column){
//先检测鸟是否在柱子范围内
if(x>column.x-column.width/2-size/2 && x<column.x+column.width/2+size/2){
//检测是否在缝隙中
if(y>column.y-column.gap/2+size/2 && y<column.y+column.gap/2-size/2){
return false;
}
return true;
}
return false;
}
//鸟移动的方法
public void step(){
double v0 = speed;
s=v0 * t + g * t * t / 2;//计算上抛运动的位移
y = y - (int) s;//计算鸟坐标位置
double v = v0 - g * t;
speed = v;
//调用API提供反正切函数,计算倾角
alpha=Math.atan(s/8);
}
}
BirdGame类
public class BirdGame extends JPanel{
Bird bird;
Column column1;Column column2;
Ground ground;
BufferedImage background;
//boolean gameOver;
BufferedImage gameOverImage;
BufferedImage startImage;
//游戏状态
int state;
public static final int START = 0;
public static final int RUNNING =1;
public static final int GAME_OVER=2;
//分数
int score;
public BirdGame() throws Exception{
//gameOver = false;
startImage = ImageIO.read(new File("img/start.png"));
gameOverImage = ImageIO.read(new File("img/gameover.png"));
//初始化属性
bird = new Bird();
column1 = new Column(1);
column2 = new Column(2);
ground = new Ground();
background = ImageIO.read(new File("img/bg.png"));
}
//重写paint方法,实现绘制
public void paint(Graphics g) {
g.drawImage(background,0,0,null);//名字,x坐标,y坐标
g.drawImage(column1.image,column1.x-column1.width/2,column1.y-column1.height/2,null);
g.drawImage(column2.image,column2.x-column2.width/2,column2.y-column1.height/2,null);
g.drawImage(ground.image,ground.x,ground.y,null);
//旋转绘制坐标系;API提供方法
Graphics2D g2= (Graphics2D)g;
g2.rotate(-bird.alpha,bird.x,bird.y);
g.drawImage(bird.image,bird.x-bird.width/2,bird.y-bird.height/2,null);
g2.rotate(bird.alpha,bird.x,bird.y);
//添加绘制分数的算法
Font f = new Font(Font.SANS_SERIF,Font.BOLD,40);
g.setFont(f);
g.drawString(""+score, 40, 60);
g.setColor(Color.PINK);
g.drawString(""+score, 40-3, 60-3);
//if(gameOver){
// g.drawImage(gameOverImage,0,0,null);
//}
//添加显示游戏结束状态的代码
switch(state){
case GAME_OVER:
g.drawImage(gameOverImage,0,0,null);
case START:
g.drawImage(startImage,0,0,null);
break;
}
}
//
public void action() throws Exception{
MouseListener l = new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
//鼠标按下的方法
public void mousePressed(MouseEvent e) {
try {
switch(state){
case GAME_OVER:
column1= new Column(1);
column2=new Column(2);
bird = new Bird();
score =0;
state =START;
break;
case START:
state = RUNNING;
case RUNNING:
bird.flappy();
}
} catch (Exception e2) {
// TODO: handle exception
}
//鸟向上飞
//bird.flappy();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
};
//将l挂接到当前的面板(game)上
addMouseListener(l);
while(true){
if(true){
/*ground.step();
column1.step();
column2.step();
bird.step();
bird.fly();
}
if(bird.hit(ground) || bird.hit(column1) || bird.hit(column2)){
//gameOver = true;
}
//积分逻辑
if(bird.x==column1.x || bird.x==column2.x){
score++;
}*/
switch(state){
case START:
bird.fly();
ground.step();
break;
case RUNNING:
column1.step();
column2.step();
bird.step();//上下移动
bird.fly();//翅膀挥动
ground.step();
//积分逻辑
if(bird.x==column1.x || bird.x==column2.x){
score++;
}
//如果鸟撞到地面,游戏结束
if(bird.hit(ground) || bird.hit(column1) || bird.hit(column2)){
state = GAME_OVER;
}
break;
}
repaint();//重新绘制界面
Thread.sleep(1000/30);//设置屏幕的刷新频率,1秒刷新30次
}}
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
BirdGame game = new BirdGame();
frame.add(game);
frame.setSize(440, 670);//设置窗口大小
frame.setLocationRelativeTo(null);//让窗口居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关联关闭按钮
frame.setVisible(true);//设置窗口可见
game.action();
}
}
Column类
//柱子
public class Column {
BufferedImage image;
int x,y,width,height;
//柱子的缝隙
int gap;
//两个柱子之间就距离
int distance;
Random random = new Random();
//初始化数据,n代表第几根柱子
public Column(int n) throws Exception{
image = ImageIO.read(new File("img/column.png"));
width = image.getWidth();
height = image.getHeight();
gap = 144;
distance = 245;
x = 550 + (n - 1)*distance;
y = random.nextInt(218)+132;
}
//柱子移动的方法
public void step(){
x-=3;
if(x == -width/2){
x=distance*2-width/2;
y = random.nextInt(218)+132;
}
}
}
Ground类
public class Ground{
BufferedImage image;
int x,y,width,height;
public Ground() throws Exception{
image = ImageIO.read(new File("img/ground.png"));
width = image.getWidth();
height = image.getHeight();
x=0;
y=500;
}
//地面移动的方法
public void step(){
x--;
if(x==-109){
x=0;
}
}
}