refactor: restructure the project
This commit is contained in:
86
languages/python/main.py
Normal file
86
languages/python/main.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from ctypes import *
|
||||
from time import time
|
||||
|
||||
rl = CDLL("../raylib-5.5_linux_amd64/lib/libraylib.so")
|
||||
|
||||
class Image(Structure):
|
||||
_fields_ = [
|
||||
("data", c_void_p),
|
||||
("width", c_int),
|
||||
("height", c_int),
|
||||
("mipmaps", c_int),
|
||||
("format", c_int),
|
||||
]
|
||||
|
||||
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.LoadImage.restype = Image
|
||||
rl.LoadTextureFromImage.restype = Texture2D
|
||||
rl.GetFrameTime.restype = c_float
|
||||
|
||||
WINDOW_WIDTH = 800
|
||||
WINDOW_HEIGHT = 600
|
||||
LOGO_HEIGHT = 64
|
||||
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, b"Raylib in Python")
|
||||
rl.SetTargetFPS(60)
|
||||
rl.SetRandomSeed(int(time()))
|
||||
|
||||
logo_image = rl.LoadImage(b"./python_logo.png")
|
||||
rl.ImageResize(byref(logo_image), logo_image.width * LOGO_HEIGHT // logo_image.height, LOGO_HEIGHT)
|
||||
logo_texture = rl.LoadTextureFromImage(logo_image)
|
||||
rl.UnloadImage(logo_image)
|
||||
|
||||
x = rl.GetRandomValue(0, WINDOW_WIDTH - logo_texture.width)
|
||||
y = rl.GetRandomValue(0, WINDOW_HEIGHT - logo_texture.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_texture.width) >= WINDOW_WIDTH - 1:
|
||||
dx *= -1
|
||||
x += dx * delta_time
|
||||
if y < 0 or (y + logo_texture.height) >= WINDOW_HEIGHT - 1:
|
||||
dy *= -1
|
||||
y += dy * delta_time
|
||||
|
||||
rl.BeginDrawing()
|
||||
|
||||
rl.ClearBackground(BLACK)
|
||||
rl.DrawTextureV(logo_texture, Vector2(x=x, y=y), WHITE)
|
||||
|
||||
rl.EndDrawing()
|
||||
|
||||
rl.UnloadTexture(logo_texture)
|
||||
rl.CloseWindow()
|
||||
Reference in New Issue
Block a user