Commit b6bf4720 by BellCodeEditor

auto save

parent 9d8ede2e
Showing with 76 additions and 60 deletions
...@@ -10,6 +10,7 @@ grid_num_height = 25 # 纵向格子数量 ...@@ -10,6 +10,7 @@ grid_num_height = 25 # 纵向格子数量
FPS = 30 # 帧率 FPS = 30 # 帧率
count = 0 count = 0
states = False states = False
gameover = False # 新建变量gameover初始值设为False,表示游戏正常运行
# 创建窗口 # 创建窗口
screen = pygame.display.set_mode((460, 500)) screen = pygame.display.set_mode((460, 500))
...@@ -18,6 +19,8 @@ clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) ...@@ -18,6 +19,8 @@ clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入素材 # 载入素材
background = pygame.image.load('bg.png') background = pygame.image.load('bg.png')
font = pygame.font.Font('STKAITI.TTF', 60) # 字体 font = pygame.font.Font('STKAITI.TTF', 60) # 字体
# 新建字体对象
font_restart = pygame.font.Font('STKAITI.TTF', 25)
# 俄罗斯方块所有形状 # 俄罗斯方块所有形状
O = [[(0, 0), (0, 1), (1, 0), (1, 1)]] O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
...@@ -50,7 +53,7 @@ cube_colors = [ ...@@ -50,7 +53,7 @@ cube_colors = [
# 创建一个含有25个小列表,每个小列表都有15个元素,每个元素的值都是0的列表 # 创建一个含有25个小列表,每个小列表都有15个元素,每个元素的值都是0的列表
num_list = [] # 创建空列表存储最终的列表元素并命名为num_list num_list = [] # 创建空列表存储最终的列表元素并命名为num_list
for i in range(25): # 使用for循环让程序重复执行25次 for i in range(25): # 使用for循环让程序重复执行25次
num_list.append([0] * 15) # 用num_list.append()方法将小列表添加到列表里 num_list.append([0] * 15) # 用num_list.append()方法将小列表添加到列表里
def check(center): def check(center):
for cube in current_shape: for cube in current_shape:
...@@ -93,66 +96,79 @@ while True: ...@@ -93,66 +96,79 @@ while True:
index = old_index index = old_index
current_shape = shape[index] current_shape = shape[index]
if states == False: if gameover == False: # if判断变量gameover的值等于False
states = True # 生成新的俄罗斯方块
center = [2, 8] # 第2行第8列 if states == False:
shape = random.choice(shape_list) states = True
index = random.randint(0, len(shape)-1) # 随机形状索引 center = [2, 8] # 第2行第8列
current_shape = shape[index] shape = random.choice(shape_list)
color = random.choice(cube_colors) # 随机选取一种颜色 index = random.randint(0, len(shape)-1) # 随机形状索引
count += 1 current_shape = shape[index]
if count % FPS == 0: # 降落速度的算式 color = random.choice(cube_colors) # 随机选取一种颜色
center[0] = center[0] + 1 count += 1
if check(center) == False: if count % FPS == 0: # 降落速度的算式
center[0] = center[0] - 1 center[0] = center[0] + 1
states = False if check(center) == False:
# for循环进行遍历取出每一个小方块的行、列位置 center[0] = center[0] - 1
for cube in current_pos: states = False
# 用它们减去1作为索引找到对应的值改成俄罗斯方块的颜色 # for循环进行遍历取出每一个小方块的行、列位置
num_list[cube[0]-1][cube[1]-1] = color for cube in current_pos:
# 将背景图画上去 # 用它们减去1作为索引找到对应的值改成俄罗斯方块的颜色
screen.blit(background, (0, 0)) num_list[cube[0]-1][cube[1]-1] = color
# 计算出所有小方块的行、列位置 # 将背景图画上去
current_pos = [] screen.blit(background, (0, 0))
for cube in current_shape: # 计算出所有小方块的行、列位置
pos = (cube[0] + center[0], cube[1] + center[1]) current_pos = []
current_pos.append(pos) for cube in current_shape:
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块 pos = (cube[0] + center[0], cube[1] + center[1])
for cube in current_pos: current_pos.append(pos)
pygame.draw.rect(screen, color, # 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0) for cube in current_pos:
pygame.draw.rect(screen, (255, 255, 255), pygame.draw.rect(screen, color,
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1) (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0)
pygame.draw.rect(screen, (255, 255, 255),
# 创建一个1-25的列表进行遍历(记录行的位置) (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1)
for i, row in zip(range(1,26), num_list):
# 创建一个1-15的列表进行遍历(记录列的位置) # 创建一个1-25的列表进行遍历(记录行的位置)
for j, colors in zip(range(1,16), row): for i, row in zip(range(1,26), num_list):
if colors != 0: # if判断每一个格子对应的值不等于0 # 创建一个1-15的列表进行遍历(记录列的位置)
# 用pygame.draw.rect()方法画出小方块 for j, colors in zip(range(1,16), row):
pygame.draw.rect(screen, colors, if colors != 0: # if判断每一个格子对应的值不等于0
(j * 20-20, i * 20-20, 20, 20)) # 用pygame.draw.rect()方法画出小方块
pygame.draw.rect(screen, (255, 255, 255), pygame.draw.rect(screen, colors,
(j * 20-20, i * 20-20, 20, 20), 1) (j * 20-20, i * 20-20, 20, 20))
pygame.draw.rect(screen, (255, 255, 255),
(j * 20-20, i * 20-20, 20, 20), 1)
new_list = [] # 新的地图列表 # 新的地图列表
for i in range(25): new_list = [] # 给新的空白列表命名为new_list
new_list.append([0] * 15) for i in range(25): # 使用for循环让程序重复执行25次
row_index = 24 new_list.append([0] * 15) # 用num_list.append()方法将小列表添加到大列表里
for i in range(24, -1, -1): # 新建变量row_index用与记录新网格列表改变的行数序号,并设置为24表最后一行
is_full = Ture row_index = 24
for j in range(grid_num_width): for i in range(24, -1, -1): # 结合for循环生成一个倒着排序的列表索引
if num_list[i][j] == 0: # (确认每一行网格有没有被小方块填满)
is_full = False is_full = True # 建立变量is_full并设置为Ture默认网格状态被小方块填满
if is_full == False: for j in range(grid_num_width): # for循环取出这一行网格的每一小格检查
new_list[row_index] = num_list[i] if num_list[i][j] == 0: # if判断这一行网格的每一小格的值是否等于0
row_index -= 1 is_full = False # 如果等于0,说明还有格子没有填满,就将值设置为False
else: if is_full == False: # if判断这一行没有被填满
score += 1 new_list[row_index] = num_list[i] # 通过下标将这一行数值赋值到新列表里
num_list = new_list row_index -= 1 # 将序列号减少1
# 得分 else: # 否则(这一行填满)
text_surface = font.render(str(score), True, (0, 0, 0)) score += 1 # 分数变量score的值增加1
screen.blit(text_surface, (350,70)) num_list = new_list # 网格列表的值new_list赋值给变量num_list,替换原来的数值
# 小方块超出高度
if num_list[1][7] != 0: # 判断列表num_list[1][7]不等于0
gameover = True # 将变量gameover的值设为True
# 得分
text_surface = font.render(str(score), True, (0, 0, 0))
screen.blit(text_surface, (350,70))
if gameover == True:
# 渲染游戏失败的文字提示
text = font_restart.render("游戏失败,按下任意键开始")
screen.blit(text, (20, 250))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
clock.tick(FPS) clock.tick(FPS)
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