著作權宣告:原創不易,本文禁止抄襲、轉載,侵權必究!
一、開發環境&需求分析
開發環境:python3.6.4
第三方庫:pygame1.9.6
集成開發環境:PyCharm/Sublime Text
- 利用pygame開發俄羅斯方塊游戲,左邊提供游戲界面,右邊提供顯示界面,包括游戲得分、方塊速度以及下一個方塊的形狀
- 實作游戲方塊精靈旋轉,停靠,消除等互動動作
- 用二維陣列來實作7種不同型別的游戲方塊,可以通過調整陣列引數進而改變方塊形狀
- 提供網格線,使方塊精靈更直觀清晰
二、功能模塊
游戲初始化
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'下一個:')
游戲畫面

三、游戲視頻
點我觀看視頻,最下面有驚喜!
四、原始碼下載
關注我的原創公眾號【小鴻星空科技】,回復【游戲開發】獲取完整專案,包括原始碼及素材
五、作者Info
作者:南柯樹下,Goal:讓編程更有趣!
原創微信公眾號:『小鴻星空科技』,專注于演算法、爬蟲,網站,游戲開發,資料分析、自然語言處理,AI等,期待你的關注,讓我們一起成長、一起Coding!
著作權宣告:本文禁止抄襲、轉載 ,侵權必究!
歡迎掃碼關注我的原創公眾號【小鴻星空科技】,回復【游戲開發】獲取完整專案,包括原始碼及素材
—— —— —— —— — END —— —— —— —— ————
歡迎掃碼關注我的公眾號
小鴻星空科技

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/373799.html
標籤:其他
