飛揚的小鳥(FlapyBird)Java版開發製作全過程

本案例存在一些小bug,但主體功能均已實現。算是一個高完成度的半成品,可以參考學習下並且有修正提升的空間。主要思路是用canvas將畫面畫出來。這個案例不難,兩個小時就寫完了,最耗時間的就是圖片的尺寸計算,案例如下,供大家參考。先看效果:

這裏寫圖片描述

下面是目錄結構:

這裏寫圖片描述

源碼如下:

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 (x<Game.WIDTH/2) {
        c.setVx(-6);
        return true;
    }
    return false;

}
public void update() {

    x+=vx;
    if(x<-75){
        count++;
        x=Game.WIDTH;
        y=0-((random.nextInt(17)+28)*10);
    }
    if(((x-Bird.x)<50 &&(x-Bird.x)>0)&&(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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章