Main client can restart the game

This commit is contained in:
snsd0805 2020-06-29 02:26:51 +08:00
parent b62d6adc5e
commit 5ac4e377a5
2 changed files with 26 additions and 10 deletions

11
draw.py
View File

@ -31,6 +31,7 @@ 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)
white= (255, 255, 255) white= (255, 255, 255)
black= (0, 0, 0) black= (0, 0, 0)
@ -53,12 +54,20 @@ def sendDraw(sock,nowUserList,screen,problem):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type== pygame.QUIT: if event.type== pygame.QUIT:
detectFlag = False detectFlag = False
return True
# 如果按下滑鼠 # 如果按下滑鼠
# get_pressed() 告訴按下哪個滑鼠按鈕 # get_pressed() 告訴按下哪個滑鼠按鈕
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
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()
# 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 mouseFlag = True
# 如果釋放滑鼠 # 如果釋放滑鼠
elif event.type == pygame.MOUSEBUTTONUP: elif event.type == pygame.MOUSEBUTTONUP:

25
main.py
View File

@ -122,6 +122,7 @@ class Room:
def game(self): def game(self):
mainSocket = random.choice(self.sockList) mainSocket = random.choice(self.sockList)
print("GAME SEND PROBLEM")
for sock in self.sockList: for sock in self.sockList:
if sock == mainSocket: if sock == mainSocket:
sock.send('[prob] {}'.format(self.problem).encode('utf-8')) sock.send('[prob] {}'.format(self.problem).encode('utf-8'))
@ -149,7 +150,10 @@ class Room:
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]
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 else: # it is from other client. He/she want to send answer to check the answer
if data == self.problem: if data == self.problem:
@ -224,22 +228,24 @@ class Client:
pygame.display.set_caption('Mouse Example') pygame.display.set_caption('Mouse Example')
size= [1080, 480] size= [1080, 480]
screen= pygame.display.set_mode(size) 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 continueFlag = False
while not continueFlag: 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') data = sock.recv(1024).decode('utf-8')
role = data[1:5] role = data[1:5]
problem = data.split(' ')[1] problem = data.split(' ')[1]
#print("Role: ",role) print("Role: ",role)
if role == "prob": if role == "prob":
draw.sendDraw(sock,userList,screen,problem) continueFlag = draw.sendDraw(sock,userList,screen,problem)
continueFlag = True
elif role == "gues": elif role == "gues":
draw.receiveDraw(sock,screen) draw.receiveDraw(sock,screen)
continueFlag = True continueFlag = True
@ -249,6 +255,7 @@ class Client:
continueFlag = False continueFlag = False
else: #useless position else: #useless position
continueFlag = False continueFlag = False
pygame.quit() pygame.quit()
# def receiveData(self,sock): # def receiveData(self,sock):
# while True: # while True: