用的python的pygame模块,先画一个简单的屏幕
点击(此处)折叠或打开
- import sys
- import pygame
- def main():
- pygame.init()
- screen = pygame.display.set_mode((500, 700)) # 画布大小
- pygame.display.set_caption('俄罗斯方块') # 设置标题样式
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if __name__ == '__main__':
- main()
现在是把上面画布分为两大部分,左边为游戏区,为了更直观看到方块,一齐画上方块的网格线;右边为游戏信息区,包括积分、消除行数、下一个方块提示等信息;中间用一条竖线区分开来
点击(此处)折叠或打开
- import sys
- import pygame
- SIZE = 30 # 每个小方格大小
- BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
- BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量
- BORDER_WIDTH = 4 # 游戏区边框分割线宽度
- BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色
- INFO_WIDTH = 160 # 信息区域宽度
- BG_COLOR = (60, 60, 60) # 背景色
- BLACK = (0, 0, 0) # 网格线颜色
- WIDTH = 1 # 网格线宽度
- SCREEN_WIDTH = (SIZE + WIDTH) * BLOCK_WIDTH_NUM + WIDTH + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
- SCREEN_HEIGHT = (SIZE + WIDTH) * BLOCK_HEIGHT_NUM # 游戏屏幕的高
- # 将文字划到画布上指定位置
- def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
- imgText = font.render(text, True, fcolor)
- screen.blit(imgText, (x, y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('俄罗斯方块')
-
- font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
- font1_width, font1_height = font1.size('得分:') # 字体高度
- font1_height= int(font1_height)
- font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
-
- score = 0 # 得分
- removes = 0 # 消除行数
- orispeed = 0.5 # 原始速度
- speed = orispeed # 当前速度
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
-
-
- # 填充背景色
- screen.fill(BG_COLOR)
- # 画游戏区域分隔线
- pygame.draw.line(screen, BORDER_COLOR,
- ((SIZE + WIDTH) * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
- ((SIZE + WIDTH) * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
-
- # 画网格线 竖线
- for x in range(BLOCK_WIDTH_NUM+1):
- pygame.draw.line(screen, BLACK, (x * (SIZE + WIDTH), 0), (x * (SIZE + WIDTH), SCREEN_HEIGHT), 1)
- # 画网格线 横线
- for y in range(BLOCK_HEIGHT_NUM+1):
- pygame.draw.line(screen, BLACK, (0, y * (SIZE + WIDTH)), (BLOCK_WIDTH_NUM * (SIZE + WIDTH), y * (SIZE + WIDTH)), 1)
-
- print_text(screen, font1, font_pos_x, 10, f'得分: ')
- print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
- print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
- print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
- print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
- print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
- print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
-
- pygame.display.flip()
- if __name__ == '__main__':
- main()
接下来就是各种方块的绘制了
先统计方块种类,分别有 T 型、L 型、J 型、I 型、Z 型、S 型、正方形,网上找的block模块直接拿来用
点击(此处)折叠或打开
- import random
- from collections import namedtuple
- Point = namedtuple('Point', 'X Y')
- Shape = namedtuple('Shape', 'X Y Width Height')
- Block = namedtuple('Block', 'template start_pos end_pos name next')
- # 方块形状的设计,我最初我是做成 4 × 4,因为长宽最长都是4,这样旋转的时候就不考虑怎么转了,就是从一个图形替换成另一个
- # 其实要实现这个功能,只需要固定左上角的坐标就可以了
- # S形方块
- S_BLOCK = [Block(['.OO',
- 'OO.',
- '...'], Point(0, 0), Point(2, 1), 'S', 1),
- Block(['O..',
- 'OO.',
- '.O.'], Point(0, 0), Point(1, 2), 'S', 0)]
- # Z形方块
- Z_BLOCK = [Block(['OO.',
- '.OO',
- '...'], Point(0, 0), Point(2, 1), 'Z', 1),
- Block(['.O.',
- 'OO.',
- 'O..'], Point(0, 0), Point(1, 2), 'Z', 0)]
- # I型方块
- I_BLOCK = [Block(['.O..',
- '.O..',
- '.O..',
- '.O..'], Point(1, 0), Point(1, 3), 'I', 1),
- Block(['....',
- '....',
- 'OOOO',
- '....'], Point(0, 2), Point(3, 2), 'I', 0)]
- # O型方块
- O_BLOCK = [Block(['OO',
- 'OO'], Point(0, 0), Point(1, 1), 'O', 0)]
- # J型方块
- J_BLOCK = [Block(['O..',
- 'OOO',
- '...'], Point(0, 0), Point(2, 1), 'J', 1),
- Block(['.OO',
- '.O.',
- '.O.'], Point(1, 0), Point(2, 2), 'J', 2),
- Block(['...',
- 'OOO',
- '..O'], Point(0, 1), Point(2, 2), 'J', 3),
- Block(['.O.',
- '.O.',
- 'OO.'], Point(0, 0), Point(1, 2), 'J', 0)]
- # L型方块
- L_BLOCK = [Block(['..O',
- 'OOO',
- '...'], Point(0, 0), Point(2, 1), 'L', 1),
- Block(['.O.',
- '.O.',
- '.OO'], Point(1, 0), Point(2, 2), 'L', 2),
- Block(['...',
- 'OOO',
- 'O..'], Point(0, 1), Point(2, 2), 'L', 3),
- Block(['OO.',
- '.O.',
- '.O.'], Point(0, 0), Point(1, 2), 'L', 0)]
- # T型方块
- T_BLOCK = [Block(['.O.',
- 'OOO',
- '...'], Point(0, 0), Point(2, 1), 'T', 1),
- Block(['.O.',
- '.OO',
- '.O.'], Point(1, 0), Point(2, 2), 'T', 2),
- Block(['...',
- 'OOO',
- '.O.'], Point(0, 1), Point(2, 2), 'T', 3),
- Block(['.O.',
- 'OO.',
- '.O.'], Point(0, 0), Point(1, 2), 'T', 0)]
- BLOCKS = {'O': O_BLOCK,
- 'I': I_BLOCK,
- 'Z': Z_BLOCK,
- 'T': T_BLOCK,
- 'L': L_BLOCK,
- 'S': S_BLOCK,
- 'J': J_BLOCK}
- def get_block():
- block_name = random.choice('OIZTLSJ')
- b = BLOCKS[block_name]
- idx = random.randint(0, len(b) - 1)
- return b[idx]
- def get_next_block(block):
- b = BLOCKS[block.name]
- return b[block.next]
现在加载block模块, 画下一个提示的方块及绘制一个下落的方块(发现上面那个网格线1的宽度可以不用特意计算)
点击(此处)折叠或打开
- import sys
- import pygame
- import blocks
- import time
- SIZE = 30 # 每个小方格大小
- BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
- BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量
- BORDER_WIDTH = 4 # 游戏区边框分割线宽度
- BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色
- INFO_WIDTH = 160 # 信息区域宽度
- BG_COLOR = (60, 60, 60) # 背景色
- BLACK = (0, 0, 0) # 网格线颜色
- WIDTH = 1 # 网格线宽度
- SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
- SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高
- # 将文字划到画布上指定位置
- def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
- imgText = font.render(text, True, fcolor)
- screen.blit(imgText, (x, y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('俄罗斯方块')
-
- font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
- font1_width, font1_height = font1.size('得分:') # 字体高度
- font1_height= int(font1_height)
- font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
-
- score = 0 # 得分
- removes = 0 # 消除行数
- orispeed = 0.5 # 原始速度
- speed = orispeed # 当前速度
- cur_block = None # 当前下落方块
- cur_pos_x, cur_pos_y = 0, 0
- next_block = None # 下一个方块
- block_color = (20, 128, 200) # 方块颜色
- last_drop_time = 0
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RETURN:
- cur_block = blocks.get_block()
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
- # 填充背景色
- screen.fill(BG_COLOR)
- # 画游戏区域分隔线
- pygame.draw.line(screen, BORDER_COLOR,
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
-
- # 画网格线 竖线
- for x in range(BLOCK_WIDTH_NUM+1):
- pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
- # 画网格线 横线
- for y in range(BLOCK_HEIGHT_NUM+1):
- pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)
-
- # 画当前下落方块
- if cur_block:
- for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color,
- ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
- 0)
- cur_drop_time = time.time()
- if cur_block and cur_drop_time - last_drop_time > speed:
- last_drop_time = cur_drop_time
- cur_pos_y += 1
-
- print_text(screen, font1, font_pos_x, 10, f'得分: ')
- print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
- print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
- print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
- print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
- print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
- print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
- if next_block:
- next_block_y = 40 + (font1_height + 6) * 7
- for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
- for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
- if next_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)
-
- pygame.display.flip()
- if __name__ == '__main__':
- main()
点击(此处)折叠或打开
- import sys
- import pygame
- import blocks
- import time
- SIZE = 30 # 每个小方格大小
- BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
- BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量
- BORDER_WIDTH = 4 # 游戏区边框分割线宽度
- BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色
- INFO_WIDTH = 160 # 信息区域宽度
- BG_COLOR = (60, 60, 60) # 背景色
- BLACK = (0, 0, 0) # 网格线颜色
- WIDTH = 1 # 网格线宽度
- SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
- SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高
- # 将文字划到画布上指定位置
- def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
- imgText = font.render(text, True, fcolor)
- screen.blit(imgText, (x, y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('俄罗斯方块')
-
- font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
- font1_width, font1_height = font1.size('得分:') # 字体高度
- font1_height= int(font1_height)
- font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
-
- score = 0 # 得分
- removes = 0 # 消除行数
- orispeed = 0.5 # 原始速度
- speed = orispeed # 当前速度
- cur_block = None # 当前下落方块
- cur_pos_x, cur_pos_y = 0, 0
- next_block = None # 下一个方块
- block_color = (20, 128, 200) # 方块颜色
-
- last_press_time = 0
- last_drop_time = 0
-
-
- def _judge(pos_x, pos_y, block):
- if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
- return False
- return True
-
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RETURN:
- cur_block = blocks.get_block()
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
-
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- if cur_block and time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x > - cur_block.start_pos.X:
- if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
- cur_pos_x -= 1
- if event.key == pygame.K_RIGHT:
- if cur_block and time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
- if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
- cur_pos_x += 1
- # 填充背景色
- screen.fill(BG_COLOR)
- # 画游戏区域分隔线
- pygame.draw.line(screen, BORDER_COLOR,
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
-
- # 画网格线 竖线
- for x in range(BLOCK_WIDTH_NUM+1):
- pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
- # 画网格线 横线
- for y in range(BLOCK_HEIGHT_NUM+1):
- pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)
-
- # 画当前下落方块
- if cur_block:
- for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color,
- ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
- 0)
- cur_drop_time = time.time()
- if cur_block and cur_drop_time - last_drop_time > speed:
- if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
- pass # 下一个方块在上方显示
- else:
- last_drop_time = cur_drop_time
- cur_pos_y += 1
-
- print_text(screen, font1, font_pos_x, 10, f'得分: ')
- print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
- print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
- print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
- print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
- print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
- print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
- if next_block:
- next_block_y = 40 + (font1_height + 6) * 7
- for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
- for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
- if next_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)
-
- pygame.display.flip()
- if __name__ == '__main__':
- main()
但是现在的情况是, 当前方块下落到最底部时不会继续下落, 但是一直能左右移动,而且还未处理当前方块停靠后的下一个方块下落,这是因为对方块停靠处理方式有待完善,判断方块停靠条件,一是刚开始的时候不能一直下落掉出屏幕底部,而是当已有方块时,当前方块不能和已有方块有重叠的部分
将代码
点击(此处)折叠或打开
- pass # 下一个方块在上方显示
点击(此处)折叠或打开
- cur_block = next_block
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
新增一个游戏区的坐标变量,再将新方块生成写进一个方法,初步类似下面这样:
点击(此处)折叠或打开
- import sys
- import pygame
- import blocks
- import time
- SIZE = 30 # 每个小方格大小
- BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
- BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量
- BORDER_WIDTH = 4 # 游戏区边框分割线宽度
- BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色
- INFO_WIDTH = 160 # 信息区域宽度
- BG_COLOR = (60, 60, 60) # 背景色
- BLACK = (0, 0, 0) # 网格线颜色
- WIDTH = 1 # 网格线宽度
- SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
- SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高
- # 将文字划到画布上指定位置
- def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
- imgText = font.render(text, True, fcolor)
- screen.blit(imgText, (x, y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('俄罗斯方块')
-
- font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
- font1_width, font1_height = font1.size('得分:') # 字体高度
- font1_height= int(font1_height)
- font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
-
- score = 0 # 得分
- removes = 0 # 消除行数
- orispeed = 0.5 # 原始速度
- speed = orispeed # 当前速度
- game_area = None # 整个游戏区域
- cur_block = None # 当前下落方块
- cur_pos_x, cur_pos_y = 0, 0
- next_block = None # 下一个方块
- block_color = (20, 128, 200) # 方块颜色
-
- last_press_time = 0
- last_drop_time = 0
- game_area = None # 整个游戏区域
-
- def _judge(pos_x, pos_y, block):
- if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
- return False
- return True
- def _dock():
- nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y
-
- # 记录现有方块
- for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[_i][_j] != '.':
- game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
-
- cur_block = next_block
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RETURN:
- game_area = [['.'] * BLOCK_WIDTH_NUM for _ in range(BLOCK_HEIGHT_NUM)]
- cur_block = blocks.get_block()
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
-
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- if cur_block and time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x > - cur_block.start_pos.X:
- if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
- cur_pos_x -= 1
- if event.key == pygame.K_RIGHT:
- if cur_block and time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
- if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
- cur_pos_x += 1
- # 填充背景色
- screen.fill(BG_COLOR)
- # 画游戏区域分隔线
- pygame.draw.line(screen, BORDER_COLOR,
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
-
- # 画网格线 竖线
- for x in range(BLOCK_WIDTH_NUM+1):
- pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
- # 画网格线 横线
- for y in range(BLOCK_HEIGHT_NUM+1):
- pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)
- # 画现有方块
- if game_area:
- for i, row in enumerate(game_area):
- for j, cell in enumerate(row):
- if cell != '.':
- pygame.draw.rect(screen, block_color, (j * SIZE, i * SIZE, SIZE, SIZE), 0)
-
- # 画当前下落方块
- if cur_block:
- for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color,
- ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
- 0)
- cur_drop_time = time.time()
- if cur_block and cur_drop_time - last_drop_time > speed:
- if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
- _dock() # 下一个方块在上方显示
- else:
- last_drop_time = cur_drop_time
- cur_pos_y += 1
-
- print_text(screen, font1, font_pos_x, 10, f'得分: ')
- print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
- print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
- print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
- print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
- print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
- print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
- if next_block:
- next_block_y = 40 + (font1_height + 6) * 7
- for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
- for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
- if next_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)
-
- pygame.display.flip()
- if __name__ == '__main__':
- main()
但是判断停靠的方法还有问题, 下落方块和已停靠的方块不能重叠穿插,故修正_judge方法:
点击(此处)折叠或打开
- def _judge(pos_x, pos_y, block):
- nonlocal game_area
- if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
- return False
- for _i in range(block.start_pos.Y, block.end_pos.Y + 1): # 方块模板不能与现有方块重叠
- for _j in range(block.start_pos.X, block.end_pos.X + 1):
- if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
- return False
- return True
但会发现当长度方向上堆满方块后, 程序还一直在运行,此时应该结束游戏,就是不应该再生产新的方块了,故判断放在生成新方块的方法里,这你可以给生成新方块方法一个返回值,或者增加一个全局变量来告诉主循环,此时应退出循环结束程序了
点击(此处)折叠或打开
- import sys
- import pygame
- import blocks
- import time
- SIZE = 30 # 每个小方格大小
- BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
- BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量
- BORDER_WIDTH = 4 # 游戏区边框分割线宽度
- BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色
- INFO_WIDTH = 160 # 信息区域宽度
- BG_COLOR = (60, 60, 60) # 背景色
- BLACK = (0, 0, 0) # 网格线颜色
- WIDTH = 1 # 网格线宽度
- SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
- SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高
- # 将文字划到画布上指定位置
- def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
- imgText = font.render(text, True, fcolor)
- screen.blit(imgText, (x, y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('俄罗斯方块')
-
- font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
- font1_width, font1_height = font1.size('得分:') # 字体高度
- font1_height= int(font1_height)
- font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
-
- score = 0 # 得分
- removes = 0 # 消除行数
- orispeed = 0.5 # 原始速度
- speed = orispeed # 当前速度
- game_area = None # 整个游戏区域
- cur_block = None # 当前下落方块
- cur_pos_x, cur_pos_y = 0, 0
- next_block = None # 下一个方块
- block_color = (20, 128, 200) # 方块颜色
-
- last_press_time = 0
- last_drop_time = 0
- game_area = None # 整个游戏区域
-
- game_over = True
-
- def _judge(pos_x, pos_y, block):
- nonlocal game_area
- if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
- return False
- for _i in range(block.start_pos.Y, block.end_pos.Y + 1): # 方块模板不能与现有方块重叠
- for _j in range(block.start_pos.X, block.end_pos.X + 1):
- if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
- return False
- return True
- def _dock():
- nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over
-
- # 记录现有方块
- for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[_i][_j] != '.':
- game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
-
- if cur_pos_y + cur_block.start_pos.Y <= 0:
- game_over = True
-
- cur_block = next_block
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RETURN:
- if game_over:
- game_over = False
- game_area = [['.'] * BLOCK_WIDTH_NUM for _ in range(BLOCK_HEIGHT_NUM)]
- cur_block = blocks.get_block()
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
-
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- if cur_block and time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x > - cur_block.start_pos.X:
- if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
- cur_pos_x -= 1
- if event.key == pygame.K_RIGHT:
- if cur_block and time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
- if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
- cur_pos_x += 1
- # 填充背景色
- screen.fill(BG_COLOR)
- # 画游戏区域分隔线
- pygame.draw.line(screen, BORDER_COLOR,
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
-
- # 画网格线 竖线
- for x in range(BLOCK_WIDTH_NUM+1):
- pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
- # 画网格线 横线
- for y in range(BLOCK_HEIGHT_NUM+1):
- pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)
- # 画现有方块
- if game_area:
- for i, row in enumerate(game_area):
- for j, cell in enumerate(row):
- if cell != '.':
- pygame.draw.rect(screen, block_color, (j * SIZE, i * SIZE, SIZE, SIZE), 0)
-
- # 画当前下落方块
- if cur_block:
- for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color,
- ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
- 0)
- if not game_over:
- cur_drop_time = time.time()
- if cur_block and cur_drop_time - last_drop_time > speed:
- if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
- _dock() # 下一个方块在上方显示
- else:
- last_drop_time = cur_drop_time
- cur_pos_y += 1
-
- print_text(screen, font1, font_pos_x, 10, f'得分: ')
- print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
- print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
- print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
- print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
- print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
- print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
- if next_block:
- next_block_y = 40 + (font1_height + 6) * 7
- for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
- for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
- if next_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)
-
- pygame.display.flip()
- if __name__ == '__main__':
- main()
现在加上上键是改变方块形状的功能, 只需在event的for循环中对按下按键事件增加一个上键按钮的判断就好了
点击(此处)折叠或打开
- elif event.key in (pygame.K_w, pygame.K_UP):
- if 0 <= cur_pos_x <= BLOCK_WIDTH_NUM - len(cur_block.template[0]):
- _next_block = blocks.get_next_block(cur_block)
- if _judge(cur_pos_x, cur_pos_y, _next_block):
- cur_block = _next_block
点击(此处)折叠或打开
- else:
- # 计算消除
- remove_idxs = []
- for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- if all(_x == '0' for _x in game_area[cur_pos_y + _i]):
- remove_idxs.append(cur_pos_y + _i)
- if remove_idxs:
- # 消除
- _i = _j = remove_idxs[-1]
- while _i >= 0:
- while _j in remove_idxs:
- _j -= 1
- if _j < 0:
- game_area[_i] = ['.'] * BLOCK_WIDTH
- else:
- game_area[_i] = game_area[_j]
- _i -= 1
- _j -= 1
点击(此处)折叠或打开
- import sys
- import pygame
- import blocks
- import time
- SIZE = 30 # 每个小方格大小
- BLOCK_HEIGHT_NUM = 20 # 游戏区高度方向方块数量
- BLOCK_WIDTH_NUM = 10 # 游戏区宽度方向方块数量
- BORDER_WIDTH = 4 # 游戏区边框分割线宽度
- BORDER_COLOR = (40, 40, 200) # 游戏区边框分割线颜色
- INFO_WIDTH = 160 # 信息区域宽度
- BG_COLOR = (60, 60, 60) # 背景色
- BLACK = (0, 0, 0) # 网格线颜色
- WIDTH = 1 # 网格线宽度
- SCREEN_WIDTH = SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH + INFO_WIDTH # 游戏屏幕的宽
- SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT_NUM # 游戏屏幕的高
- # 将文字划到画布上指定位置
- def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
- imgText = font.render(text, True, fcolor)
- screen.blit(imgText, (x, y))
- def main():
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('俄罗斯方块')
-
- font1 = pygame.font.SysFont('SimHei', 24) # 黑体24
- font1_width, font1_height = font1.size('得分:') # 字体高度
- font1_height= int(font1_height)
- font_pos_x = SCREEN_WIDTH - INFO_WIDTH + 10 # 右侧信息显示区域字体位置的X坐标
-
- fontgg = pygame.font.Font(None, 72)
- gameover_size = fontgg.size('GAME OVER')
-
- score = 0 # 得分
- removes = 0 # 消除行数
- orispeed = 0.5 # 原始速度
- speed = orispeed # 当前速度
- game_area = None # 整个游戏区域
- cur_block = None # 当前下落方块
- cur_pos_x, cur_pos_y = 0, 0
- next_block = None # 下一个方块
- block_color = (20, 128, 200) # 方块颜色
-
- last_press_time = 0
- last_drop_time = 0
- game_area = None # 整个游戏区域
-
- game_over = True
-
- pause = False # 暂停
- start = False # 是否开始,开始游戏后game_over为True游戏就是真正结束
- last_drop_time = None # 上次下落时间
- last_press_time = None # 上次按键时间
- last_key_down = False
-
- def _judge(pos_x, pos_y, block):
- nonlocal game_area
- if pos_y + block.end_pos.Y >= BLOCK_HEIGHT_NUM: #不能超越下边界
- return False
- for _i in range(block.start_pos.Y, block.end_pos.Y + 1): # 方块模板不能与现有方块重叠
- for _j in range(block.start_pos.X, block.end_pos.X + 1):
- if pos_y + _i >= 0 and block.template[_i][_j] != '.' and game_area[pos_y + _i][pos_x + _j] != '.':
- return False
- return True
- def _dock():
- nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over, score, speed, removes
-
- # 记录现有方块
- for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[_i][_j] != '.':
- game_area[cur_pos_y + _i][cur_pos_x + _j] = '0'
-
- if cur_pos_y + cur_block.start_pos.Y <= 0:
- game_over = True
- else:
- # 计算消除
- remove_idxs = []
- for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- if all(_x == '0' for _x in game_area[cur_pos_y + _i]):
- remove_idxs.append(cur_pos_y + _i)
- if remove_idxs:
- # 计算得分
- remove_count = len(remove_idxs)
- removes += remove_count;
- if remove_count == 1:
- score += 100
- elif remove_count == 2:
- score += 300
- elif remove_count == 3:
- score += 700
- elif remove_count == 4:
- score += 1500
- speed = orispeed - 0.03 * (score // 10000)
- # 消除
- _i = _j = remove_idxs[-1]
- while _i >= 0:
- while _j in remove_idxs:
- _j -= 1
- if _j < 0:
- game_area[_i] = ['.'] * BLOCK_WIDTH_NUM
- else:
- game_area[_i] = game_area[_j]
- _i -= 1
- _j -= 1
-
- cur_block = next_block
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
-
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RETURN:
- last_key_down = False
- if game_over:
- game_over = False
- start = True
- score = 0
- speed = orispeed
- removes = 0
- last_drop_time = time.time()
- last_press_time = time.time()
- game_area = [['.'] * BLOCK_WIDTH_NUM for _ in range(BLOCK_HEIGHT_NUM)]
- cur_block = blocks.get_block()
- next_block = blocks.get_block()
- cur_pos_x, cur_pos_y = (BLOCK_WIDTH_NUM - cur_block.end_pos.X - 1) // 2, -1 - cur_block.end_pos.Y
- else:
- pause = not pause
- elif event.key in (pygame.K_w, pygame.K_UP):
- last_key_down = False
- if 0 <= cur_pos_x <= BLOCK_WIDTH_NUM - len(cur_block.template[0]):
- _next_block = blocks.get_next_block(cur_block)
- if _judge(cur_pos_x, cur_pos_y, _next_block):
- cur_block = _next_block
- elif event.key == pygame.K_SPACE:
- if not last_key_down or time.time() - last_press_time > 0.2 : # 连续两字短时间按键触发
- last_press_time = time.time()
- last_key_down = True
- while _judge(cur_pos_x, cur_pos_y + 1, cur_block):
- cur_pos_y += 1
- last_drop_time = time.time()
- else:
- _dock()
-
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- last_key_down = False
- if not game_over and not pause:
- if time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x > - cur_block.start_pos.X:
- if _judge(cur_pos_x - 1, cur_pos_y, cur_block):
- cur_pos_x -= 1
- if event.key == pygame.K_RIGHT:
- last_key_down = False
- if not game_over and not pause:
- if time.time() - last_press_time > 0.1:
- last_press_time = time.time()
- if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH_NUM:
- if _judge(cur_pos_x + 1, cur_pos_y, cur_block):
- cur_pos_x += 1
- if event.key == pygame.K_DOWN:
- last_key_down = False
- if not game_over and not pause:
- if time.time() - last_press_time > 0.1 :
- last_press_time = time.time()
- if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
- _dock()
- else:
- last_drop_time = time.time()
- cur_pos_y += 1
- # 填充背景色
- screen.fill(BG_COLOR)
- # 画游戏区域分隔线
- pygame.draw.line(screen, BORDER_COLOR,
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, 0),
- (SIZE * BLOCK_WIDTH_NUM + BORDER_WIDTH // 2, SCREEN_HEIGHT), BORDER_WIDTH)
- # 画现有方块
- if game_area:
- for i, row in enumerate(game_area):
- for j, cell in enumerate(row):
- if cell != '.':
- pygame.draw.rect(screen, block_color, (j * SIZE, i * SIZE, SIZE, SIZE), 0)
-
- # 画网格线 竖线
- for x in range(BLOCK_WIDTH_NUM+1):
- pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)
- # 画网格线 横线
- for y in range(BLOCK_HEIGHT_NUM+1):
- pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH_NUM * SIZE, y * SIZE), 1)
-
- # 画当前下落方块
- if cur_block:
- for i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1):
- for j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1):
- if cur_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color,
- ((cur_pos_x + j) * SIZE, (cur_pos_y + i) * SIZE, SIZE, SIZE),
- 0)
- if not game_over:
- cur_drop_time = time.time()
- if cur_drop_time - last_drop_time > speed:
- if not pause:
- if not _judge(cur_pos_x, cur_pos_y + 1, cur_block):
- _dock()
- else:
- last_drop_time = cur_drop_time
- cur_pos_y += 1
- else:
- if start:
- print_text(screen, fontgg,
- (SCREEN_WIDTH - gameover_size[0]) // 2, (SCREEN_HEIGHT - gameover_size[1]) // 2,
- 'GAME OVER', (200, 30, 30))
-
- print_text(screen, font1, font_pos_x, 10, f'得分: ')
- print_text(screen, font1, font_pos_x + SIZE, 10 + font1_height + 6, f'{score}')
- print_text(screen, font1, font_pos_x, 20 + (font1_height + 6) * 2, f'行数: ')
- print_text(screen, font1, font_pos_x + SIZE, 20 + (font1_height + 6) * 3, f'{removes}')
- print_text(screen, font1, font_pos_x, 30 + (font1_height + 6) * 4, f'速度: ')
- print_text(screen, font1, font_pos_x + SIZE, 30 + (font1_height + 6) * 5, f'{score // 10000}')
- print_text(screen, font1, font_pos_x, 40 + (font1_height + 6) * 6, f'下一个:')
- if next_block:
- next_block_y = 40 + (font1_height + 6) * 7
- for i in range(next_block.start_pos.Y, next_block.end_pos.Y + 1):
- for j in range(next_block.start_pos.X, next_block.end_pos.X + 1):
- if next_block.template[i][j] != '.':
- pygame.draw.rect(screen, block_color, (font_pos_x + j * SIZE, next_block_y + i * SIZE, SIZE, SIZE), 0)
-
- pygame.display.flip()
- if __name__ == '__main__':
- main()