Example "mouse input" in Python
01/01/20 19:10 Filed in:
python | RaylibThis example uses python 3 with a Raylib (
https://www.raylib.com) wrapper (
https://pypi.org/project/raylib/) that uses CFFI API static bindings (this is the fastest approach, and keeps the code as close as possible to the original C, which I feel is the best approach when using a C library).
https://www.raylib.com/examples/web/core/loader.html?name=core_input_mouseThe original C example, edited for easier comparison:
#include "raylib.h"
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");
Vector2 ballPosition = { -100.0f, -100.0f };
Color ballColor = DARKBLUE;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
while (!WindowShouldClose()) // Detect window close button or ESC key
{
ballPosition = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) ballColor = MAROON;
else if (IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON)) ballColor = LIME;
else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) ballColor = DARKBLUE;
BeginDrawing();
ClearBackground(RAYWHITE);
DrawCircleV(ballPosition, 40, ballColor);
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
EndDrawing();
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
return 0;
}
The Python version:
from raylib.static import *
screenWidth = 800
screenHeight = 450
InitWindow(screenWidth, screenHeight, b"raylib [core] example - mouse input")
# Vector2 may be list or tuple or dict or struct-cdata...going with dictionary for ballPosition
ballPosition = {'x': -100, 'y': -100}
ballColor = DARKBLUE
SetTargetFPS(60)
while not WindowShouldClose():
ballPosition = GetMousePosition()
if IsMouseButtonPressed(MOUSE_LEFT_BUTTON):
ballColor = MAROON
elif IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON):
ballColor = LIME
elif IsMouseButtonPressed(MOUSE_RIGHT_BUTTON):
ballColor = DARKBLUE
BeginDrawing()
ClearBackground(RAYWHITE)
DrawText(b"move ball with mouse and click mouse button to change color", 10, 10, 20, LIGHTGRAY)
DrawCircleV(ballPosition, 50, ballColor)
EndDrawing()
CloseWindow()
Tags: mouse input