滑鼠可同步運動
This commit is contained in:
parent
b85bd74f7b
commit
9c760b1863
86
draw.py
Normal file
86
draw.py
Normal file
@ -0,0 +1,86 @@
|
||||
import pygame, sys
|
||||
|
||||
|
||||
def sendDraw(sock):
|
||||
white= (255, 255, 255)
|
||||
black= (0, 0, 0)
|
||||
|
||||
pygame.init()
|
||||
pygame.display.set_caption('Mouse Example')
|
||||
size= [640, 480]
|
||||
screen= pygame.display.set_mode(size)
|
||||
clock= pygame.time.Clock()
|
||||
|
||||
# 使系統鼠標圖標不可見
|
||||
pygame.mouse.set_visible(False)
|
||||
|
||||
mouseFlag = False
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type== pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
# 如果按下鼠標
|
||||
# get_pressed() 告訴您按下哪個鼠標按鈕
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
print ('mouse pressed',pygame.mouse.get_pressed())
|
||||
if pygame.mouse.get_pressed()[0]:
|
||||
mouseFlag = True
|
||||
# 如果釋放鼠標
|
||||
elif event.type == pygame.MOUSEBUTTONUP:
|
||||
print ('mouse released', pygame.mouse.get_pressed())
|
||||
if pygame.mouse.get_pressed()[0] == 0:
|
||||
mouseFlag = False
|
||||
# 如果鼠標在運動中
|
||||
# get_rel() - 返回自上次調用此函數以來X和Y的移動量
|
||||
if event.type == pygame.MOUSEMOTION:
|
||||
print ('mouse is moving', pygame.mouse.get_pos())
|
||||
if mouseFlag:
|
||||
sock.send("{}+".format(pygame.mouse.get_pos()).encode('utf-8'))
|
||||
|
||||
screen.fill((255, 255, 255))
|
||||
|
||||
# 在鼠標周圍畫一個圓
|
||||
pos= (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
|
||||
pygame.draw.circle(screen, black, pos, 10, 0)
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
clock.tick(30)
|
||||
|
||||
def receiveDraw(sock):
|
||||
white= (255, 255, 255)
|
||||
black= (0, 0, 0)
|
||||
|
||||
pygame.init()
|
||||
pygame.display.set_caption('Mouse Example')
|
||||
size= [640, 480]
|
||||
|
||||
screen= pygame.display.set_mode(size)
|
||||
|
||||
clock= pygame.time.Clock()
|
||||
print("draw start")
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type== pygame.QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
data = sock.recv(1024).decode('utf-8')
|
||||
li = data.split('+')
|
||||
#print((li))
|
||||
for i in li:
|
||||
if i!="":
|
||||
screen.fill((255, 255, 255))
|
||||
|
||||
i = i.lstrip('(')
|
||||
i = i.rstrip(')')
|
||||
i = i.replace(',','')
|
||||
i = i.split(' ')
|
||||
# 在鼠標周圍畫一個圓
|
||||
pos = (int(i[0]),int(i[1]))
|
||||
print(pos)
|
||||
pygame.draw.circle(screen, black, pos, 10, 0)
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
31
main.py
31
main.py
@ -1,5 +1,5 @@
|
||||
import socket,threading,sys,random,multiprocessing,time
|
||||
|
||||
import draw
|
||||
MAX = 1024
|
||||
|
||||
class Server:
|
||||
@ -80,10 +80,9 @@ class Room:
|
||||
while True:
|
||||
sock,sockname = listensock.accept()
|
||||
print("[ {} ]{} has connected.".format(self.portNum,sockname))
|
||||
sock.send('WELCOME'.encode('utf-8'))
|
||||
|
||||
self.sockList.append(sock) # 把sock放入list
|
||||
|
||||
|
||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||
receiveDataThread.start()
|
||||
|
||||
@ -94,12 +93,13 @@ class Room:
|
||||
return sock
|
||||
def receiveData(self,sock):
|
||||
while True:
|
||||
data = sock.recv(MAX).decode('utf-8')
|
||||
origin = sock.recv(MAX)
|
||||
data = origin.decode('utf-8')
|
||||
if data:
|
||||
print(data)
|
||||
for clientSock in self.sockList: # 遍歷socket list
|
||||
if clientSock != sock: # 不是自己的才傳送資料
|
||||
clientSock.send("[{}]:{}".format(sock.getpeername(),data).encode('utf-8'))
|
||||
clientSock.send(origin)
|
||||
|
||||
class Client:
|
||||
def __init__(self,ip,port):
|
||||
@ -123,10 +123,13 @@ class Client:
|
||||
room = Room(self.ip,int(connectData[1]))
|
||||
sock = room.connect() # sock可覆蓋了
|
||||
|
||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||
receiveDataThread.start()
|
||||
sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||
sendDataThread.start()
|
||||
# receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||
# receiveDataThread.start()
|
||||
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||
# sendDataThread.start()
|
||||
|
||||
a = threading.Thread(target=draw.sendDraw,args=(sock,))
|
||||
a.start()
|
||||
|
||||
elif receiveMsg=="OK.CLIENT":
|
||||
roomNum = input("Room Number> ")
|
||||
@ -135,10 +138,12 @@ class Client:
|
||||
room = Room(self.ip,int(receiveMsg))
|
||||
sock = room.connect() # sock可覆蓋了
|
||||
|
||||
receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||
receiveDataThread.start()
|
||||
sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||
sendDataThread.start()
|
||||
# receiveDataThread = threading.Thread(target=self.receiveData,args=(sock,))
|
||||
# receiveDataThread.start()
|
||||
# sendDataThread = threading.Thread(target=self.sendData,args=(sock,))
|
||||
# sendDataThread.start()
|
||||
a = threading.Thread(target=draw.receiveDraw,args=(sock,))
|
||||
a.start()
|
||||
|
||||
else:
|
||||
print("ERROR TYPE")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user