Add some program notes
This commit is contained in:
parent
3d087babce
commit
7d520f9522
30
draw.py
30
draw.py
@ -47,7 +47,7 @@ def sendDraw(sock):
|
||||
pygame.draw.circle(screen,black,pygame.mouse.get_pos(),5,0)
|
||||
|
||||
|
||||
|
||||
# feature
|
||||
# 在鼠標周圍畫一個圓
|
||||
#for i in range(len(dotPos)-1):
|
||||
# pygame.draw.line(screen,black,dotPos[i],dotPos[i+1],5)
|
||||
@ -62,21 +62,21 @@ def guessInput(screen):
|
||||
guessStr = ""
|
||||
while True:
|
||||
for event in pygame.event.get() :
|
||||
if event.type== pygame.QUIT:
|
||||
if event.type== pygame.QUIT: # close GUI
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key>=97 and event.key<122:
|
||||
elif event.type == pygame.KEYDOWN: # Key down.
|
||||
if event.key>=97 and event.key<122: #a~z
|
||||
guessStr = guessStr + chr(event.key)
|
||||
elif event.key == 13: #enter
|
||||
elif event.key == 13: #enter,send to server,clean
|
||||
guessStr = ""
|
||||
elif event.key == 8 : #backspace
|
||||
guessStr = guessStr[0:-1]
|
||||
|
||||
pygame.draw.rect(screen,(171, 254, 250),[100,450,500,550],0)
|
||||
pgStringVar = pygame.font.Font(None,30).render(guessStr,False,(0,0,0))
|
||||
print(guessStr)
|
||||
screen.blit(pgStringVar,(120,455))
|
||||
pygame.draw.rect(screen,(171, 254, 250),[100,450,500,550],0) # 輸入匡的矩形
|
||||
pgStringVar = pygame.font.Font(None,30).render(guessStr,False,(0,0,0))# 文字物件
|
||||
print(guessStr) # for test
|
||||
screen.blit(pgStringVar,(120,455))# draw font
|
||||
pygame.display.update()
|
||||
|
||||
def receiveDraw(sock):
|
||||
@ -95,25 +95,21 @@ def receiveDraw(sock):
|
||||
pygame.display.update()
|
||||
print("draw start")
|
||||
|
||||
guessThreading = threading.Thread(target=guessInput,args=(screen,))# guest input
|
||||
guessThreading = threading.Thread(target=guessInput,args=(screen,)) # guest input
|
||||
guessThreading.setDaemon(False)
|
||||
guessThreading.start()
|
||||
|
||||
while True:
|
||||
data = sock.recv(1024).decode('utf-8')
|
||||
li = data.split('+')
|
||||
#print((li))
|
||||
li = data.split('+') # 送來的座標可能一次有多個,規範以+隔開
|
||||
for i in li:
|
||||
if i!="":
|
||||
#screen.fill((255, 255, 255))
|
||||
|
||||
# 切割取得座標
|
||||
i = i.lstrip('(')
|
||||
i = i.rstrip(')')
|
||||
i = i.replace(',','')
|
||||
i = i.split(' ')
|
||||
# 在鼠標周圍畫一個圓
|
||||
#print(int(i[0]),int(i[1]))
|
||||
print(i)
|
||||
print(i) #for test
|
||||
if len(i)==2 : # 防止傳送過程疏漏
|
||||
if i[0]!='' and i[1]!='': # 防止傳送過程疏漏
|
||||
pos = (int(i[0]),int(i[1]))
|
||||
|
||||
50
main.py
50
main.py
@ -8,6 +8,9 @@ class Server:
|
||||
self.ip = ip
|
||||
self.port = port
|
||||
def start(self):
|
||||
|
||||
# Base server
|
||||
# Set the room for mainClient and redirect client into the room
|
||||
print("Set server on {}:{}".format(self.ip,self.port))
|
||||
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
listensock.bind((self.ip,self.port))
|
||||
@ -24,18 +27,20 @@ class Server:
|
||||
print(data)
|
||||
if data == "MAIN":
|
||||
sock.send("OK.SERVER".encode('utf-8'))
|
||||
self.main(sock)
|
||||
self.main(sock) # Set main client mode
|
||||
elif data == "CLIENT":
|
||||
sock.send("OK.CLIENT".encode('utf-8'))
|
||||
self.client(sock)
|
||||
self.client(sock) # Set client mode
|
||||
else:
|
||||
sock.send("FAIL".encode('utf-8'))
|
||||
sock.send("FAIL".encode('utf-8')) # Error type
|
||||
|
||||
def main(self,sock):
|
||||
portNum = random.randint(1025,65535) #決定port
|
||||
roomNum = chr(random.randint(65,90)) + chr(random.randint(65,90))
|
||||
roomNum = chr(random.randint(65,90)) + chr(random.randint(65,90)) # 隨機房號,給client方便導向,不需要ip:port
|
||||
|
||||
flag = 1
|
||||
|
||||
# 防止房號相同,若重複必須重新設定
|
||||
while flag:
|
||||
portNum = random.randint(1025,65535) #決定port
|
||||
roomNum = chr(random.randint(65,90)) + chr(random.randint(65,90))
|
||||
@ -43,9 +48,15 @@ class Server:
|
||||
for i in self.roomList:
|
||||
if i['portNum'] == portNum or i['roomNum'] == roomNum:
|
||||
flag =1
|
||||
|
||||
|
||||
room = Room(self.ip,portNum)
|
||||
# Room process, process the game.
|
||||
# Should handle all client which are redirected to this room.
|
||||
roomProcess = multiprocessing.Process(target=room.start)
|
||||
roomProcess.start()
|
||||
|
||||
# Set a list which save all rooms' info
|
||||
self.roomList.append(
|
||||
{
|
||||
'portNum' : portNum,
|
||||
@ -53,26 +64,33 @@ class Server:
|
||||
}
|
||||
)
|
||||
print(self.roomList)
|
||||
|
||||
# Let main client know the roomNumber.So it can connect to this room and share it's roomNumber to his/her friends.
|
||||
sock.send("{} {}".format(roomNum,portNum).encode('utf-8'))
|
||||
# def getPort(self):
|
||||
# sock
|
||||
|
||||
|
||||
def client(self,sock):
|
||||
# Get roomNumber which client want to join.
|
||||
receiveMsg = sock.recv(MAX).decode('utf-8')
|
||||
flag = False
|
||||
for i in self.roomList:
|
||||
# Find PORT NUMBER to let clients' socket can connect to.
|
||||
if i['roomNum'] == receiveMsg:
|
||||
# send PORT NUMBER to let client can connecte
|
||||
sock.send(str(i['portNum']).encode('utf-8'))
|
||||
flag = True
|
||||
if not flag:
|
||||
# NO THIS NUMBER
|
||||
sock.send("FAIL".encode('utf-8'))
|
||||
|
||||
class Room:
|
||||
startFlag = False
|
||||
sockList = []
|
||||
sockList = [] # Client's sock list
|
||||
def __init__(self,ip,portNum):
|
||||
self.ip = ip
|
||||
self.portNum = portNum
|
||||
def start(self):
|
||||
# Build a room's socket to start game
|
||||
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
listensock.bind((self.ip,self.portNum))
|
||||
print("\t{}:{}".format(self.ip,self.portNum))
|
||||
@ -84,6 +102,7 @@ class Room:
|
||||
self.sockList.append(sock) # 把sock放入list
|
||||
|
||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||
# 負責與client通信,傳輸遊戲所必須的指令
|
||||
receiveDataThread.start()
|
||||
|
||||
def connect(self):
|
||||
@ -91,14 +110,18 @@ class Room:
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
sock.connect((self.ip,self.portNum))
|
||||
return sock
|
||||
def receiveData(self,sock):
|
||||
|
||||
def receiveData(self,sock):
|
||||
# room's socket receive MAINCLIENT'S mouse position.
|
||||
# And send this MOUSE position to other client.
|
||||
# Let them can draw the same picture on the screen
|
||||
while True:
|
||||
origin = sock.recv(MAX)
|
||||
data = origin.decode('utf-8')
|
||||
if data:
|
||||
print(data)
|
||||
for clientSock in self.sockList: # 遍歷socket list
|
||||
if clientSock != sock: # 不是自己的才傳送資料
|
||||
if clientSock != sock: # 不是自己的才傳送資料.Needn't send position to MAIN
|
||||
clientSock.send(origin)
|
||||
|
||||
class Client:
|
||||
@ -107,6 +130,7 @@ class Client:
|
||||
self.port = port
|
||||
|
||||
def start(self):
|
||||
# START to choose MAIN OR CLIENT.
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
sock.connect((self.ip,self.port))
|
||||
self.setType(sock)
|
||||
@ -115,10 +139,11 @@ class Client:
|
||||
print("Type in 'MAIN' to set a new room\nType in 'CLIENT' to join a room")
|
||||
msg = input("> ")
|
||||
sock.send(msg.encode('utf-8'))
|
||||
receiveMsg = sock.recv(MAX).decode('utf-8')
|
||||
receiveMsg = sock.recv(MAX).decode('utf-8') # Get response
|
||||
if receiveMsg=="OK.SERVER":
|
||||
receiveMsg = sock.recv(MAX).decode('utf-8')
|
||||
print(receiveMsg)
|
||||
# get ROOMNUM and PORTNUM
|
||||
connectData = receiveMsg.split(' ')
|
||||
room = Room(self.ip,int(connectData[1]))
|
||||
sock = room.connect() # sock可覆蓋了
|
||||
@ -127,12 +152,13 @@ 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> ")
|
||||
sock.send(roomNum.encode('utf-8'))
|
||||
receiveMsg = sock.recv(MAX).decode('utf-8')
|
||||
# Get port number
|
||||
room = Room(self.ip,int(receiveMsg))
|
||||
sock = room.connect() # sock可覆蓋了
|
||||
|
||||
@ -140,7 +166,7 @@ class Client:
|
||||
# receiveDataThread.start()
|
||||
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||
# sendDataThread.start()
|
||||
draw.receiveDraw(sock)
|
||||
draw.receiveDraw(sock) # Start to receive mouse position to draw picture what MAIN client draw.
|
||||
else:
|
||||
print("ERROR TYPE")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user