Client can send answer to server to check.

This commit is contained in:
snsd0805 2020-06-28 04:08:42 +08:00
parent 7d520f9522
commit 14be453ef7
2 changed files with 17 additions and 5 deletions

View File

@ -58,7 +58,7 @@ def sendDraw(sock):
clock.tick(30)
def guessInput(screen):
def guessInput(screen,sock):
guessStr = ""
while True:
for event in pygame.event.get() :
@ -69,6 +69,9 @@ def guessInput(screen):
if event.key>=97 and event.key<122: #a~z
guessStr = guessStr + chr(event.key)
elif event.key == 13: #enter,send to server,clean
sock.send(guessStr.encode('utf-8'))
ans = sock.recv(1024).decode('utf-8')
print(ans)
guessStr = ""
elif event.key == 8 : #backspace
guessStr = guessStr[0:-1]
@ -95,7 +98,7 @@ def receiveDraw(sock):
pygame.display.update()
print("draw start")
guessThreading = threading.Thread(target=guessInput,args=(screen,)) # guest input
guessThreading = threading.Thread(target=guessInput,args=(screen,sock)) # guest input
guessThreading.setDaemon(False)
guessThreading.start()

15
main.py
View File

@ -86,6 +86,9 @@ class Server:
class Room:
startFlag = False
sockList = [] # Client's sock list
problem = "apple"
def __init__(self,ip,portNum):
self.ip = ip
self.portNum = portNum
@ -120,9 +123,15 @@ class Room:
data = origin.decode('utf-8')
if data:
print(data)
for clientSock in self.sockList: # 遍歷socket list
if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN
clientSock.send(origin)
if data[0]=='(': # Form MAIN CLIENT,it is position data
for clientSock in self.sockList: # 遍歷socket list
if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN
clientSock.send(origin)
else: # it is from other client. He/she want to send answer to check the answer
if data == self.problem:
sock.send('y'.encode('utf-8'))
else:
sock.send('n'.encode('utf-8'))
class Client:
def __init__(self,ip,port):