Java實現經典遊戲Flappy Bird的示例代碼

前言

《佈谷鳥闖關-簡單版》是一個基於java的佈谷鳥闖關遊戲,摁上鍵控制鳥的位置穿過管道間的縫隙,需要做碰撞檢測,監聽鍵盤事件,背景圖片的切換,障礙物管道產生時y軸上需要隨機位置。

主要設計

  • 設計遊戲界面,用swing實現
  • 設計背景
  • 設計移動墻
  • 設計佈谷鳥
  • 設計障礙物
  • 設計背景音樂和音效
  • 由幾個關鍵的佈爾類型變量start,crash,over是產生鍵鍵盤事件時用來控制界面顯示的彈框的
  • 操作:空格鍵開始遊戲,enter鍵取消"game over"彈框,摁空格鍵繼續,上鍵(up)是對鳥的控制
  • 工具類ImageMgr專用於讀取圖片

功能截圖

遊戲開始:

控制佈谷鳥

飛過障礙物

撞到障礙物,遊戲結束

代碼實現

遊戲啟動類

public class Main {
    public static void main(String[] args) {
        Cuckoo frame = new Cuckoo();
        while (true){
            try {
                Thread.sleep(100);
                if(!frame.crash){
                    frame.repaint();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

核心類

/**
 * 1.窗口
 * 2.背景
 * 3.移動墻
 * 4.佈谷鳥
 * 5.障礙物
 * 6.背景音樂
 * 7.音效
 * 8.歡迎界面
 * 9.game over
 */
public class Cuckoo extends Frame {
    private int step;
    private int score;
    private int height;
    private int moveLeft;
    private int pipeMove;

    boolean crash = false;
    private boolean over = false;
    private boolean start = false;

    private static int bird_y = 270;
    private static final int L_X = 800;
    private static final int L_Y = 200;
    private static final int SPEED = 2;
    private static final int BIRD_X = 100;
    private static final int GAME_X_Y_0 = 0;
    private static final int GAME_WIDTH = 288;
    private static final int GAME_HEIGHT = 512;
    private static final int PIPE_WIDTH = ImageMgr.pipe.getWidth();
    private static final int PIPE_HEIGHT = ImageMgr.pipe.getHeight();
    private static final int READY_WIDTH = ImageMgr.ready.getWidth();
    private static final int READY_HEIGHT = ImageMgr.ready.getHeight();
    private static final int START_WIDTH = ImageMgr.start.getWidth();
    private static final int BIRD_WIDTH = ImageMgr.bird[0].getWidth();
    private static final int BIRD_HEIGHT = ImageMgr.bird[0].getWidth();
    private static final int GROUND_HEIGHT = ImageMgr.ground.getHeight();
    private static final int GAME_OVER_WIDTH = ImageMgr.gameOver.getWidth();
    private static final int GAME_OVER_HEIGHT = ImageMgr.gameOver.getHeight();
    private static final int BACKGROUND_HEIGHT = ImageMgr.background.getHeight();

    private static Image offScreenImage;
    private static Rectangle pipeRectangle1;
    private static Rectangle pipeRectangle2;
    private static Rectangle birdRectangle;

    Cuckoo(){
        setVisible(true);
        setResizable(false);
        //setTitle("cuckoo");
        setBounds(L_X, L_Y,GAME_WIDTH,GAME_HEIGHT);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()){
                    case KeyEvent.VK_SPACE : start = true;break;
                    case KeyEvent.VK_ENTER : reopen();break;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    bird_y -= 20;
                }
            }
        });

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void update(Graphics g) {
        if(offScreenImage == null){
            offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage,0,0,null);
    }

    @Override
    public void paint(Graphics g) {
        background(g);
        ground(g);
        start(g);

        if(start){
            bird(g);
            pipe(g);
            colliedWith();
            gameOver(g);
            score(g);
        }

    }

    //如果碰撞後,可以嗯enter鍵重開
    private void reopen(){
        if(crash){
            score = 0;
            over = false;
            crash = false;
            start = false;
            moveLeft = 0;
            pipeMove = 0;
            //bird_y = 270;//繼上次死的位置開始
        }
    }

    private void score(Graphics g){
        Font font = new Font("微軟雅黑",Font.BOLD,18);
        g.setFont(font);
        Color c = g.getColor();
        g.setColor(Color.lightGray);
        g.drawString("score " + score,20,70);
        g.setColor(c);
    }

    private void background(Graphics g){
        g.drawImage(ImageMgr.background,
                GAME_X_Y_0 - moveLeft,GAME_X_Y_0,
                GAME_WIDTH - moveLeft,GAME_HEIGHT,
                GAME_X_Y_0,
                GAME_X_Y_0,
                GAME_WIDTH,
                GAME_HEIGHT, null);
        g.drawImage(ImageMgr.background,
                GAME_WIDTH-1 - moveLeft,GAME_X_Y_0,
                GAME_WIDTH*2 - moveLeft, GAME_HEIGHT,
                GAME_X_Y_0,
                GAME_X_Y_0,
                GAME_WIDTH,
                GAME_HEIGHT, null);
        moveLeft += SPEED;
        //遊戲啟動計分
        if(start && moveLeft % 100 == 0){
            score += 1;
        }
        if(moveLeft >= GAME_WIDTH){
            moveLeft = 0;
        }
    }

    private void ground(Graphics g){
        g.drawImage(ImageMgr.ground,0,BACKGROUND_HEIGHT - GROUND_HEIGHT,null);
    }

    private void bird(Graphics g){
        Color c = g.getColor();
        g.setColor(Color.BLUE);
        g.drawRect(BIRD_X, bird_y,BIRD_WIDTH,BIRD_HEIGHT);
        g.setColor(c);

        g.drawImage(ImageMgr.bird[step++], BIRD_X, bird_y,null);
        if(step >= ImageMgr.bird.length){
            step = 0;
        }

        if(start){
            bird_y += 4;
        }

        birdRectangle = new Rectangle(BIRD_X,bird_y,BIRD_WIDTH,BIRD_HEIGHT);
    }

    private void pipe(Graphics g){
        drawPipe(g);
        pipeMove += SPEED;
        if(pipeMove >= GAME_WIDTH + PIPE_WIDTH){
            pipeMove = 0;
            Random ran = new Random();
            height = ran.nextInt(10)*20;
            drawPipe(g);
        }

    }

    private void drawPipe(Graphics g){
        g.setColor(Color.RED);
//        g.drawRect(GAME_WIDTH - pipeMove,GAME_HEIGHT/5*3 - height,PIPE_WIDTH,PIPE_HEIGHT);
//        g.drawRect(GAME_WIDTH - pipeMove,-GAME_HEIGHT/5*4 - height,PIPE_WIDTH,PIPE_HEIGHT);

        g.drawImage(ImageMgr.pipe,GAME_WIDTH - pipeMove,GAME_HEIGHT/5*3 - height,null);
        g.drawImage(ImageMgr.pipe,GAME_WIDTH - pipeMove,-GAME_HEIGHT/5*4 - height,null);

        pipeRectangle1 = new Rectangle(GAME_WIDTH - pipeMove,GAME_HEIGHT/5*3 - height,PIPE_WIDTH,PIPE_HEIGHT);
        pipeRectangle2 = new Rectangle(GAME_WIDTH - pipeMove,-GAME_HEIGHT/5*4 - height,PIPE_WIDTH,PIPE_HEIGHT);
    }

    private void gameOver(Graphics g){
        if(over){
            g.drawImage(ImageMgr.gameOver,GAME_WIDTH/2 - GAME_OVER_WIDTH/2, GAME_HEIGHT/2 - GAME_OVER_HEIGHT,null);
        }
    }

    private void start(Graphics g){
        if(!start){
            g.drawImage(ImageMgr.ready,GAME_WIDTH/2 - READY_WIDTH/2, GAME_HEIGHT/2 - READY_HEIGHT,null);
            g.drawImage(ImageMgr.start,GAME_WIDTH/2 - START_WIDTH/2, GAME_HEIGHT/2,null);
        }
    }

    private void colliedWith(){
        if(birdRectangle.intersects(pipeRectangle1) || birdRectangle.intersects(pipeRectangle2)){
            crash = true;
            over = true;
            //gameOver(g);
        }
    }
}

工具類

public class ImageMgr {
    static BufferedImage background,ground,pipe,gameOver,start,ready;
    static BufferedImage[] bird = new BufferedImage[3];
    private static final ImageMgr INSTANCE = new ImageMgr();
    //nothing
    private ImageMgr(){
        initImage();
    };

    private void initImage(){
        try {
            background = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/bg_light.png")));
            ground = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/ground.png")));
            pipe = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/pipe.png")));
            gameOver = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/gameover.png")));
            start = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/start.png")));
            ready = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/ready.png")));

            for(int i = 0;i < 3;i++){
                bird[i] = ImageIO.read(Objects.requireNonNull(Cuckoo.class.getClassLoader().getResourceAsStream("images/blueBird_" + (i+1) + ".png")));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static ImageMgr getInstance(){
        return INSTANCE;
    }
}

總結

通過此次的《佈谷鳥闖關-簡單版》實現,讓我對JAVA的相關知識有瞭進一步的瞭解,對java這門語言也有瞭比以前更深刻的認識。

java的一些基本語法,比如數據類型、運算符、程序流程控制和數組等,理解更加透徹。java最核心的核心就是面向對象思想,對於這一個概念,終於悟到瞭一些。

以上就是Java實現經典遊戲Flappy Bird的示例代碼的詳細內容,更多關於Java Flappy Bird遊戲的資料請關註WalkonNet其它相關文章!

推薦閱讀: