柚子快報(bào)激活碼778899分享:五子棋小游戲-簡單開發(fā)版
柚子快報(bào)激活碼778899分享:五子棋小游戲-簡單開發(fā)版
一、需求分析
開發(fā)一個(gè)基于 Pygame 庫的五子棋小游戲,允許兩名玩家在棋盤上輪流落子,當(dāng)有一方達(dá)成五子連珠時(shí)游戲結(jié)束,顯示獲勝信息,并提供退出游戲和重新開始游戲的操作選項(xiàng)。
1.棋盤顯示 :
????????顯示一個(gè) 15x15 的五子棋棋盤,棋盤背景為木頭淡黃色,網(wǎng)格線為黑色。
2.落子操作 :
????????玩家通過鼠標(biāo)點(diǎn)擊棋盤上的交叉點(diǎn)來落子,黑子和白子輪流交替落子。
????????只有在空的交叉點(diǎn)上才能落子。
3.勝負(fù)判斷 :
????????每次落子后,檢查是否有五子連珠的情況(橫向、縱向、正斜向、反斜向)。
????????如果有一方達(dá)成五子連珠,則判定該方獲勝。
4.游戲結(jié)束處理 :
????????游戲結(jié)束時(shí),在屏幕中央顯示獲勝信息(如“Player 1 Win!” 或 “Player 2 Win!”)。
????????同時(shí)顯示操作提示,告知玩家可以按 “Q” 鍵退出游戲,按 “N” 鍵重新開始游戲。
????????顯示一個(gè)半透明的黑色提示框,將獲勝信息和操作提示包含在內(nèi)。
5.重新開始和退出 :
????????游戲結(jié)束后,玩家可以按 “Q” 鍵退出游戲,按 “N” 鍵重新開始游戲。
????????重新開始游戲時(shí),清空棋盤,重置當(dāng)前玩家為黑子先手。
二、關(guān)鍵模塊
1. 常量定義
????????定義游戲中使用的各種常量,如棋盤大小、網(wǎng)格大小、窗口尺寸、顏色、字體等。
2. 初始化
????????初始化 Pygame 庫。
????????創(chuàng)建游戲窗口。
????????初始化棋盤狀態(tài)。
3. 繪制模塊
????????繪制棋盤:使用木頭淡黃色填充屏幕,并繪制黑色的網(wǎng)格線。
????????繪制棋子:根據(jù)棋盤狀態(tài),在相應(yīng)位置繪制黑子和白子。
4. 勝負(fù)判斷
????????檢查當(dāng)前落子位置是否形成五子連珠,分別從橫向、縱向、正斜向、反斜向四個(gè)方向進(jìn)行檢查。
5. 游戲控制
????????處理鼠標(biāo)點(diǎn)擊事件,實(shí)現(xiàn)落子操作。
????????處理鍵盤事件,實(shí)現(xiàn)退出游戲和重新開始游戲的功能。
????????控制游戲的主循環(huán),更新游戲狀態(tài)。 6. 提示信息顯示模塊
????????在游戲結(jié)束時(shí),顯示獲勝信息和操作提示,并繪制半透明的提示框。
三、完整代碼
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)
# 創(chuàng)建游戲窗口
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 * dx
new_y = y + i * dy
if 0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE and board[new_x][new_y] == player:
count += 1
else:
break
# 反向檢查
for i in range(1, 5):
new_x = x - i * dx
new_y = y - i * dy
if 0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE and board[new_x][new_y] == player:
count += 1
else:
break
if count >= 5:
return True
return False
# 重置游戲狀態(tài)
def reset_game():
global board, current_player, game_over
board = [[0] * BOARD_SIZE for _ in range(BOARD_SIZE)]
current_player = 1
game_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)
# 創(chuàng)建半透明的矩形框
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))
# 調(diào)整內(nèi)邊距以確保提示框足夠大
padding = 30
combined_width = max(winner_rect.width, prompt_rect.width) + 2 * padding
combined_height = winner_rect.height + prompt_rect.height + 3 * padding
rect = pygame.Rect(winner_rect.x - padding, winner_rect.y - padding, combined_width, combined_height)
rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
# 創(chuàng)建一個(gè)帶有透明度的表面
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()
# 主游戲循環(huán)
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.pos
col = (x - GRID_SIZE // 2) // GRID_SIZE
row = (y - GRID_SIZE // 2) // GRID_SIZE
if 0 <= col < BOARD_SIZE and 0 <= row < BOARD_SIZE and board[row][col] == 0:
board[row][col] = current_player
if check_win(row, col, current_player):
print(f"Player {current_player} Win!")
# 先顯示最后一個(gè)子
draw_board()
draw_pieces()
pygame.display.flip()
# 顯示獲勝信息和操作提示
show_winner_info(current_player)
game_over = True
# 進(jìn)入等待狀態(tài),直到用戶按下 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 = False
current_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()
四、代碼運(yùn)行方式
1.代碼運(yùn)行環(huán)境
????????Python 3.x
????????Pygame 庫
pip install pygame
2.運(yùn)行代碼
????????將上述代碼保存為 gobang_game.py 文件,然后在終端中運(yùn)行以下命令:
python gobang_game.py
3.游戲操作說明
????????游戲開始后,黑子先手,玩家通過鼠標(biāo)點(diǎn)擊棋盤上的交叉點(diǎn)落子。 ????????當(dāng)有一方達(dá)成五子連珠時(shí),游戲結(jié)束,屏幕中央會顯示獲勝信息和操作提示。 ????????按 “Q” 鍵退出游戲,按 “N” 鍵重新開始游戲。
4.游戲畫面
柚子快報(bào)激活碼778899分享:五子棋小游戲-簡單開發(fā)版
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。