Chinaunix首页 | 论坛 | 博客

elu

  • 博客访问: 18488
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-22 13:59
个人简介

积累, 沉淀

文章分类

全部博文(2)

文章存档

2019年(1)

2016年(1)

我的朋友

分类: Python/Ruby

2019-11-21 20:56:50

      俄罗斯方块, 自己基本玩的还行。
      用的python的pygame模块,先画一个简单的屏幕




点击(此处)折叠或打开

  1. import sys
  2. import pygame

  3. def main():
  4.     pygame.init()
  5.     screen = pygame.display.set_mode((500, 700))  # 画布大小
  6.     pygame.display.set_caption('俄罗斯方块')       # 设置标题样式
  7.     
  8.     while True:
  9.         for event in pygame.event.get():
  10.             if event.type == pygame.QUIT:
  11.                 pygame.quit()
  12.                 sys.exit()                    

  13. if __name__ == '__main__':
  14.     main()
运行代码后情况如下:


现在是把上面画布分为两大部分,左边为游戏区,为了更直观看到方块,一齐画上方块的网格线;右边为游戏信息区,包括积分、消除行数、下一个方块提示等信息;中间用一条竖线区分开来

点击(此处)折叠或打开

  1. import sys
  2. import pygame


  3. SIZE = 30 # 每个小方格大小

  4. BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
  5. BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量

  6. BORDER_WIDTH = 4 # 游戏区边框分割线宽度
  7. BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色

  8. INFO_WIDTH = 160 # 信息区域宽度

  9. BG_COLOR = (60, 60, 60) # 背景色

  10. BLACK = (0, 0, 0) # 网格线颜色
  11. WIDTH = 1 # 网格线宽度

  12. SCREEN_WIDTH = (SIZE + WIDTH) * BLOCK_WIDTH_NUM + WIDTH + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
  13. SCREEN_HEIGHT = (SIZE + WIDTH) * BLOCK_HEIGHT_NUM # 游戏屏幕的高


  14. # 将文字划到画布上指定位置
  15. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  16.     imgText = font.render(text, True, fcolor)
  17.     screen.blit(imgText, (x, y))


  18. def main():
  19.     pygame.init()
  20.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  21.     pygame.display.set_caption('俄罗斯方块')
  22.     
  23.     font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
  24.     font1_width, font1_height = font1.size('得分:') # 字体高度
  25.     font1_height= int(font1_height)
  26.     font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
  27.     
  28.     score = 0 # 得分
  29.     removes = 0 # 消除行数
  30.     orispeed = 0.5 # 原始速度
  31.     speed = orispeed # 当前速度
  32.     
  33.     while True:
  34.         for event in pygame.event.get():
  35.             if event.type == pygame.QUIT:
  36.                 pygame.quit()
  37.                 sys.exit()
  38.                 
  39.                 
  40.         # 填充背景色
  41.         screen.fill(BG_COLOR)
  42.         # 画游戏区域分隔线
  43.         pygame.draw.line(screen, BORDER_COLOR,
  44.                          ((SIZE + WIDTH) * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
  45.                          ((SIZE + WIDTH) * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
  46.               
  47.         # 画网格线 竖线
  48.         for x in range(BLOCK_WIDTH_NUM+1):
  49.             pygame.draw.line(screen, BLACK, (x * (SIZE + WIDTH), 0), (x * (SIZE + WIDTH), SCREEN_HEIGHT), 1)
  50.         # 画网格线 横线
  51.         for y in range(BLOCK_HEIGHT_NUM+1):
  52.             pygame.draw.line(screen, BLACK, (0, y * (SIZE + WIDTH)), (BLOCK_WIDTH_NUM * (SIZE + WIDTH), y * (SIZE + WIDTH)), 1)

  53.         
  54.         print_text(screen, font1, font_pos_x, 10, f'得分: ')
  55.         print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
  56.         print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
  57.         print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
  58.         print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
  59.         print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
  60.         print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
  61.         
  62.         pygame.display.flip()


  63. if __name__ == '__main__':
  64.     main()
现在运行后的效果如下:


接下来就是各种方块的绘制了
先统计方块种类,分别有 T 型、L 型、J 型、I 型、Z 型、S 型、正方形,网上找的block模块直接拿来用

点击(此处)折叠或打开

  1. import random
  2. from collections import namedtuple

  3. Point = namedtuple('Point', 'X Y')
  4. Shape = namedtuple('Shape', 'X Y Width Height')
  5. Block = namedtuple('Block', 'template start_pos end_pos name next')

  6. # 方块形状的设计,我最初我是做成 4 × 4,因为长宽最长都是4,这样旋转的时候就不考虑怎么转了,就是从一个图形替换成另一个
  7. # 其实要实现这个功能,只需要固定左上角的坐标就可以了

  8. # S形方块
  9. S_BLOCK = [Block(['.OO',
  10.                   'OO.',
  11.                   '...'], Point(0, 0), Point(2, 1), 'S', 1),
  12.            Block(['O..',
  13.                   'OO.',
  14.                   '.O.'], Point(0, 0), Point(1, 2), 'S', 0)]
  15. # Z形方块
  16. Z_BLOCK = [Block(['OO.',
  17.                   '.OO',
  18.                   '...'], Point(0, 0), Point(2, 1), 'Z', 1),
  19.            Block(['.O.',
  20.                   'OO.',
  21.                   'O..'], Point(0, 0), Point(1, 2), 'Z', 0)]
  22. # I型方块
  23. I_BLOCK = [Block(['.O..',
  24.                   '.O..',
  25.                   '.O..',
  26.                   '.O..'], Point(1, 0), Point(1, 3), 'I', 1),
  27.            Block(['....',
  28.                   '....',
  29.                   'OOOO',
  30.                   '....'], Point(0, 2), Point(3, 2), 'I', 0)]
  31. # O型方块
  32. O_BLOCK = [Block(['OO',
  33.                   'OO'], Point(0, 0), Point(1, 1), 'O', 0)]
  34. # J型方块
  35. J_BLOCK = [Block(['O..',
  36.                   'OOO',
  37.                   '...'], Point(0, 0), Point(2, 1), 'J', 1),
  38.            Block(['.OO',
  39.                   '.O.',
  40.                   '.O.'], Point(1, 0), Point(2, 2), 'J', 2),
  41.            Block(['...',
  42.                   'OOO',
  43.                   '..O'], Point(0, 1), Point(2, 2), 'J', 3),
  44.            Block(['.O.',
  45.                   '.O.',
  46.                   'OO.'], Point(0, 0), Point(1, 2), 'J', 0)]
  47. # L型方块
  48. L_BLOCK = [Block(['..O',
  49.                   'OOO',
  50.                   '...'], Point(0, 0), Point(2, 1), 'L', 1),
  51.            Block(['.O.',
  52.                   '.O.',
  53.                   '.OO'], Point(1, 0), Point(2, 2), 'L', 2),
  54.            Block(['...',
  55.                   'OOO',
  56.                   'O..'], Point(0, 1), Point(2, 2), 'L', 3),
  57.            Block(['OO.',
  58.                   '.O.',
  59.                   '.O.'], Point(0, 0), Point(1, 2), 'L', 0)]
  60. # T型方块
  61. T_BLOCK = [Block(['.O.',
  62.                   'OOO',
  63.                   '...'], Point(0, 0), Point(2, 1), 'T', 1),
  64.            Block(['.O.',
  65.                   '.OO',
  66.                   '.O.'], Point(1, 0), Point(2, 2), 'T', 2),
  67.            Block(['...',
  68.                   'OOO',
  69.                   '.O.'], Point(0, 1), Point(2, 2), 'T', 3),
  70.            Block(['.O.',
  71.                   'OO.',
  72.                   '.O.'], Point(0, 0), Point(1, 2), 'T', 0)]

  73. BLOCKS = {'O': O_BLOCK,
  74.           'I': I_BLOCK,
  75.           'Z': Z_BLOCK,
  76.           'T': T_BLOCK,
  77.           'L': L_BLOCK,
  78.           'S': S_BLOCK,
  79.           'J': J_BLOCK}


  80. def get_block():
  81.     block_name = random.choice('OIZTLSJ')
  82.     b = BLOCKS[block_name]
  83.     idx = random.randint(0, len(b) - 1)
  84.     return b[idx]


  85. def get_next_block(block):
  86.     b = BLOCKS[block.name]
  87.     return b[block.next]

现在加载block模块, 画下一个提示的方块及绘制一个下落的方块(发现上面那个网格线1的宽度可以不用特意计算)

点击(此处)折叠或打开

  1. import sys
  2. import pygame
  3. import blocks
  4. import time

  5. SIZE = 30 # 每个小方格大小

  6. BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
  7. BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量

  8. BORDER_WIDTH = 4 # 游戏区边框分割线宽度
  9. BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色

  10. INFO_WIDTH = 160 # 信息区域宽度

  11. BG_COLOR = (60, 60, 60) # 背景色

  12. BLACK = (0, 0, 0) # 网格线颜色
  13. WIDTH = 1 # 网格线宽度

  14. SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
  15. SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高


  16. # 将文字划到画布上指定位置
  17. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  18.     imgText = font.render(text, True, fcolor)
  19.     screen.blit(imgText, (x, y))


  20. def main():
  21.     pygame.init()
  22.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  23.     pygame.display.set_caption('俄罗斯方块')
  24.     
  25.     font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
  26.     font1_width, font1_height = font1.size('得分:') # 字体高度
  27.     font1_height= int(font1_height)
  28.     font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
  29.     
  30.     score = 0 # 得分
  31.     removes = 0 # 消除行数
  32.     orispeed = 0.5 # 原始速度
  33.     speed = orispeed # 当前速度

  34.     cur_block = None # 当前下落方块
  35.     cur_pos_x, cur_pos_y = 0, 0
  36.     next_block = None # 下一个方块
  37.     block_color = (20, 128, 200) # 方块颜色
  38.     
  39.     last_drop_time = 0
  40.     
  41.     while True:
  42.         for event in pygame.event.get():
  43.             if event.type == pygame.QUIT:
  44.                 pygame.quit()
  45.                 sys.exit()
  46.             elif event.type == pygame.KEYDOWN:
  47.                 if event.key == pygame.K_RETURN:
  48.                     cur_block = blocks.get_block()
  49.                     next_block = blocks.get_block()
  50.                     cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  51.                 
  52.                 
  53.         # 填充背景色
  54.         screen.fill(BG_COLOR)
  55.         # 画游戏区域分隔线
  56.         pygame.draw.line(screen, BORDER_COLOR,
  57.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
  58.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
  59.               
  60.         # 画网格线 竖线
  61.         for x in range(BLOCK_WIDTH_NUM+1):
  62.             pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
  63.         # 画网格线 横线
  64.         for y in range(BLOCK_HEIGHT_NUM+1):
  65.             pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)

  66.         
  67.         # 画当前下落方块
  68.         if cur_block:
  69.             for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  70.                 for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  71.                     if cur_block.template[i][j] != '.':
  72.                         pygame.draw.rect(screen, block_color,
  73.                                          ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
  74.                                          0)
  75.         cur_drop_time = time.time()
  76.         if cur_block and cur_drop_time - last_drop_time > speed:
  77.             last_drop_time = cur_drop_time
  78.             cur_pos_y += 1
  79.         
  80.         print_text(screen, font1, font_pos_x, 10, f'得分: ')
  81.         print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
  82.         print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
  83.         print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
  84.         print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
  85.         print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
  86.         print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')

  87.         if next_block:
  88.             next_block_y = 40 + (font1_height + 6) * 7
  89.             for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
  90.                 for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
  91.                     if next_block.template[i][j] != '.':
  92.                         pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)

  93.         
  94.         pygame.display.flip()


  95. if __name__ == '__main__':
  96.     main()
现在运行后, 方块显示出来了, 并且有当前的方块会下落,添加左右按键控制当前方块的左右移动,并考虑到方块不能移出游戏边界及到底要停靠,加上初步的判断条件

点击(此处)折叠或打开

  1. import sys
  2. import pygame
  3. import blocks
  4. import time

  5. SIZE = 30 # 每个小方格大小

  6. BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
  7. BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量

  8. BORDER_WIDTH = 4 # 游戏区边框分割线宽度
  9. BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色

  10. INFO_WIDTH = 160 # 信息区域宽度

  11. BG_COLOR = (60, 60, 60) # 背景色

  12. BLACK = (0, 0, 0) # 网格线颜色
  13. WIDTH = 1 # 网格线宽度

  14. SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
  15. SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高


  16. # 将文字划到画布上指定位置
  17. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  18.     imgText = font.render(text, True, fcolor)
  19.     screen.blit(imgText, (x, y))


  20. def main():
  21.     pygame.init()
  22.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  23.     pygame.display.set_caption('俄罗斯方块')
  24.     
  25.     font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
  26.     font1_width, font1_height = font1.size('得分:') # 字体高度
  27.     font1_height= int(font1_height)
  28.     font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
  29.     
  30.     score = 0 # 得分
  31.     removes = 0 # 消除行数
  32.     orispeed = 0.5 # 原始速度
  33.     speed = orispeed # 当前速度

  34.     cur_block = None # 当前下落方块
  35.     cur_pos_x, cur_pos_y = 0, 0
  36.     next_block = None # 下一个方块
  37.     block_color = (20, 128, 200) # 方块颜色
  38.     
  39.     last_press_time = 0
  40.     last_drop_time = 0
  41.     
  42.     
  43.     def _judge(pos_x, pos_y, block):
  44.         if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
  45.             return False
  46.         return True

  47.     
  48.     
  49.     while True:
  50.         for event in pygame.event.get():
  51.             if event.type == pygame.QUIT:
  52.                 pygame.quit()
  53.                 sys.exit()
  54.             elif event.type == pygame.KEYDOWN:
  55.                 if event.key == pygame.K_RETURN:
  56.                     cur_block = blocks.get_block()
  57.                     next_block = blocks.get_block()
  58.                     cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  59.                 
  60.                 
  61.         if event.type == pygame.KEYDOWN:
  62.             if event.key == pygame.K_LEFT:
  63.                 if cur_block and time.time() - last_press_time > 0.1:
  64.                     last_press_time = time.time()
  65.                     if cur_pos_x > - cur_block.start_pos.X:
  66.                         if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
  67.                             cur_pos_x -= 1
  68.             if event.key == pygame.K_RIGHT:
  69.                 if cur_block and time.time() - last_press_time > 0.1:
  70.                     last_press_time = time.time()
  71.                     if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
  72.                         if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
  73.                             cur_pos_x += 1
  74.         # 填充背景色
  75.         screen.fill(BG_COLOR)
  76.         # 画游戏区域分隔线
  77.         pygame.draw.line(screen, BORDER_COLOR,
  78.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
  79.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
  80.               
  81.         # 画网格线 竖线
  82.         for x in range(BLOCK_WIDTH_NUM+1):
  83.             pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
  84.         # 画网格线 横线
  85.         for y in range(BLOCK_HEIGHT_NUM+1):
  86.             pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)

  87.         
  88.         # 画当前下落方块
  89.         if cur_block:
  90.             for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  91.                 for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  92.                     if cur_block.template[i][j] != '.':
  93.                         pygame.draw.rect(screen, block_color,
  94.                                          ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
  95.                                          0)
  96.         cur_drop_time = time.time()
  97.         if cur_block and cur_drop_time - last_drop_time > speed:
  98.             if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  99.                 pass # 下一个方块在上方显示
  100.             else:
  101.                 last_drop_time = cur_drop_time
  102.                 cur_pos_y += 1
  103.         
  104.         print_text(screen, font1, font_pos_x, 10, f'得分: ')
  105.         print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
  106.         print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
  107.         print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
  108.         print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
  109.         print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
  110.         print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')

  111.         if next_block:
  112.             next_block_y = 40 + (font1_height + 6) * 7
  113.             for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
  114.                 for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
  115.                     if next_block.template[i][j] != '.':
  116.                         pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)

  117.         
  118.         pygame.display.flip()


  119. if __name__ == '__main__':
  120.     main()
现在运行后如下如所示:

但是现在的情况是, 当前方块下落到最底部时不会继续下落, 但是一直能左右移动,而且还未处理当前方块停靠后的下一个方块下落,这是因为对方块停靠处理方式有待完善,判断方块停靠条件,一是刚开始的时候不能一直下落掉出屏幕底部,而是当已有方块时,当前方块不能和已有方块有重叠的部分

将代码

点击(此处)折叠或打开

  1. pass # 下一个方块在上方显示
改成 

点击(此处)折叠或打开

  1. cur_block = next_block
  2. next_block = blocks.get_block()
  3. cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
可以有下一个方块从上方继续下落, 但是未解决已停靠在最下方的方块显示的问题
新增一个游戏区的坐标变量,再将新方块生成写进一个方法,初步类似下面这样:

点击(此处)折叠或打开

  1. import sys
  2. import pygame
  3. import blocks
  4. import time

  5. SIZE = 30 # 每个小方格大小

  6. BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
  7. BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量

  8. BORDER_WIDTH = 4 # 游戏区边框分割线宽度
  9. BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色

  10. INFO_WIDTH = 160 # 信息区域宽度

  11. BG_COLOR = (60, 60, 60) # 背景色

  12. BLACK = (0, 0, 0) # 网格线颜色
  13. WIDTH = 1 # 网格线宽度

  14. SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
  15. SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高


  16. # 将文字划到画布上指定位置
  17. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  18.     imgText = font.render(text, True, fcolor)
  19.     screen.blit(imgText, (x, y))


  20. def main():
  21.     pygame.init()
  22.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  23.     pygame.display.set_caption('俄罗斯方块')
  24.     
  25.     font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
  26.     font1_width, font1_height = font1.size('得分:') # 字体高度
  27.     font1_height= int(font1_height)
  28.     font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
  29.     
  30.     score = 0 # 得分
  31.     removes = 0 # 消除行数
  32.     orispeed = 0.5 # 原始速度
  33.     speed = orispeed # 当前速度

  34.     game_area = None # 整个游戏区域

  35.     cur_block = None # 当前下落方块
  36.     cur_pos_x, cur_pos_y = 0, 0
  37.     next_block = None # 下一个方块
  38.     block_color = (20, 128, 200) # 方块颜色
  39.     
  40.     last_press_time = 0
  41.     last_drop_time = 0

  42.     game_area = None # 整个游戏区域
  43.     
  44.     def _judge(pos_x, pos_y, block):
  45.         if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
  46.             return False
  47.         return True


  48.     def _dock():
  49.         nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y
  50.         
  51.         # 记录现有方块
  52.         for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  53.             for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  54.                 if cur_block.template[_i][_j] != '.':
  55.                     game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
  56.                     
  57.         cur_block = next_block
  58.         next_block = blocks.get_block()
  59.         cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  60.     
  61.     while True:
  62.         for event in pygame.event.get():
  63.             if event.type == pygame.QUIT:
  64.                 pygame.quit()
  65.                 sys.exit()
  66.             elif event.type == pygame.KEYDOWN:
  67.                 if event.key == pygame.K_RETURN:
  68.                     game_area = [['.'] * BLOCK_WIDTH_NUM for _ in range(BLOCK_HEIGHT_NUM)]
  69.                     cur_block = blocks.get_block()
  70.                     next_block = blocks.get_block()
  71.                     cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  72.                 
  73.                 
  74.         if event.type == pygame.KEYDOWN:
  75.             if event.key == pygame.K_LEFT:
  76.                 if cur_block and time.time() - last_press_time > 0.1:
  77.                     last_press_time = time.time()
  78.                     if cur_pos_x > - cur_block.start_pos.X:
  79.                         if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
  80.                             cur_pos_x -= 1
  81.             if event.key == pygame.K_RIGHT:
  82.                 if cur_block and time.time() - last_press_time > 0.1:
  83.                     last_press_time = time.time()
  84.                     if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
  85.                         if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
  86.                             cur_pos_x += 1
  87.         # 填充背景色
  88.         screen.fill(BG_COLOR)
  89.         # 画游戏区域分隔线
  90.         pygame.draw.line(screen, BORDER_COLOR,
  91.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
  92.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
  93.               
  94.         # 画网格线 竖线
  95.         for x in range(BLOCK_WIDTH_NUM+1):
  96.             pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
  97.         # 画网格线 横线
  98.         for y in range(BLOCK_HEIGHT_NUM+1):
  99.             pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)


  100.         # 画现有方块
  101.         if game_area:
  102.             for i, row in enumerate(game_area):
  103.                 for j, cell in enumerate(row):
  104.                     if cell != '.':
  105.                         pygame.draw.rect(screen, block_color, (j * SIZE, i * SIZE, SIZE, SIZE), 0)
  106.         
  107.         # 画当前下落方块
  108.         if cur_block:
  109.             for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  110.                 for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  111.                     if cur_block.template[i][j] != '.':
  112.                         pygame.draw.rect(screen, block_color,
  113.                                          ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
  114.                                          0)
  115.         cur_drop_time = time.time()
  116.         if cur_block and cur_drop_time - last_drop_time > speed:
  117.             if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  118.                 _dock() # 下一个方块在上方显示
  119.             else:
  120.                 last_drop_time = cur_drop_time
  121.                 cur_pos_y += 1
  122.         
  123.         print_text(screen, font1, font_pos_x, 10, f'得分: ')
  124.         print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
  125.         print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
  126.         print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
  127.         print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
  128.         print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
  129.         print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')

  130.         if next_block:
  131.             next_block_y = 40 + (font1_height + 6) * 7
  132.             for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
  133.                 for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
  134.                     if next_block.template[i][j] != '.':
  135.                         pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)

  136.         
  137.         pygame.display.flip()


  138. if __name__ == '__main__':
  139.     main()
