Commit de509fba by BellCodeEditor

auto save

parent 9afe71bc
Showing with 32 additions and 14 deletions
......@@ -7,6 +7,7 @@ grid_size = 20 # 格子大小
grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量
FPS = 30
#通过帧率进行运算
count = 0
states = False
......@@ -39,20 +40,33 @@ L = [[(-1, 0), (0, 0), (1, 0), (1, 1)],
[(-1, 0), (0, 0), (1, 0), (-1, -1)],
[(0, -1), (0, 0), (0, 1), (-1, 1)]]
shape_list = [I, J, L, O, S, T, Z] # 7种类型俄罗斯方块
current_shape = [(0, -1), (0, 0), (0, 1), (-1, 0)]
# current_shape = [(0, -1), (0, 0), (0, 1), (-1, 0)]
# 一些RGB颜色
cube_colors = [
(204, 153, 153), (102, 102, 153),(153, 0, 102),
(255, 204, 0), (204, 0, 51),(255, 0, 51), (0, 102, 153),
(153, 0, 51), (204, 255, 102), (255, 153, 0)]
# center = [2,8]
# #随机选出俄罗斯类型
# shape = random.choice(shape_list)
# #随机选出俄罗斯类型的一种具体形状的下标
# index = random.randint(0,len(shape)-1)
# #根据下标选取具体形状
# current_shape=shape[index]
# #随机选取颜色
# color = random.choice(cube_colors)
#封装函数
def cleck(center):
def cleck(center): #注意形参为中心点
for cube in current_shape:
#先计算每一个小方块行和列的位置
cube = (cube[0] + center[0], cube[1] + center[1])
#判断小方块有没有超出网格范围
if cube[0]<1 or cube[1]<1 or cube[0]> grid_num_height or cube[1]> grid_num_width:
#返回值
return False
while True:
......@@ -62,8 +76,9 @@ while True:
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT: # 向右
center[1] = center[1]+1 #先让中心点向左移动1列
if cleck(center) == False:
center[1] = center[1]-1
if cleck(center) == False: #小方块超出了网格范围
center[1] = center[1]-1 #将小方块还原
elif event.key == locals.K_LEFT: # 向左
center[1] = center[1]-1 #先让中心点向左移动1列
if cleck(center) == False:
......@@ -78,32 +93,35 @@ while True:
center[0] = center[0]+1 #先让中心点向下移动1列
if cleck(center) == False:
center[0] = center[0]-1
elif event.key == locals.K_UP: # 向
old_index = index
#实现俄罗斯方块变形,并且防止在边缘变形是超出网格范围
elif event.key == locals.K_UP: # 向
old_index = index #先将原来索引备份下来
index+=1
if index>=len(shape):
if index>=len(shape): #注意下标,最多4种,超过即重置
index = 0
current_shape=shape[index]
if cleck(center) == False:
index = old_index
current_shape=shape[index]
if cleck(center) == False: #防止在网格边缘变形,利用自定义函数检测
index = old_index #若是超出则还原回来
current_shape=shape[index]#重新取出原来形状
#到达底部时要不要创建新的俄罗斯方块
if states == False:
states = True
#不断生成俄罗斯方块
center = [2, 8] # 第2行第8列
#随机
shape = random.choice(shape_list) #随机形状
index = random.randint(0,len(shape)-1) #随机形状的索引位置
current_shape = shape[index] #根据下标取出方块的形状
color = random.choice(cube_colors) #随机颜色
#下落速度
count+=1
#如果下落速度对帧率能够整除
if count%FPS == 0:
#就下降一个网格
center[0] = center[0]+1 #先让中心点向下移动1列
if cleck(center) == False:
if cleck(center) == False: #如果下降到底部,就重新生成一个
center[0] = center[0]-1
states = False
# 将背景图画上去
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment