rayjs/examples/core/core_input_keys.js

42 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-05-10 21:26:53 +00:00
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
2023-05-14 20:19:47 +00:00
initWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input");
2023-05-10 21:26:53 +00:00
2023-05-14 20:19:47 +00:00
const ballPosition = new Vector2(screenWidth/2, screenHeight/2);
2023-05-10 21:26:53 +00:00
2023-05-14 20:19:47 +00:00
setTargetFPS(60); // Set our game to run at 60 frames-per-second
2023-05-10 21:26:53 +00:00
//--------------------------------------------------------------------------------------
// Main game loop
2023-05-14 20:19:47 +00:00
while (!windowShouldClose()) // Detect window close button or ESC key
2023-05-10 21:26:53 +00:00
{
// Update
//----------------------------------------------------------------------------------
2023-05-14 20:19:47 +00:00
if (isKeyDown(KEY_RIGHT)) ballPosition.x += 2;
if (isKeyDown(KEY_LEFT)) ballPosition.x -= 2;
if (isKeyDown(KEY_UP)) ballPosition.y -= 2;
if (isKeyDown(KEY_DOWN)) ballPosition.y += 2;
2023-05-10 21:26:53 +00:00
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
2023-05-14 20:19:47 +00:00
beginDrawing();
2023-05-10 21:26:53 +00:00
2023-05-14 20:19:47 +00:00
clearBackground(RAYWHITE);
2023-05-10 21:26:53 +00:00
2023-05-14 20:19:47 +00:00
drawText("move the ball with arrow keys", 10, 10, 20, DARKGRAY);
2023-05-10 21:26:53 +00:00
2023-05-14 20:19:47 +00:00
drawCircleV(ballPosition, 50, MAROON);
2023-05-10 21:26:53 +00:00
2023-05-14 20:19:47 +00:00
endDrawing();
2023-05-10 21:26:53 +00:00
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
2023-05-14 20:19:47 +00:00
closeWindow(); // Close window and OpenGL context
2023-05-10 21:26:53 +00:00
//--------------------------------------------------------------------------------------