栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

五子棋(Python)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

五子棋(Python)

五子棋(Python)
  • 效果图
  • 代码

效果图

代码
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  # 鼠标点击的x轴坐标
pos_y = -1  # 鼠标点击的y轴坐标
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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/829648.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号