import pygame,sys
from const import *
from pygame.locals import *
#封装一个方块类
class block(pygame.sprite.Sprite):
# def __init__(self,colour_type_url,pos):
# # 重写初始化init方法
# super().__init__()

def __init__(self, colour_type_url, rowidx,colidx,wight,height,relpos,block_pos_list1):
# 重写初始化init方法
super().__init__()
self.colour_type_url=colour_type_url
self.rowidx = rowidx
self.colidx = colidx
self.wight = wight
self.height = height
self.relpos = relpos
self.loading_image()
self.block_pos()
self.time = 0
self.block_pos_list=[]
self.block_pos_list1=block_pos_list1
# print(self.wight,self.height)
def loading_image(self):
# 加载本地图片
self.image = pygame.image.load(colour_url[self.colour_type_url])
#加载后缩放图片到指定大小
self.image =pygame.transform.scale(self.image,(self.wight,self.height))
def block_pos(self):
# 获取形状、位置
self.rect = self.image.get_rect()
# print(self.rect)
self.rect.left=self.relpos[0]+self.wight * self.rowidx
self.rect.top = self.relpos[1] + self.height * self.colidx
# 方块放置的位置
self.rect.center = self.relpos
# self.block_pos_list.append(self.relpos)
#获取方块当前位置
def pos(self):
rect = self.rect.center
rect = [int(rect[0]), int(rect[1])]
return rect
#获取即将下落得位置
def dorp_pos(self):
# 获取键盘输入方式
Keyboard_input = pygame.key.get_pressed()
# 上下左右控制图片
if Keyboard_input[K_LEFT] or Keyboard_input[K_a]:
pos0 = self.pos()
Keyboard="K_LEFT"
return [pos0[0] - 32, pos0[1]],1,Keyboard
elif Keyboard_input[K_RIGHT] or Keyboard_input[K_d]:
pos0 = self.pos()
Keyboard ="K_RIGHT"
return [pos0[0] + 32, pos0[1]],1,Keyboard
elif Keyboard_input[K_UP] or Keyboard_input[K_w]:
pos0 = self.pos()
Keyboard ="K_UP"
return [pos0[0], pos0[1] - 32],1,Keyboard
elif Keyboard_input[K_DOWN] or Keyboard_input[K_s]:
pos0 = self.pos()
Keyboard ="K_DOWN"
return [pos0[0], pos0[1] + 32],1,Keyboard

else:
pos0 = self.pos()
Keyboard="storp"
return [pos0[0], pos0[1]],0,Keyboard
def Keyboard_input(self):
# 获取键盘输入方式
Keyboard_input = pygame.key.get_pressed()
# 上下左右控制图片
if Keyboard_input[K_LEFT] or Keyboard_input[K_a]:
pos0 = self.pos()
if [pos0[0]-32,pos0[1]] not in self.block_pos_list1:
self.rect.move_ip(-32, 0)
pos1 = self.pos()
isdrop_true = 1
print(isdrop_true, pos1)
return isdrop_true, pos1
else:
isdrop_true=0
return isdrop_true, pos0
print(isdrop_true, pos1)

# print(f"############{pos0}#{pos1}########")
# rect =self.pos()
# rect = str(rect).replace(" ", "").replace("[", "").replace("]", "").split(",")[
# 0:2]
# rect = [int(rect[0]), int(rect[1])]

elif Keyboard_input[K_RIGHT]or Keyboard_input[K_d]:
pos0 = self.pos()
if [pos0[0] + 32, pos0[1]] not in self.block_pos_list1:
self.rect.move_ip(32, 0)
pos1 = self.pos()
isdrop_true = 1
return isdrop_true, pos1
else:
isdrop_true = 0
return isdrop_true, pos0
elif Keyboard_input[K_UP] or Keyboard_input[K_w]:
pos0 = self.pos()
if [pos0[0], pos0[1]-32] not in self.block_pos_list1:
self.rect.move_ip(0, -32)
pos1 = self.pos()
isdrop_true = 1
return isdrop_true, pos1
else:
isdrop_true = 0
return isdrop_true, pos0
elif Keyboard_input[K_DOWN]or Keyboard_input[K_s]:
pos0 = self.pos()
if [pos0[0], pos0[1] + 32] not in self.block_pos_list1:
self.rect.move_ip(0, 32)
pos1 = self.pos()
isdrop_true = 1
return isdrop_true, pos1
else:
isdrop_true = 0
return isdrop_true, pos0
elif Keyboard_input[K_q]:
pos0 = self.pos()
Keyboard = "K_q"
print("点击了变形")
return [pos0[0], pos0[1]],1,Keyboard
else:
pos0 = self.pos()
isdrop_true=0
return isdrop_true, pos0
def draw(self,window):
# 渲染窗口图片
window.blit(self.image, self.rect)

#下落位置修改
def drop(self):
if self.rect[1]<=560 :

isdrop_true,pos_re=self.Keyboard_input()
print(isdrop_true,pos_re)
if isdrop_true:
drop_true = 1
return drop_true,pos_re
else:
drop_true = 0
return drop_true, pos_re
else:
pos_re=self.pos()
drop_true = 0
return drop_true,pos_re
# self.rect = self.image.get_rect()
# print(self.rect[1])
# else:
# self.relpos = (self.relpos[0],50 )
# print(self.block)
# for k in self.blocks:
# self.GenratevlockgroupConfig()