0

我的新项目是一个用 Java 编写的 Flappy Bird 游戏,我遇到了一个小问题。它口吃很多。我不知道该怎么办。我一直在寻找其他主题的解决方案,但不幸的是我不知道出了什么问题。我使用了缓冲策略,但我不确定它是否做得好。这是整个代码。

public final class FlappyBird extends JFrame implements ActionListener, MouseListener{

private static FlappyBird flappyBird;
private final int Width = 800, Height =600;
private Rectangle bird;
private ArrayList<Rectangle> columns;
private Random random;
private int ticks, yMotion;
private boolean gameOver=false;
private boolean started=true;
private int score = 0;
private BufferedImage img = null;
public FlappyBird(){
    setIgnoreRepaint(true);
    Timer timer = new Timer(20, this);
    random = new Random();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(Width, Height);
    setResizable(false);
    addMouseListener(this);
    setTitle("Flappy Bird");
    setVisible(true);


    bird = new Rectangle(Width/2-10,Height/3-100,20,20);
    columns = new ArrayList<>();

    addColumn(true);
    addColumn(true);
    addColumn(true);
    addColumn(true);
    setIgnoreRepaint(true);
    timer.start();
}

public void UP(){
    if(gameOver){
        bird = new Rectangle(Width/2-10,Height/3-100,20,20);
        columns.clear();
        yMotion=0;
        score = 0;
        addColumn(true);
        addColumn(true);
        addColumn(true);
        addColumn(true);
        gameOver = false;
    }
    if(!started){
        started=true;
    }else if(!gameOver){
        if(yMotion>0){
            yMotion=0;
        }
        yMotion-=10;
    }
}


public void addColumn(boolean start){
    int space = 300;
    int width = 100;
    int height = 30 + random.nextInt(300);

    if(start){
        columns.add(new Rectangle(Width+width+columns.size()*200,Height-height-100,width,height));
        columns.add(new Rectangle(Width+width+(columns.size()-1)*200, 0,width,Height-height-space));
    }
    else{
        columns.add(new Rectangle(columns.get(columns.size()-1).x+300,Height-height-100,width,height));
        columns.add(new Rectangle(columns.get(columns.size()-1).x, 0,width,Height-height-space));
    }
}




@Override
public void actionPerformed(ActionEvent ae) {
ticks++;
int speed = 10; 
if(started){
       for(int i = 0;i<columns.size();i++){
           Rectangle column = columns.get(i);
           column.x-=speed;
       }

       if(ticks%2==0&&yMotion<15){
           yMotion+=2;
       }
       bird.y+=yMotion;

       for(int i = 0;i<columns.size();i++){
           Rectangle column = columns.get(i);
           if(column.x+column.width<0){
               columns.remove(column);

               if(column.y==0){
                    addColumn(false);
               }
       }
       }

       for(Rectangle column:columns){
           if(bird.x+bird.width/2>column.x+column.width/2&&bird.x+bird.width<column.x+column.width){
               score++;
           }
           if(column.intersects(bird)){
              gameOver = true;
           }
       }

       if(bird.y>Height-120||bird.y<0){
           gameOver = true;
       }

 }
   render();
}


public void render(){
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(2);
        return;
    }

 //   Graphics g = bs.getDrawGraphics();
    //g = (Graphics2D)g;
Graphics2D g = null;
g = (Graphics2D) bs.getDrawGraphics();


    try {
        img = ImageIO.read(getClass().getResourceAsStream("/img/niebo.png"));
    } catch (IOException ex) {
    }
    g.drawImage(img,0,0,Width,Height, this);
    try {
        img = ImageIO.read(getClass().getResourceAsStream("/img/plaza.png"));
    } catch (IOException ex) {
    }
    g.drawImage(img,0,Height-120,Width,120, this);

    try {
        img = ImageIO.read(getClass().getResourceAsStream("/img/flapy.png"));
    } catch (IOException ex) {
    }
    g.drawImage(img,bird.x,bird.y,bird.width,bird.height, this);

    for(Rectangle col: columns){
         try {
         img = ImageIO.read(getClass().getResourceAsStream("/img/tra.png"));
     } catch (IOException ex) {
     }
       //  g.setColor(Color.yellow);
       //  g.fillRect(col.x, col.y, col.width, col.height);
     g.drawImage(img,col.x,col.y,col.width,col.height, this);
    }
    g.setColor(Color.green.darker());
    g.setFont(new Font("Arial", 1, 50));
    if(!started){
        g.drawString("Kliknij, aby zacząć grę!", 200, Height/2);
    }
    if(gameOver){
        g.drawString("Koniec Gry!", 200, Height/2);
    }
    if(!gameOver&&started){
        g.drawString(String.valueOf(score), Width/2-25, 100);

    }


    g.dispose();
    bs.show();
          Toolkit.getDefaultToolkit().sync();
    try {
            Thread.sleep(1);
    } catch (InterruptedException ex) {
        Logger.getLogger(FlappyBird.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public static void main(String[] args){
  System.setProperty("sun.java2d.d3d", "True");
  Toolkit.getDefaultToolkit().sync();
  System.setProperty("sun.java2d.trace", "count");
  flappyBird = new FlappyBird(); 
}

@Override
public void mouseClicked(MouseEvent me) {
    UP();
}

@Override
public void mousePressed(MouseEvent me) {
}

@Override
public void mouseReleased(MouseEvent me) {
}

@Override
public void mouseEntered(MouseEvent me) {
}

@Override
public void mouseExited(MouseEvent me) {

}

}

4

0 回答 0