五子棋(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()

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
五子棋是一种非常受欢迎的棋类游戏,使用Python编程语言可以实现五子棋的功能。首先,可以定义一个二维数组来表示棋盘上的落子情况,其中0表示没有子,1表示黑子,-1表示白子。可以使用列表生成器来创建一个15×15的二维数组。 在实现落子功能时,可以使用鼠标点击事件来确定落子的位置。通过遍历每个点,判断鼠标点击的位置与遍历的位置的距离,如果距离在范围内,并且该位置上没有子,则可以实现落子。然后需要交替更改落子者。 判断是否五子连线可以按照以下几种情况进行判断: 1. 横向:从落子位置所在行的第一个位置开始,判断往右5个位置是否都是刚才所落颜色的子,如果是则判断为五子连线,否则继续判断下一组5个位置,直到判断到最后一个子。 2. 纵向:同样从落子位置所在列的第一个位置开始,判断往下5个位置是否都是刚才所落颜色的子,如果是则判断为五子连线,否则继续判断下一组5个位置,直到判断到最后一个子。 3. 对角线:从落子位置开始,分别判断左上到右下和左下到右上两个方向的斜线上的5个位置是否都是刚才所落颜色的子,如果是则判断为五子连线,否则继续判断下一组5个位置,直到判断到最后一个子。 4. 反对角线:同样从落子位置开始,分别判断左下到右上和左上到右下两个方向的斜线上的5个位置是否都是刚才所落颜色的子,如果是则判断为五子连线,否则继续判断下一组5个位置,直到判断到最后一个子。 通过以上步骤,就可以实现五子棋的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python实现简单五子棋](https://blog.csdn.net/djasjkyu/article/details/124881713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值