Commit b6bf4720 by BellCodeEditor

auto save

parent 9d8ede2e
Showing with 31 additions and 15 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,6 +96,8 @@ while True: ...@@ -93,6 +96,8 @@ while True:
index = old_index index = old_index
current_shape = shape[index] current_shape = shape[index]
if gameover == False: # if判断变量gameover的值等于False
# 生成新的俄罗斯方块
if states == False: if states == False:
states = True states = True
center = [2, 8] # 第2行第8列 center = [2, 8] # 第2行第8列
...@@ -135,24 +140,35 @@ while True: ...@@ -135,24 +140,35 @@ while True:
pygame.draw.rect(screen, (255, 255, 255), pygame.draw.rect(screen, (255, 255, 255),
(j * 20-20, i * 20-20, 20, 20), 1) (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次
new_list.append([0] * 15) # 用num_list.append()方法将小列表添加到大列表里
# 新建变量row_index用与记录新网格列表改变的行数序号,并设置为24表最后一行
row_index = 24 row_index = 24
for i in range(24, -1, -1): for i in range(24, -1, -1): # 结合for循环生成一个倒着排序的列表索引
is_full = Ture # (确认每一行网格有没有被小方块填满)
for j in range(grid_num_width): is_full = True # 建立变量is_full并设置为Ture默认网格状态被小方块填满
if num_list[i][j] == 0: for j in range(grid_num_width): # for循环取出这一行网格的每一小格检查
is_full = False if num_list[i][j] == 0: # if判断这一行网格的每一小格的值是否等于0
if is_full == False: is_full = False # 如果等于0,说明还有格子没有填满,就将值设置为False
new_list[row_index] = num_list[i] if is_full == False: # if判断这一行没有被填满
row_index -= 1 new_list[row_index] = num_list[i] # 通过下标将这一行数值赋值到新列表里
else: row_index -= 1 # 将序列号减少1
score += 1 else: # 否则(这一行填满)
num_list = new_list score += 1 # 分数变量score的值增加1
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)) text_surface = font.render(str(score), True, (0, 0, 0))
screen.blit(text_surface, (350,70)) 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