Skip to content

Instantly share code, notes, and snippets.

@adamchainz
Created August 29, 2021 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamchainz/60c96aad8ea4dda38dac9ef21592b656 to your computer and use it in GitHub Desktop.
Save adamchainz/60c96aad8ea4dda38dac9ef21592b656 to your computer and use it in GitHub Desktop.
raylib python example
# Example of using raylib using only Python standard library
# whilst raylib-py looks useful, it seems quite out of date
from pathlib import Path
from ctypes import CDLL, c_int, c_char_p, c_ubyte, c_bool, Structure
raylib = CDLL(Path(__file__).parent / "lib" / "libraylib.3.7.0.dylib")
raylib.InitWindow.argtypes = [c_int, c_int, c_char_p]
raylib.InitWindow.restype = None
raylib.SetTargetFPS.argtypes = [c_int]
raylib.SetTargetFPS.restype = None
raylib.WindowShouldClose.argtypes = []
raylib.WindowShouldClose.restype = c_bool
raylib.BeginDrawing.argtypes = []
raylib.BeginDrawing.restype = None
raylib.EndDrawing.argtypes = []
raylib.EndDrawing.restype = None
class _Color(Structure):
_fields_ = [
("r", c_ubyte),
("g", c_ubyte),
("b", c_ubyte),
("a", c_ubyte),
]
class Color(_Color):
pass
raylib.ClearBackground.argtypes = [Color]
raylib.ClearBackground.restype = None
LIGHTGRAY = Color(200, 200, 200, 255)
RAYWHITE = Color(245, 245, 245, 255) # raylib logo white
raylib.DrawText.argtypes = [c_char_p, c_int, c_int, c_int, Color]
raylib.DrawText.restype = None
raylib.CloseWindow.argtypes = []
raylib.CloseWindow.restype = None
def main() -> int:
raylib.InitWindow(800, 450, b"raylib [core] example - basic window")
raylib.SetTargetFPS(60)
while not raylib.WindowShouldClose():
raylib.BeginDrawing()
raylib.ClearBackground(RAYWHITE)
raylib.DrawText(
b"Congrats! You created your first window!",
190,
200,
20,
LIGHTGRAY,
)
raylib.EndDrawing()
raylib.CloseWindow()
return 0
if __name__ == "__main__":
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment