版权声明:原创不易,本文禁止抄袭、转载,侵权必究!
开发环境:python3.6.4
第三方库:pygame1.9.6
集成开发环境:PyCharm/Sublime Text
游戏初始化
1 SIZE = 30 # 每个小方格大小 2 BLOCK_HEIGHT = 25 # 游戏区高度 3 BLOCK_WIDTH = 10 # 游戏区宽度 4 BORDER_WIDTH = 4 # 游戏区边框宽度 5 BORDER_COLOR = (40, 40, 200) # 游戏区边框颜色 6 SCREEN_WIDTH = SIZE * (BLOCK_WIDTH + 5) # 游戏屏幕的宽 7 SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT # 游戏屏幕的高 8 BG_COLOR = (40, 40, 60) # 背景色 9 BLOCK_COLOR = (20, 128, 200) # 10 BLACK = (0, 0, 0) 11 RED = (200, 30, 30) # GAME OVER 的字体颜色 12 def main(): 13 pygame.init() 14 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 15 pygame.display.set_caption('俄罗斯方块')
方块定义
1 # S形方块 2 S_BLOCK = [Block(['.OO', 3 'OO.', 4 '...'], Point(0, 0), Point(2, 1), 'S', 1), 5 Block(['O..', 6 'OO.', 7 '.O.'], Point(0, 0), Point(1, 2), 'S', 0)] 8 # Z形方块 9 Z_BLOCK = [Block(['OO.', 10 '.OO', 11 '...'], Point(0, 0), Point(2, 1), 'Z', 1), 12 Block(['.O.', 13 'OO.', 14 'O..'], Point(0, 0), Point(1, 2), 'Z', 0)]
判断是否可以旋转,下落,移动
1 def _judge(pos_x, pos_y, block): 2 nonlocal game_area 3 for _i in range(block.start_pos.Y, block.end_pos.Y + 1): 4 if pos_y + block.end_pos.Y >= BLOCK_HEIGHT: 5 return False 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 def _dock(): 2 nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over, score, speed 3 for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1): 4 for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1): 5 if cur_block.template[_i][_j] != '.': 6 game_area[cur_pos_y + _i][cur_pos_x + _j] = '0' 7 if cur_pos_y + cur_block.start_pos.Y <= 0: 8 game_over = True 9 else: 10 # 计算消除 11 remove_idxs = [] 12 for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1): 13 if all(_x == '0' for _x in game_area[cur_pos_y + _i]): 14 remove_idxs.append(cur_pos_y + _i)
网格线
1 def _draw_gridlines(screen): 2 # 画网格线 竖线 3 for x in range(BLOCK_WIDTH): 4 pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1) 5 # 画网格线 横线 6 for y in range(BLOCK_HEIGHT): 7 pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH * SIZE, y * SIZE), 1)
分数
1 def _draw_info(screen, font, pos_x, font_height, score): 2 print_text(screen, font, pos_x, 10, f'得分: ') 3 print_text(screen, font, pos_x, 10 + font_height + 6, f'{score}') 4 print_text(screen, font, pos_x, 20 + (font_height + 6) * 2, f'速度: ') 5 print_text(screen, font, pos_x, 20 + (font_height + 6) * 3, f'{score // 10000}') 6 print_text(screen, font, pos_x, 30 + (font_height + 6) * 4, f'下一个:')
游戏画面
点我观看视频,最下面有惊喜!
关注我的原创公众号【小鸿星空科技】,回复【游戏开发】获取完整项目,包括源码及素材
作者:南柯树下,Goal:让编程更有趣!
原创微信公众号:『小鸿星空科技』,专注于算法、爬虫,网站,游戏开发,数据分析、自然语言处理,AI等,期待你的关注,让我们一起成长、一起Coding!
版权声明:本文禁止抄袭、转载 ,侵权必究!
欢迎扫码关注我的原创公众号【小鸿星空科技】,回复【游戏开发】获取完整项目,包括源码及素材
—— —— —— —— — END —— —— —— —— ————
欢迎扫码关注我的公众号
小鸿星空科技