今天我们用python来实现小时候玩过的俄罗斯方块游戏吧
具体代码与文件可以访问我的获取
第一步——构建各种方块
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]
第二部——构建主函数
import sys import time import pygame from pygame.locals import * import blocks size = 30 # 每个小方格大小 block_height = 25 # 游戏区高度 block_width = 10 # 游戏区宽度 border_width = 4 # 游戏区边框宽度 border_color = (40, 40, 200) # 游戏区边框颜色 screen_width = size * (block_width 5) # 游戏屏幕的宽 screen_height = size * block_height # 游戏屏幕的高 bg_color = (40, 40, 60) # 背景色 block_color = (20, 128, 200) # black = (0, 0, 0) red = (200, 30, 30) # game over 的字体颜色 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 font2 = pygame.font.font(none, 72) # game over 的字体 font_pos_x = block_width * size border_width 10 # 右侧信息显示区域字体位置的x坐标 gameover_size = font2.size('game over') font1_height = int(font1.size('得分')[1]) cur_block = none # 当前下落方块 next_block = none # 下一个方块 cur_pos_x, cur_pos_y = 0, 0 game_area = none # 整个游戏区域 game_over = true start = false # 是否开始,当start = true,game_over = true 时,才显示 game over score = 0 # 得分 orispeed = 0.5 # 原始速度 speed = orispeed # 当前速度 pause = false # 暂停 last_drop_time = none # 上次下落时间 last_press_time = none # 上次按键时间 def _dock(): nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over, score, speed 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) 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 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 - cur_block.end_pos.x - 1) // 2, -1 - cur_block.end_pos.y def _judge(pos_x, pos_y, block): nonlocal game_area for _i in range(block.start_pos.y, block.end_pos.y 1): if pos_y block.end_pos.y >= block_height: return false 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 while true: for event in pygame.event.get(): if event.type == quit: sys.exit() elif event.type == keydown: if event.key == k_return: if game_over: start = true game_over = false score = 0 last_drop_time = time.time() last_press_time = time.time() game_area = [['.'] * block_width for _ in range(block_height)] cur_block = blocks.get_block() next_block = blocks.get_block() cur_pos_x, cur_pos_y = (block_width - cur_block.end_pos.x - 1) // 2, -1 - cur_block.end_pos.y elif event.key == k_space: if not game_over: pause = not pause elif event.key in (k_w, k_up): # 旋转 # 其实记得不是很清楚了,比如 # .0. # .00 # ..0 # 这个在最右边靠边的情况下是否可以旋转,我试完了网上的俄罗斯方块,是不能旋转的,这里我们就按不能旋转来做 # 我们在形状设计的时候做了很多的空白,这样只需要规定整个形状包括空白部分全部在游戏区域内时才可以旋转 if 0 <= cur_pos_x <= block_width - 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 if event.type == pygame.keydown: if event.key == pygame.k_left: 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: 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: if _judge(cur_pos_x 1, cur_pos_y, cur_block): cur_pos_x = 1 if event.key == pygame.k_down: 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 _draw_background(screen) _draw_game_area(screen, game_area) _draw_gridlines(screen) _draw_info(screen, font1, font_pos_x, font1_height, score) # 画显示信息中的下一个方块 _draw_block(screen, next_block, font_pos_x, 30 (font1_height 6) * 5, 0, 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, font2, (screen_width - gameover_size[0]) // 2, (screen_height - gameover_size[1]) // 2, 'game over', red) # 画当前下落方块 _draw_block(screen, cur_block, 0, 0, cur_pos_x, cur_pos_y) pygame.display.flip() # 画背景 def _draw_background(screen): # 填充背景色 screen.fill(bg_color) # 画游戏区域分隔线 pygame.draw.line(screen, border_color, (size * block_width border_width // 2, 0), (size * block_width border_width // 2, screen_height), border_width) # 画网格线 def _draw_gridlines(screen): # 画网格线 竖线 for x in range(block_width): pygame.draw.line(screen, black, (x * size, 0), (x * size, screen_height), 1) # 画网格线 横线 for y in range(block_height): pygame.draw.line(screen, black, (0, y * size), (block_width * size, y * size), 1) # 画已经落下的方块 def _draw_game_area(screen, game_area): 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) # 画单个方块 def _draw_block(screen, block, offset_x, offset_y, pos_x, pos_y): if block: 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 block.template[i][j] != '.': pygame.draw.rect(screen, block_color, (offset_x (pos_x j) * size, offset_y (pos_y i) * size, size, size), 0) # 画得分等信息 def _draw_info(screen, font, pos_x, font_height, score): print_text(screen, font, pos_x, 10, f'得分: ') print_text(screen, font, pos_x, 10 font_height 6, f'{score}') print_text(screen, font, pos_x, 20 (font_height 6) * 2, f'速度: ') print_text(screen, font, pos_x, 20 (font_height 6) * 3, f'{score // 10000}') print_text(screen, font, pos_x, 30 (font_height 6) * 4, f'下一个:') if __name__ == '__main__': main()
游戏截图
运行效果