453 lines
9.8 KiB
Python
453 lines
9.8 KiB
Python
import random
|
|
from PIL import Image
|
|
import math
|
|
|
|
n0 = [
|
|
[1,1,1],
|
|
[1,0,1],
|
|
[1,0,1],
|
|
[1,0,1],
|
|
[1,1,1]
|
|
]
|
|
|
|
n1 = [
|
|
[0,1,0],
|
|
[1,1,0],
|
|
[0,1,0],
|
|
[0,1,0],
|
|
[1,1,1]
|
|
]
|
|
|
|
n2 = [
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[1,1,1],
|
|
[1,0,0],
|
|
[1,1,1]
|
|
]
|
|
|
|
n3 = [
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[1,1,1]
|
|
]
|
|
|
|
n4 = [
|
|
[1,0,1],
|
|
[1,0,1],
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[0,0,1]
|
|
]
|
|
|
|
n5 = [
|
|
[1,1,1],
|
|
[1,0,0],
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[1,1,1]
|
|
]
|
|
|
|
n6 = [
|
|
[1,1,1],
|
|
[1,0,0],
|
|
[1,1,1],
|
|
[1,0,1],
|
|
[1,1,1]
|
|
]
|
|
|
|
n7 = [
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[0,0,1],
|
|
[0,0,1],
|
|
[0,0,1]
|
|
]
|
|
|
|
n8 = [
|
|
[1,1,1],
|
|
[1,0,1],
|
|
[1,1,1],
|
|
[1,0,1],
|
|
[1,1,1]
|
|
]
|
|
|
|
n9 = [
|
|
[1,1,1],
|
|
[1,0,1],
|
|
[1,1,1],
|
|
[0,0,1],
|
|
[1,1,1]
|
|
]
|
|
|
|
# a_a = [
|
|
# [1,1,1],
|
|
# [1,0,1],
|
|
# [1,1,1],
|
|
# [1,0,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
# a_b = [
|
|
# [1,1,0],
|
|
# [1,1,1],
|
|
# [1,0,1],
|
|
# [1,1,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
# a_c = [
|
|
# [1,1,1],
|
|
# [1,0,0],
|
|
# [1,0,0],
|
|
# [1,1,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
# a_e = [
|
|
# [1,1,1],
|
|
# [1,1,0],
|
|
# [1,0,0],
|
|
# [1,1,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
# a_o = [
|
|
# [1,1,1],
|
|
# [1,0,1],
|
|
# [1,0,1],
|
|
# [1,1,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
# a_r = [
|
|
# [1,1,1],
|
|
# [1,0,1],
|
|
# [1,1,0],
|
|
# [1,0,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
# a_s = [
|
|
# [1,1,1],
|
|
# [1,0,0],
|
|
# [0,1,1],
|
|
# [1,1,1],
|
|
# [0,0,0]
|
|
# ]
|
|
|
|
### FONT: https://github.com/teryror/pixel-fonts/tree/master
|
|
|
|
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]
|
|
]
|
|
|
|
class color:
|
|
def __init__(self, red: int, green: int, blue: int, alpha: int = 255) -> None:
|
|
self.r = red
|
|
self.g = green
|
|
self.b = blue
|
|
def get(self) -> tuple[int, int, int]:
|
|
return (self.r, self.g, self.b)
|
|
def multiply(self, other) -> tuple[int, int, int]:
|
|
r = other[0]
|
|
g = other[1]
|
|
b = other[2]
|
|
# if self.r:
|
|
# new_r = (self.r + r) >> 1
|
|
# else:
|
|
# new_r = self.r
|
|
# if self.g:
|
|
# new_g = (self.g + g) >> 1
|
|
# else:
|
|
# new_g = self.g
|
|
# if self.b:
|
|
# new_b = (self.b + b) >> 1
|
|
# else:
|
|
# new_b = self.b
|
|
# return (new_r, new_g, new_b)
|
|
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
|
|
|
|
def make_next_board():
|
|
next_board = [
|
|
[0, 0, 0, 0],
|
|
[0, 0, 0, 0],
|
|
[0, 1, 0, 0],
|
|
[1, 1, 1, 0],
|
|
]
|
|
return next_board
|
|
|
|
def make_board():
|
|
board_height = 20
|
|
board_width = 10
|
|
|
|
board = []
|
|
|
|
for _ in range(2):
|
|
line = []
|
|
|
|
for _ in range(board_width):
|
|
line.append(0)
|
|
|
|
board.append(line)
|
|
|
|
for _ in range(board_height - 2):
|
|
line = []
|
|
|
|
for _ in range(board_width):
|
|
line.append(random.randint(0, 5))
|
|
|
|
board.append(line)
|
|
|
|
return board_height, board_width, board
|
|
|
|
def make_sprite():
|
|
sprite = Image.open("img/bitmap.png")
|
|
|
|
sprite_pixels = sprite.load()
|
|
|
|
sprite_width, sprite_height = sprite.size
|
|
|
|
return sprite_pixels, sprite_width, sprite_height
|
|
|
|
def make_score():
|
|
score = random.randint(0, 999999999)
|
|
return score
|
|
|
|
# red = color(255, 0, 0, 255)
|
|
# blue = color(0, 0, 255, 255)
|
|
# green = color(0, 255, 0, 255)
|
|
# purple = color(160, 32, 240, 255)
|
|
# orange = color(255, 165, 0, 255)
|
|
black = color(0, 0, 0, 255)
|
|
white = color(255, 255, 255, 255)
|
|
|
|
red = color(255, 0, 0, 255)
|
|
cyan = color(0, 255, 255, 255)
|
|
|
|
blue = color(0, 0, 255, 255)
|
|
yellow = color(255, 255, 0, 255)
|
|
|
|
green = color(0, 255, 0, 255)
|
|
magenta = color(255, 0, 255, 255)
|
|
|
|
orange = color(255, 128, 0, 255)
|
|
sky_blue = color(0, 128, 255, 255)
|
|
|
|
purple = color(160, 32, 240, 255)
|
|
lime = color(64, 255, 64, 255)
|
|
|
|
colors = [
|
|
black,
|
|
red,
|
|
blue,
|
|
green,
|
|
orange,
|
|
purple
|
|
]
|
|
|
|
colors2 = [
|
|
black,
|
|
cyan,
|
|
yellow,
|
|
magenta,
|
|
sky_blue,
|
|
lime
|
|
]
|
|
|
|
text_bg = black
|
|
|
|
next_board = make_next_board()
|
|
nb1 = coord(88, 96, 128, 128)
|
|
nb2 = coord(1064, 96, 128, 128)
|
|
|
|
board_height, board_width, board = make_board()
|
|
b1 = coord(304, 40, 320, height=640)
|
|
board2_height, board2_width, board2 = make_board()
|
|
b2 = coord(656, 40, 320, height=640)
|
|
|
|
score1 = make_score()
|
|
s1 = coord(84, 454, 136, height=32)
|
|
sn1 = coord(74, 490, 156, height=20)
|
|
score2 = make_score()
|
|
s2 = coord(304, 40, 320, height=640)
|
|
sn2 = coord(304, 40, 320, height=640)
|
|
|
|
sprite_pixels, sprite_width, sprite_height = make_sprite()
|
|
|
|
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.x+b1.width and y>=b1.y and y<b1.y+b1.height:
|
|
xd = x-b1.x
|
|
yd = y-b1.y
|
|
# pixels[x, y] = colors[board[yd>>5][xd>>5]].multiply(sprite_pixels[yd&0b0000000000011111, xd&0b0000000000011111])
|
|
xs = xd&0x1F
|
|
ys = yd&0x1F
|
|
if (xs == 31) or (ys==31):
|
|
if (xs == 0) or (ys == 0):
|
|
pixels[x, y] = colors[board[yd>>5][xd>>5]].shiftr(1)
|
|
else:
|
|
pixels[x, y] = colors[board[yd>>5][xd>>5]].shiftr(2)
|
|
elif (not xs) or (not ys):
|
|
pixels[x, y] = colors[board[yd>>5][xd>>5]].get()
|
|
else:
|
|
pixels[x, y] = colors[board[yd>>5][xd>>5]].mask(0xFFFFFFC0)
|
|
### Next Piece
|
|
elif x>=nb1.x and x<nb1.x+nb1.width and y>=nb1.y and y<nb1.y+nb1.height:
|
|
xd = x-nb1.x
|
|
yd = y-nb1.y
|
|
xs = xd&0x1F
|
|
ys = yd&0x1F
|
|
if (xs == 31) or (ys==31):
|
|
if (xs == 0) or (ys == 0):
|
|
pixels[x, y] = colors[next_board[yd>>5][xd>>5]].shiftr(1)
|
|
else:
|
|
pixels[x, y] = colors[next_board[yd>>5][xd>>5]].shiftr(2)
|
|
elif (not xs) or (not ys):
|
|
pixels[x, y] = colors[next_board[yd>>5][xd>>5]].get()
|
|
else:
|
|
pixels[x, y] = colors[next_board[yd>>5][xd>>5]].mask(0xFFFFFFC0)
|
|
### Score
|
|
elif x>=s1.x and x<s1.x+s1.width and y>=s1.y and y<s1.y+s1.height:
|
|
yd = y-s1.y
|
|
if x < (24+s1.x) and a_s[yd>>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()
|
|
####################### player 2
|
|
### Board
|
|
elif x>=b2.x and x<b2.x+b2.width and y>=b2.y and y<b2.y+b2.height:
|
|
xd = x-b2.x
|
|
yd = y-b2.y
|
|
pixels[x, y] = colors[board2[yd>>5][xd>>5]].multiply(sprite_pixels[yd&0b0000000000011111, xd&0b0000000000011111])
|
|
### Next Piece
|
|
elif x>=nb2.x and x<nb2.x+nb2.width and y>=nb2.y and y<nb2.y+nb2.height:
|
|
xd = x-nb2.x
|
|
yd = y-nb2.y
|
|
pixels[x, y] = colors[next_board[yd>>5][xd>>5]].multiply(sprite_pixels[yd&0b0000000000011111, xd&0b0000000000011111])
|
|
# ####################### player 2
|
|
# ### Board
|
|
# elif x>=b2.x and x<b2.x+b2.width and y>=b2.y and y<b2.y+b2.height:
|
|
# xd = x-b2.x
|
|
# yd = y-b2.y
|
|
# pixels[x, y] = colors2[board2[yd>>5][xd>>5]].multiply(sprite_pixels[yd&0b0000000000011111, xd&0b0000000000011111])
|
|
# ### Next Piece
|
|
# elif x>=nb2.x and x<nb2.x+nb2.width and y>=nb2.y and y<nb2.y+nb2.height:
|
|
# xd = x-nb2.x
|
|
# yd = y-nb2.y
|
|
# 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)
|
|
offset = 0
|
|
|
|
numbers = [a_s, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9]
|
|
for num in numbers:
|
|
for y in range(len(num)<<2):
|
|
for x in range(len(num[y>>2])<<2):
|
|
if num[y>>2][x>>2]:
|
|
pixels[x+offset, y] = white.get()
|
|
else:
|
|
pixels[x+offset, y] = black.get()
|
|
offset += (len(num[0])<<2)+4
|
|
|
|
letters = [a_s, a_c, a_o, a_r, a_e]
|
|
offset = 0
|
|
|
|
base_offset = 720-32
|
|
|
|
for letter in letters:
|
|
for y in range(len(letter)<<2):
|
|
for x in range(len(letter[y>>2])<<2):
|
|
if letter[y>>2][x>>2]:
|
|
pixels[x+offset, y+base_offset] = white.get()
|
|
else:
|
|
pixels[x+offset, y+base_offset] = black.get()
|
|
offset += (len(letter[0])<<2) + 4
|
|
|
|
|
|
|
|
img.save("output/alpha.png")
|
|
img.show()
|