gamej docs

Overview

gamej is a collection of files to help with making games with Java Graphics2D. This is mainly meant for pixel art games, as the way stuff is rendered is very pixelated.

Jump to

setup structure graphics physics sound input utils

setup

Main

Use this template as a starting point and find the rest of the files here, or download this template with everything ready to go


import game.*;

public class Template extends GameJava {

    public Template() {
        super(640, 480, 60, 60);

        LoopManager.startLoops(this);
    }

    public static void main(String[] args) {
        new Template();   
    }

    @Override
    public void update() {
        // called at the set frame rate
    }	

    @Override
    public void draw() {
        // called at the set update rate
    }

    @Override
    public void absoluteDraw() {
        // called immediately after draw, all drawing is the same but without the camera affecting anything
    }
}

Your main loops will be in a class that extends GameJava

Loading Assets

Images (.png or .jpg) will automatically be loaded from the assets/images folder in your project. Audio (.wav or .mp3) will automatically be loaded from the assets/audio folder. Any subfolders in said directories will also be scanned for assets

Start the game

Starting the game requires the class that extends GameJava to be constructed with super(width, height, frame rate, update rate);, and the LoopManager to be started with LoopManager.startLoops(this);

initialization code

If you need to run code once before the game, call it in the construter after super, and before LoopManager.startLoops

There are a few things built in that can be ran here

String frameTitle - what you want the frame title to be

Boolean allowFullScreen - should full screen be enabled. Use full screen by pressing f11. Fullscreen is very buggy and it is recommended that it be disabled

Boolean resizable - should the user be allowed to resize the window

Boolean Draw.antialiasing - if antialiasing should be used

Float Draw.alphaBetweenFrames - used for motion blur, 0.01f is a lot, 1.0f is none

structure

There are three methods in the main class

Physics

update - will try to be called at the set update rate

Graphics

draw - will try to be called at the set frame rate

functions found in graphics can be called here

absoluteDraw called after draw and is the same, but the camera is ignored by and method called in here

graphics

Variables

gw - game width

gh - game height

methods

Draw.setWindowSize(w, h) - sets the size of the window

Draw.setColor(Color) - sets the color to draw with

Draw.setLineWidth(width) - sets the lines width

Draw.setFontSize(size) - sets the font size to the number entered x 8 pixels


Draw.getWidthOfText(text) - returns the width of a String if it were to be drawn


Draw.rect(x, y, w, h) or (Rect) - draws a rectangle centered on x, y

Draw.rectOutline(x, y, w, h) or (Rect) - draws a rectangle outline centered on x, y

Draw.circle(x, y, r) or (Circle) - draws a circle centered on x, y

Draw.circleOutline(x, y, r) or (Circle) - draws a circle outline centered on x, y

Draw.line(x1, y1, x2, y2) or (Point, Point) - line from x1,y1 to x2,y2

Draw.arc(x, y, size, start angle, arc angle) - draws an arc

Draw.text(text, x, y) - draws a string. x,y should be top left

Draw.image(name, x, y) or (name, x, y, angle, scale) - draws an image specified by a string which is the name of the file without the extension (assets/images/example.png would be "example"). centered on x,y at an angle in radians, scaled based on scale

Draw.imageIgnoreCutoff(name, x, y) or (name, x, y, angle, scale) - same as image, but will draw even if it is consider not visible

Camera

The camera will offset all graphics according to its values

Variables

Camera.x - x offset of graphics

Camera.y - y offset of graphics

Camera.zoom - amount graphics are scaled by. Partial numbers work, but whole numbers look better

Camera.angle - angle, in radians, the graphics are rotated by

methods

Camera.centerCameraOn(x, y) or (Point) - centers the x and y of the camera on the x and y passed in

Camera.moveCamera(x, y) - moves the camera taking into account the angle. Example: move camera "forwards" moveCamera(0, -1)

physics

Points, Circles, and Rectangles

3 different data types can be used for physics (which is just collision detection), points, circles, and rectangles. Make sure you are importing the gamej versions, not the ones built into java

new Point(x, y)

new Circle(x, y, r)

new Rectangle(x, y, w, h)

Collision Detection

detect collision between and

returns true if there is a collision, returns false if no collision

Misc.

dist(x1, y1, x2, y2) or (point, point) - returns the distance between two points

sound

Sounds.play(sound name) - plays a sound. The String should be the file name without the extension (assets/audio/example.wav would be "example")

Sounds.loop(sound name) - plays a sound and sets it to loop every time it finishes

Sounds.ajustGain(sound name, volume) - sets the gain for a sound. 1.0f is the default volume, 0.0f is no sound

Sounds.stop(sound name) - stops the sound if it is playing

input

Keys

Input.keyDown(key code) - True when a key is held down. keyCode can be an int if you know it, or use the KeyCodes enum. for example if(Input.keyDown(KeyCodes.A)) {}

Input.keyPress(key code) - same as keyDown, but only true the first frame a key is pressed

Mouse

Input.mouseDown(buttonID) - True when a mouse button is held down

Input.mousePress(buttonID) - same as mouseDown, but only true the first frame a button is pressed

mouse buttons - 0 = LMB, 1 = middle click, 2 = RMB

Input.mousePos - a Point containing the mouse position in the world

Input.rawMousePos - a Point containing the mouse position ignoring the camera

utils

Utils.pointTo(x1, y1, x2, y2) or (Point, target Point) - returns the angle (in radians) between point, and target point

Utils.turnTo(current angle, target angle, turn speed) - returns a number that is the current angle turn towards target angle, by turn speed amount

Utils.rand(minimum, maximum) - returns a random whole number between the minimum and maximum (both inclusive)

Utils.lerp(start, end, amount) - moves one value towards another

Utils.friction(value, amount) - moves a value towards 0 by the amount, once close to 0 the value will snap to 0

Utils.limit(value, max, min) - clamps a value between a min and max

Utils.mapRange(value, value low, value high, remapped low, remapped high) - changes a value in one range, to its equivalent value in another

debug

The debug menu can be shown by pressing f3

Utils.putInDebugMenu(label, value) - puts a value in the debug menu