From 14be453ef727ab6ba949434dc5c8c1b3b045dd14 Mon Sep 17 00:00:00 2001 From: snsd0805 Date: Sun, 28 Jun 2020 04:08:42 +0800 Subject: [PATCH] Client can send answer to server to check. --- draw.py | 7 +++++-- main.py | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/draw.py b/draw.py index 86d2415..d8c5a7e 100644 --- a/draw.py +++ b/draw.py @@ -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() diff --git a/main.py b/main.py index 142bdf9..8deaaa4 100644 --- a/main.py +++ b/main.py @@ -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):