Skip to content

Instantly share code, notes, and snippets.

@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
@nitori
nitori / pygame-mask-mouse.py
Last active February 15, 2025 13:06
Check if mouse position collides with the non-transparent shape of an image
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
class Triangle(pygame.sprite.Sprite):
@nitori
nitori / constant_reverses.py
Created February 10, 2025 21:15
Simple double linked list with constant time list reversal.
class List:
_head: 'Node|None'
_tail: 'Node|None'
def __init__(self):
self.reversed = False
self._head = None
self._tail = None
def append(self, value):