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

Today I was setting up the location of the first boss. His name is "Father" and he is the first of the Patagonians. His task is to guard the road to the House.🛡🗡 Bookmark pre-launch page🔖🔖🔖

https://www.kickstarter.com/projects/rdvindiegame/the-patagonian…

"Our work is never over" they said.

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

It nicely separates the snowy biome from the grassy one.

It's built with painted Ebonstone.

Terraria

An infected room from the second part of Cybel ! Are you up for some cleansing?

Cash Banooca

Hi all, Quick update with a bunch of fixes and changes.

Updated save system, optimisation and screen tear fixes, a new shoutout room and more sounds.

Don't forget to follow the kickstarter going live on the 20th! https://www.kickstarter.com/projects/9fingergames/zapling-bygone

Enjoy!

Among us_into Madness

Ladies & gentlemen.. The Blender logo factory! I had a ton of un making this animation. I'll be making a short tutorial on picking up/dropping object in Blender for animations next week! What do you think?

Someone seems to be a little angry