Commit 8860c0f7 by BellCodeEditor

save project

parent b5792c37
Showing with 70 additions and 55 deletions
...@@ -18,6 +18,8 @@ clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) ...@@ -18,6 +18,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) # 字体
font1 = pygame.font.Font('STKAITI.TTF', 25) # 字体
gameover = False
# 俄罗斯方块所有形状 # 俄罗斯方块所有形状
O = [[(0, 0), (0, 1), (1, 0), (1, 1)]] O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
...@@ -67,6 +69,8 @@ while True: ...@@ -67,6 +69,8 @@ while True:
if event.type == locals.QUIT: if event.type == locals.QUIT:
exit() exit()
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if gameover==True:
gameover=False
if event.key == locals.K_LEFT: # 向左 if event.key == locals.K_LEFT: # 向左
center[1] = center[1] - 1 # 左移1列 center[1] = center[1] - 1 # 左移1列
if check(center) == False: if check(center) == False:
...@@ -92,61 +96,71 @@ while True: ...@@ -92,61 +96,71 @@ while True:
index = old_index index = old_index
current_shape = shape[index] current_shape = shape[index]
# 生成新俄罗斯方块 # 生成新俄罗斯方块
if states == False: if num_list[1][7]!=0:
states = True gameover=True
center = [2, 8] # 第2行第8列 if gameover == False:
shape = random.choice(shape_list) if states == False:
index = random.randint(0, len(shape)-1) # 随机形状索引 states = True
current_shape = shape[index] center = [2, 8] # 第2行第8列
color = random.choice(cube_colors) # 随机选取一种颜色 shape = random.choice(shape_list)
count += 1 index = random.randint(0, len(shape)-1) # 随机形状索引
if count % FPS == 0: # 降落速度的算式 current_shape = shape[index]
center[0] = center[0] + 1 color = random.choice(cube_colors) # 随机选取一种颜色
if check(center) == False: count += 1
center[0] = center[0] - 1 if count % FPS == 0: # 降落速度的算式
states = False center[0] = center[0] + 1
for cube in current_pos: if check(center) == False:
num_list[cube[0]-1][cube[1]-1] = color center[0] = center[0] - 1
# 将背景图画上去 states = False
screen.blit(background, (0, 0)) for cube in current_pos:
# 计算出所有小方块的行、列位置 num_list[cube[0]-1][cube[1]-1] = color
current_pos = [] # 将背景图画上去
for cube in current_shape: screen.blit(background, (0, 0))
pos = (cube[0] + center[0], cube[1] + center[1]) # 计算出所有小方块的行、列位置
current_pos.append(pos) current_pos = []
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块 for cube in current_shape:
for cube in current_pos: pos = (cube[0] + center[0], cube[1] + center[1])
pygame.draw.rect(screen, color, current_pos.append(pos)
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0) # 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
pygame.draw.rect(screen, (255, 255, 255), for cube in current_pos:
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1) pygame.draw.rect(screen, color,
# 画出降落后的方块 (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0)
for i, row in zip(range(1,26), num_list): pygame.draw.rect(screen, (255, 255, 255),
for j, colors in zip(range(1,16), row): (cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1)
if colors != 0: # 画出降落后的方块
pygame.draw.rect(screen, colors, for i, row in zip(range(1,26), num_list):
(j * 20-20, i * 20-20, 20, 20)) for j, colors in zip(range(1,16), row):
pygame.draw.rect(screen, (255, 255, 255), if colors != 0:
(j * 20-20, i * 20-20, 20, 20), 1) pygame.draw.rect(screen, colors,
(j * 20-20, i * 20-20, 20, 20))
new_list = [] # 新的地图列表 pygame.draw.rect(screen, (255, 255, 255),
for i in range(25): (j * 20-20, i * 20-20, 20, 20), 1)
new_list.append([0] * 15)
row_index = 24 new_list = [] # 新的地图列表
for i in range(24, -1, -1): for i in range(25):
is_full = True new_list.append([0] * 15)
for j in range(grid_num_width): row_index = 24
if num_list[i][j] == 0: for i in range(24, -1, -1):
is_full = False is_full = True
if is_full == False: for j in range(grid_num_width):
new_list[row_index] = num_list[i] if num_list[i][j] == 0:
row_index -= 1 is_full = False
else: if is_full == False:
score += 1 new_list[row_index] = num_list[i]
num_list = new_list row_index -= 1
# 得分 else:
text_surface = font.render(str(score), True, (0, 0, 0)) score += 1
screen.blit(text_surface, (350,70)) num_list = new_list
# 得分
text_surface = font.render(str(score), True, (0, 0, 0))
screen.blit(text_surface, (350,70))
if gameover == True:
text_surface1 = font1.render("按任意键重新开始游戏!", True, (0, 0, 0))
screen.blit(text_surface1, (0,250))
score = 0
num_list = []
for i in range(25):
num_list.append([0] * 15)
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
clock.tick(FPS) clock.tick(FPS)
\ No newline at end of file
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