Client can waiting for the next game. Server can show user list
This commit is contained in:
parent
4c77d25632
commit
62fc412e95
46
draw.py
46
draw.py
@ -1,7 +1,28 @@
|
|||||||
import pygame, sys,threading
|
import pygame, sys,threading
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def sendDraw(sock):
|
def drawUserList(nowUserList,screen):
|
||||||
|
listSTR = nowUserList[7:]
|
||||||
|
|
||||||
|
listJSON = json.loads(listSTR)
|
||||||
|
#[['127.0.0.1', 52362], ['127.0.0.1', 52370]]
|
||||||
|
y = 100
|
||||||
|
cross = 20
|
||||||
|
pygame.draw.rect(screen,(255,255,255),[900,100,200,380])
|
||||||
|
for sockName in listJSON:
|
||||||
|
pygame.draw.rect(screen,(171, 254, 250),[900,y,200,30],0) # 輸入匡的矩形
|
||||||
|
pgStringVar = pygame.font.Font(None,25).render(str(sockName),False,(0,0,0))# 文字物件
|
||||||
|
screen.blit(pgStringVar,(910,y+10))# draw font
|
||||||
|
pygame.display.update()
|
||||||
|
y = y+30+cross
|
||||||
|
|
||||||
|
def drawerReceive(sock,screen):
|
||||||
|
while True:
|
||||||
|
data = sock.recv(1024).decode('utf-8')
|
||||||
|
if data[0:6] == "[list]":
|
||||||
|
drawUserList(data,screen)
|
||||||
|
|
||||||
|
def sendDraw(sock,nowUserList):
|
||||||
white= (255, 255, 255)
|
white= (255, 255, 255)
|
||||||
black= (0, 0, 0)
|
black= (0, 0, 0)
|
||||||
|
|
||||||
@ -10,14 +31,21 @@ def sendDraw(sock):
|
|||||||
size= [1080, 480]
|
size= [1080, 480]
|
||||||
screen= pygame.display.set_mode(size)
|
screen= pygame.display.set_mode(size)
|
||||||
clock= pygame.time.Clock()
|
clock= pygame.time.Clock()
|
||||||
|
screen.fill((255, 255, 255))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 開始話user list
|
||||||
|
drawUserList(nowUserList,screen)
|
||||||
|
drawerRecvThreading = threading.Thread(target=drawerReceive,args=(sock,screen))
|
||||||
|
drawerRecvThreading.start()
|
||||||
# 使系統滑鼠圖標不可見
|
# 使系統滑鼠圖標不可見
|
||||||
#pygame.mouse.set_visible(False)
|
#pygame.mouse.set_visible(False)
|
||||||
|
|
||||||
dotPos = []
|
dotPos = []
|
||||||
|
|
||||||
mouseFlag = False
|
mouseFlag = False
|
||||||
screen.fill((255, 255, 255))
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
tempPos = ()
|
tempPos = ()
|
||||||
while True:
|
while True:
|
||||||
@ -112,18 +140,8 @@ def receiveDraw(sock):
|
|||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif data[0:6] == "[list]":
|
elif data[0:6] == "[list]":
|
||||||
listSTR = data[7:]
|
drawUserList(data,screen)
|
||||||
|
|
||||||
listJSON = json.loads(listSTR)
|
|
||||||
#[['127.0.0.1', 52362], ['127.0.0.1', 52370]]
|
|
||||||
y = 100
|
|
||||||
cross = 20
|
|
||||||
for sockName in listJSON:
|
|
||||||
pygame.draw.rect(screen,(171, 254, 250),[900,y,200,30],0) # 輸入匡的矩形
|
|
||||||
pgStringVar = pygame.font.Font(None,25).render(str(sockName),False,(0,0,0))# 文字物件
|
|
||||||
screen.blit(pgStringVar,(910,y+10))# draw font
|
|
||||||
pygame.display.update()
|
|
||||||
y = y+30+cross
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
#print(data)
|
#print(data)
|
||||||
|
|||||||
66
main.py
66
main.py
@ -99,12 +99,15 @@ class Room:
|
|||||||
listensock.bind((self.ip,self.portNum))
|
listensock.bind((self.ip,self.portNum))
|
||||||
print("\t{}:{}".format(self.ip,self.portNum))
|
print("\t{}:{}".format(self.ip,self.portNum))
|
||||||
listensock.listen(5)
|
listensock.listen(5)
|
||||||
|
emptyFlag = True
|
||||||
while True:
|
while True:
|
||||||
sock,sockname = listensock.accept()
|
sock,sockname = listensock.accept()
|
||||||
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
||||||
|
|
||||||
self.sockList.append(sock) # 把sock放入list
|
|
||||||
allPeerName = []
|
allPeerName = []
|
||||||
|
self.sockList.append(sock) # 把sock放入list
|
||||||
|
|
||||||
|
# Send socket list to client to build a user list and put it beside picture.
|
||||||
for i in self.sockList:
|
for i in self.sockList:
|
||||||
allPeerName.append(i.getpeername())
|
allPeerName.append(i.getpeername())
|
||||||
for sock in self.sockList:
|
for sock in self.sockList:
|
||||||
@ -112,7 +115,18 @@ class Room:
|
|||||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||||
# 負責與client通信,傳輸遊戲所必須的指令
|
# 負責與client通信,傳輸遊戲所必須的指令
|
||||||
receiveDataThread.start()
|
receiveDataThread.start()
|
||||||
|
|
||||||
|
if emptyFlag:
|
||||||
|
self.game()
|
||||||
|
emptyFlag = False
|
||||||
|
|
||||||
|
def game(self):
|
||||||
|
mainSocket = random.choice(self.sockList)
|
||||||
|
for sock in self.sockList:
|
||||||
|
if sock == mainSocket:
|
||||||
|
sock.send('[prob] {}'.format(self.problem).encode('utf-8'))
|
||||||
|
else:
|
||||||
|
sock.send('[gues]'.encode('utf-8'))
|
||||||
def connect(self):
|
def connect(self):
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
@ -143,6 +157,13 @@ class Room:
|
|||||||
elif data == 'exit':
|
elif data == 'exit':
|
||||||
sock.send('exitok'.encode('utf-8'))
|
sock.send('exitok'.encode('utf-8'))
|
||||||
self.sockList.remove(sock)
|
self.sockList.remove(sock)
|
||||||
|
allPeerName = []
|
||||||
|
# Send socket list to client to build a user list and put it beside picture.
|
||||||
|
for i in self.sockList:
|
||||||
|
allPeerName.append(i.getpeername())
|
||||||
|
for sock in self.sockList:
|
||||||
|
sock.send("[list] {}".format(json.dumps(allPeerName)).encode('utf-8'))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
sock.send('n'.encode('utf-8'))
|
sock.send('n'.encode('utf-8'))
|
||||||
|
|
||||||
@ -174,7 +195,6 @@ class Client:
|
|||||||
# receiveDataThread.start()
|
# receiveDataThread.start()
|
||||||
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||||
# sendDataThread.start()
|
# sendDataThread.start()
|
||||||
draw.sendDraw(sock) # 開始繪圖
|
|
||||||
|
|
||||||
elif receiveMsg=="OK.CLIENT":
|
elif receiveMsg=="OK.CLIENT":
|
||||||
roomNum = input("Room Number> ")
|
roomNum = input("Room Number> ")
|
||||||
@ -188,19 +208,39 @@ class Client:
|
|||||||
# receiveDataThread.start()
|
# receiveDataThread.start()
|
||||||
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||||
# sendDataThread.start()
|
# sendDataThread.start()
|
||||||
draw.receiveDraw(sock) # Start to receive mouse position to draw picture what MAIN client draw.
|
|
||||||
else:
|
else:
|
||||||
print("ERROR TYPE")
|
print("ERROR TYPE")
|
||||||
|
exit
|
||||||
|
|
||||||
def receiveData(self,sock):
|
userList = sock.recv(1024).decode('utf-8')
|
||||||
while True:
|
print("List: ",userList)
|
||||||
data = sock.recv(MAX).decode('utf-8')
|
|
||||||
if data:
|
continueFlag = False
|
||||||
print(data)
|
while not continueFlag:
|
||||||
def sendData(self,sock):
|
data = sock.recv(1024).decode('utf-8')
|
||||||
while True:
|
role = data[1:5]
|
||||||
data = input("> ")
|
#print("Role: ",role)
|
||||||
sock.send(data.encode('utf-8'))
|
if role == "prob":
|
||||||
|
draw.sendDraw(sock,userList)
|
||||||
|
elif role == "gues":
|
||||||
|
draw.receiveDraw(sock)
|
||||||
|
continueFlag = True
|
||||||
|
elif role == "list":
|
||||||
|
userList = data
|
||||||
|
print("List: ",userList)
|
||||||
|
continueFlag = False
|
||||||
|
else: #useless position
|
||||||
|
continueFlag = False
|
||||||
|
# def receiveData(self,sock):
|
||||||
|
# while True:
|
||||||
|
# data = sock.recv(MAX).decode('utf-8')
|
||||||
|
# if data:
|
||||||
|
# print(data)
|
||||||
|
# def sendData(self,sock):
|
||||||
|
# while True:
|
||||||
|
# data = input("> ")
|
||||||
|
# sock.send(data.encode('utf-8'))
|
||||||
def main():
|
def main():
|
||||||
ip = sys.argv[2]
|
ip = sys.argv[2]
|
||||||
port = sys.argv[3]
|
port = sys.argv[3]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user