效果图
代码
import pgzrun
TITLE = "五子棋"
WIDTH = 640
HEIGHT = 480
backgroun = Actor("chess_background")
chess_pieces = []
who_play = 0
chessboard = [([-1] * 11) for i in range(11)]
pos_x = -1
pos_y = -1
isFinish = False
color = ["red", "black"]
def draw():
screen.clear()
backgroun.draw()
screen.draw.text("玩家1", (50, 220), fontsize=20, fontname='simhei', color=color[who_play % 2])
screen.draw.text("白棋", (53, 260), fontsize=20, fontname='simhei', color="black")
screen.draw.text("玩家2", (540, 220), fontsize=20, fontname='simhei', color=color[(who_play + 1) % 2])
screen.draw.text("黑棋", (543, 260), fontsize=20, fontname='simhei', color="black")
for i in range(11):
screen.draw.line((120, 40 + 40 * i), (520, 40 + 40 * i), "black")
screen.draw.line((120 + 40 * i, 40), (120 + 40 * i, 440), "black")
for chess_piece in chess_pieces:
chess_piece.draw()
if isFinish:
if dogfall(chessboard):
screen.draw.text('旗鼓相当', (205, 175), fontsize=50, fontname='fzshuangqtjw_cu', color="red")
elif who_play % 2 == 0:
screen.draw.text('玩家2获胜', (205, 175), fontsize=50, fontname='fzshuangqtjw_cu', color="red")
elif who_play % 2 == 1:
screen.draw.text('玩家1获胜', (205, 175), fontsize=50, fontname='fzshuangqtjw_cu', color="red")
screen.draw.text('重新开始', (240, 255), fontsize=40, fontname='fzshuangqtjw_cu', color="green")
def update():
global pos_x, pos_y, isFinish
if not isFinish:
t = who_play % 2
for i in range(pos_y - 4, pos_y + 1):
if i >= 0 and i < 7 and chessboard[pos_x][i] == chessboard[pos_x][i + 1] == chessboard[pos_x][i + 2] == \
chessboard[pos_x][i + 3] == chessboard[pos_x][i + 4] != -1:
if t:
isFinish = True
else:
isFinish = True
for i in range(pos_x - 4, pos_x + 1):
if i >= 0 and i < 7 and chessboard[i][pos_y] == chessboard[i + 1][pos_y] == chessboard[i + 2][pos_y] == \
chessboard[i + 3][pos_y] == chessboard[i + 4][pos_y] != -1:
if t:
isFinish = True
else:
isFinish = True
for i, j in zip(range(pos_x - 4, pos_x + 1), range(pos_y - 4, pos_y + 1)):
if i >= 0 and i < 7 and j >= 0 and j < 7 and chessboard[i][j] == chessboard[i + 1][j + 1] == \
chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] == chessboard[i + 4][j + 4] != -1:
if t:
isFinish = True
else:
isFinish = True
for i, j in zip(range(pos_x - 4, pos_x + 1), range(pos_y + 4, pos_y - 1, -1)):
if i >= 0 and i < 7 and j <= 10 and chessboard[i][j] == chessboard[i + 1][j - 1] == chessboard[i + 2][
j - 2] == chessboard[i + 3][j - 3] == chessboard[i + 4][j - 4] != -1:
if t:
isFinish = True
else:
isFinish = True
if dogfall(chessboard):
isFinish = True
def on_mouse_down(pos, button):
global who_play, pos_x, pos_y, isFinish, chess_pieces, chessboard
if button == mouse.LEFT and not isFinish:
for i in range(11):
for j in range(11):
if abs(pos[0] - 120 - 40 * i) < 20 and abs(pos[1] - 40 - 40 * j) < 20:
pos_x = j
pos_y = i
if who_play % 2 == 0 and chessboard[j][i] == -1:
white_chess_piece = Actor("white_chess_piece")
white_chess_piece.left = 102 + 40 * i
white_chess_piece.top = 22 + 40 * j
chess_pieces.append(white_chess_piece)
who_play += 1
chessboard[j][i] = 0
elif who_play % 2 == 1 and chessboard[j][i] == -1:
black_chess_piece = Actor("black_chess_piece")
black_chess_piece.left = 102 + 40 * i
black_chess_piece.top = 22 + 40 * j
chess_pieces.append(black_chess_piece)
who_play += 1
chessboard[j][i] = 1
if isFinish:
if button == mouse.LEFT and pos[0] > 240 and pos[0] < 400 and pos[1] > 255 and pos[1] < 300:
chess_pieces.clear()
who_play = 0
pos_x = -1
pos_y = -1
chessboard = [([-1] * 11) for i in range(11)]
isFinish = False
def dogfall(chessboard):
for i in range(11):
for j in range(11):
if chessboard[i][j] == -1:
return False
return True
pgzrun.go()
|