现在可以保存下现有的停靠方块了

但是判断停靠的方法还有问题, 下落方块和已停靠的方块不能重叠穿插,故修正_judge方法:

点击(此处)折叠或打开

  1. def _judge(pos_x, pos_y, block):
  2.         nonlocal game_area
  3.         if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
  4.             return False
  5.         for _i in range(block.start_pos.Y, block.end_pos.Y + 1): # 方块模板不能与现有方块重叠
  6.             for _j in range(block.start_pos.X, block.end_pos.X + 1):
  7.                 if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
  8.                     return False
  9.         return True
此时已修正上面的问题

但会发现当长度方向上堆满方块后, 程序还一直在运行,此时应该结束游戏,就是不应该再生产新的方块了,故判断放在生成新方块的方法里,这你可以给生成新方块方法一个返回值,或者增加一个全局变量来告诉主循环,此时应退出循环结束程序了

点击(此处)折叠或打开

  1. import sys
  2. import pygame
  3. import blocks
  4. import time

  5. SIZE = 30 # 每个小方格大小

  6. BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
  7. BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量

  8. BORDER_WIDTH = 4 # 游戏区边框分割线宽度
  9. BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色

  10. INFO_WIDTH = 160 # 信息区域宽度

  11. BG_COLOR = (60, 60, 60) # 背景色

  12. BLACK = (0, 0, 0) # 网格线颜色
  13. WIDTH = 1 # 网格线宽度

  14. SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
  15. SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高


  16. # 将文字划到画布上指定位置
  17. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  18.     imgText = font.render(text, True, fcolor)
  19.     screen.blit(imgText, (x, y))


  20. def main():
  21.     pygame.init()
  22.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  23.     pygame.display.set_caption('俄罗斯方块')
  24.     
  25.     font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
  26.     font1_width, font1_height = font1.size('得分:') # 字体高度
  27.     font1_height= int(font1_height)
  28.     font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
  29.     
  30.     score = 0 # 得分
  31.     removes = 0 # 消除行数
  32.     orispeed = 0.5 # 原始速度
  33.     speed = orispeed # 当前速度

  34.     game_area = None # 整个游戏区域

  35.     cur_block = None # 当前下落方块
  36.     cur_pos_x, cur_pos_y = 0, 0
  37.     next_block = None # 下一个方块
  38.     block_color = (20, 128, 200) # 方块颜色
  39.     
  40.     last_press_time = 0
  41.     last_drop_time = 0

  42.     game_area = None # 整个游戏区域
  43.     
  44.     game_over = True
  45.     
  46.     def _judge(pos_x, pos_y, block):
  47.         nonlocal game_area
  48.         if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
  49.             return False
  50.         for _i in range(block.start_pos.Y, block.end_pos.Y + 1): # 方块模板不能与现有方块重叠
  51.             for _j in range(block.start_pos.X, block.end_pos.X + 1):
  52.                 if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
  53.                     return False
  54.         return True


  55.     def _dock():
  56.         nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over
  57.         
  58.         # 记录现有方块
  59.         for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  60.             for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  61.                 if cur_block.template[_i][_j] != '.':
  62.                     game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
  63.                     
  64.         if cur_pos_y + cur_block.start_pos.Y <= 0:
  65.             game_over = True
  66.                     
  67.         cur_block = next_block
  68.         next_block = blocks.get_block()
  69.         cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  70.     
  71.     while True:
  72.         for event in pygame.event.get():
  73.             if event.type == pygame.QUIT:
  74.                 pygame.quit()
  75.                 sys.exit()
  76.             elif event.type == pygame.KEYDOWN:
  77.                 if event.key == pygame.K_RETURN:
  78.                     if game_over:
  79.                         game_over = False
  80.                         game_area = [['.'] * BLOCK_WIDTH_NUM for _ in range(BLOCK_HEIGHT_NUM)]
  81.                         cur_block = blocks.get_block()
  82.                         next_block = blocks.get_block()
  83.                         cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  84.                 
  85.                 
  86.         if event.type == pygame.KEYDOWN:
  87.             if event.key == pygame.K_LEFT:
  88.                 if cur_block and time.time() - last_press_time > 0.1:
  89.                     last_press_time = time.time()
  90.                     if cur_pos_x > - cur_block.start_pos.X:
  91.                         if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
  92.                             cur_pos_x -= 1
  93.             if event.key == pygame.K_RIGHT:
  94.                 if cur_block and time.time() - last_press_time > 0.1:
  95.                     last_press_time = time.time()
  96.                     if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
  97.                         if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
  98.                             cur_pos_x += 1
  99.         # 填充背景色
  100.         screen.fill(BG_COLOR)
  101.         # 画游戏区域分隔线
  102.         pygame.draw.line(screen, BORDER_COLOR,
  103.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
  104.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
  105.               
  106.         # 画网格线 竖线
  107.         for x in range(BLOCK_WIDTH_NUM+1):
  108.             pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
  109.         # 画网格线 横线
  110.         for y in range(BLOCK_HEIGHT_NUM+1):
  111.             pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)


  112.         # 画现有方块
  113.         if game_area:
  114.             for i, row in enumerate(game_area):
  115.                 for j, cell in enumerate(row):
  116.                     if cell != '.':
  117.                         pygame.draw.rect(screen, block_color, (j * SIZE, i * SIZE, SIZE, SIZE), 0)
  118.         
  119.         # 画当前下落方块
  120.         if cur_block:
  121.             for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  122.                 for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  123.                     if cur_block.template[i][j] != '.':
  124.                         pygame.draw.rect(screen, block_color,
  125.                                          ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
  126.                                          0)
  127.         if not game_over:
  128.             cur_drop_time = time.time()
  129.             if cur_block and cur_drop_time - last_drop_time > speed:
  130.                 if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  131.                     _dock() # 下一个方块在上方显示
  132.                 else:
  133.                     last_drop_time = cur_drop_time
  134.                     cur_pos_y += 1
  135.         
  136.         print_text(screen, font1, font_pos_x, 10, f'得分: ')
  137.         print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
  138.         print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
  139.         print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
  140.         print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
  141.         print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
  142.         print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')

  143.         if next_block:
  144.             next_block_y = 40 + (font1_height + 6) * 7
  145.             for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
  146.                 for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
  147.                     if next_block.template[i][j] != '.':
  148.                         pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)

  149.         
  150.         pygame.display.flip()


  151. if __name__ == '__main__':
  152.     main()
