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

Short intro of us 💕✌️

We're an eletro duo based in Seoul 🇰🇷 Heavily influenced by the 90s.

Our new album #Xennials is all about the nostalgia of that era 💽 CD listenin 📟 beeper beepin 💾 floppy disks floppin days 😎

Stream now! 🎧

https://open.spotify.com/album/3YwWhnHWVy5cA8XOpbaGRA?si=8E9awqU…

This Satuday we invite to watch another #speedpainting of our artist Daniel Faiad.

How great is this scene of #Pecaminosa?

#ScreenShotSaturday | #IndieDev | #DigitalArt

Smile! Here, take some happy pills! ✨💊 #Blender #3DModelling #3DArt Buy me a Ko-fi: https://ko-fi.com/barbarafb_

Protege el conocimiento, salva la historia. Guardian of Lore es un platformer 2D en el que debes luchar para mantener viva la memoria de la mitología latinoamericana. El juego llegará a Steam el 18 de mayo: https://steam.pm/app/1211740 #ScreenshotSaturday

I was bored, so... ManutKat.

Terraria

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

Out of boredom and curiosity, I coded Sailor Jupiter's Argentine Backbreaker.

Here's a Fredbear sprite i made in Aseprite! I'm pretty proud of this!

(Btw, sorry for the double post! I'll try to post less)

Development of my game Juno. This is the rune equipment system.

Usually update over on twitter: https://twitter.com/synrec_kaisyl