Fix: Let Main client which is drawing can exit the program

This commit is contained in:
snsd0805 2020-06-29 04:53:24 +08:00
parent f8fb53fca5
commit 5e387a4cd3
2 changed files with 17 additions and 7 deletions

14
draw.py
View File

@ -24,7 +24,10 @@ def drawerReceive(sock,screen):
elif data == "restart": elif data == "restart":
print("restart") print("restart")
break break
elif data == 'exitok':
break
print("drawerRECEIVE CLOSED") print("drawerRECEIVE CLOSED")
def sendDraw(sock,nowUserList,screen,problem): def sendDraw(sock,nowUserList,screen,problem):
# Set white background # Set white background
@ -34,7 +37,8 @@ def sendDraw(sock,nowUserList,screen,problem):
pgStringVar = pygame.font.Font(None,30).render("Problem: {}".format(problem),False,(0,0,0))# 文字物件 pgStringVar = pygame.font.Font(None,30).render("Problem: {}".format(problem),False,(0,0,0))# 文字物件
screen.blit(pgStringVar,(30,450)) screen.blit(pgStringVar,(30,450))
pygame.draw.rect(screen,(171, 254, 250),[100,400,50,20],0) pygame.draw.rect(screen,(171, 254, 250),[100,400,100,50],0)
pygame.draw.rect(screen,(255, 0, 0),[250,400,100,50],0)
white= (255, 255, 255) white= (255, 255, 255)
black= (0, 0, 0) black= (0, 0, 0)
@ -65,14 +69,18 @@ def sendDraw(sock,nowUserList,screen,problem):
print ('mouse pressed',pygame.mouse.get_pressed()) print ('mouse pressed',pygame.mouse.get_pressed())
if pygame.mouse.get_pressed()[0]: if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos() pos = pygame.mouse.get_pos()
# 100,400,50,20 # 100,400,100,50
if pos[0]>=100 and pos[0]<=150 and pos[1]>=400 and pos[1]<=420: if pos[0]>=100 and pos[0]<=200 and pos[1]>=400 and pos[1]<=450:
sock.send("[restart]".encode('utf-8')) sock.send("[restart]".encode('utf-8'))
#if data == 'restart': #if data == 'restart':
time.sleep(1) #drawerReceive time.sleep(1) #drawerReceive
#detectFlag = False #detectFlag = False
return False return False
#print("restart") #print("restart")
# 250,400,100,50
if pos[0]>=250 and pos[0]<=350 and pos[1]>=400 and pos[1]<=450:
sock.send('exit'.encode('utf-8'))
return True
mouseFlag = True mouseFlag = True
# 如果釋放滑鼠 # 如果釋放滑鼠
elif event.type == pygame.MOUSEBUTTONUP: elif event.type == pygame.MOUSEBUTTONUP:

10
main.py
View File

@ -85,7 +85,7 @@ class Server:
sock.send("FAIL".encode('utf-8')) sock.send("FAIL".encode('utf-8'))
class Room: class Room:
startFlag = False startFlag = True
sockList = [] # Client's sock list sockList = [] # Client's sock list
def __init__(self,ip,portNum): def __init__(self,ip,portNum):
@ -100,7 +100,7 @@ class Room:
print("\t{}:{}".format(self.ip,self.portNum)) print("\t{}:{}".format(self.ip,self.portNum))
listensock.listen(5) listensock.listen(5)
emptyFlag = True emptyFlag = True
while True: while self.startFlag:
sock,sockname = listensock.accept() sock,sockname = listensock.accept()
print("[ {} ]{} has connected.".format(self.portNum,sockname)) print("[ {} ]{} has connected.".format(self.portNum,sockname))
@ -150,7 +150,7 @@ class Room:
if data[0]=='(': # Form MAIN CLIENT,it is position data if data[0]=='(': # Form MAIN CLIENT,it is position data
for clientSock in self.sockList: # 遍歷socket list for clientSock in self.sockList: # 遍歷socket list
if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN
clientSock.send(origin) clientSock.send(origin)
elif data=='[restart]': # [restart] elif data=='[restart]': # [restart]
print('game restart') print('game restart')
time.sleep(1) time.sleep(1)
@ -171,7 +171,9 @@ class Room:
allPeerName.append(i.getpeername()) allPeerName.append(i.getpeername())
for sock in self.sockList: for sock in self.sockList:
sock.send("[list] {}".format(json.dumps(allPeerName)).encode('utf-8')) sock.send("[list] {}".format(json.dumps(allPeerName)).encode('utf-8'))
# Close Process
if len(self.sockList):
self.startFlag = False
else: else:
sock.send('n'.encode('utf-8')) sock.send('n'.encode('utf-8'))