我自己创造了一只脆弱的鸟类克隆,我开始创建Jframe,一个代表鸟的红色圆圈和一些管道作为矩形。但是,当我运行程序时,除了框架外,它不会在屏幕上显示任何内容。有时候我很幸运,我在里面得到了一个红圈。我正在运行xubuntu,而其他所有ide也是如此。当我在窗户上时,我没有遇到这个问题。问题是什么?
Game.java
package flappybird;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.Timer;
//Create game gui
/*
Game:
-Bird that responds to mouse clicks ----> check
-Pipes which have random heights ----->
-Scores everytime the bird passes successfully between pipes ----- >
-Game speeds up as you score higher and higher ------>
*/
public class Game extends JFrame implements MouseListener{
private int time;
private final int DELAY = 10;
private Timer gameTimer;
private Bird b;
private Pipes p;
public Game(){
super("Flappy Bird");
time=0;
setSize(600,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
b=new Bird();
p=new Pipes();
add(b);
add(p);
addMouseListener(this);
class TimeListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
time+=2*DELAY;
b.fall(time);
}
}
ActionListener listener = new TimeListener();
gameTimer = new Timer(DELAY,listener);
//gameTimer.start();
}
@Override
public void mouseClicked(MouseEvent e) {
b.fly(time);
time=0;
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
FlappyBird.java
package flappybird;
public class FlappyBird {
public static void main(String[] args) {
Game newgame = new Game();
}
}
Bird.java
package flappybird;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class Bird extends JComponent{
private Ellipse2D.Double birdy;
private final int HEIGHT = 25;
private final int WEIGHT = 25;
private final int BIRD_X = 300;
private final int BIRD_Y = 200;
private final double G = 9.8;
private int distance;
public Bird(){
birdy = new Ellipse2D.Double(BIRD_X,BIRD_Y,HEIGHT,WEIGHT);
distance = BIRD_Y;
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.draw(birdy);
g2.fill(birdy);
}
public void fall(int t){
distance += (int) ((G*t*t)/(2*Math.pow(10, 6)));
birdy = new Ellipse2D.Double(BIRD_X,distance,HEIGHT,WEIGHT);
repaint();
}
public void fly(int t){
distance -= (int) 10*((G*t*t)/(2*Math.pow(10, 6)));
birdy = new Ellipse2D.Double(BIRD_X,distance,HEIGHT,WEIGHT);
repaint();
}
}
Pipes.java
package flappybird;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.JComponent;
public class Pipes extends JComponent{
private Rectangle upperPipe;
private Rectangle lowerPipe;
private final int width = 100;
private final int posX = 350;
private final int posY = 0;
private Random h;
public Pipes(){
h = new Random(100);
int startingHeight = 101 + h.nextInt();
int remainingHeight = (int)(400 - startingHeight) / 2;
upperPipe = new Rectangle(posX,posY,width,startingHeight);
lowerPipe = new Rectangle(posX,remainingHeight,width,remainingHeight);
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.GREEN);
g2.draw(upperPipe);
g2.fill(upperPipe);
g2.draw(lowerPipe);
g2.fill(lowerPipe);
}
}