Python wrapper for SFML via C# (SFML.NET) and pythonnet
Project description
pysfml-net
A Python wrapper around SFML via C# (SFML.NET) and pythonnet.
Write 2D games in Python — with the performance of native C++.
Python → pysfml → pysfmllib.dll (C# / SFML.NET) → csfml → SFML (C++)
Requirements
| Dependency | Version |
|---|---|
| Python | 3.10+ |
| pythonnet | 3.x |
| .NET | 6+ (coreclr) |
| SFML | 2.6.x / 3.x |
| SFML.NET | matching SFML version |
Installation
pip install pysfml-net
pysfml-net installs only the Python wrapper.
You must place the following DLLs in the same folder as your script:
csfml-system.dll
csfml-window.dll
csfml-graphics.dll
csfml-audio.dll
sfml-system-3.dll (or sfml-system-2.dll depending on your SFML version)
sfml-window-3.dll
sfml-graphics-3.dll
sfml-audio-3.dll
Download SFML from sfml-dev.org — make sure the bitness matches your Python (x64).
Quick Start
from pysfml import Window, Sprite, Label, Sound
win = Window(800, 600, "My Game")
player = Sprite(48, 48)
player.set_fill_color(100, 180, 255)
player.set_position(100, 270)
label = Label("roboto.ttf", 24)
label.set_text("Score: 0")
label.set_position(16, 12)
while win.is_open():
win.dispatch_events()
dt = win.get_delta_time()
if win.is_key_pressed("Escape"):
win.close()
win.clear()
player.draw(win)
label.draw(win)
win.display()
API
Window
win = Window(width: int, height: int, title: str)
| Method | Returns | Description |
|---|---|---|
is_open() |
bool |
False after window is closed |
dispatch_events() |
— | Process OS events — call every frame |
get_delta_time() |
float |
Seconds since last frame |
clear() |
— | Clear screen |
display() |
— | Present frame |
close() |
— | Close the window |
draw(sprite) |
— | Draw a Sprite |
is_key_pressed(key) |
bool |
Check keyboard key by name |
is_button_pressed(btn) |
bool |
Check mouse button by name |
get_mouse_position() |
Vector2i |
Current mouse position |
Key names: "Left", "Right", "Up", "Down", "Space", "Escape", "A"–"Z", "Num0"–"Num9" — full list at SFML Keyboard.Key
Mouse button names: "Left", "Right", "Middle"
Sprite
sp = Sprite(width: float, height: float)
| Method | Description |
|---|---|
set_position(x, y) |
Set position |
set_size(w, h) |
Resize the sprite |
set_fill_color(r, g, b, a=255) |
Set fill color (0–255 each) |
load_texture(path) |
Load texture from file — path is resolved to absolute automatically |
set_texture_sprite(x, y, w, h) |
Select a frame from a spritesheet |
draw(win) |
Draw to window |
Note: after
load_texture()setFillColorto white in your C#Spriteclass,
otherwise the texture will be tinted by the fill color.
Label
lbl = Label(font_path: str, size: int)
| Method | Description |
|---|---|
set_text(value) |
Set displayed text (auto-converts to str) |
set_position(x, y) |
Set position |
draw(win) |
Draw to window |
Sound
snd = Sound(path: str)
| Method | Description |
|---|---|
play() |
Play the sound |
stop() |
Stop playback |
pause() |
Pause playback |
set_volume(v) |
Volume from 0.0 to 100.0 |
Movement with delta time
player = Sprite(48, 48)
player.set_fill_color(255, 100, 100)
player.set_position(400, 300)
vx, vy = 0.0, 0.0
x, y = 400.0, 300.0
SPEED = 250.0
while win.is_open():
win.dispatch_events()
dt = win.get_delta_time()
if win.is_key_pressed("Escape"): win.close()
vx = 0.0
vy = 0.0
if win.is_key_pressed("Left"): vx = -SPEED
if win.is_key_pressed("Right"): vx = SPEED
if win.is_key_pressed("Up"): vy = -SPEED
if win.is_key_pressed("Down"): vy = SPEED
x += vx * dt
y += vy * dt
player.set_position(x, y)
win.clear()
player.draw(win)
win.display()
Project layout
your_game/
├── main.py
├── pysfmllib.dll ← C# bridge
├── SFML.*.dll ← managed SFML.NET
├── csfml-*.dll ← native C bindings
├── sfml-*-3.dll ← native SFML
└── assets/
├── textures/
├── fonts/
└── sounds/
Troubleshooting
Failed to load SFML assemblies
All DLLs must be in the same folder as your script. Run your script from that folder:
cd your_game
python main.py
Failed to activate OpenGL context
Install Visual C++ Redistributable x64.
Texture loads but sprite stays white
Set FillColor = Color.White inside LoadTexture() in your C# Sprite class.
Wrong bitness error
Check your Python bitness:
import struct; print(struct.calcsize("P") * 8) # 32 or 64
All DLLs must match (all x64 or all x32).
License
MIT — © 2025 Neyzi
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysfml_net-1.1.1.tar.gz.
File metadata
- Download URL: pysfml_net-1.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d73c0a2a4e981958952520c3666786e2f76ffd6afadc984ab14bae117c63168
|
|
| MD5 |
05047b63a084af49d2dd2482ddae287d
|
|
| BLAKE2b-256 |
09af21c771b7d1eb503ba574599365e8005accd9ef5af0950c41e580326b0c33
|
File details
Details for the file pysfml_net-1.1.1-py3-none-any.whl.
File metadata
- Download URL: pysfml_net-1.1.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96e4f1cb1da3515cefedb24627f1ad104631f595b4e01a446c5c11d0e70cf9a9
|
|
| MD5 |
0fa3aed8c84bc10b8f95d5d21842b24a
|
|
| BLAKE2b-256 |
4b7b7be64db3e546a1fbf5ee5ab47a20eef9b8880f64766b7b87623ba027afbf
|