Paulo Laureano Because coding is fun!

Example "mouse input" in Python

This example uses python 3 with a Raylib ( wrapper ( 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).

The 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;



DrawCircleV(ballPosition, 40, ballColor);

DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);


// 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


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

DrawText(b"move ball with mouse and click mouse button to change color", 10, 10, 20, LIGHTGRAY)
DrawCircleV(ballPosition, 50, ballColor)