HI all, this app is a simple game where the objective is to avoid incoming balls (adversaries) however at present I can only figure out how to get one ball on screen at a time. What I'd really like is, when a player hits 100 points, a second ball is spawned (thus making the game harder) I would like the same to occur at 200 points (3 balls), 400 pts (4 balls) and so forth. I am sure there is a simple way of doing this. what I have tried is creatin new instances of Ball, but to no avail.any help would be greatly appreciated.
cheers
/* A game that is Saucer-like
* Phillip Wells
*/
//to do - make more balls when 100 points met
import java.awt.*;
import java.awt.Toolkit.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;
public class Saucer extends JFrame {
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
Image img;
private PaintSurface canvas;
public void init() {
this.setSize(WIDTH, HEIGHT);
this.setTitle("Saucer");
canvas = new PaintSurface();
img = Toolkit.getDefaultToolkit().getImage("space.jpg");
this.getContentPane().add(canvas, BorderLayout.CENTER);
ScheduledThreadPoolExecutor executor =
new ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(new AnimationThread(this),
0L, 20L, TimeUnit.MILLISECONDS);
setVisible(true);
}
class AnimationThread implements Runnable {
JFrame c;
public AnimationThread(JFrame c) {
this.c = c;
}
public void run() {
c.repaint();
}
}
class PaintSurface extends JComponent {
int saucer_x = 0; // start position of saucer
int saucer_y = 180;
int score = 0;
Ball Ball;
Color[] color = {Color.RED, Color.ORANGE,
Color.MAGENTA, Color.ORANGE,
Color.CYAN, Color.BLUE};
int colorIndex;
public PaintSurface() {
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
saucer_x = e.getX() -30;
saucer_y = e.getY() -0;
}
} );
Ball = new Ball(20);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Shape saucer = new Ellipse2D.Float(
saucer_x, saucer_y, 60, 10); // saucer dimensions
g2.drawImage(img, 0, 0, this);
g2.setColor(color[colorIndex % 6]);
if (Ball.intersects(saucer_x, saucer_y, 60, 10)
&& Ball.y_speed > 0) {
Ball = new Ball(20);
score -= 100;
colorIndex = 0;
}
else if (Ball.getY() + Ball.getHeight() >= Saucer.HEIGHT)
{
Ball = new Ball(20);
score += 10;
colorIndex = 0;
}
Ball.move();
g2.fill(Ball);
g2.setColor(Color.GREEN);
g2.fill(saucer);
g2.drawString("Score: " + score, 300, 20);
if (score == 20)
{
//code needed to create a second ball on screen
}
}
}
class Ball extends Ellipse2D.Float {
public int x_speed, y_speed;
private int d;
private int width = Saucer.WIDTH;
private int height = Saucer.HEIGHT;
public Ball(int diameter) {
super((int)(Math.random() * (Saucer.WIDTH - 20) + 1),
0, diameter, diameter);
this.d = diameter;
this.x_speed = (int)(Math.random() * 5 + 5);
this.y_speed = (int)(Math.random() * 5 + 5);
}
public void move() {
if (super.x < 0 || super.x > width - d)
x_speed = -x_speed;
if (super.y < 0 || super.y > height - d)
y_speed = -y_speed;
super.x += x_speed;
super.y += y_speed;
}
}
}