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 json
|
||||
|
||||
def sendDraw(sock):
|
||||
white= (255, 255, 255)
|
||||
@ -7,7 +7,7 @@ def sendDraw(sock):
|
||||
|
||||
pygame.init()
|
||||
pygame.display.set_caption('Mouse Example')
|
||||
size= [640, 480]
|
||||
size= [1080, 480]
|
||||
screen= pygame.display.set_mode(size)
|
||||
clock= pygame.time.Clock()
|
||||
|
||||
@ -19,6 +19,7 @@ def sendDraw(sock):
|
||||
mouseFlag = False
|
||||
screen.fill((255, 255, 255))
|
||||
pygame.display.update()
|
||||
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type== pygame.QUIT:
|
||||
@ -89,7 +90,7 @@ def receiveDraw(sock):
|
||||
|
||||
pygame.init()
|
||||
pygame.display.set_caption('Mouse Example')
|
||||
size= [640, 480]
|
||||
size= [1080, 480]
|
||||
|
||||
screen= pygame.display.set_mode(size)
|
||||
|
||||
@ -109,7 +110,22 @@ def receiveDraw(sock):
|
||||
startFlag = False
|
||||
pygame.quit()
|
||||
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('+') # 送來的座標可能一次有多個,規範以+隔開
|
||||
for i in li:
|
||||
if i!="":
|
||||
|
||||
13
main.py
13
main.py
@ -92,6 +92,7 @@ class Room:
|
||||
self.ip = ip
|
||||
self.portNum = portNum
|
||||
self.problem = self.getProblem()
|
||||
print(self.problem)
|
||||
def start(self):
|
||||
# Build a room's socket to start game
|
||||
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
@ -103,11 +104,15 @@ class Room:
|
||||
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
||||
|
||||
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,))
|
||||
# 負責與client通信,傳輸遊戲所必須的指令
|
||||
receiveDataThread.start()
|
||||
|
||||
|
||||
def connect(self):
|
||||
time.sleep(0.5)
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
@ -130,6 +135,8 @@ class Room:
|
||||
for clientSock in self.sockList: # 遍歷socket list
|
||||
if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN
|
||||
clientSock.send(origin)
|
||||
|
||||
|
||||
else: # it is from other client. He/she want to send answer to check the answer
|
||||
if data == self.problem:
|
||||
sock.send('y'.encode('utf-8'))
|
||||
@ -167,7 +174,7 @@ class Client:
|
||||
# receiveDataThread.start()
|
||||
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||
# sendDataThread.start()
|
||||
draw.sendDraw(sock,) # 開始繪圖
|
||||
draw.sendDraw(sock) # 開始繪圖
|
||||
|
||||
elif receiveMsg=="OK.CLIENT":
|
||||
roomNum = input("Room Number> ")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user