import random
from const import *
from block import *
import time

class block_group(object):
def GenratevlockgroupConfig(rowidx,cloidx,relpos):
idnamex=random.randint(0,len(BLOCK_SHAPE_NAME)-1)
idname=BLOCK_SHAPE_NAME[idnamex]
print(idname)
idx=0
# print(colour_type.blockmax-1)
# print()

btype=random.randint(0,colour_type.blockmax-1)
config_list=[]
for x in range(len(BLOCK_SHAPE[idname][idx])):
config={
"blocktype":btype,
"rowidx": rowidx+BLOCK_SHAPE[idname][idx][x][0]*relpos[0],
"cloidx": cloidx+BLOCK_SHAPE[idname][idx][x][1]*relpos[1],
"idnamex":idname,
"idx":idx,
"rowidxyuanshi":rowidx,
"cloidxyuanshi":cloidx,
"maxidx":len(BLOCK_SHAPE[BLOCK_SHAPE_NAME[idnamex]])-1
}
config_list.append(config)
return config_list
def __init__(self,width,heght,blockconfig,relpos,block_pos_list):
super().__init__()
self.blocks=[]
self.time=0
self.rowid=20
self.drop_true = 1
self.width=width
self.heght = heght
self.blockconfig = blockconfig
# self.width = width
self.block_pos_list=block_pos_list
# relpos[0]=240
self.relpos=relpos
self.pos_block_list=[]
for config in self.blockconfig :
blk=block(config['blocktype'],config['rowidx'],config['cloidx'],self.width,self.heght,(config['rowidx'],config['cloidx']),self.pos_block_list)

self.blocks.append(blk)

self.block_pos_list.append([config['rowidx'],config['cloidx']])
def draw(self,surface):
for b in self.blocks:
b.draw(surface)

return self.drop_true
def block_Amorphosis(self):
# for x in range(len(BLOCK_SHAPE[config["idnamex"]][config['idx']):
for config in self.blockconfig:
# 转换模式
print(config)
x=0
if config['idx']!=config['maxidx']:
config['idx']+=1
config['rowidx']=config["rowidxyuanshi"]+BLOCK_SHAPE[config["idnamex"]][config['idx']][x][0]*self.relpos[0]
config['cloidx']=config["cloidxyuanshi"]+BLOCK_SHAPE[config["idnamex"]][config['idx']][x][1]*self.relpos[1]
print(config['rowidx'],config['cloidx'])
blk = block(config['blocktype'], config['rowidx'], config['cloidx'],self.width, self.heght,(config['rowidx'], config['cloidx']), self.pos_block_list)
print(config)
self.blocks.append(blk)

self.block_pos_list.append([config['rowidx'], config['cloidx']])
x+=1
def update(self):
self.time += 1
if self.time >= 50:
self.time = 0
bin_pos = []
#检测方块是否可以下移
for k in self.blocks:
pos_re_1 = k.pos()
bin_pos.append(pos_re_1)
if pos_re_1[1]>=580:
self.drop_true=0
isblocksgroupmove=[]
for k in self.blocks:
pos_re = k.pos()
if self.drop_true:
dorp_pos, is_remove,Keyboard = k.dorp_pos()
if is_remove:
if dorp_pos not in self.block_pos_list:
isblocksgroupmove.append(1)
isblocksgroupmove.append(Keyboard)
elif dorp_pos in bin_pos:
isblocksgroupmove.append(1)
isblocksgroupmove.append(Keyboard)
else:
isblocksgroupmove.append(0)
isblocksgroupmove.append(Keyboard)
if 0 not in isblocksgroupmove :
if K_q in isblocksgroupmove:
self.block_Amorphosis()

for k in self.blocks:
pos_re = k.pos()
if self.drop_true :
dorp_pos,is_remove,Keyboard=k.dorp_pos()
print(f"{is_remove},{Keyboard}")
# if Keyboard=="":
# self.block_Amorphosis()
if dorp_pos not in self.block_pos_list:
self.block_pos_list.remove(pos_re)
self.block_pos_list.append(dorp_pos)
drop_true, pos_re_1 = k.drop()
elif dorp_pos in bin_pos:

self.block_pos_list.remove(pos_re)
self.block_pos_list.append(dorp_pos)
drop_true, pos_re_1 = k.drop()
else:
self.drop_true = 0
else:
if "K_LEFT"in isblocksgroupmove or "K_RIGHT" in isblocksgroupmove:
self.drop_true = 1
else:
self.drop_true = 0