Skip to content

Instantly share code, notes, and snippets.

import math
import random
from typing import NamedTuple, Literal, Callable
import pygame
from PIL import Image
import numpy as np
type Pos = tuple[int, int]
type Color = tuple[int, int, int] | str
@nitori
nitori / pygame_plus_async.py
Last active May 24, 2024 16:42
Possible way to combine pygame and asyncio
import asyncio
import threading
import pygame
USER_EV1 = pygame.event.custom_type()
def pygame_loop(async_stopper):
pygame.init()
@nitori
nitori / gfxc.py
Last active April 21, 2024 11:16
from dataclasses import dataclass, field
import struct
from typing import BinaryIO
import zlib
import typing
"""
TFG1 Data files desired to be extracted (data directory)
https://gitea.xanax.lol/root/tfg-client
@nitori
nitori / test.py
Created April 6, 2024 15:27
sample of how to quantize an image with a predefined palette of colors
import random
from PIL import Image, ImageDraw
COLORS = [
(0x5e, 0x1f, 0x1d), (0xaa, 0x38, 0x37), (0x41, 0x2c, 0x33), (0xb3, 0x5a, 0x48), (0xfd, 0x63, 0x65),
(0xa0, 0xa5, 0xa9), (0x6c, 0x6e, 0x68), (0xb2, 0x00, 0x2a), (0xc0, 0x12, 0x26), (0xe1, 0x53, 0x85),
(0x93, 0x37, 0x4e), (0xe6, 0xa0, 0xc5), (0xe3, 0xb4, 0xa4), (0x00, 0x00, 0x00), (0x24, 0x27, 0x28),
(0x3f, 0x41, 0x41), (0x67, 0x1b, 0x56), (0x90, 0x00, 0x45), (0x34, 0x21, 0x50), (0x83, 0x58, 0x8b),
(0xa1, 0x89, 0xae), (0xd1, 0xb4, 0xc8), (0x87, 0x8e, 0x90), (0xa2, 0xa8, 0xab), (0xff, 0xff, 0xff),
(0x0a, 0x1a, 0x19), (0x23, 0x28, 0x1c), (0x34, 0x3b, 0x22), (0x61, 0x5c, 0x3d), (0x4f, 0x73, 0x6e),
@nitori
nitori / choose.py
Created March 29, 2024 08:52
Simple linux cli style user choice function
def choose(text, options="Yn"):
"""
text: prompt to the user.
options: set of characters. one upper case may be
used to indicate a default value.
Examples:
answer = choose("Continue?")
# outputs: "Continue? (Y/n): "
@nitori
nitori / simple_mc_status_ping.py
Created March 25, 2024 10:28
Get status of some minecraft server.
import asyncio
import struct
import io
import json
def pack_varint(val):
total = b''
if val < 0:
val = (1 << 32) + val
@nitori
nitori / pygame_simple_text_dialog.py
Last active March 23, 2024 20:10
Simple Textbox/dialog example for pygame
import textwrap
from collections import deque
import pygame
class TextBox:
NUM_LINES = 5
TEXT_FONT = 'VT323-Regular.ttf'
import numpy as np
def main():
coord_and_names = np.genfromtxt('coordinates.txt',
dtype=[('lat', 'float'), ('lng', 'float'), ('name', 'U20')],
delimiter=',', skip_header=True)
# make lat and lng into 2D array
coords = np.column_stack((coord_and_names['lat'], coord_and_names['lng']))
import pygame
from PIL import Image, ImageFilter
import numpy as np
def create_highlight_surf(surf):
# surf should be an image in RGBA format, where anything not transparent gets highlighted.
data = pygame.image.tostring(surf, 'RGBA')
image = Image.frombytes('RGBA', surf.get_size(), data)
@nitori
nitori / foo.py
Last active February 2, 2024 09:14
from collections import Counter
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
d = {}
for s in strs:
c = frozenset(Counter(s).items())
d.setdefault(c, []).append(s)