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.

Shadow The Hedgehog X pixel art

Just a Pico sprite

Today I tackled drawing different variations of facial expressions for one of the main characters in Eden. What do you think of it? :) Eden: https://gamejolt.com/games/Eden/592698

If you have more of an acquired taste, the restaurants in Niravasi have you covered! Maybe skip the salad bar, though.

"Day to Day" (also check spud out here he's a great guy and writer: https://soundcloud.com/spudward)

Very ambitious construction going on in Planet Zoo on Plays today. We're tryin'. LIVE: www.twitch.tv/dreamhackplays

If you’ve played the Vault demo and enjoyed it, please consider ‘liking’ the game page here on Gamejolt and/or leave a comment!

Also please consider tossing Vault on your Wishlist, I really appreciate your support!

https://store.steampowered.com/app/1251800

Explosive domino effect