diff --git a/gpu.py b/gpu.py index ee64d50..d150cca 100644 --- a/gpu.py +++ b/gpu.py @@ -363,6 +363,7 @@ colors2 = [ color("#fe4b0f"), ] + text_bg = black next_board = make_next_board() @@ -511,7 +512,11 @@ for y in range(height): # pixels[x, y] = colors2[next_board[yd>>5][xd>>5]].multiply(sprite_pixels[yd&0b0000000000011111, xd&0b0000000000011111]) ####################### BG else: - pixels[x, y] = ((x+y)>>3,0,0) + if (x+y)&1: + pixels[x, y] = (0, 0, 255) + else: + pixels[x, y] = ((x+y)>>3, (((1280+720)>>3)-((x+y)>>3)), 0) + # offset = 0 # numbers = [a_s, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9] diff --git a/gpu2.py b/gpu2.py new file mode 100644 index 0000000..b2b0409 --- /dev/null +++ b/gpu2.py @@ -0,0 +1,500 @@ +import random +from PIL import Image + +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)) + +class coord: + def __init__(self, x, y, width, height) -> None: + self.x = x + self.y = y + self.width = width + self.height = height + +black = color("#000000") +white = color("#ffffff") + +red = color("#ff0000") +cyan = color("#00ffff") + +blue = color("#0000ff") +yellow = color("#fff200") + +green = color("#00ff00") +magenta = color("#ff00ff") + +orange = color("#ff8000") +sky_blue = color("#0080ff") + +purple = color("#9d00ff") +lime = color("#5fdf5f") + +a_s = [ + [0,1,1,1,1,0], + [1,1,0,0,1,1], + [1,1,0,0,0,0], + [0,1,1,1,0,0], + [0,0,1,1,1,0], + [0,0,0,0,1,1], + [1,1,0,0,1,1], + [0,1,1,1,1,0] +] + +a_c = [ + [0,1,1,1,1,0], + [1,1,1,0,1,1], + [1,1,0,0,0,0], + [1,1,0,0,0,0], + [1,1,0,0,0,0], + [1,1,0,0,0,0], + [1,1,1,0,1,1], + [0,1,1,1,1,0] +] + +a_o = [ + [0,1,1,1,1,0], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [0,1,1,1,1,0] +] + +a_r = [ + [1,1,1,1,1,0], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [1,1,0,0,1,1], + [1,1,1,1,1,0], + [1,1,0,1,1,1], + [1,1,0,0,1,1], + [1,1,0,0,1,1] +] + +a_e = [ + [1,1,1,1,1,1], + [1,1,0,0,0,0], + [1,1,0,0,0,0], + [1,1,1,1,1,1], + [1,1,0,0,0,0], + [1,1,0,0,0,0], + [1,1,0,0,0,0], + [1,1,1,1,1,1] +] + +colors = [ + black, + cyan, + blue, + orange, + yellow, + green, + purple, + red +] + +colors2 = [ + black, + cyan, + blue, + orange, + yellow, + green, + purple, + red +] + +def make_picture(board0, board1, next_board0, next_board1, output, score0=0, score1=0):# -> Any: + + text_bg = black + + nb1 = coord(88, 96, 128, 128) + nb2 = coord(1064, 96, 128, 128) + + b1 = coord(304, 40, 320, 640) + b2 = coord(656, 40, 320, 640) + + s1 = coord(84, 454, 136, 32) + sn1 = coord(74, 490, 156, 20) + s2 = coord(1060, 454, 136, 32) + sn2 = coord(1050, 490, 156, 20) + + width = 1280 + height = 720 + + img = Image.new("RGB", (width, height)) + + pixels = img.load() + for y in range(height): + for x in range(width): + ####################### player 1 + ### Board + if x>=b1.x and x=b1.y and y>5][xd>>5]].multiply(sprite_pixels[yd&0b0000000000011111, xd&0b0000000000011111]) + xs = xd&0x1F + ys = yd&0x1F + if (xs > 7 and xs < 24) and (ys > 7 and ys < 24): # middle + pixels[x, y] = colors[board0[yd>>5][xd>>5]].get() + elif (xs > 5 and xs < 26) and (ys > 5 and ys < 26): # mid boarder + pixels[x, y] = colors[board0[yd>>5][xd>>5]].mask(0xFFFFFFB0) + elif xs+ys<31: # top left edge + if xs>ys: + pixels[x, y] = colors[board0[yd>>5][xd>>5]].get() + else: + pixels[x, y] = colors[board0[yd>>5][xd>>5]].mask(0xFFFFFFEF) + else: + if xs>ys: + pixels[x, y] = colors[board0[yd>>5][xd>>5]].mask(0xFFFFFFD7) + else: + pixels[x, y] = colors[board0[yd>>5][xd>>5]].mask(0xFFFFFFC0) + ### Next Piece + elif x>=nb1.x and x=nb1.y and y>5][xd>>5]].shiftr(1) + else: + pixels[x, y] = colors[next_board0[yd>>5][xd>>5]].shiftr(2) + elif (not xs) or (not ys): + pixels[x, y] = colors[next_board0[yd>>5][xd>>5]].get() + else: + pixels[x, y] = colors[next_board0[yd>>5][xd>>5]].mask(0xFFFFFFC0) + ### Score + elif x>=s1.x and x=s1.y and y>2][(x-s1.x)>>2]: + pixels[x, y] = white.get() + elif x<(52+s1.x) and x > (28+s1.x) and a_c[yd>>2][(x-(28+s1.x))>>2]: + pixels[x, y] = white.get() + elif x<(80+s1.x) and x > (56+s1.x) and a_o[yd>>2][(x-(56+s1.x))>>2]: + pixels[x, y] = white.get() + elif x<(108+s1.x) and x > (84+s1.x) and a_r[yd>>2][(x-(84+s1.x))>>2]: + pixels[x, y] = white.get() + elif x > (112+s1.x) and a_e[yd>>2][(x-(112+s1.x))>>2]: + pixels[x, y] = white.get() + else: + pixels[x, y] = text_bg.get() + elif x>=sn1.x and x=sn1.y and y=b2.x and x=b2.y and y>5][xd>>5]].shiftr(1) + else: + pixels[x, y] = colors[board1[yd>>5][xd>>5]].shiftr(2) + elif (not xs) or (not ys): + pixels[x, y] = colors[board1[yd>>5][xd>>5]].get() + else: + pixels[x, y] = colors[board1[yd>>5][xd>>5]].mask(0xFFFFFFC0) + ### Next Piece + elif x>=nb2.x and x=nb2.y and y>5][xd>>5]].shiftr(1) + else: + pixels[x, y] = colors[next_board1[yd>>5][xd>>5]].shiftr(2) + elif (not xs) or (not ys): + pixels[x, y] = colors[next_board1[yd>>5][xd>>5]].get() + else: + pixels[x, y] = colors[next_board1[yd>>5][xd>>5]].mask(0xFFFFFFC0) + ### Score + elif x>=s2.x and x=s2.y and y>2][(x-s2.x)>>2]: + pixels[x, y] = white.get() + elif x<(52+s2.x) and x > (28+s2.x) and a_c[yd>>2][(x-(28+s2.x))>>2]: + pixels[x, y] = white.get() + elif x<(80+s2.x) and x > (56+s2.x) and a_o[yd>>2][(x-(56+s2.x))>>2]: + pixels[x, y] = white.get() + elif x<(108+s2.x) and x > (84+s2.x) and a_r[yd>>2][(x-(84+s2.x))>>2]: + pixels[x, y] = white.get() + elif x > (112+s2.x) and a_e[yd>>2][(x-(112+s2.x))>>2]: + pixels[x, y] = white.get() + else: + pixels[x, y] = text_bg.get() + elif x>=sn2.x and x=sn2.y and y>3, (((1280+720)>>3)-((x+y)>>3)), 0) + + img.save(output) + img.show() + +I = 1 +O = 2 +T = 3 +S = 4 +Z = 5 +J = 6 +L = 7 + +next_board_0 = [ + [0,0,0,0], + [0,0,0,0], + [0,0,0,0], + [0,0,0,0]] +next_board_i = [ + [0,0,0,0], + [0,0,0,0], + [0,0,0,0], + [I,I,I,I]] +next_board_o = [ + [0,0,0,0], + [0,0,0,0], + [0,O,O,0], + [0,O,O,0]] +next_board_t = [ + [0,0,0,0], + [0,0,0,0], + [0,T,0,0], + [T,T,T,0]] +next_board_s = [ + [0,0,0,0], + [0,0,0,0], + [0,S,S,0], + [S,S,0,0]] +next_board_z = [ + [0,0,0,0], + [0,0,0,0], + [Z,Z,0,0], + [0,Z,Z,0]] +next_board_l = [ + [0,0,0,0], + [0,L,0,0], + [0,L,0,0], + [0,L,L,0]] +next_board_j = [ + [0,0,0,0], + [0,0,J,0], + [0,0,J,0], + [0,J,J,0]] + +board = [ + [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,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,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,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,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] +] + +next_board0 = [ + next_board_t, next_board_s, next_board_s +] + +board0 = [ + [[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,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,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,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], + [I,0,0,0,0,0,0,S,0,I], + [I,0,T,0,J,J,J,S,S,I], + [I,T,T,T,O,O,J,T,S,I], + [I,I,I,I,O,O,T,T,T,I]], + + [[0,0,0,0,T,T,T,0,0,0], + [0,0,0,0,0,T,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,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,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,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], + [I,0,0,0,0,0,0,S,0,I], + [I,0,T,0,J,J,J,S,S,I]], + + [[0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,T,T,T,0,0,0], + [0,0,0,0,0,T,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,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,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,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], + [I,0,0,0,0,0,0,S,0,I], + [I,0,T,0,J,J,J,S,S,I]]] + +next_board1 = [ + next_board_t, next_board_t, next_board_t +] + +board1 = [ + [ + [0,0,0,I,I,I,I,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], + [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,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,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,I,0,0,0], + [0,0,0,0,0,0,I,0,0,0], + [0,0,Z,Z,0,L,I,0,O,O], + [0,Z,Z,L,L,L,I,0,O,O]], + + [[0,0,0,0,0,0,0,0,0,0], + [0,0,0,I,I,I,I,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], + [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,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,0,0,0,0], + [0,0,0,0,0,0,L,0,0,0], + [0,0,0,0,0,0,I,0,0,0], + [0,0,0,L,0,L,I,0,0,0], + [0,0,Z,Z,0,L,I,0,O,O], + [0,Z,Z,L,L,L,I,0,O,O]], + + [[0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0], + [0,0,0,I,I,I,I,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], + [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,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,L,0,0,0], + [0,0,0,0,0,0,I,0,0,0], + [0,0,0,L,0,L,I,0,0,0], + [0,0,Z,Z,0,L,I,0,O,O], + [0,Z,Z,L,L,L,I,0,O,O]]] + +for i in range(len(board0)): + make_picture(board0[i], board1[i], next_board0[i], next_board1[i], f"output/picture_{i}.png") \ No newline at end of file diff --git a/make_gif.py b/make_gif.py new file mode 100644 index 0000000..39f8165 --- /dev/null +++ b/make_gif.py @@ -0,0 +1,34 @@ +from PIL import Image +import os +import re + +frame_folder = "./output" +output_gif = "output.gif" + +# same duration for every frame (in milliseconds) +frame_duration = 250 + +def frame_number(filename): + return int(re.search(r"frame_(\d+)\.png", filename).group(1)) + +files = [ + f for f in os.listdir(frame_folder) + if f.startswith("frame_") and f.endswith(".png") +] + +files.sort(key=frame_number) + +frames = [ + Image.open(os.path.join(frame_folder, f)).convert("RGBA") + for f in files +] + +frames[0].save( + output_gif, + save_all=True, + append_images=frames[1:], + duration=frame_duration, + loop=0 +) + +print(f"Saved {output_gif}") \ No newline at end of file diff --git a/output.gif b/output.gif new file mode 100644 index 0000000..f8be27c Binary files /dev/null and b/output.gif differ diff --git a/test.png b/test.png new file mode 100644 index 0000000..1d115f2 Binary files /dev/null and b/test.png differ diff --git a/tet-cpu-plan b/tet-cpu-plan index 5ffaef2..bff840b 160000 --- a/tet-cpu-plan +++ b/tet-cpu-plan @@ -1 +1 @@ -Subproject commit 5ffaef21b66ccf44846f4ecdd4699a96fd917d02 +Subproject commit bff840b94f89348aa1618fe4323dc000a8088954