Skip to content

Keyboard Input Example

Calvin Day edited this page Apr 18, 2022 · 2 revisions

Taking a look at basic user input

The standard Raylib Examples are provided in the Raylib-J Examples github repo. For this I'll be breaking down the example InputKeys.java and explaining as we go.

If you have not read through the breakdown of the Basic Window Example I'd recommend doing that first.


InputKeys.java

The following is the code I'll be referencing:

package core;

import com.creedvi.raylib.java.rlj.Raylib;
import com.creedvi.raylib.java.rlj.core.Color;
import com.creedvi.raylib.java.rlj.raymath.Vector2;

import static com.creedvi.raylib.java.rlj.core.input.Keyboard.*;

public class InputKeys{

    public static void main(String[] args){

        // Initialization
        //--------------------------------------------------------------------------------------
        int screenWidth = 800;
        int screenHeight = 450;
        Raylib rlj = new Raylib();

        rlj.core.InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input");

        Vector2 ballPosition = new Vector2((float) screenWidth / 2, (float) screenHeight / 2);

        rlj.core.SetTargetFPS(60);
        //--------------------------------------------------------------------------------------

        // Main game loop
        while (!rlj.core.WindowShouldClose()){    // Detect window close button or ESC key

            // Update
            //----------------------------------------------------------------------------------
            if (rlj.core.IsKeyDown(KEY_RIGHT)){
                ballPosition.x += 2.0f;
            }
            if (rlj.core.IsKeyDown(KEY_LEFT)){
                ballPosition.x -= 2.0f;
            }
            if (rlj.core.IsKeyDown(KEY_UP)){
                ballPosition.y -= 2.0f;
            }
            if (rlj.core.IsKeyDown(KEY_DOWN)){
                ballPosition.y += 2.0f;
            }
            //----------------------------------------------------------------------------------

            // Draw
            //----------------------------------------------------------------------------------
            rlj.core.BeginDrawing();

            rlj.core.ClearBackground(Color.RAYWHITE);

            rlj.text.DrawText("move the ball with arrow keys", 10, 10, 20, Color.DARKGRAY);

            rlj.shapes.DrawCircleV(ballPosition, 50, Color.MAROON);

            rlj.core.EndDrawing();
            //----------------------------------------------------------------------------------
        }
    }

}

The goal of this program is to draw a circle to the screen the player can move about by pressing the arrow keys on the keyboard. We'll begin by looking at the code in the "Initialization" section.

int screenWidth = 800;
int screenHeight = 450;
Raylib rlj = new Raylib();

rlj.core.InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input");

Vector2 ballPosition = new Vector2((float) screenWidth / 2, (float) screenHeight / 2);

rlj.core.SetTargetFPS(60);

The first two lines of this block are used to create Integer variables that we will use to store the width and height of the screen. Next we create a new instance of a Raylib object.

Next, we Initialize our window with our stored width, height, and a title.

Then we create a Vector2 named "ballPosition" and initialize it to the center of the screen.

Lastly we set our program to run at 60 Frames Per Second.

NOTE: As mentioned in the Basic Window Example, you can also use the Raylib(int screenWidth, int screenHeight, String windowTitle) constructor to create a Raylib instance and initialize the window all at once.

Next we begin our game loop.

        while(!rlj.core.WindowShouldClose()){

As mentioned in the Basic Window Example breakdown the game loop is split into halves. First we will take a look at the update portion:

if (rlj.core.IsKeyDown(KEY_RIGHT)){
    ballPosition.x += 2.0f;
}
if (rlj.core.IsKeyDown(KEY_LEFT)){
    ballPosition.x -= 2.0f;
}
if (rlj.core.IsKeyDown(KEY_UP)){
    ballPosition.y -= 2.0f;
}
if (rlj.core.IsKeyDown(KEY_DOWN)){
    ballPosition.y += 2.0f;
}

In this section of code we check if the user is pressing the arrow keys on the keyboard. If they are pressing right or left we decrement or increment the x value of our ballPosition vector by two respectively.

We do the same for up and down, but affecting the y value instead.

NOTE: We decrement the y position when the user presses up because the top left corner of the screen is regarded as (0, 0)

Next we begin to render everything:

rlj.core.BeginDrawing();

rlj.core.ClearBackground(Color.RAYWHITE);

rlj.text.DrawText("move the ball with arrow keys", 10, 10, 20, Color.DARKGRAY);

rlj.shapes.DrawCircleV(ballPosition, 50, Color.MAROON);

rlj.core.EndDrawing();           

First we begin drawing by calling the BeginDrawing() method then we clear the background by calling ClearBackground(Color color). Next we draw text to give the player instructions on how to play our game with the DrawText(String text, int x, int y, int size, Color color) method.

Now we draw the circle. We use the method DrawCircleV(Vector2 position, int size, Color color) from the Shapes module so we can pass a Vector2 instead of two separate float values.

Finally we finish rendering by calling EndDrawing().

Now the player can steer around their circle until their heart is content, or they hit the Escape key by mistake.

That's all there is to checking input from the user. For extra credit try changing the shape or color of the object based on user input!

Clone this wiki locally