用PYTHON写个小游戏,实现通讯功能,代码模块有了,不知道该怎么添加
这是游戏部分
import random
import pgzrun
import time
HEIGHT = 800
WIDTH = 450
WORD = '''春风化雨 天罗地网 一心一意 一言为定 头头是道 助人为乐 井井有条 乐极生悲 万水千山 桃红柳绿
贪生怕死 走马观花 一路平安 莺歌燕舞 鸟语花香 目中无人 旁若无人 百发百中 安居乐业 拔苗助长 风和日丽
风平浪静 东山再起 马到成功 满面春风 毛手毛脚 长年累月 面目全非 天网恢恢 瓜田李下'''.split()
background = Actor('background')
ground = Actor('background')
daan = ('')
def draw():
tishi = random.sample(range(4), 2)
tiankong = 'XXXX'
xuanci = random.choice(WORD)
for i in tishi: # 把X换成提示字
tiankong = tiankong[:i] + xuanci[i] + tiankong [i+1:]
ground.draw()
screen.draw.text("全 民 猜 猜 猜", (70, 75), fontsize=50,
fontname='s', color='red')
screen.draw.text(tiankong, (80, 180), fontsize=80,
fontname='s', color='green')
pgzrun.go()
这是接收部分
#-*- coding:utf-8 -*-
#vxcode : tiantianjt2020
import datetime
import socket
import time
HOST = '0.0.0.0'
PORT = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(5)
def sub_run_time(timetemp):
start = datetime.datetime.fromtimestamp(timetemp)
run_time = datetime.datetime.now()-start
return int(run_time.microseconds / 1000.0)
print('server start at: %s:%s' % (HOST, PORT))
while True:
print("wait client connect.....")
conn, addr = s.accept()
print('connected by ' + str(addr))
while True:
start = datetime.datetime.now()
try:
indata = conn.recv(1024)
except:
conn.close()
print('client closed connection.')
break
if len(indata) == 0: # connection closed
conn.close()
print('client closed connection.')
break
print('accept:'+str(indata.decode()))
conn.send(str("server name:zhangsan,path: D://files").encode())
run_time = datetime.datetime.now()-start
print('alltime:'+str(int(run_time.microseconds / 1000.0))+'ms')
time.sleep(5)
continue
这是发送部分
#-*- coding:utf-8 -*-
import datetime
import socket
import time
import sys
HOST = '127.0.0.1'
PORT = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
def sub_run_time(timetemp):
start = datetime.datetime.fromtimestamp(timetemp)
run_time = datetime.datetime.now()-start
return int(run_time.microseconds / 1000.0)
def err(a,b,c):
s.close()
sys.excepthook = err
while True:
try:
start = datetime.datetime.now()
outdata = input('please input message: ')
print('send: ' + outdata)
s.send(outdata.encode())
try:
indata = s.recv(1024)
except Exception as e:
print(e)
s.close()
print('server closed connection.')
break
print('aceept:'+str(indata.decode()))
run_time = datetime.datetime.now()-start
print('alltime:'+str(int(run_time.microseconds / 1000.0))+'ms')
time.sleep(5)
except Exception as e:
print(e)
s.close()
print('server closed connection.')