文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java实现经典游戏FlappyBird的示例代码

2024-04-02 19:55

关注

前言

《布谷鸟闯关-简单版》是一个基于java的布谷鸟闯关游戏,摁上键控制鸟的位置穿过管道间的缝隙,需要做碰撞检测,监听键盘事件,背景图片的切换,障碍物管道产生时y轴上需要随机位置。

主要设计

功能截图

游戏开始:

控制布谷鸟

飞过障碍物

撞到障碍物,游戏结束

代码实现

游戏启动类


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();
            }
        }
    }
}

核心类


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游戏的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