Randomise Asteroids from sky

Good evening programmers!
I want to program a game where asteroids fall from the sky. Does anyone know how I can happen this to come from above? The asteroids are just normal circles.
Thanks in advance!
Kind regards

Make the circles move, e.g. increase the y-value of the coordinates over time. If you start with y=0 and then increase y it should look as if they are falling down.


When you have a problem like this, the first thing to do is to identify the properties of the object that you are going to model. In this case, it’s an Asteroid :

  • It can fall from the sky (in fact the top of the screen) so it needs a location in 2D space (since a screen is in two dimensions)

  • Every asteroid is not the same so the don’t fall at the same speed, you need to take that into account (their velocity)

  • It has a certain shape, here it’s a circle with a size, its diameter

With that you have a description of your Asteroid but how do you translate it to code? Naively you could do it like this :

int xAsteroid = 50, yAsteroid = 100;
int velocity = 5;
int diameter = 10;

// Draw your asteroid
// Move it...

This can work for a single asteroid (even it’s not recommended) but you need to have multiple rocks. So you are going to duplicate all the above variables? This is not possible if you have 100 asteroids…

So what is the solution? Luckily for us, Java is an object oriented programming language. This means that we can create objects in code. Objects are defined by a class, it’s a data representation of the objects you want to model. Enough theory, this is how to apply it to the Asteroid :

class Asteroid {
  float diameter;
  PVector location, velocity;

This simply says that our Asteroid class is storing a diameter, location and velocity (defined as PVector since they are vectors…). This is exactly what we want. Think of it as a container for your data.

Now how do I create an Asteroid? I need a way to construct an asteroid by specifying it’s properties (like initial location). This is the role of the constructor :

class Asteroid {
  float diameter;
  PVector location, velocity;

  // Constructor of the class
  Asteroid(float diameter, PVector location, PVector velocity) {
    this.diameter = diameter;
    this.location = location;
    this.velocity = velocity;

Here we just pass the values for the variables we defined earlier and then just say the current object’s diameter equals the diameter that we pass to the constructor. This is more clear when we use it :

// Create an asteroid with a size of 50, 
// at location 0, 0 and speed to fall from the top
Asteroid asteroid = new Asteroid(50, new PVector(0, 0), new PVector(0, 1));

(notice that here we also used new to create the vectors since they are objects)

Now we have a class representing our asteroid but we can do a lot more! For example updating it’s location, display it…

If you already know this, you know what to do next! Otherwise I encourage you to check this tutorial from the Processing website :

After you should see why object oriented programming is the way to go :slight_smile: