Sorry, I did not know how to word the title well. But I am making a Java game where you have a paddle at the bottom of your screen and you can move it left and right in order to avoid falling asteroids. I have the paddle working, but I do not know how to make a falling asteroid. The asteroid will just be a rectangle for now. I also would like to be able to adjust the speed and how many asteroids fall. Here is my code:
public class Main extends Applet implements KeyListener, MouseListener {
private Rectangle rect;
private Rectangle asteroidrect;
private ArrayList<Integer> keysDown;
private Image dbImage;
private Graphics dbg;
Random randomGenerator = new Random();
int speed = 8;
int level = 1; // change to 0 once start menu works
int xpos, ypos;
int blockx = 0;
int blocky = 0;
int width = 1024;
int height = 768;
String version = "0.0.1";
public static final int START_X_POS = 160;
public static final int START_Y_POS = 160;
public static final int START_WIDTH = 256;
public static final int START_HEIGHT = 64;
boolean startClicked;
boolean asteroid = false;
public void init() {
setSize(width, height);
addKeyListener(this);
addMouseListener(this);
setBackground(Color.black);
Frame c = (Frame)this.getParent().getParent();
c.setTitle("Asteroid Attack - Version " + version);
keysDown = new ArrayList<Integer>();
rect = new Rectangle(460, 700, 64, 12);
asteroidrect = new Rectangle(0, 0, 48, 48);
addAsteroid();
}
public void update(Graphics g) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
if (dbImage == null) {}
dbg.setColor(getBackground ());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
if (level != 0) {
g2.setPaint(Color.gray);
g2.fill(rect);
//if (asteroid == true) {
g2.setPaint(Color.red);
g2.fill(asteroidrect);
//}
}
}
@Override
public void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode()))
keysDown.add(new Integer(e.getKeyCode()));
moveChar();
}
@Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveChar() {
if (level != 0) {
int x = rect.x;
int y = rect.y;
if (keysDown.contains(KeyEvent.VK_LEFT)) {
x -= speed;
}
if (keysDown.contains(KeyEvent.VK_RIGHT)) {
x += speed;
}
rect.setLocation(x, y);
repaint();
}
}
public void addAsteroid() {
asteroid = true;
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void mouseClicked(MouseEvent me) {
if (level == 0) {
xpos = me.getX();
ypos = me.getY();
if (xpos >= START_X_POS && ypos >= START_Y_POS && xpos <= START_X_POS + START_WIDTH && ypos <= START_X_POS + START_HEIGHT ) {
level = 1;
}
}
}
@Override
public void mouseEntered(MouseEvent me) {}
@Override
public void mouseExited(MouseEvent me) {}
@Override
public void mouseReleased(MouseEvent me) {}
@Override
public void mousePressed(MouseEvent me) {}
}
As you can see, I have already added an asteroid object, and the rectangle.