python五子棋游戏源代码
时间: 2023-12-11 22:33:40 浏览: 109
python游戏源码-09 五子棋.zip源码python项目实例源码打包下载
5星 · 资源好评率100%
以下是Python实现的五子棋游戏源代码,代码中包含了游戏界面的绘制、玩家与电脑的下棋逻辑以及胜负判断等功能。如果需要获取完整的源码,可以关注微信公众号“码农升级”并后台回复“五子棋”获取。
```python
import pygame
import sys
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口大小
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 640
CHESSBOARD_SIZE = 600
CHESSBOARD_LEFT = (WINDOW_WIDTH - CHESSBOARD_SIZE) // 2
CHESSBOARD_TOP = (WINDOW_HEIGHT - CHESSBOARD_SIZE) // 2
CHESS_SIZE = 30
CHESS_RADIUS = CHESS_SIZE // 2
LINE_WIDTH = 2
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("五子棋")
# 加载棋子图片
black_chess = pygame.image.load("black.png")
white_chess = pygame.image.load("white.png")
# 创建棋盘
chessboard = [[0] * 15 for i in range(15)]
# 判断是否结束游戏
game_over = False
# 判断当前是玩家还是电脑下棋
player_turn = True
# 判断是否可以悔棋
can_undo = False
# 判断是否可以撤销悔棋
can_redo = False
# 记录下棋步骤
steps = []
# 判断是否是第一次下棋
first_move = True
# 判断是否是电脑先手
computer_first = False
# 判断是否显示提示信息
show_tip = False
# 判断是否已经选择了难度
difficulty_selected = False
# 判断是否已经选择了先手或后手
turn_selected = False
# 判断是否已经开始游戏
game_started = False
# 判断是否已经结束游戏
game_ended = False
# 判断是否已经退出游戏
game_quit = False
# 判断是否已经显示了胜利信息
show_win_info = False
# 判断是否已经显示了平局信息
show_draw_info = False
# 判断是否已经显示了悔棋信息
show_undo_info = False
# 判断是否已经显示了撤销悔棋信息
show_redo_info = False
# 判断是否已经显示了提示信息
show_tip_info = False
# 判断是否已经显示了难度选择界面
show_difficulty_select = False
# 判断是否已经显示了先手后手选择界面
show_turn_select = False
# 判断是否已经显示了游戏开始界面
show_game_start = False
# 判断是否已经显示了游戏结束界面
show_game_end = False
# 判断是否已经显示了退出游戏界面
show_game_quit = False
# 判断是否已经显示了重新开始界面
show_restart = False
# 判断是否已经显示了重新开始确认界面
show_restart_confirm = False
# 判断是否已经显示了重新开始取消界面
show_restart_cancel = False
# 判断是否已经显示了重新开始提示信息
show_restart_info = False
# 判断是否已经显示了重新开始确认提示信息
show_restart_confirm_info = False
# 判断是否已经显示了重新开始取消提示信息
show_restart_cancel_info = False
# 判断是否已经显示了背景音乐开关
show_music_switch = False
# 判断是否已经显示了音效开关
show_sound_switch = False
# 判断是否已经播放了背景音乐
music_played = False
# 判断是否已经播放了下棋音效
sound_played = False
# 判断是否已经播放了胜利音效
win_sound_played = False
# 判断是否已经播放了平局音效
draw_sound_played = False
# 判断是否已经播放了悔棋音效
undo_sound_played = False
# 判断是否已经播放了撤销悔棋音效
redo_sound_played = False
# 判断是否已经播放了提示音效
tip_sound_played = False
# 判断是否已经播放了重新开始音效
restart_sound_played = False
# 判断是否已经播放了退出游戏音效
quit_sound_played = False
# 判断是否已经播放了背景音乐
music_on = True
# 判断是否已经开启了音效
sound_on = True
# 加载背景音乐
pygame.mixer.music.load("bgm.mp3")
# 加载音效
move_sound = pygame.mixer.Sound("move.wav")
win_sound = pygame.mixer.Sound("win.wav")
draw_sound = pygame.mixer.Sound("draw.wav")
undo_sound = pygame.mixer.Sound("undo.wav")
redo_sound = pygame.mixer.Sound("redo.wav")
tip_sound = pygame.mixer.Sound("tip.wav")
restart_sound = pygame.mixer.Sound("restart.wav")
quit_sound = pygame.mixer.Sound("quit.wav")
# 设置背景音乐音量
pygame.mixer.music.set_volume(0.5)
# 设置音效音量
move_sound.set_volume(0.5)
win_sound.set_volume(0.5)
draw_sound.set_volume(0.5)
undo_sound.set_volume(0.5)
redo_sound.set_volume(0.5)
tip_sound.set_volume(0.5)
restart_sound.set_volume(0.5)
quit_sound.set_volume(0.5)
# 播放背景音乐
pygame.mixer.music.play(-1)
# 绘制棋盘
def draw_chessboard():
screen.fill(WHITE)
pygame.draw.rect(screen, BLACK, (CHESSBOARD_LEFT, CHESSBOARD_TOP, CHESSBOARD_SIZE, CHESSBOARD_SIZE), LINE_WIDTH)
for i in range(15):
pygame.draw.line(screen, BLACK, (CHESSBOARD_LEFT + CHESS_SIZE // 2, CHESSBOARD_TOP + CHESS_SIZE // 2 + i * CHESS_SIZE), (CHESSBOARD_LEFT + CHESSBOARD_SIZE - CHESS_SIZE // 2, CHESSBOARD_TOP + CHESS_SIZE // 2 + i * CHESS_SIZE), LINE_WIDTH)
pygame.draw.line(screen, BLACK, (CHESSBOARD_LEFT + CHESS_SIZE // 2 + i * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2), (CHESSBOARD_LEFT + CHESS_SIZE // 2 + i * CHESS_SIZE, CHESSBOARD_TOP + CHESSBOARD_SIZE - CHESS_SIZE // 2), LINE_WIDTH)
pygame.draw.circle(screen, BLACK, (CHESSBOARD_LEFT + CHESSBOARD_SIZE // 2, CHESSBOARD_TOP + CHESSBOARD_SIZE // 2), 5, 0)
pygame.draw.circle(screen, BLACK, (CHESSBOARD_LEFT + CHESS_SIZE // 2 + 3 * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2 + 3 * CHESS_SIZE), 5, 0)
pygame.draw.circle(screen, BLACK, (CHESSBOARD_LEFT + CHESS_SIZE // 2 + 11 * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2 + 3 * CHESS_SIZE), 5, 0)
pygame.draw.circle(screen, BLACK, (CHESSBOARD_LEFT + CHESS_SIZE // 2 + 3 * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2 + 11 * CHESS_SIZE), 5, 0)
pygame.draw.circle(screen, BLACK, (CHESSBOARD_LEFT + CHESS_SIZE // 2 + 11 * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2 + 11 * CHESS_SIZE), 5, 0)
# 绘制棋子
def draw_chess(x, y, color):
if color == 1:
screen.blit(black_chess, (x - CHESS_RADIUS, y - CHESS_RADIUS))
else:
screen.blit(white_chess, (x - CHESS_RADIUS, y - CHESS_RADIUS))
# 判断是否胜利
def check_win(x, y, color):
count = 1
# 横向判断
for i in range(1, 5):
if x - i < 0 or chessboard[x - i][y] != color:
break
count += 1
for i in range(1, 5):
if x + i > 14 or chessboard[x + i][y] != color:
break
count += 1
if count >= 5:
return True
count = 1
# 纵向判断
for i in range(1, 5):
if y - i < 0 or chessboard[x][y - i] != color:
break
count += 1
for i in range(1, 5):
if y + i > 14 or chessboard[x][y + i] != color:
break
count += 1
if count >= 5:
return True
count = 1
# 左上到右下判断
for i in range(1, 5):
if x - i < 0 or y - i < 0 or chessboard[x - i][y - i] != color:
break
count += 1
for i in range(1, 5):
if x + i > 14 or y + i > 14 or chessboard[x + i][y + i] != color:
break
count += 1
if count >= 5:
return True
count = 1
# 右上到左下判断
for i in range(1, 5):
if x - i < 0 or y + i > 14 or chessboard[x - i][y + i] != color:
break
count += 1
for i in range(1, 5):
if x + i > 14 or y - i < 0 or chessboard[x + i][y - i] != color:
break
count += 1
if count >= 5:
return True
return False
# 玩家下棋
def player_move(x, y):
global player_turn, can_undo, can_redo, steps, first_move, show_tip, show_tip_info, sound_played
if not game_over and player_turn and chessboard[x][y] == 0:
chessboard[x][y] = 1
draw_chess(CHESSBOARD_LEFT + CHESS_SIZE // 2 + x * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2 + y * CHESS_SIZE, 1)
steps.append((x, y))
can_undo = True
can_redo = False
if first_move:
first_move = False
else:
if show_tip:
show_tip_info = True
tip_sound.play()
tip_sound_played = True
else:
sound_played = True
move_sound.play()
if check_win(x, y, 1):
game_over = True
show_win_info = True
win_sound.play()
win_sound_played = True
else:
player_turn = False
# 电脑下棋
def computer_move():
global player_turn, can_undo, can_redo, steps, first_move, show_tip, show_tip_info, sound_played
if not game_over and not player_turn:
if difficulty == 1:
x, y = random.randint(0, 14), random.randint(0, 14)
while chessboard[x][y] != 0:
x, y = random.randint(0, 14), random.randint(0, 14)
else:
x, y = 0, 0
max_score = -1
for i in range(15):
for j in range(15):
if chessboard[i][j] == 0:
score = get_score(i, j, 2)
if score > max_score:
max_score = score
x, y = i, j
chessboard[x][y] = 2
draw_chess(CHESSBOARD_LEFT + CHESS_SIZE // 2 + x * CHESS_SIZE, CHESSBOARD_TOP + CHESS_SIZE // 2 + y * CHESS_SIZE, 2)
steps.append((x, y))
can_undo = True
can_redo = False
if first_move:
first_move = False
else:
if show_tip:
show_tip_info = True
tip_sound.play()
tip_sound_played = True
else:
sound_played = True
move_sound.play()
if check_win(x, y, 2):
game_over = True
show_win_info = True
win_sound.play()
win_sound_played = True
else:
player_turn = True
# 获取当前
阅读全文