CodingForFun312
CodingForFun312

Reputation: 11

Flappy bird Pipes how to randomize in java the program I am using for school is JGrasp

In the paint component.graphics g I was creating pipes for my flappy bird game but I don't know how to approach making multiple pipes that are randomized. I've made the first one randomize but I don't know how to make more and have those new ones be randomized. Does anyone know any ways I can do this?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class FlappyPanel extends JPanel
{
   private static final int FRAME = 1000;
   private static final Color BACKGROUND = new Color(135, 206, 235);
   private static final Color GREEN = new Color(0, 255, 0);     
   private BufferedImage myImage;
   private Graphics myBuffer;
   private Polkadot pd;
   private Timer t; 
   int top = (int)(Math.random()*1000/2);
   int bot = (int)(Math.random()*1000/2);
   int x = 1000;
   int w = 20;
   int speed = 1;
   public FlappyPanel()
   {
      myImage = new BufferedImage(400, 400, 1);
      myBuffer = this.myImage.getGraphics();
      pd = new Polkadot(200, 200, 25, Color.black);
      pd.jump(400, 400);
      pd.setX(75);
      pd.setY(200);
      setFocusable(true);
      t = new Timer(10, new Listener());
      t.start(); 
      addKeyListener(new Key());
      setFocusable(true);
      
    }

   public void paintComponent(Graphics g)
   {
       g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
       g.setColor(GREEN);
       g.drawRect(x, 0, w, top);
       g.drawRect(x, 1000-bot, w, bot);
       g.fillRect(x, 0, w, top);
       g.fillRect(x, 1000-bot, w, bot);
       
       
   }
   

   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
          x -= speed;  
          myBuffer.setColor(BACKGROUND);
          myBuffer.fillRect(0, 0, FRAME, FRAME);
          if(pd.getY()+1 <= 400)
          {
            pd.setY(pd.getY()+1);
          }
          pd.draw(myBuffer);
          repaint(); 
      }
   } 
   
   private class Key extends KeyAdapter
   {
      public void keyPressed(KeyEvent e)
      {
         
         if(e.getKeyCode() == KeyEvent.VK_UP)//bumper2 goes up 30 pixels when UP arrow key is pressed
         {
            if(pd.getY()-1 >= 0){
            pd.setY(pd.getY()-50);//stops pd from getting out of frame
            }  
         } 
      }
   }  
}

Upvotes: 1

Views: 202

Answers (0)

Related Questions