加入收藏 | 设为首页 | 会员中心 | 我要投稿 南通站长网 (https://www.0513zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

使用python如何写一个五子棋游戏?

发布时间:2022-04-07 15:39:17 所属栏目:语言 来源:互联网
导读:五子棋应该很多朋友都有玩过,这是一款比较简单的棋类游戏。那么我们如果使用python,怎么写一个网络五子棋游戏呢?下面就给大家分享使用Python实现网络五子棋代码,感兴趣的朋友可以参考学习。 服务器端: import os import socket import threading from t
     五子棋应该很多朋友都有玩过,这是一款比较简单的棋类游戏。那么我们如果使用python,怎么写一个网络五子棋游戏呢?下面就给大家分享使用Python实现网络五子棋代码,感兴趣的朋友可以参考学习。
 
       服务器端:
import os
import socket
import threading
 
from tkinter import *
from tkinter.messagebox import *
 
 
def drawQiPan():
    for i in range(0, 15):
        cv.create_line(20, 20 + 40 * i, 580, 20 + 40 * i, width=2)
    for i in range(0, 15):
        cv.create_line(20 + 40 * i, 20, 20 + 40 * i, 580, width=2)
    cv.pack()
 
 
# 走棋函数
def callPos(event):
    global turn
    global MyTurn
    if MyTurn == -1:  # 第一次确认自己的角色
        MyTurn = turn
    else:
        if MyTurn != turn:
            showinfo(title="提示", message="还没轮到自己下棋")
            return
    # print("clicked at",event.x,event.y,true)
    x = event.x // 40
    y = event.y // 40
    print("clicked at", x, y, turn)
    if maps[x][y] != " ":
        showinfo(title="提示", message="已有棋子")
    else:
        img1 = images[turn]
        cv.create_image((x * 40 + 20, y * 40 + 20), image=img1)
        cv.pack()
        maps[x][y] = str(turn)
        pos = str(x) + "," + str(y)
        sendMessage("move|" + pos)
        print("服务器走的位置", pos)
        label1["text"] = "服务器走的位置" + pos
        # 输出输赢信息
        if win_lose():
            if turn == 0:
 
 
# 退出函数
def callExit(event):
    pos = "exit|"
    sendMessage(pos)
    os.exit()

(编辑:南通站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读