Buggy Pipe Game输出到JFrame

时间:2014-02-04 23:34:53

标签: java jframe awt drawrect flappy-bird-clone

我曾经把我的程序作为applet运行,然后它工作正常,但我决定尝试让它在JFrame中运行。我最近有一个覆盖问题,但我想我终于解决了这个问题。这是我最近一直在努力的游戏,它基本上是Flappy Bird的娱乐。我不打算出售我的成品版本,因为我认为这笔款项不属于我。现在我正在寻找一个管道穿过屏幕,我计划在任何给定时间在屏幕上最多制作3组管道。

框架未打开时出现FIXED错误 新错误:不需要的输出

这是我的主要课程(游戏)

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;

    public class Game {

        Pipes panel = new Pipes();

        public Game() {
            JFrame f = new JFrame();

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(panel);
            f.setTitle("Pipe Game");
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            f.setResizable(false);

            Timer timer = new Timer(10, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    panel.move();
                    panel.repaint();
                }
            });
            timer.start();
        }

        public static void main(String args[]) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    new Game();
                }
            });
        }
}

这是我的班级(管道)

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Pipes extends JPanel {
    //Declare and initialiaze variables
    int height = setHeight();
    int x1 = 754;   //xVal start
    int x2 = 75;    //pipe width
    int y1 = -1;    //yVal start
    int y2;         //pipe height
    int vel;        //pipe velocity
    int gap = 130;  //gap height

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        y2 = height;
        g.clearRect(0,0,750,500);                       //Clear screen
        g.drawRect(x1,y1,x2,y2);                        //Draw part 1
        g.drawRect(x1-3,y2-1,x2+6,25);                  //Draw part 2
        g.drawRect(x1-3,y2+25+gap,x2+6,25);             //Draw part 3
        g.drawRect(x1,y2+25+gap+25,x2,500-y2-49-gap);   //Draw part 4
    }

    public void move() {
        x1--;
    }

    public int getY() {
        return y2+25;
    }

    public int getX() {
        return x1-3;
    }

    public int setHeight() {
        int num = (int)(9*Math.random() + 1);
        int val = 0;
        if (num == 9)
        {
            val = 295;
        }
        else if (num == 8)
        {
            val = 246;
        }
        else if (num == 7)
        {
            val = 216;
        }
        else if (num == 6)
        {
            val = 185;
        }
        else if (num == 5)
        {
            val = 156;
        }
        else if (num == 4)
        {
            val = 125;
        }
        else if (num == 3)
        {
            val = 96;
        }
        else if (num == 2)
        {
            val = 66;
        }
        else
        {
            val = 25;
        }
        return val;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(751, 501);
    }
}

1 个答案:

答案 0 :(得分:2)

我想你想要

public void run() {
   new Game();
}

而不是

public void run() {
   new Pipes();
}