What's the best way I can make random obstacles come my way so my square can dodge them? Kinda like flappy bird but small box like obstacles.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Jumpy
extends Applet
implements KeyListener{
private Rectangle rect;
public void init()
{
this.addKeyListener(this);
rect = new Rectangle(50, 400, 50, 50);
}
public void paint(Graphics g)
{
setSize(500, 500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP)
{
rect.setLocation(rect.x, rect.y - 13);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
rect.setLocation(rect.x, rect.y + 13);
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}}
I don't want you to write my code. I'm just asking what method/way/how would be best to make moving obstacles come my way? I'm new to java so something simple and smooth...Thanks