0

I'm making a flappy bird clone on my own and I have started by creating the Jframe, a red circle representing the bird and some pipes as rectangles. However when I run the program it doesn't show anything on screen except for the frame. Sometimes I get lucky and I get the red circle inside it. I'm running xubuntu and every other ide does the same. When I was on windows I didn't have this problem. What's the problem?

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);
}
}
  • Take `setVisible(true);` move it to the end of the `Game` constructor – MadProgrammer May 02 '16 at 21:01
  • @MadProgrammer I did that but still doesn't do anything... – Xherdi Lika May 02 '16 at 21:08
  • 1
    Remove `add(p);`, enable `gameTimer.start();`. See [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) for why you're having problems. Basically, you're going to find that using components this way is not going to work and that you would be better off having a single "game view" which does all the custom painting itself (and makes use of "game entities") – MadProgrammer May 02 '16 at 21:22
  • Get all repaints out of your paintComponent calls, and be sure to call the super.paintComponent within your override. – Hovercraft Full Of Eels May 02 '16 at 21:23
  • Thanks guys! I got it, helped a lot! – Xherdi Lika May 02 '16 at 21:48

0 Answers0