Java infinite star field

So I’ve been faffing about with Java lately (well, I’m waiting for a new job, I got no cash, so I gotta do something!!) Anyway I’m working on a simply amazing space battle game and I wanted to create an infinite universe.. so here is version 1! It’s a pretty basic starfield with a fake feeling of depth to it.

Star field

Just use arrow keys to change directions. (You may need to click on the applet first if nothing is happening)

Creating the starfield is pretty straight forward, and sits in the StarField class:

[highlighter]

So that will build a starfield for us. Now for a simple test application:

And that’s it! When using the constructor – StarField(no_stars, getWidth(), getHeight(), depth) I’m using the applet height and width

as the starfield size. Obviously you can make that much bigger so that the stars don’t loop around so quickly.

To give the impression of depth each star is given a z value. Whenever the screen is moved, all you have to do is if divide the

depth by the screen offset (the width and height divided by 2 so we are centered) and that will alter the speed at which the star moves. Easy eh!

I might get round to a version 2 with images for stars and nebula and so on but I’ll see how it goes.. Stay tuned!

And finally.. I’m a bit new at this Java lark though so if anyone can point out ways to improve the above let me know!

One thought on “Java infinite star field

  1. Man… That’s a nice System, I was working on something similar in a Halloween program but my stars are all static and they have no depth, nice work 😉

Leave a Reply

Your email address will not be published.