python实现的socket石头剪刀布,带数据库
【文件目录】socket游戏├── 规则.py
├── 主程序.py
├── 单人游戏.py
├── 联机游戏.py
├── 账号操作.py
├── 获取公网ip.py
└── 石头剪刀布数据库.txt
0 directories, 7 files
def p1_socket():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
print("p2输入{}即可建立连接".format(host))
server_socket.bind((host, 12345))
server_socket.listen(1)
client_socket,addr = server_socket.accept()
while True:
p1 = input("请输入“石头”或“剪刀”或“布”或“退出”:\n")
p2 = client_socket.recv(32).decode('utf-8')
result = 规则.pvp(p1, p2)
print(result)
client_socket.send(result.encode('utf-8'))
if result == "游戏结束":
client_socket.close()
break
def p2_socket():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = input("请输入p1玩家ip")
client_socket.connect((host, 12345))
while True:
p2 = input("请输入“石头”或“剪刀”或“布”或“退出”:\n")
client_socket.send(p2.encode('utf-8'))
result = client_socket.recv(128).decode('utf-8')
print(result)
if result == "游戏结束":
client_socket.close()
break
评论