Files
minimal_tetris_py/bg_mult.py
T
2026-07-21 20:25:17 +02:00

166 lines
6.3 KiB
Python

import math
I = 1
O = 2
T = 3
S = 4
Z = 5
J = 6
L = 7
class color:
def __init__(self, color=None, red=None, green=None, blue=None, alpha=255) -> None:
# Allow hex string input like "#ff8800" or "ff8800"
if isinstance(color, str):
hexcode = color.lstrip("#")
if len(hexcode) not in (6, 8):
raise ValueError("Hex color must be 6 or 8 characters long")
self.r = int(hexcode[0:2], 16)
self.g = int(hexcode[2:4], 16)
self.b = int(hexcode[4:6], 16)
if len(hexcode) == 8:
self.a = int(hexcode[6:8], 16)
else:
self.a = alpha
else:
# Regular RGB(A) input
self.r = red
self.g = green
self.b = blue
self.a = alpha
def get(self) -> tuple[int, int, int]:
return (self.r, self.g, self.b)
def sub(self, n) -> tuple[int, int, int]:
if self.r:
r=self.r-n
else:
r=0
if self.g:
g=self.g-n
else:
g=0
if self.b:
b=self.b-n
else:
b=0
return (r,g,b)
def multiply(self, other) -> tuple[int, int, int]:
r = other[0]
g = other[1]
b = other[2]
return (((self.r * r) >> 8), ((self.g * g) >> 8), ((self.b * b)>>8))
def shiftr(self, n):
return ((self.r >> n), (self.g >> n), (self.b >>n))
def mask(self, n):
return ((self.r & n), (self.g & n), (self.b & n))
black = color("#000000")
white = color("#ffffff")
colors = [
color("#000000"),
color("#00ffff"),
color("#fff200"),
color("#9d00ff"),
color("#00ff00"),
color("#ff0000"),
color("#0000ff"),
color("#ff8000")
]
base_board = [
[I,I,I,I,L,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[S,S,L,L,L,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[T,S,S,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,S,0,0,0,L,0,0,S,0,O,O,0,J,J,T,0,0,0,0,J],
[J,0,T,T,T,J,T,T,T,Z,Z,Z,Z,J,O,O,L,0,O,O,0,0,O,O,0,Z,Z,T,S,S,O,O,L,Z,Z,S,S,O,O,L,J,T,T,T,0,0,0,J],
[J,J,J,T,J,J,I,I,I,I,Z,Z,J,J,O,O,L,L,O,O,I,I,I,I,Z,Z,T,T,T,S,O,O,L,L,Z,Z,S,L,L,L,J,I,I,I,I,0,J,J]
]
next_piece = [
[T,T,T],
[0,T,0]
]
coord = 44
board_width = len(base_board[0])
board_height = len(base_board)
width = board_width*32
height = board_height*32
from PIL import Image
img2 = Image.open("output.jpg")
pixels2 = img2.load()
for t in range(len(base_board)-1):
img = Image.new("RGB", (width, height))
pixels = img.load()
board = [row[:] for row in base_board]
for i in range(len(next_piece)):
i_int = i + t
for j in range(len(next_piece[i])):
j_int = j + coord
if board[i_int][j_int] == 0:
board[i_int][j_int] = next_piece[i][j]
for y in range(height):
for x in range(width):
xd = x
yd = y
color_internal = colors[board[yd>>5][xd>>5]]
if board[yd>>5][xd>>5] != 0:
xs = xd&0x1F
ys = yd&0x1F
if (xs > 7 and xs < 24) and (ys > 7 and ys < 24): # middle
pixels[x, y] = color_internal.get()
elif (xs > 5 and xs < 26) and (ys > 5 and ys < 26): # mid boarder
pixels[x, y] = color_internal.mask(0xFFFFFFB0)
elif xs+ys<31: # top left edge
if xs>ys:
pixels[x, y] = color_internal.get()
else:
pixels[x, y] = color_internal.mask(0xFFFFFFEF)
else:
if xs>ys:
pixels[x, y] = color_internal.mask(0xFFFFFFD7)
else:
pixels[x, y] = color_internal.mask(0xFFFFFFC0)
else:
# if (x+y)&1:
# pixels[x, y] = (0, 0, 255)
# else:
pixels[x, y] = pixels2[math.floor(x/1.5), math.floor(y/1.5)]#((x+y)>>2, (((1280+720)>>2)-((x+y)>>2)), 255)
#
img.save(f"output/folie_{t}_background.png")
if t == 1:
img.show()