Skip to content

Instantly share code, notes, and snippets.

@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):
@nitori
nitori / bsearch_game.py
Last active February 1, 2025 11:28
Very simple guessing game, to demonstrate binary search. Select a bound, and choose for yourself a number within that bound.
def which() -> str:
print(f'1) Correct!\n2) My number is smaller.\n3) My number is bigger')
while True:
res = input('Type 1, 2 or 3: ')
if not res.isdecimal() or int(res) not in (1, 2, 3):
print('Invalid input!')
continue
break
return ['correct', 'smaller', 'bigger'][int(res) - 1]
@nitori
nitori / rollback_with_exitstack.py
Last active September 3, 2024 22:12
Simple rollback on error with contextlib.ExitStack
import os
import shutil
from contextlib import ExitStack
def create_folder(stack: ExitStack, path):
try:
os.mkdir(path)
except FileExistsError:
pass
@nitori
nitori / wfc.py
Last active August 17, 2024 23:59
very simple wave function collapse thingy
from collections.abc import Iterable
import math
import heapq
import random
from collections import deque
import time
type WeightedOptions = list[tuple[int, int]]
UP = 0b1000
@nitori
nitori / sample_app.py
Last active July 10, 2024 17:35
Sample App with routes and route parameters
import re
def extract_variables(endpoint: str):
"""
Very simple route "parsing" using regex:
The route:
"/item/{item_id}"