• 大小: 904KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2022-10-05
  • 语言: Python
  • 标签: python  pygame  五子棋  

资源简介

python使用pygame实现的单机五子棋源码,自动判断输赢

资源截图

代码片段和文件信息

import socket
import threading
import json
import time

receiveData  = [] # 存放发送的数据
sendData     = [] # 存放接收的数据

rCondition = threading.Condition()
sCondition = threading.Condition()

# 收数据的
class ChessServer(threading.Thread):

    def __init__(self ip=‘10.0.14.204‘ port=18090 connectSize=100):
        ‘‘‘
        :param port: 服务器的端口号
        :param connectSize: 默认的并发数量
        ‘‘‘
        threading.Thread.__init__(self)
        self.__ip = ip
        self.__port = port
        self.__connectSize = connectSize
        pass

    def run(self):
        ‘‘‘
        服务启动主程序
        :return:
        ‘‘‘
        server = None
        try:
            server = socket.socket(socket.AF_INET socket.SOCK_STREAM)
            server.setsockopt(socket.SOL_SOCKET socket.SO_REUSEADDR 1)
            server.bind((self.__ip self.__port))
            server.listen(self.__connectSize)
            while True:
                clientConn clientAddr = server.accept()  # 等待客户端请求
                # 启动独立的线程,处理每一次用户请求
                wt = WorkThread(clientConn clientAddr)
                wt.start()
                pass
        except socket.gaierror as g:
            print(g)
        finally:
            server.close()

        pass

    pass

class WorkThread(threading.Thread):
    def __init__(self connection addr bufferSize=8096):
        threading.Thread.__init__(self)
        self.__connection = connection
        self.__addr = addr
        self.__bufferSize = bufferSize
        pass

    def run(self):
        while True:
            receiveMsg = self.__connection.recv(self.__bufferSize)
            print(“收到的原始数据:“ + json.loads(receiveMsg.decode(“utf-8“)))
            if rCondition.acquire():
                print(“开始收数据:“ + json.loads(receiveMsg.decode(“utf-8“)))
                obj = json.loads(receiveMsg.decode(“utf-8“))
                print(“rev:“ obj)
                receiveData.append(obj)
                pass
            rCondition.release()
            pass
        # self.__connection.close()
        pass
    pass

# 发数据的
class ChessClient(threading.Thread):

    def __init__(self ip=‘10.0.14.204‘ port=18080 bufferSize=8096):
        threading.Thread.__init__(self)
        self.__ip = ip
        self.__port = port
        self.__bufferSize = 8096
        pass

    def run(self):
        client = socket.socket(socket.AF_INET socket.SOCK_STREAM)
        time.sleep(30)
        client.connect((self.__ip self.__port))
        print(“连接服务器成功:“ self.__port)
        while True:
            if rCondition.acquire():
                print(“开始发送数据:“ sendData)
                if len(sendData) >= 1:
                    client.send((json.dumps(sendData[0])).encode(“utf-8“))
                    sendData.remove(sendData[0])

                    print(“发送数据成功:“ sendData[0])
                else:
                    rCondition.wait()
                    pass

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3249  2019-08-16 16:33  day19\chessserver.py

     文件       6897  2019-08-29 14:04  day19\fivechess.py

     文件     448072  2019-02-28 12:57  day19\images\bg.png

     文件       2444  2019-08-29 13:58  day19\images\black.png

     文件       7451  2019-08-16 14:01  day19\images\result.jpg

     文件       1173  2019-02-28 12:57  day19\images\storn_black.png

     文件       1268  2019-02-28 12:57  day19\images\storn_white.png

     文件       3103  2019-08-29 13:56  day19\images\white.png

     文件     461091  2019-08-29 14:49  day19\images.rar

     文件          0  2019-08-16 15:07  day19\__init__.py

     目录          0  2019-08-29 13:59  day19\images

     目录          0  2019-08-29 14:49  day19

----------- ---------  ---------- -----  ----

               934748                    12


评论

共有 条评论