现在的这个版本,一些基本的性能:生成方块, 方块左右移动,方块的停靠,长度方向上堆满游戏结束等已经完成
现在加上上键是改变方块形状的功能, 只需在event的for循环中对按下按键事件增加一个上键按钮的判断就好了

点击(此处)折叠或打开

  1. elif event.key in (pygame.K_w, pygame.K_UP):
  2.      if 0 <= cur_pos_x <= BLOCK_WIDTH_NUM - len(cur_block.template[0]):
  3.           _next_block = blocks.get_next_block(cur_block)
  4.           if _judge(cur_pos_x, cur_pos_y, _next_block):
  5.                 cur_block = _next_block
接下来就是对满行的方格进行消除,优化_dock方法, 在判断完垂直方向还有空间游戏未结束后,判断是否有满行的方格可以消除

点击(此处)折叠或打开

  1.         else:
  2.             # 计算消除
  3.             remove_idxs = []
  4.             for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  5.                 if all(_x == '0' for _x in game_area[cur_pos_y + _i]):
  6.                     remove_idxs.append(cur_pos_y + _i)
  7.             if remove_idxs:
  8.                 # 消除
  9.                 _i = _j = remove_idxs[-1]
  10.                 while _i >= 0:
  11.                     while _j in remove_idxs:
  12.                         _j -= 1
  13.                     if _j < 0:
  14.                         game_area[_i] = ['.'] * BLOCK_WIDTH
  15.                     else:
  16.                         game_area[_i] = game_area[_j]
  17.                     _i -= 1
  18.                     _j -= 1
