俄羅斯方塊(Tetris)是一款經典的俄羅斯益智游戲,游戲的核心玩法是通過移動、旋轉和放置不同形狀的方塊,使它們在游戲界面的底部形成完整的水平線。一旦水平線填滿,就會被清除,為新的方塊騰出空間。
在 Python 中可以使用第三方庫pygame
來實現簡單的俄羅斯方塊游戲,使用pygame
前需確保其已經安裝。示例代碼如下:
import random
import sysimport pygame# 定義俄羅斯方塊類
class Tetris:# 初始化方塊類型all_block_shapes = [[[0, 0], [0, -1], [0, 1], [0, 2]],[[0, 0], [0, 1], [1, 1], [1, 0]],[[0, 0], [0, -1], [-1, 0], [-1, 1]],[[0, 0], [0, 1], [-1, -1], [-1, 0]],[[0, 0], [0, 1], [1, 0], [0, -1]],[[0, 0], [1, 0], [-1, 0], [1, -1]],[[0, 0], [1, 0], [-1, 0], [1, 1]]]current_block_shape = list(random.choice(all_block_shapes))score = [0]game_over = []# 初始化def __init__(self, height, width, block_initial_position, screen):self.background = [[0 for column in range(0, width)] for row inrange(0, height)]self.background[0] = [1 for column in range(0, width)]self.block_initial_position = block_initial_positionself.screen = screen# 旋轉方塊函數def rotate_block(self):# 獲取方塊的初始位置y_drop, x_move = self.block_initial_position# 計算方塊旋轉后的位置rotating_position = [(-column, row) for row, column inself.current_block_shape]# 檢查旋轉后的位置是否合法for row, column in rotating_position:row += y_dropcolumn += x_move# 如果超出邊界或和背景方塊重疊,則跳出循環if column < 0 or column > 9 or self.background[row][column]:breakelse:# 如果旋轉后的位置合法,則更新方塊的位置self.current_block_shape.clear()self.current_block_shape.extend(rotating_position)# 方塊下移函數def block_move_down(self):# 獲取方塊的初始位置y_drop = self.block_initial_position[0]x_move = self.block_initial_position[1]y_drop -= 1# 檢查方塊下移后的位置是否合法for row, column in self.current_block_shape:row += y_dropcolumn += x_move# 如果下方有背景方塊,則停止下移if self.background[row][column] == 1:breakelse:# 如果下移位置合法,則更新方塊的位置self.block_initial_position.clear()self.block_initial_position.extend([y_drop, x_move])return# 如果方塊無法下移,則將方塊固定在背景上,并處理消除的行y_drop, x_move = self.block_initial_positionfor row, column in self.current_block_shape:self.background[y_drop + row][x_move + column] = 1complete_row = []# 檢查是否有行滿了for row in range(1, 21):if 0 not in self.background[row]:complete_row.append(row)complete_row.sort(reverse=True)# 消除滿行,并得分for row in complete_row:self.background.pop(row)self.background.append([0 for column in range(0, 10)])self.score[0] += len(complete_row)pygame.display.set_caption(str(self.score[0]) + '分')# 選擇下一個方塊并放置在頂部self.current_block_shape.clear()self.current_block_shape.extend(list(random.choice(self.all_block_shapes)))self.block_initial_position.clear()self.block_initial_position.extend([20, 5])y_drop, x_move = self.block_initial_position# 檢查是否游戲結束for row, column in self.current_block_shape:row += y_dropcolumn += x_moveif self.background[row][column]:self.game_over.append(1)# 繪制元素函數def draw_elements(self):# 繪制方塊y_drop, x_move = self.block_initial_positionfor row, column in self.current_block_shape:row += y_dropcolumn += x_movepygame.draw.rect(self.screen, (255, 165, 0),(column * 25, 500 - row * 25, 23, 23))# 繪制背景方塊for row in range(0, 20):for column in range(0, 10):bottom_block = self.background[row][column]if bottom_block:pygame.draw.rect(self.screen, (0, 0, 255),(column * 25, 500 - row * 25, 23, 23))# 方塊左右移動函數def move_block_left_right(self, n):# 方塊水平移動y_drop, x_move = self.block_initial_positionx_move += nfor row, column in self.current_block_shape:row += y_dropcolumn += x_move# 如果超出邊界或和背景方塊重疊,則跳出循環if column < 0 or column > 9 or self.background[row][column]:breakelse:# 如果移動位置合法,則更新方塊的位置self.block_initial_position.clear()self.block_initial_position.extend([y_drop, x_move])# 事件處理函數def handle_events(self):times = 0is_press = Falsewhile True:self.screen.fill((255, 255, 255))# 按鍵事件for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:self.move_block_left_right(-1)elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:self.move_block_left_right(1)elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:self.rotate_block()elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:is_press = Trueelif event.type == pygame.KEYUP and event.key == pygame.K_DOWN:is_press = False# 如果下箭頭鍵被按下,則加快方塊下落速度if is_press:times += 10# 達到時間閾值時讓方塊向下移動,并重置時間if times >= 100:self.block_move_down()times = 0else:times += 1# 如果游戲結束,則退出程序if self.game_over:sys.exit()self.draw_elements()pygame.time.Clock().tick(200)pygame.display.flip()# 游戲入口
def main():height = 22width = 10block_initial_position = [21, 5]pygame.init()screen = pygame.display.set_mode((250, 500))pygame.display.set_caption("俄羅斯方塊")tetris = Tetris(height, width, block_initial_position, screen)# 調用按鍵事件處理函數tetris.handle_events()# 啟動游戲
main()
上述代碼定義的Tetris
類將游戲功能進行了封裝,main
是游戲的入口,運行代碼,你就可以得到一個簡單的俄羅斯方塊游戲。