diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..01e7454 --- /dev/null +++ b/python/README.md @@ -0,0 +1,7 @@ +# Raylib in Python + +## Quick start + +```sh +$ python ./main.py +``` \ No newline at end of file diff --git a/python/main.py b/python/main.py new file mode 100644 index 0000000..04a9307 --- /dev/null +++ b/python/main.py @@ -0,0 +1,76 @@ +from ctypes import * +from time import time + +rl = CDLL("../_raylib-5.5_linux_amd64/lib/libraylib.so") + +class Texture2D(Structure): + _fields_ = [ + ("id", c_uint), + ("width", c_int), + ("height", c_int), + ("mipmaps", c_int), + ("format", c_int), + ] + +class Vector2(Structure): + _fields_ = [ + ("x", c_float), + ("y", c_float), + ] + +class Color(Structure): + _fields_ = [ + ("r", c_ubyte), + ("g", c_ubyte), + ("b", c_ubyte), + ("a", c_ubyte), + ] + +rl.LoadTexture.restype = Texture2D +rl.GetFrameTime.restype = c_float + +WINDOW_WIDTH = 800 +WINDOW_HEIGHT = 600 +LOGO_SCALE = 0.04 +LOGO_SPEED = 300 +BLACK = Color(r=0,g=0,b=0,a=255) +WHITE = Color(r=255,g=255,b=255,a=255) + + +rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Python") +rl.SetTargetFPS(60) +rl.SetRandomSeed(int(time())) + +logo_texture: Texture2D = rl.LoadTexture(b"./python_logo.png") + +logo_width = int(logo_texture.width * LOGO_SCALE) +logo_height = int(logo_texture.height * LOGO_SCALE) + +x = rl.GetRandomValue(0, WINDOW_WIDTH - logo_width) +y = rl.GetRandomValue(0, WINDOW_HEIGHT - logo_height) + +dx = LOGO_SPEED * (-1 if rl.GetRandomValue(0, 1) else 1) +dy = LOGO_SPEED * (-1 if rl.GetRandomValue(0, 1) else 1) + +while not rl.WindowShouldClose(): + delta_time = rl.GetFrameTime() + + x += dx * delta_time + y += dy * delta_time + + if x < 0 or (x + logo_width) >= WINDOW_WIDTH - 1: + dx *= -1 + x += dx * delta_time + if y < 0 or (y + logo_height) >= WINDOW_HEIGHT - 1: + dy *= -1 + y += dy * delta_time + + rl.BeginDrawing() + + rl.ClearBackground(BLACK) + rl.DrawTextureEx(logo_texture, Vector2(x=x, y=y), c_float(0), c_float(LOGO_SCALE), WHITE) + + rl.EndDrawing() + +rl.UnloadTexture(logo_texture) +rl.CloseWindow() \ No newline at end of file diff --git a/python/python_logo.png b/python/python_logo.png new file mode 100644 index 0000000..201c834 Binary files /dev/null and b/python/python_logo.png differ