我有这两节课。一个是游戏,另一个是菜单类。我的目标是从类(Starting.java)中打开一个类(FlappyBird.java),但是我不能使用"new Flappybird().setVisible(true)“,有人知道如何解决这个问题吗?
public class FlappyBird implements ActionListener, MouseListener, KeyListener {
public static FlappyBird flappybird;
int width=800,height=600;
public Renderer renderer;
public Rectangle bird;
public Random rand;
public ArrayList <Rectangle>columns;
public int ticks,yMotion;
public boolean gameOver,started;
public int score;
public FlappyBird() {
renderer=new Renderer();
rand=new Random();
Timer timer=new Timer(20, this);
JFrame jframe=new JFrame();
jframe.add(renderer);
jframe.setSize(width, height);
jframe.setVisible(true);
jframe.setResizable(false);
jframe.setTitle("Flappy Bird");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.addMouseListener(this);
jframe.addKeyListener(this);
bird=new Rectangle(width/2-10,height/2-10,20,20);
columns=new ArrayList<Rectangle>();
addColumn(true);
addColumn(true);
timer.start();
}
public void addColumn(boolean start) {
int space=300;
int widthh=100;
int heightt=50+rand.nextInt(300);
if(start) {
columns.add(new Rectangle(width+widthh+columns.size()*300,height-heightt-120,widthh,heightt));
columns.add(new Rectangle(width+widthh+(columns.size()-1)*300,0,widthh,height-heightt-space));
}else {
columns.add(new Rectangle(columns.get(columns.size()-1).x+600,height-heightt-120,widthh,heightt));
columns.add(new Rectangle(columns.get(columns.size()-1).x,0,widthh,height-heightt-space));
}
}
public void paintColumn(Graphics g,Rectangle column) {
g.setColor(Color.GREEN.darker());
g.fillRect(column.x, column.y, column.width, column.height);
}
public void repaint(Graphics g) {
g.setColor(Color.CYAN);
g.fillRect(0, 0, width, height);
g.setColor(Color.orange);
g.fillRect(0, height-120, width, 130);
g.setColor(Color.green);
g.fillRect(0, height-120, width, 20);
g.setColor(Color.red);
g.fillRect(bird.x, bird.y, bird.width, bird.height);
for(Rectangle column: columns) {
paintColumn(g,column);
}
g.setColor(Color.white);
g.setFont(new Font("Ariel",1,100));
if(gameOver) {
g.drawString("Game Over", 100, height/2-50);
}
if(!started) {
g.drawString("Click to Start", 100, height/2-50);
}
if(!gameOver && started) {
g.drawString(String.valueOf(score), width/2-50, 100);
}
}
public static void main(String[] args) {
flappybird=new FlappyBird();
}
@Override
public void actionPerformed(ActionEvent arg0) {
int speed=5;
ticks++;
if(started) {
for(int i=0;i<columns.size();i++) {
Rectangle rec = columns.get(i);
rec.x-=speed;
}
if(ticks %2 == 0 && yMotion<15) {
yMotion += 2;
}
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);
}
}
}
bird.y+= yMotion;
for(Rectangle column: columns) {
if(column.y==0 && bird.x+bird.width/2 > column.x+column.width/2- 5 && bird.x+bird.width/2<column.x+column.width/2+5) {
score++;
}
if(column.intersects(bird)) {
gameOver=true;
if(bird.x<=column.x) {
bird.x=column.x-bird.width;
}else {
if(column.y !=0 ) {
bird.y=column.y-bird.height;
}
}
bird.x=column.x-bird.width;
}
}
if(bird.y > height-120 || bird.y<0 ||bird.y>475 ) {
gameOver=true;
}
if(bird.y+yMotion >= height-120){
bird.y=height-120-bird.height;
}
}
renderer.repaint();
}
public void jump() {
if(gameOver) {
bird=new Rectangle(width/2-10,height/2-10,20,20);
columns.clear();
yMotion=0;
score=0;
addColumn(true);
addColumn(true);
gameOver=false;
}
if(!started) {
started=true;
}
else if(!gameOver) {
if(yMotion>0) {
yMotion=0;
}
yMotion-=10;
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
jump();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_SPACE) {
jump();
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}}
这是启动类:
public class Starting extends JFrame{
JLabel l = new JLabel("Name:");
JTextField t= new JTextField(10);
JButton start = new JButton("Start");
JButton back = new JButton("Back");
JPanel p1 = new JPanel();
public Starting() {
p1.add(l);
p1.add(t);
p1.add(start);
p1.add(back);
this.add(p1);
this.setTitle("Starting");
this.setSize(300,300);
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
dispose();
new FlappyBird().setVisible(true);
}
});
}
public static void main(String[] args) {
Starting l = new Starting();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}问题是我不能输入"new FlappyBird().setVisible(true);“,有人能修复这个错误吗?
发布于 2017-12-01 22:58:40
您的Game类没有实现java的任何可视界面...添加"extends“
public class Game extends JFrame implements ActionListener, MouseListener, KeyListener { ... }这将会起到作用。
发布于 2017-12-01 23:06:05
一些可能的原因:
public修饰符发布于 2017-12-01 23:15:11
您还应该将UI与功能逻辑分开。
我的意思是你应该有一个游戏类,只是为了存储数据,制作操作等,所需的数据应该通过可见的方法提供给正在制作游戏实例的类,并只在相应的标签中显示它们等。
方案可以看起来像:"Parent UI" (带有信息标签,仅仅是UI组件等)- Game class (用于操作、统计等的控制器)- Game instance data storage class (玩家名称,例如)
在这种情况下,您不需要扩展swing组件(例如,JFrame)-要么在游戏类中,要么在实例数据存储类中,只在“查看器”中-在我的例子中是父UI,你可以通过控制器方法来提供数据。
(UI可以包含游戏控制器的实例,在游戏控制器中还可以创建特定游戏的实例)
https://stackoverflow.com/questions/47595980
复制相似问题