Beta 0.1 Clients can start the game in turn
This commit is contained in:
parent
5ac4e377a5
commit
f8fb53fca5
41
draw.py
41
draw.py
@ -1,5 +1,5 @@
|
||||
import pygame, sys,threading
|
||||
import json
|
||||
import json,time
|
||||
|
||||
def drawUserList(nowUserList,screen):
|
||||
listSTR = nowUserList[7:]
|
||||
@ -21,7 +21,10 @@ def drawerReceive(sock,screen):
|
||||
data = sock.recv(1024).decode('utf-8')
|
||||
if data[0:6] == "[list]":
|
||||
drawUserList(data,screen)
|
||||
|
||||
elif data == "restart":
|
||||
print("restart")
|
||||
break
|
||||
print("drawerRECEIVE CLOSED")
|
||||
def sendDraw(sock,nowUserList,screen,problem):
|
||||
|
||||
# Set white background
|
||||
@ -49,11 +52,11 @@ def sendDraw(sock,nowUserList,screen,problem):
|
||||
|
||||
pygame.display.update()
|
||||
tempPos = ()
|
||||
detectFlag = True
|
||||
while detectFlag:
|
||||
#detectFlag = True
|
||||
print("可以開始畫圖")
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type== pygame.QUIT:
|
||||
detectFlag = False
|
||||
return True
|
||||
|
||||
# 如果按下滑鼠
|
||||
@ -65,7 +68,9 @@ def sendDraw(sock,nowUserList,screen,problem):
|
||||
# 100,400,50,20
|
||||
if pos[0]>=100 and pos[0]<=150 and pos[1]>=400 and pos[1]<=420:
|
||||
sock.send("[restart]".encode('utf-8'))
|
||||
detectFlag = False
|
||||
#if data == 'restart':
|
||||
time.sleep(1) #drawerReceive
|
||||
#detectFlag = False
|
||||
return False
|
||||
#print("restart")
|
||||
mouseFlag = True
|
||||
@ -95,8 +100,11 @@ def sendDraw(sock,nowUserList,screen,problem):
|
||||
|
||||
|
||||
clock.tick(30)
|
||||
startFlag = True
|
||||
print("停止畫圖")
|
||||
|
||||
|
||||
|
||||
startFlag = True
|
||||
def guessInput(screen,sock):
|
||||
global startFlag
|
||||
guessStr = ""
|
||||
@ -121,9 +129,12 @@ def guessInput(screen,sock):
|
||||
pygame.display.update()
|
||||
|
||||
def receiveDraw(sock,screen):
|
||||
global startFlag
|
||||
print("進入receiveDraw")
|
||||
print(startFlag)
|
||||
white= (255, 255, 255)
|
||||
black= (0, 0, 0)
|
||||
global startFlag
|
||||
|
||||
screen.fill((255, 255, 255))
|
||||
pygame.draw.rect(screen,(171, 254, 250),[100,450,500,550],0)
|
||||
pygame.display.update()
|
||||
@ -136,13 +147,15 @@ def receiveDraw(sock,screen):
|
||||
data = sock.recv(1024).decode('utf-8')
|
||||
if data == 'exitok':
|
||||
startFlag = False
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
return True
|
||||
elif data[0:6] == "[list]":
|
||||
drawUserList(data,screen)
|
||||
|
||||
|
||||
continue
|
||||
elif data == 'restart':
|
||||
startFlag = False
|
||||
time.sleep(0.5)
|
||||
startFlag = True
|
||||
return False
|
||||
#print(data)
|
||||
li = data.split('+') # 送來的座標可能一次有多個,規範以+隔開
|
||||
for i in li:
|
||||
@ -159,5 +172,5 @@ def receiveDraw(sock,screen):
|
||||
# pygame.draw.circle(screen, black, pos, 5, 0)
|
||||
pygame.draw.circle(screen,black,pos,5,0)
|
||||
pygame.display.update()
|
||||
sock.close()
|
||||
|
||||
startFlag = True
|
||||
print("退出receiveDraw")
|
||||
|
||||
25
main.py
25
main.py
@ -122,12 +122,13 @@ class Room:
|
||||
|
||||
def game(self):
|
||||
mainSocket = random.choice(self.sockList)
|
||||
time.sleep(4)
|
||||
print("GAME SEND PROBLEM")
|
||||
for sock in self.sockList:
|
||||
if sock == mainSocket:
|
||||
sock.send('[prob] {}'.format(self.problem).encode('utf-8'))
|
||||
for clientSock in self.sockList:
|
||||
if clientSock == mainSocket:
|
||||
clientSock.send('[prob] {}'.format(self.problem).encode('utf-8'))
|
||||
else:
|
||||
sock.send('[gues]'.encode('utf-8'))
|
||||
clientSock.send('[gues]'.encode('utf-8'))
|
||||
def connect(self):
|
||||
time.sleep(0.5)
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
@ -152,7 +153,10 @@ class Room:
|
||||
clientSock.send(origin)
|
||||
elif data=='[restart]': # [restart]
|
||||
print('game restart')
|
||||
time.sleep(4)
|
||||
time.sleep(1)
|
||||
for clientSock in self.sockList:
|
||||
clientSock.send('restart'.encode('utf-8'))
|
||||
|
||||
self.game() #restart
|
||||
|
||||
else: # it is from other client. He/she want to send answer to check the answer
|
||||
@ -230,29 +234,30 @@ class Client:
|
||||
screen= pygame.display.set_mode(size)
|
||||
|
||||
|
||||
|
||||
continueFlag = False
|
||||
while not continueFlag:
|
||||
screen.fill((255, 255, 255))
|
||||
pgStringVar = pygame.font.Font(None,25).render("Please wait...",False,(0,0,0))# 文字物件
|
||||
screen.blit(pgStringVar,(500,240))# draw font
|
||||
pygame.display.update()
|
||||
time.sleep(1)
|
||||
|
||||
print("START RECEIVE.")
|
||||
data = sock.recv(1024).decode('utf-8')
|
||||
role = data[1:5]
|
||||
problem = data.split(' ')[1]
|
||||
|
||||
print("Role: ",role)
|
||||
if role == "prob":
|
||||
problem = data.split(' ')[1]
|
||||
continueFlag = draw.sendDraw(sock,userList,screen,problem)
|
||||
|
||||
elif role == "gues":
|
||||
draw.receiveDraw(sock,screen)
|
||||
continueFlag = True
|
||||
continueFlag = draw.receiveDraw(sock,screen)
|
||||
|
||||
elif role == "list":
|
||||
userList = data
|
||||
print("List: ",userList)
|
||||
continueFlag = False
|
||||
|
||||
else: #useless position
|
||||
continueFlag = False
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user