飞扬的小鸟(FlapyBird)Java版开发制作全过程

本案例存在一些小bug,但主体功能均已实现。算是一个高完成度的半成品,可以参考学习下并且有修正提升的空间。主要思路是用canvas将画面画出来。这个案例不难,两个小时就写完了,最耗时间的就是图片的尺寸计算,案例如下,供大家参考。先看效果:

飞扬的小鸟(FlapyBird)Java版开发制作全过程_第1张图片

下面是目录结构:

飞扬的小鸟(FlapyBird)Java版开发制作全过程_第2张图片

源码如下:

Bird:

package com.xiaoma.items;

import java.awt.Graphics;

import com.xiaoma.tool.Resource;

public class Bird {
    private int index=0;
    public static int x=Game.WIDTH/8;
    public static int y=Game.HEIGHT/2;
    private int vy=5;
    public static double G=0.8;



    public int getVy() {
        return vy;
    }
    public void setVy(int vy) {
        this.vy = vy;
    }
    public void draw(Graphics g){
        g.drawImage(Resource.bird[index], x, y, null);
        index++;
        if(index==8)index=0;
    }
    public void update(){
        if(vy==0)vy=5;
        y+=vy;
        vy+=G;
        if(y>Game.HEIGHT-190){
            Game.running=false;
        }

    }

}

Column:

package com.xiaoma.items;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;

import com.xiaoma.tool.Resource;

public class Column {
public int x=Game.WIDTH;
static Random random = new Random();
public int y=0-((random.nextInt(17)+28)*10);
private int vx=0;
public static int count;
Font font=new Font("微软雅黑", 13,25);
Color color=new Color(255,255,0);
public void draw(Graphics g){
    g.drawImage(Resource.Column, x, y, null);
    g.setFont(font);
    g.setColor(color);
    g.drawString("分数:"+count, 20, 25);
}
public boolean judge(Column c) {
    if (x0)&&(Bird.y<(483+y)||Bird.y>(483+y+90))){
    Game.running=false; 
}
}
public int getVx() {
    return vx;
}
public void setVx(int vx) {
    this.vx = vx;
}
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}

}

Game:

package com.xiaoma.items;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import com.xiaoma.tool.Resource;

public class Game extends Canvas implements Runnable,KeyListener{
/**
     * 
     */
private static final long serialVersionUID = 1L;
public static int WIDTH=717;
public static int HEIGHT=537;
private Bird bird = new Bird();
Ground ground=new Ground();
Column column=new Column();
Column column2=new Column();
public static boolean running=true;
@SuppressWarnings("static-access")
public static void main(String[] args) {
    JFrame jframe =new JFrame();
    Game game = new Game();
    jframe.add(game);
    jframe.setTitle("FlapyBird");
    jframe.setVisible(true);
    jframe.setSize(WIDTH, HEIGHT);
    jframe.setResizable(false);
    jframe.setLocationRelativeTo(null);
    jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);

    new Thread(game).start();
    game.addKeyListener(game);

}
public void update(){
    ground.update();
    column.update();
    column2.update();
    bird.update();
}
BufferStrategy bS;
public void draw() {
    bS = this.getBufferStrategy();
    if(bS==null){
        this.createBufferStrategy(2);
        return;
    }
    Graphics graphics = bS.getDrawGraphics();
    graphics.clearRect(0, 0, WIDTH, HEIGHT);
    graphics.drawImage(Resource.bgImage, 0, 0, null);
    bird.draw(graphics);
    column.setVx(-6);
    column.draw(graphics);
    column2.draw(graphics);
    if(column.judge(column2)){
        column2.draw(graphics);
    }
    ground.draw(graphics);
    bS.show();
}

@Override
public void run() {

    while (running) {
        update();
        draw();
        try {
            Thread.sleep(40);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
@Override
public void keyTyped(KeyEvent e) {
    if(e.getKeyCode()==0){
        bird.setVy(-8);
    }
}
@Override
public void keyPressed(KeyEvent e) {

}
@Override
public void keyReleased(KeyEvent e) {

}

}

Ground:

package com.xiaoma.items;

import java.awt.Graphics;

import com.xiaoma.tool.Resource;

public class Ground {
private int x=0;
private int y=Game.HEIGHT-146;
private int vx=-6;
public void draw(Graphics g){
    g.drawImage(Resource.land, x, y, null);
}
public void update(){
    x+=vx;
    if(x<-70)x=0;
}
}

Resource:

package com.xiaoma.tool;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Resource {
public static BufferedImage bgImage;
public static BufferedImage land;
public static BufferedImage Column;
public static BufferedImage[] bird = new BufferedImage[8];
static{
    try {
        bgImage=ImageIO.read(new File("img/bg.png"));
        land =ImageIO.read(new File("img/ground.png"));
        Column =ImageIO.read(new File("img/column.png"));
        for (int i = 0; i < bird.length; i++) {
            bird[i]=ImageIO.read(new File("img/"+i+".png"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

素材/源码下载地址:http://download.csdn.net/download/weixin_38891048/10238169

你可能感兴趣的:(实例讲解)