From e5419fe56da4624fb5889757aae5d080268f6eac Mon Sep 17 00:00:00 2001 From: tzzzzzzzx <414850847@qq.com> Date: Tue, 20 Sep 2022 17:19:00 +0800 Subject: [PATCH] ar --- SafariChess_Gamev1.0.py | 69 +++++++++++++----- .../SafariChess_Classes.cpython-39.pyc | Bin 836 -> 795 bytes .../SafariChess_backend.cpython-39.pyc | Bin 9523 -> 9482 bytes network/config.txt | 4 +- 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/SafariChess_Gamev1.0.py b/SafariChess_Gamev1.0.py index 77f4ee6..4b86371 100644 --- a/SafariChess_Gamev1.0.py +++ b/SafariChess_Gamev1.0.py @@ -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 @@ -429,15 +453,7 @@ def main(mode): click_queue = [] 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") - game_over = True - clock.tick(5) + 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 + + 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() diff --git a/__pycache__/SafariChess_Classes.cpython-39.pyc b/__pycache__/SafariChess_Classes.cpython-39.pyc index a8f5bb0428c98dc0bee08f1a1c9598b755274729..f522a807260310069fa23f0638fe01ff411ead1a 100644 GIT binary patch delta 48 zcmX@YHk*w*k(ZZ?0SGn}Ds1GAW)%Bn?qU@aP?VpQnp_eQV`Su5T2h*uw|Ob!d`192 C*$&tM delta 89 zcmbQuc7%;Pk(ZZ?0SK5F#5ZzBGunTR1#;Z1VqUCU_AHC@wr`Db%*)J8%yG_0EiT?%!#JN20H