到这里基本能玩起来了, 最后做细节优化,增加下键可以加快下落速度,增加暂停功能,增加直落功能, 增加信息面板的一些信息等等,下面是最终版本

点击(此处)折叠或打开

  1. import sys
  2. import pygame
  3. import blocks
  4. import time

  5. SIZE = 30 # 每个小方格大小

  6. BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
  7. BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量

  8. BORDER_WIDTH = 4 # 游戏区边框分割线宽度
  9. BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色

  10. INFO_WIDTH = 160 # 信息区域宽度

  11. BG_COLOR = (60, 60, 60) # 背景色

  12. BLACK = (0, 0, 0) # 网格线颜色
  13. WIDTH = 1 # 网格线宽度

  14. SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
  15. SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高


  16. # 将文字划到画布上指定位置
  17. def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
  18.     imgText = font.render(text, True, fcolor)
  19.     screen.blit(imgText, (x, y))


  20. def main():
  21.     pygame.init()
  22.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  23.     pygame.display.set_caption('俄罗斯方块')
  24.     
  25.     font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
  26.     font1_width, font1_height = font1.size('得分:') # 字体高度
  27.     font1_height= int(font1_height)
  28.     font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
  29.     
  30.     fontgg = pygame.font.Font(None, 72)
  31.     gameover_size = fontgg.size('GAME OVER')
  32.     
  33.     score = 0 # 得分
  34.     removes = 0 # 消除行数
  35.     orispeed = 0.5 # 原始速度
  36.     speed = orispeed # 当前速度

  37.     game_area = None # 整个游戏区域

  38.     cur_block = None # 当前下落方块
  39.     cur_pos_x, cur_pos_y = 0, 0
  40.     next_block = None # 下一个方块
  41.     block_color = (20, 128, 200) # 方块颜色
  42.     
  43.     last_press_time = 0
  44.     last_drop_time = 0

  45.     game_area = None # 整个游戏区域
  46.     
  47.     game_over = True
  48.     
  49.     pause = False # 暂停
  50.     start = False # 是否开始,开始游戏后game_over为True游戏就是真正结束
  51.     last_drop_time = None # 上次下落时间
  52.     last_press_time = None # 上次按键时间
  53.     last_key_down = False
  54.     
  55.     def _judge(pos_x, pos_y, block):
  56.         nonlocal game_area
  57.         if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
  58.             return False
  59.         for _i in range(block.start_pos.Y, block.end_pos.Y + 1): # 方块模板不能与现有方块重叠
  60.             for _j in range(block.start_pos.X, block.end_pos.X + 1):
  61.                 if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
  62.                     return False
  63.         return True


  64.     def _dock():
  65.         nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over, score, speed, removes
  66.         
  67.         # 记录现有方块
  68.         for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  69.             for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  70.                 if cur_block.template[_i][_j] != '.':
  71.                     game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
  72.                     
  73.         if cur_pos_y + cur_block.start_pos.Y <= 0:
  74.             game_over = True
  75.         else:
  76.             # 计算消除
  77.             remove_idxs = []
  78.             for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  79.                 if all(_x == '0' for _x in game_area[cur_pos_y + _i]):
  80.                     remove_idxs.append(cur_pos_y + _i)
  81.             if remove_idxs:
  82.                 # 计算得分
  83.                 remove_count = len(remove_idxs)
  84.                 removes += remove_count;
  85.                 if remove_count == 1:
  86.                     score += 100
  87.                 elif remove_count == 2:
  88.                     score += 300
  89.                 elif remove_count == 3:
  90.                     score += 700
  91.                 elif remove_count == 4:
  92.                     score += 1500
  93.                 speed = orispeed - 0.03 * (score // 10000)
  94.                 # 消除
  95.                 _i = _j = remove_idxs[-1]
  96.                 while _i >= 0:
  97.                     while _j in remove_idxs:
  98.                         _j -= 1
  99.                     if _j < 0:
  100.                         game_area[_i] = ['.'] * BLOCK_WIDTH_NUM
  101.                     else:
  102.                         game_area[_i] = game_area[_j]
  103.                     _i -= 1
  104.                     _j -= 1
  105.                     
  106.         cur_block = next_block
  107.         next_block = blocks.get_block()
  108.         cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  109.     
  110.     while True:
  111.         for event in pygame.event.get():
  112.             if event.type == pygame.QUIT:
  113.                 pygame.quit()
  114.                 sys.exit()
  115.             elif event.type == pygame.KEYDOWN:
  116.                 if event.key == pygame.K_RETURN:
  117.                     last_key_down = False
  118.                     if game_over:
  119.                         game_over = False
  120.                         start = True
  121.                         score = 0
  122.                         speed = orispeed
  123.                         removes = 0
  124.                         last_drop_time = time.time()
  125.                         last_press_time = time.time()
  126.                         game_area = [['.'] * BLOCK_WIDTH_NUM for _ in range(BLOCK_HEIGHT_NUM)]
  127.                         cur_block = blocks.get_block()
  128.                         next_block = blocks.get_block()
  129.                         cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
  130.                     else:
  131.                         pause = not pause
  132.                 elif event.key in (pygame.K_w, pygame.K_UP):
  133.                     last_key_down = False
  134.                     if 0 <= cur_pos_x <= BLOCK_WIDTH_NUM - len(cur_block.template[0]):
  135.                         _next_block = blocks.get_next_block(cur_block)
  136.                         if _judge(cur_pos_x, cur_pos_y, _next_block):
  137.                             cur_block = _next_block
  138.                 elif event.key == pygame.K_SPACE:
  139.                     if not last_key_down or time.time() - last_press_time > 0.2 : # 连续两字短时间按键触发
  140.                         last_press_time = time.time()
  141.                         last_key_down = True
  142.                         while _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  143.                             cur_pos_y += 1
  144.                             last_drop_time = time.time()
  145.                         else:
  146.                             _dock()
  147.                             
  148.         if event.type == pygame.KEYDOWN:
  149.             if event.key == pygame.K_LEFT:
  150.                 last_key_down = False
  151.                 if not game_over and not pause:
  152.                     if time.time() - last_press_time > 0.1:
  153.                         last_press_time = time.time()
  154.                         if cur_pos_x > - cur_block.start_pos.X:
  155.                             if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
  156.                                 cur_pos_x -= 1
  157.             if event.key == pygame.K_RIGHT:
  158.                 last_key_down = False
  159.                 if not game_over and not pause:
  160.                     if time.time() - last_press_time > 0.1:
  161.                         last_press_time = time.time()
  162.                         if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
  163.                             if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
  164.                                 cur_pos_x += 1
  165.             if event.key == pygame.K_DOWN:
  166.                 last_key_down = False
  167.                 if not game_over and not pause:
  168.                     if time.time() - last_press_time > 0.1 :
  169.                         last_press_time = time.time()
  170.                         if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  171.                             _dock()
  172.                         else:
  173.                             last_drop_time = time.time()
  174.                             cur_pos_y += 1
  175.         # 填充背景色
  176.         screen.fill(BG_COLOR)
  177.         # 画游戏区域分隔线
  178.         pygame.draw.line(screen, BORDER_COLOR,
  179.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
  180.                          (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)

  181.         # 画现有方块
  182.         if game_area:
  183.             for i, row in enumerate(game_area):
  184.                 for j, cell in enumerate(row):
  185.                     if cell != '.':
  186.                         pygame.draw.rect(screen, block_color, (j * SIZE, i * SIZE, SIZE, SIZE), 0)
  187.                         
  188.         # 画网格线 竖线
  189.         for x in range(BLOCK_WIDTH_NUM+1):
  190.             pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
  191.         # 画网格线 横线
  192.         for y in range(BLOCK_HEIGHT_NUM+1):
  193.             pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)
  194.         
  195.         # 画当前下落方块
  196.         if cur_block:
  197.             for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
  198.                 for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
  199.                     if cur_block.template[i][j] != '.':
  200.                         pygame.draw.rect(screen, block_color,
  201.                                          ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
  202.                                          0)
  203.         if not game_over:
  204.             cur_drop_time = time.time()
  205.             if cur_drop_time - last_drop_time > speed:
  206.                 if not pause:
  207.                     if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
  208.                         _dock()
  209.                     else:
  210.                         last_drop_time = cur_drop_time
  211.                         cur_pos_y += 1
  212.         else:
  213.             if start:
  214.                 print_text(screen, fontgg,
  215.                            (SCREEN_WIDTH - gameover_size[0]) // 2, (SCREEN_HEIGHT - gameover_size[1]) // 2,
  216.                            'GAME OVER', (200, 30, 30))
  217.         
  218.         print_text(screen, font1, font_pos_x, 10, f'得分: ')
  219.         print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
  220.         print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
  221.         print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
  222.         print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
  223.         print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
  224.         print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')

  225.         if next_block:
  226.             next_block_y = 40 + (font1_height + 6) * 7
  227.             for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
  228.                 for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
  229.                     if next_block.template[i][j] != '.':
  230.                         pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)

  231.         
  232.         pygame.display.flip()


  233. if __name__ == '__main__':
  234.     main()








阅读(1623) | 评论(0) | 转发(0) |
2

上一篇:awk 做全排列

下一篇:没有了

给主人留下些什么吧!~~