操作画面:

 

 

按空格开始,按空格向上跳动

可以在control方法里设置birdY后的数字来改变每次空格鸟跳动的高度

 public void control() {

        System.out.println("hello");
        //加键盘的监视器,后面就是键盘监视器的类
        addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e) {
                System.out.println(e.getKeyCode());
              if(e.getKeyCode()==32) {
                  birdY -= 70;
                  welcom1=false;
                  start=true;
              }else if(e.getKeyCode()==82)
              {
                  start=false;
                  welcom1=true;
                  show=false;
                  FlappyBird c=new FlappyBird();
                  mainWindows();
              }
            }
        });
    }

完整代码:

package zuoye;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

public class FlappyBird extends JFrame {

    public void init() {
        control();
        mainWindows();
    }

int score=0;
    boolean start=false;
    boolean welcom1=true;


    public void crash(){
        //碰撞检测
        if(birdRectangle.intersects(di)||birdRectangle.intersects(shang1)||birdRectangle.intersects(shang2)||birdRectangle.intersects(xia1)||birdRectangle.intersects(xia2)){
            System.out.println("游戏结束");
            start=false;
            show=true;
        }
    }

    //g起到的是一个画笔的作用
    public void paint(Graphics g) {
        //在原来的图上在画图,也就是父类paint
        super.paint(g);
        drawBackGrround(g);
        drawmoveGround(g);
        bird(g);
        addpipe(g);
        crash();
        End(g);
        text(g);
        welcom(g);
    }

    public void welcom( Graphics g){
        Image wel=new ImageIcon("image/text_ready.png").getImage();
        Image sta=new ImageIcon("image/button_play.png").getImage();
      if (welcom1==true) {
          g.drawImage(wel,30,250,450,100,null);
          g.drawImage(sta,150,350,200,100,null);
      }
    }

    Image[] pics=new Image[10];
    public void text(Graphics g){
        for(int i=0;i<=9;i++) {
            pics[i] =new ImageIcon(("image/number_score_0"+i+".png")).getImage();
        }
        int qian=score/1000;
        int bai=score%1000/100;
        int shi=score%100/10;
        int ge=0;
        g.drawImage(pics[qian],400,40,null);
        g.drawImage(pics[bai],420,40,null);
        g.drawImage(pics[shi],440,40,null);
        g.drawImage(pics[ge],460,40,null);

    }

    boolean show=false;
    public void End(Graphics g){
        Image end=new ImageIcon("image/text_game_over.png").getImage();
        if (show){
            g.drawImage(end,30,300,450,100,null);
        }
    }

    int moveleft = 0;

    Random b2=new Random(System.currentTimeMillis()); int z2=b2.nextInt(10);
    public void drawBackGrround(Graphics g) {
        Image bg = new ImageIcon("image/bg_night.png").getImage();
        Image bg2 = new ImageIcon("image/bg_day.png").getImage();

        if (z2%2==0){
            g.drawImage(bg, moveleft, 0, 500, 700, null);
            g.drawImage(bg, 500 + moveleft, 0, 500, 700, null);
        }
        else{
            g.drawImage(bg2, moveleft, 0, 500, 700, null);
            g.drawImage(bg2, 500 + moveleft, 0, 500, 700, null);
        }
        moveleft -= 10;
        if (moveleft == -500)
            moveleft = 0;
    }

    Rectangle di;
    public void drawmoveGround(Graphics g) {
        Image ground = new ImageIcon("image/land.png").getImage();
        g.drawImage(ground, moveleft, 550, 500, 150, null);
        g.drawImage(ground, 500 + moveleft, 550, 500, 150, null);
        moveleft -= 10;
        if (moveleft == -500)
            moveleft = 0;
        di = new Rectangle(0,550,ground.getWidth(null),ground.getHeight(null));
    }

    Random b1=new Random(System.currentTimeMillis());
      final int z1=b1.nextInt(10);
    int i = 0;
    int birdY = 350;
    Rectangle birdRectangle;
    public void bird(Graphics g) {

        if (z1%2==0){
            Image bird = new ImageIcon("image/bird1_" + i + ".png").getImage();
            g.drawImage(bird, 140, birdY, 70, 70, null);
            i++;
            if (i == 3)
                i = 0;
            birdY += 25;
            birdRectangle = new Rectangle(140,birdY,bird.getWidth(null),bird.getHeight(null));
        }
        else
        {
            Image bird = new ImageIcon("image/bird0_" + i + ".png").getImage();
            g.drawImage(bird, 140, birdY, 70, 70, null);
            i++;
            if (i == 3)
                i = 0;
            birdY += 25;
            birdRectangle = new Rectangle(140,birdY,bird.getWidth(null)-5,bird.getHeight(null)-10);
        }


    }


