12 years ago

Spawning objects randomly using Java Slick 2d and LWJGL


So I got bored and decided to help out some one who was trying to spawn objects randomly.

I had not recently used java and never used slick2d or LWJGL or so to get up to speed I downloaded eclipse, slick2d, LWJGL then followed the directions to setup LWGL.

Essentially I just keep track of the GameObjects (my class that stores position and velocity) in a vector and then update their position and draw them based on their position.

When I create the objects I used the Random class from java.util.Random. You need to create a new object to use the method [nextFloat()](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextFloat()) or [nextInt()](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt()). IE:

  • Random r = new Random();

  • float velX=r.nextFloat()-.5f;//This line generates a value from 0 to 1 and then subtracts .5

  • int x = r.nextInt(800);//generates a value from 0 to 800 NOT including 800

So just use the random class to generate your desired position and then draw the images with draw(x,y).

To use vectors, which are just like fancy arrays since they automatically resize, you need to know 4 basic things

Declaring a vector that holds a type (in my case GameObjects):

  • private Vector<GameObject> enemies;

Instantiating the vector:

  • enemies=new Vector<GameObject>(50000);

Adding & Removing elements to the vector with add and remove

  • enemies.add(e); //where e is a game object

  • enemies.remove(i);// where i is the index number of the element you want to remove

Accessing an element with elementAt(elementNumber) - usually in a loop:

  • enemies.elementAt(i);


What’s the time since the last frame?

For some reason I ventured a little into timers too, as to keep the game consistent accross framerates this guy rages hard about not using delta time correctly and wrote a whole article about it. I found the LWJGL article on timers to be useful. Hopefully I implimented that correctly.

Press g to spawn more candy drops.

Art from luminae

5d0c7310947ac.jpg

Downloads:

Here’s the entire project and a runnable jar for windows. To get the project to work you’ll probably need to reset the paths to slick2d and LWJGL (use the steps in the tutorial to setup LWJGL linked above).

Heres the code for my main class (FrozenCandy) based on the slick tutorial barebones:. And the GameObject class.



0 comments

Loading...

Next up

Chiaki Nanami!

Here's a peaceful little forest scene for this #screenshotsaturday, from the beginning of the game.

Another house i made long time ago.

Back in my art school days I used to ride the 710 COPSA line from Parque Del Plata to Montevideo almost everyday. This is the Marcopolo Viaggio G4 Mercedes Benz model from the late 80s, one of the older bus models that was running on the line.

Hi everyone! I started to create some environment props for my new video game, here is a cozy house in the forest. I'm planning to switch from Unity to Unreal... Let's see what new challenges I have to face. Made in Blender and Substance Painter.

A house I've built a while ago. :)

It nicely separates the snowy biome from the grassy one.

It's built with painted Ebonstone.

Regular exercise is key for healthy wings.

Try it out!

https://gamejolt.com/games/AeonNightmares/605184

My D&D art for my family so far (WIP if you couldn't tell)

Explosive domino effect

A sneak peek at the new OST, can you guess what this song will be used for? 🤔