rayjs/examples/input_mouse.js

48 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-05-11 20:10:40 +00:00
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
const screenHeight = 450;
2023-05-14 20:19:47 +00:00
initWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");
2023-05-11 20:10:40 +00:00
2023-05-14 20:19:47 +00:00
let ballPosition = new Vector2(-100.0, -100.0);
let ballColor = DARKBLUE;
2023-05-11 20:10:40 +00:00
2023-05-14 20:19:47 +00:00
setTargetFPS(60); // Set our game to run at 60 frames-per-second
2023-05-11 20:10:40 +00:00
//---------------------------------------------------------------------------------------
// Main game loop
2023-05-14 20:19:47 +00:00
while (!windowShouldClose()) // Detect window close button or ESC key
2023-05-11 20:10:40 +00:00
{
// Update
//----------------------------------------------------------------------------------
2023-05-14 20:19:47 +00:00
ballPosition = getMousePosition();
if (isMouseButtonPressed(MOUSE_BUTTON_LEFT)) ballColor = MAROON;
else if (isMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) ballColor = LIME;
else if (isMouseButtonPressed(MOUSE_BUTTON_RIGHT)) ballColor = DARKBLUE;
else if (isMouseButtonPressed(MOUSE_BUTTON_SIDE)) ballColor = PURPLE;
else if (isMouseButtonPressed(MOUSE_BUTTON_EXTRA)) ballColor = YELLOW;
else if (isMouseButtonPressed(MOUSE_BUTTON_FORWARD)) ballColor = ORANGE;
else if (isMouseButtonPressed(MOUSE_BUTTON_BACK)) ballColor = BEIGE;
2023-05-11 20:10:40 +00:00
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
2023-05-14 20:19:47 +00:00
beginDrawing();
2023-05-11 20:10:40 +00:00
2023-05-14 20:19:47 +00:00
clearBackground(RAYWHITE);
2023-05-11 20:10:40 +00:00
2023-05-14 20:19:47 +00:00
drawCircleV(ballPosition, 40, ballColor);
2023-05-11 20:10:40 +00:00
2023-05-14 20:19:47 +00:00
drawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
2023-05-11 20:10:40 +00:00
2023-05-14 20:19:47 +00:00
endDrawing();
2023-05-11 20:10:40 +00:00
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
2023-05-14 20:19:47 +00:00
closeWindow(); // Close window and OpenGL context
2023-05-11 20:10:40 +00:00
//--------------------------------------------------------------------------------------