Simple Java Game Library
This is a simple 2D Java Game Library suitable for beginning Java students. It has no dependencies and all necessary files are in one folder. As it relies on the standard Java library, it is lightweight and portable. You can use a simple text editor such as Geany without having to worry about folder structure.
Compile the Java files and execute DemoGame.java.
Download the Directory to your computer. There is a file called Template.java. Open this and save it as your game name (don't forget to update the class name as well.)
Default title and canvas size. ``SJGL game = new SJGL();```
Custom title and default canvas size.
SJGL game = new SJGL("Side Scrolling Shooter Demo");
Custom title and canvas size. ``SJGL game = new SJGL("Side Scrolling Shooter Demo", 1024, 768);```
game.setBackgroundColor(Color.BLACK);
game.setFPS(30);
Default canvas size is 1024 x 768.
game.isKeyPressed(KeyEvent.VK=??) returns true or false
- Up VK_UP
- Down VK_DOWN
- Left VK_LEFT
- Right VK_RIGHT
- Space VK_SPACE
- A - Z VK_A ... VK_Z
- 0 - 9 VK_0 ... VK_9
- Others
game.getIsMousePressed()
game.getMouseX()
game.getMouseY()
All sprites are stored in an ArrayList.
game.getSprites()
game.getBackgroundSprites()
Returns an ArrayList of Sprite objects.
game.addSprite(Sprite s)
Adds a sprite to the ArrayList of game sprites
game.addBackgroundSprite(Sprite s)
Adds a sprite to the ArrayList of game sprites
There are two types of Sprite objects. Background sprites, and regular sprites. Background sprites do not interact - they are simply decoration. Regular sprites do interact (register collisions).
Sprite spriteName = new Sprite(double x, double y);
Ex: Sprite player = new Sprite(100.0, 100.0);
Sprite spriteName = new Sprite(double x, double y, String filename);
Ex: Sprite player = new Sprite(100.0, 100.0, "player.png");
.getX() .getY() .setX() .setY() .setDX() .setDY()
.isCollision(Sprite other) .isCircleCollision(Sprite other)
Sound soundName = new Sound(String filename);
Ex: Sound beep = new Sound("beep.wav");
Note: The file should be a .wav file.
soundName.play();
Ex: beep.play();
Note: The sound will play one time all the way through. Subsequent calls to .play will be ignored until the previous play has completed. To play background music, just play the file in the main game loop.
Label labelName = new Label(String text, int x, int y);
Ex: Label scoreLabel = new Label("Score: 0", 480, 100);
game.setCameraTarget(Sprite s)
The camera will follow the target sprite which will be rendered in the center of the screen. Background sprites not affected by the camera. Labels not affected by the camera.