飞扬的小鸟(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

©️2020 CSDN 皮肤主题: 1024 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值