tzzzzzzzx 3 years ago
parent 17234132a0
commit e5419fe56d

@ -73,8 +73,6 @@ def startNetworkServices():
connection = client.connect(addr)
startNewThread(target=listenFromServer)
def jsonAnalysis(networkMsg):
pass
#等待敌人移动
def loadImages():#加载图片,a,b双方分别有象狮豹狼狐鹰鼠七个角色
@ -293,6 +291,8 @@ def main(mode):
other_stage = False #network element
game_id = None #network element
MySide = None #network element
time_out_side = None #network element
foul = None #network element
mademove = False
game_over = False
square_selected = () #刚开始没有选择任何一个棋子
@ -379,15 +379,39 @@ def main(mode):
other_stage = True if networkMsg['side']==0 else False
if(other_stage == True):
print('waiting for other player to move...')
#quick_game_handler
elif networkMsg['status'] == 2 : # other player quit the game
print('other player quitted with message: ',networkMsg['request'])
running = False
elif networkMsg['status'] == 2 : # Stopping request
print('other player returned special message: ',networkMsg['request'])
if networkMsg['request']=='quit':
other_joined = False
elif networkMsg['request'] == 'stop':
pass
elif networkMsg['request'] == 'report':
foul = networkMsg['side']
otherQuitJson = {
'type': 3,
'side': 0
}
client.send(json.dumps(otherQuitJson).encode('utf-8'))
game_over = True
#timeout handler
elif networkMsg['status'] == 3 :
time_out_side = networkMsg['side']
otherQuitJson = {
'type': 3,
'side': 0
}
client.send(json.dumps(otherQuitJson).encode('utf-8'))
game_over = True
elif 'src' and 'dst' in networkMsg.keys():
theMove = backend.Move([networkMsg['src']['x'],networkMsg['src']['y']],[networkMsg['dst']['x'],networkMsg['dst']['y']],game_state.board)
game_state.makeMove(theMove)
#game_state.exchange()
valid_moves = game_state.getAllMoves()
other_stage = not other_stage
thisMove = None
@ -407,7 +431,7 @@ def main(mode):
sys.exit()
elif e.type == pg.MOUSEBUTTONDOWN:
#鼠标点击事件:用于选择棋子,移动棋子
if not game_over and not other_stage:
if not game_over and not other_stage and other_joined:
mouse_loc = pg.mouse.get_pos()
row = int((mouse_loc[1] - bias_top) / SIZE)
col = int((mouse_loc[0] - bias_left) / SIZE) #* get position of mouse click
@ -430,14 +454,6 @@ def main(mode):
else:
click_queue = [square_selected]
elif e.type == pg.KEYDOWN:
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
pass
#* 网络对战局面更新
#? 考虑将多线程状态更新指令写在backend中。
#? 但是考虑到ui刷新问题故仍尝试在main中写
#ShowGameState(screen,game_state,valid_moves,square_selected)
if mademove:
valid_moves = game_state.getAllMoves()
mademove = False
@ -467,9 +483,26 @@ def main(mode):
ShowGameState(screen,game_state,valid_moves,square_selected)
if game_state.conquer():
print("GAME OVER!")
showGameOverText(screen,"player "+game_state.win_person+" wins")
if not game_over and game_state.win_person == ('b' if MySide == 0 else 'r' ):
winJson = {
'type': 2,
'msg': {
'request': 'stop',
'game_id': game_id,
'side': MySide
}
}
client.send(json.dumps(winJson).encode("utf-8"))
game_over = True
clock.tick(5)
if game_over and not other_joined:
showGameOverText(screen,"player "+ ("b" if MySide==1 else "r") + " quitted the game")
if time_out_side != None:
showGameOverText(screen,"player "+ ("b" if time_out_side==0 else "r")+" timed out")
clock.tick(50)
pg.display.flip()

@ -1,5 +1,5 @@
SOCKET_HOST=127.0.0.1
SOCKET_PORT=50005
MAX_WAITING_TIME=600000
MAX_THNIKING_TIME=15000
MAX_WAITING_TIME=600
MAX_THNIKING_TIME=15
MAX_TOTAL_TIME=1000

Loading…
Cancel
Save