Skip to content

Instantly share code, notes, and snippets.

@nitori
nitori / cube.py
Created December 28, 2025 19:19
simple projection example in pure python. no gpu. uses pyglm for some vector math
import pygame
from pyglm.glm import vec2, vec3, vec4
from pyglm import glm
def to_screen(point: vec2, screen_v: vec2) -> vec2:
"""
ndc (-1, 1) to actual screen coords (0, width), (0, height)
"""
n = (point + 1) / 2 # (0, 1)
@nitori
nitori / dice.py
Last active December 26, 2025 02:24
simple dice expression parser using PEG like left-recursive parser (for left associativity)
from dataclasses import dataclass
from ast import BinOp, Add, Sub, Mult, Div, Mod, Constant, Tuple
import ast
import re
class Dice(Constant):
pass
@nitori
nitori / parser.py
Created November 8, 2025 14:30
continuous non-line delimited json stream
import subprocess, json
def parse_json_dynamic(stream):
dec = json.JSONDecoder()
buf = ''
for chunk in stream:
buf += chunk
while True:
try:
@nitori
nitori / capsule_collision_demo.py
Last active October 18, 2025 18:34
Simple visual demontration of circle and capsule collision. You can drag the circle around.
import math
from dataclasses import dataclass
from collections import deque
import pygame
from pygame import Vector2
@dataclass
class Circle:
@nitori
nitori / collision.py
Last active October 15, 2025 21:37
simple collision example
import pygame
# direction normals
LEFT = pygame.Vector2(-1, 0)
RIGHT = pygame.Vector2(1, 0)
UP = pygame.Vector2(0, -1)
DOWN = pygame.Vector2(0, 1)
ZERO = pygame.Vector2(0, 0)
@nitori
nitori / select_server.py
Created June 17, 2025 09:08
very simple select based multi-client server. no threads
import socket
from typing import Callable, Any, NamedTuple
import select
import heapq
import time
class Schedule(NamedTuple):
when: float
@nitori
nitori / render_order.py
Last active May 6, 2025 21:37
render order
import pygame
from pygame import Vector2
from dataclasses import dataclass
@dataclass
class Tile:
name: str
image: pygame.Surface
position: Vector2
@nitori
nitori / capsule.py
Created May 1, 2025 15:15
experiment with understanding collisions of capsules
from dataclasses import dataclass
from pygame import Vector2
import pygame
import math
@dataclass
class Circle:
center: Vector2
radius: float
@nitori
nitori / path.py
Last active April 21, 2025 13:26
path helper function
from pathlib import Path
from platformdirs import PlatformDirs
__all__ = ['path']
app_dirs = PlatformDirs('AppName', 'AppAuthor') # change this
here = Path(__file__).absolute().parent
SCHEMES = {
@nitori
nitori / experiment.py
Last active February 22, 2025 12:33
generate regular polygons with rounded corners. just experimenting to understand the math
from typing import Self
import math
import io
type Number = int | float
type TVec = tuple[Number, Number]
class Vector:
repr_precision = 6