Add user list
This commit is contained in:
parent
4395108584
commit
68b427eba9
24
draw.py
24
draw.py
@ -1,5 +1,5 @@
|
|||||||
import pygame, sys,threading
|
import pygame, sys,threading
|
||||||
|
import json
|
||||||
|
|
||||||
def sendDraw(sock):
|
def sendDraw(sock):
|
||||||
white= (255, 255, 255)
|
white= (255, 255, 255)
|
||||||
@ -7,7 +7,7 @@ def sendDraw(sock):
|
|||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption('Mouse Example')
|
pygame.display.set_caption('Mouse Example')
|
||||||
size= [640, 480]
|
size= [1080, 480]
|
||||||
screen= pygame.display.set_mode(size)
|
screen= pygame.display.set_mode(size)
|
||||||
clock= pygame.time.Clock()
|
clock= pygame.time.Clock()
|
||||||
|
|
||||||
@ -19,6 +19,7 @@ def sendDraw(sock):
|
|||||||
mouseFlag = False
|
mouseFlag = False
|
||||||
screen.fill((255, 255, 255))
|
screen.fill((255, 255, 255))
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type== pygame.QUIT:
|
if event.type== pygame.QUIT:
|
||||||
@ -89,7 +90,7 @@ def receiveDraw(sock):
|
|||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption('Mouse Example')
|
pygame.display.set_caption('Mouse Example')
|
||||||
size= [640, 480]
|
size= [1080, 480]
|
||||||
|
|
||||||
screen= pygame.display.set_mode(size)
|
screen= pygame.display.set_mode(size)
|
||||||
|
|
||||||
@ -109,7 +110,22 @@ def receiveDraw(sock):
|
|||||||
startFlag = False
|
startFlag = False
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
print(data)
|
elif data[0:6] == "[list]":
|
||||||
|
listSTR = data[7:]
|
||||||
|
|
||||||
|
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
|
||||||
|
#print(data)
|
||||||
li = data.split('+') # 送來的座標可能一次有多個,規範以+隔開
|
li = data.split('+') # 送來的座標可能一次有多個,規範以+隔開
|
||||||
for i in li:
|
for i in li:
|
||||||
if i!="":
|
if i!="":
|
||||||
|
|||||||
11
main.py
11
main.py
@ -92,6 +92,7 @@ class Room:
|
|||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.portNum = portNum
|
self.portNum = portNum
|
||||||
self.problem = self.getProblem()
|
self.problem = self.getProblem()
|
||||||
|
print(self.problem)
|
||||||
def start(self):
|
def start(self):
|
||||||
# Build a room's socket to start game
|
# Build a room's socket to start game
|
||||||
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
@ -103,7 +104,11 @@ class Room:
|
|||||||
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
||||||
|
|
||||||
self.sockList.append(sock) # 把sock放入list
|
self.sockList.append(sock) # 把sock放入list
|
||||||
|
allPeerName = []
|
||||||
|
for i in self.sockList:
|
||||||
|
allPeerName.append(i.getpeername())
|
||||||
|
for sock in self.sockList:
|
||||||
|
sock.send("[list] {}".format(json.dumps(allPeerName)).encode('utf-8'))
|
||||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||||
# 負責與client通信,傳輸遊戲所必須的指令
|
# 負責與client通信,傳輸遊戲所必須的指令
|
||||||
receiveDataThread.start()
|
receiveDataThread.start()
|
||||||
@ -130,6 +135,8 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
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:
|
||||||
sock.send('y'.encode('utf-8'))
|
sock.send('y'.encode('utf-8'))
|
||||||
@ -167,7 +174,7 @@ 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,) # 開始繪圖
|
draw.sendDraw(sock) # 開始繪圖
|
||||||
|
|
||||||
elif receiveMsg=="OK.CLIENT":
|
elif receiveMsg=="OK.CLIENT":
|
||||||
roomNum = input("Room Number> ")
|
roomNum = input("Room Number> ")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user