From 5ac4e377a5e753404b1902354e9f97b9d6340347 Mon Sep 17 00:00:00 2001 From: snsd0805 Date: Mon, 29 Jun 2020 02:26:51 +0800 Subject: [PATCH] Main client can restart the game --- draw.py | 11 ++++++++++- main.py | 25 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/draw.py b/draw.py index e21ee1b..6449d27 100644 --- a/draw.py +++ b/draw.py @@ -31,6 +31,7 @@ def sendDraw(sock,nowUserList,screen,problem): pgStringVar = pygame.font.Font(None,30).render("Problem: {}".format(problem),False,(0,0,0))# 文字物件 screen.blit(pgStringVar,(30,450)) + pygame.draw.rect(screen,(171, 254, 250),[100,400,50,20],0) white= (255, 255, 255) black= (0, 0, 0) @@ -53,12 +54,20 @@ def sendDraw(sock,nowUserList,screen,problem): for event in pygame.event.get(): if event.type== pygame.QUIT: detectFlag = False + return True # 如果按下滑鼠 - # get_pressed() 告訴您按下哪個滑鼠按鈕 + # get_pressed() 告訴按下哪個滑鼠按鈕 if event.type == pygame.MOUSEBUTTONDOWN: print ('mouse pressed',pygame.mouse.get_pressed()) if pygame.mouse.get_pressed()[0]: + pos = pygame.mouse.get_pos() + # 100,400,50,20 + if pos[0]>=100 and pos[0]<=150 and pos[1]>=400 and pos[1]<=420: + sock.send("[restart]".encode('utf-8')) + detectFlag = False + return False + #print("restart") mouseFlag = True # 如果釋放滑鼠 elif event.type == pygame.MOUSEBUTTONUP: diff --git a/main.py b/main.py index c7daba3..8975174 100644 --- a/main.py +++ b/main.py @@ -122,6 +122,7 @@ class Room: def game(self): mainSocket = random.choice(self.sockList) + print("GAME SEND PROBLEM") for sock in self.sockList: if sock == mainSocket: sock.send('[prob] {}'.format(self.problem).encode('utf-8')) @@ -149,7 +150,10 @@ class Room: for clientSock in self.sockList: # 遍歷socket list if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN clientSock.send(origin) - + elif data=='[restart]': # [restart] + print('game restart') + time.sleep(4) + self.game() #restart else: # it is from other client. He/she want to send answer to check the answer if data == self.problem: @@ -224,22 +228,24 @@ class Client: pygame.display.set_caption('Mouse Example') size= [1080, 480] screen= pygame.display.set_mode(size) - screen.fill((255, 255, 255)) - pgStringVar = pygame.font.Font(None,25).render("Please wait...",False,(0,0,0))# 文字物件 - screen.blit(pgStringVar,(500,240))# draw font - pygame.display.update() - time.sleep(2) + continueFlag = False while not continueFlag: + screen.fill((255, 255, 255)) + pgStringVar = pygame.font.Font(None,25).render("Please wait...",False,(0,0,0))# 文字物件 + screen.blit(pgStringVar,(500,240))# draw font + pygame.display.update() + time.sleep(1) + + print("START RECEIVE.") data = sock.recv(1024).decode('utf-8') role = data[1:5] problem = data.split(' ')[1] - #print("Role: ",role) + print("Role: ",role) if role == "prob": - draw.sendDraw(sock,userList,screen,problem) - continueFlag = True + continueFlag = draw.sendDraw(sock,userList,screen,problem) elif role == "gues": draw.receiveDraw(sock,screen) continueFlag = True @@ -249,6 +255,7 @@ class Client: continueFlag = False else: #useless position continueFlag = False + pygame.quit() # def receiveData(self,sock): # while True: