一、需求分析
開發一個基于 Pygame 庫的五子棋小游戲,允許兩名玩家在棋盤上輪流落子,當有一方達成五子連珠時游戲結束,顯示獲勝信息,并提供退出游戲和重新開始游戲的操作選項。
1.棋盤顯示 :
????????顯示一個 15x15 的五子棋棋盤,棋盤背景為木頭淡黃色,網格線為黑色。
2.落子操作 :
????????玩家通過鼠標點擊棋盤上的交叉點來落子,黑子和白子輪流交替落子。
????????只有在空的交叉點上才能落子。
3.勝負判斷 :
????????每次落子后,檢查是否有五子連珠的情況(橫向、縱向、正斜向、反斜向)。
????????如果有一方達成五子連珠,則判定該方獲勝。
4.游戲結束處理 :
????????游戲結束時,在屏幕中央顯示獲勝信息(如“Player 1 Win!” 或 “Player 2 Win!”)。
????????同時顯示操作提示,告知玩家可以按 “Q” 鍵退出游戲,按 “N” 鍵重新開始游戲。
????????顯示一個半透明的黑色提示框,將獲勝信息和操作提示包含在內。
5.重新開始和退出 :
????????游戲結束后,玩家可以按 “Q” 鍵退出游戲,按 “N” 鍵重新開始游戲。
????????重新開始游戲時,清空棋盤,重置當前玩家為黑子先手。
二、關鍵模塊
1. 常量定義
????????定義游戲中使用的各種常量,如棋盤大小、網格大小、窗口尺寸、顏色、字體等。
2. 初始化
????????初始化 Pygame 庫。
????????創建游戲窗口。
????????初始化棋盤狀態。
3. 繪制模塊
????????繪制棋盤:使用木頭淡黃色填充屏幕,并繪制黑色的網格線。
????????繪制棋子:根據棋盤狀態,在相應位置繪制黑子和白子。
4. 勝負判斷
????????檢查當前落子位置是否形成五子連珠,分別從橫向、縱向、正斜向、反斜向四個方向進行檢查。
5. 游戲控制
????????處理鼠標點擊事件,實現落子操作。
????????處理鍵盤事件,實現退出游戲和重新開始游戲的功能。
????????控制游戲的主循環,更新游戲狀態。 6. 提示信息顯示模塊
????????在游戲結束時,顯示獲勝信息和操作提示,并繪制半透明的提示框。
三、完整代碼
import pygame
import sys# 初始化 Pygame
pygame.init()# 定義常量
BOARD_SIZE = 15
GRID_SIZE = 40
WINDOW_WIDTH = GRID_SIZE * (BOARD_SIZE + 1)
WINDOW_HEIGHT = GRID_SIZE * (BOARD_SIZE + 1)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 定義木頭淡黃色
WOOD_COLOR = (205, 133, 63)
BLUE = (0, 0, 255)
FONT = pygame.font.Font(None, 36) # 創建游戲窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("五子棋小游戲")# 初始化棋盤
board = [[0] * BOARD_SIZE for _ in range(BOARD_SIZE)]# 繪制棋盤
def draw_board():# 使用木頭淡黃色填充屏幕screen.fill(WOOD_COLOR) for i in range(BOARD_SIZE):pygame.draw.line(screen, BLACK, (GRID_SIZE, GRID_SIZE * (i + 1)), (GRID_SIZE * BOARD_SIZE, GRID_SIZE * (i + 1)), 2)pygame.draw.line(screen, BLACK, (GRID_SIZE * (i + 1), GRID_SIZE), (GRID_SIZE * (i + 1), GRID_SIZE * BOARD_SIZE), 2)# 繪制棋子
def draw_pieces():for i in range(BOARD_SIZE):for j in range(BOARD_SIZE):if board[i][j] == 1:pygame.draw.circle(screen, BLACK, (GRID_SIZE * (j + 1), GRID_SIZE * (i + 1)), 18)elif board[i][j] == 2:pygame.draw.circle(screen, WHITE, (GRID_SIZE * (j + 1), GRID_SIZE * (i + 1)), 18)# 檢查是否有五子連珠
def check_win(x, y, player):directions = [(1, 0), (0, 1), (1, 1), (1, -1)]for dx, dy in directions:count = 1# 正向檢查for i in range(1, 5):new_x = x + i * dxnew_y = y + i * dyif 0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE and board[new_x][new_y] == player:count += 1else:break# 反向檢查for i in range(1, 5):new_x = x - i * dxnew_y = y - i * dyif 0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE and board[new_x][new_y] == player:count += 1else:breakif count >= 5:return Truereturn False# 重置游戲狀態
def reset_game():global board, current_player, game_overboard = [[0] * BOARD_SIZE for _ in range(BOARD_SIZE)]current_player = 1game_over = False# 顯示獲勝信息和操作提示
def show_winner_info(player):winner_text = FONT.render(f"Player {player} Win!", True, WHITE)prompt_text = FONT.render("Press Q to Quit, Press N to Restart", True, WHITE)# 創建半透明的矩形框winner_rect = winner_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 30))prompt_rect = prompt_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 30))# 調整內邊距以確保提示框足夠大padding = 30combined_width = max(winner_rect.width, prompt_rect.width) + 2 * paddingcombined_height = winner_rect.height + prompt_rect.height + 3 * paddingrect = pygame.Rect(winner_rect.x - padding, winner_rect.y - padding, combined_width, combined_height)rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)# 創建一個帶有透明度的表面surface = pygame.Surface((rect.width, rect.height), pygame.SRCALPHA)surface.fill((0, 0, 0, 128)) # 半透明黑色screen.blit(surface, (rect.x, rect.y))screen.blit(winner_text, (rect.centerx - winner_rect.width // 2, rect.centery - winner_rect.height - padding // 2))screen.blit(prompt_text, (rect.centerx - prompt_rect.width // 2, rect.centery + padding // 2))pygame.display.flip()# 主游戲循環
current_player = 1
game_over = False
while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:x, y = event.poscol = (x - GRID_SIZE // 2) // GRID_SIZErow = (y - GRID_SIZE // 2) // GRID_SIZEif 0 <= col < BOARD_SIZE and 0 <= row < BOARD_SIZE and board[row][col] == 0:board[row][col] = current_playerif check_win(row, col, current_player):print(f"Player {current_player} Win!")# 先顯示最后一個子draw_board()draw_pieces()pygame.display.flip()# 顯示獲勝信息和操作提示show_winner_info(current_player)game_over = True# 進入等待狀態,直到用戶按下 Q 或者 N 鍵while game_over:for inner_event in pygame.event.get():if inner_event.type == pygame.QUIT:pygame.quit()sys.exit()elif inner_event.type == pygame.KEYDOWN:if inner_event.key == pygame.K_q:pygame.quit()sys.exit()elif inner_event.key == pygame.K_n:reset_game()game_over = Falsecurrent_player = 3 - current_player # 切換玩家elif event.type == pygame.KEYDOWN:if event.key == pygame.K_q:pygame.quit()sys.exit()elif event.key == pygame.K_n:reset_game()if not game_over:draw_board()draw_pieces()pygame.display.flip()
四、代碼運行方式
1.代碼運行環境
????????Python 3.x
????????Pygame 庫
pip install pygame
2.運行代碼
????????將上述代碼保存為 gobang_game.py 文件,然后在終端中運行以下命令:
python gobang_game.py
3.游戲操作說明
????????游戲開始后,黑子先手,玩家通過鼠標點擊棋盤上的交叉點落子。
????????當有一方達成五子連珠時,游戲結束,屏幕中央會顯示獲勝信息和操作提示。
????????按 “Q” 鍵退出游戲,按 “N” 鍵重新開始游戲。