    public void mainWindows() {
        setBounds(500, 200, 500, 700);
        setTitle("鸟");
        //关闭窗口的时候可以exit
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        while (true) {
            if (start)
            repaint();
            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                System.out.println();
            }
        }
    }
    int pipeX1=400;
    //h1是下边的管道的高度
    int h1=100;
    //h2是上边的管道的高度
    int h2=200;

    int h3;
    int h4;
    int y1=550-h1;

    int y4=550-h4;
    int pipeX2=700;
    Rectangle shang1;
    Rectangle shang2;
    Rectangle xia1;
    Rectangle xia2;
    public void pipes(Graphics g) {
        Image pipeDown = new ImageIcon("image/pipe_down.png").getImage();
        Image pipeUp = new ImageIcon("image/pipe_up.png").getImage();
        Image pipeDown2 = new ImageIcon("image/pipe_down.png").getImage();
        Image pipeUp2 = new ImageIcon("image/pipe_up.png").getImage();
        g.drawImage(pipeDown, pipeX1, 0,70,h2,this);
        g.drawImage(pipeUp, pipeX1, y1, 70,h1,this);
        g.drawImage(pipeDown2, pipeX2, 0,70,h3,this);
        g.drawImage(pipeUp2, pipeX2, y4, 70,h4,this);
        pipeX1-=30;
        pipeX2-=30;
        shang1=new Rectangle(pipeX1,0,pipeDown.getWidth(null),h2);
        shang2=new Rectangle(pipeX2,0,pipeDown2.getWidth(null),h3);
        xia1=new Rectangle(pipeX1,y1,pipeUp.getWidth(null)-5,h1);
        xia2=new Rectangle(pipeX2,y4,pipeUp.getWidth(null)-5,h4);
    }
    int flag1=0;
    public void addpipe(Graphics g){
        pipes(g);
        int n1=0,n2=0;
        if(pipeX1<=200&&flag1==0) {
                Random r1=new Random();
                Random r2=new Random();

                while((n1+n2)<350||(n1+n2)>400||n1<100||n2<100)
                {
                    n1=r1.nextInt(290)+30;
                    n2=r2.nextInt(290)+30;
                }

                h3=n1;
                h4=n2;
                y4=550-h4;
                n1=0;n2=0;
                score+=10;
            flag1=1;


        }
        if(pipeX1<-50)
        {
            pipeX1=500;
            flag1=0;
        }
//        if (pipeX1<50)
//            pipeX1=pipeX2+400;
        if(pipeX2<=200&&flag1==0) {
            Random r1=new Random();
            Random r2=new Random();

            while((n1+n2)<350||(n1+n2)>400||n1<100||n2<100)
            {
                n1=r1.nextInt(290)+30;
                n2=r2.nextInt(290)+30;
            }

                h1=n1;
                h2=n2;
             y1=550-h1;
            n1=0;n2=0;
            score+=10;
            flag1=1;


        }
        if (pipeX2<-50)
        {
            pipeX2=500;
            flag1=0;
        }
//        if (pipeX2<50)
//            pipeX1=pipeX2+400;
    }

    public void control() {

        System.out.println("hello");
        //加键盘的监视器,后面就是键盘监视器的类
        addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e) {
                System.out.println(e.getKeyCode());
              if(e.getKeyCode()==32) {
                  birdY -= 70;
                  welcom1=false;
                  start=true;
              }else if(e.getKeyCode()==82)
              {
                  start=false;
                  welcom1=true;
                  show=false;
                  FlappyBird c=new FlappyBird();
                  mainWindows();
              }
            }
        });
    }


    public static void main(String[] args) {

        new FlappyBird().init();
    }

}

图片素材:

链接:https://pan.baidu.com/s/1Dg2_mp9YQUSJW7as1IFgyA?pwd=0000 
提取码:0000 

图片包建在项目地下,src之外

可以更改bird方法下的birdY+=之后的数字来更改鸟的下落速度

同理可更改pipe方法下的

pipeX1-=30;
pipeX2-=30;

之后的数字来更改管道的移动数字

# intellij-idea
Logo
华为开发者空间

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐

  • · GaussDB逻辑解码技术原理
  • · 华为云开源引领,KubeEdge晋级CNCF毕业项目
  • · 创想无限 精彩不止 2024华为开发者大赛·中国区半决赛即将开启
cover

GaussDB逻辑解码技术原理

cover

华为云开源引领,KubeEdge晋级CNCF毕业项目

cover

创想无限 精彩不止 2024华为开发者大赛·中国区半决赛即将开启