日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

python實現網絡五子棋

瀏覽:116日期:2022-06-22 16:48:43

本文實例為大家分享了python實現網絡五子棋的具體代碼,供大家參考,具體內容如下

服務器端:

import osimport socketimport threadingfrom tkinter import *from tkinter.messagebox import *def drawQiPan(): for i in range(0, 15):cv.create_line(20, 20 + 40 * i, 580, 20 + 40 * i, width=2) for i in range(0, 15):cv.create_line(20 + 40 * i, 20, 20 + 40 * i, 580, width=2) cv.pack()# 走棋函數def callPos(event): global turn global MyTurn if MyTurn == -1: # 第一次確認自己的角色MyTurn = turn else:if MyTurn != turn: showinfo(title='提示', message='還沒輪到自己下棋') return # print('clicked at',event.x,event.y,true) x = event.x // 40 y = event.y // 40 print('clicked at', x, y, turn) if maps[x][y] != ' ':showinfo(title='提示', message='已有棋子') else:img1 = images[turn]cv.create_image((x * 40 + 20, y * 40 + 20), image=img1)cv.pack()maps[x][y] = str(turn)pos = str(x) + ',' + str(y)sendMessage('move|' + pos)print('服務器走的位置', pos)label1['text'] = '服務器走的位置' + pos# 輸出輸贏信息if win_lose(): if turn == 0:showinfo(title='提示', message='黑方你贏了')sendMessage('over|黑方你贏了') else:showinfo(title='提示', message='白方你贏了')sendMessage('over|白方你贏了')# 換下一方走棋if turn == 0: turn = 1else: turn = 0# 發送消息def sendMessage(pos): global s global addr s.sendto(pos.encode(), addr)# 退出函數def callExit(event): pos = 'exit|' sendMessage(pos) os.exit()# 畫對方棋子def drawOtherChess(x, y): global turn img1 = images[turn] cv.create_image((x * 40 + 20, y * 40 + 20), image=img1) cv.pack() maps[x][y] = str(turn) # 換下一方走棋 if turn == 0:turn = 1 else:turn = 0# 判斷整個棋盤的輸贏def win_lose(): a = str(turn) print('a=', a) for i in range(0, 11):for j in range(0, 11): if maps[i][j] == a and maps[i + 1][j + 1] == a and maps[i + 2][j + 2] == a and maps[i + 3][j + 3] == a and maps[i + 4][j + 4] == a:print('x=y軸上形成五子連珠')return True for i in range(4, 15):for j in range(0, 11): if maps[i][j] == a and maps[i - 1][j + 1] == a and maps[i - 2][j + 2] == a and maps[i - 3][j + 3] == a and maps[i - 4][j + 4] == a:print('x=-y軸上形成五子連珠')return True for i in range(0, 15):for j in range(4, 15): if maps[i][j] == a and maps[i][j - 1] == a and maps[i][j - 2] == a and maps[i][j - 2] == a and maps[i][j - 4] == a:print('Y軸上形成了五子連珠')return True for i in range(0, 11):for j in range(0, 15): if maps[i][j] == a and maps[i + 1][j] == a and maps[i + 2][j] == a and maps[i + 3][j] == a and maps[i + 4][j] == a:print('X軸形成五子連珠')return True return False# 輸出map地圖def print_map(): for j in range(0, 15):for i in range(0, 15): print(maps[i][j], end=’ ’)print(’w’)# 接受消息def receiveMessage(): global s while True: # 接受客戶端發送的消息global addrdata, addr = s.recvfrom(1024)data = data.decode(’utf-8’)a = data.split('|')if not data: print(’client has exited!’) breakelif a[0] == ’join’: # 連接服務器的請求 print(’client 連接服務器!’) label1['text'] = ’client連接服務器成功,請你走棋!’elif a[0] == ’exit’: print(’client對方退出!’) label1['text'] = ’client對方退出,游戲結束!’elif a[0] == ’over’: print(’對方贏信息!’) label1['text'] = data.split('|')[0] showinfo(title='提示', message=data.split('1')[1])elif a[0] == ’move’: print(’received:’, data, ’from’, addr) p = a[1].split(',') x = int(p[0]) y = int(p[1]) print(p[0], p[1]) label1['text'] = '客戶端走的位置' + p[0] + p[1] drawOtherChess(x, y) s.close()def startNewThread(): # 啟動新線程來接受客戶端消息 thread = threading.Thread(target=receiveMessage, args=()) thread.setDaemon(True) thread.start()if __name__ == ’__main__’: root = Tk() root.title('網絡五子棋v2.0-服務器端') images = [PhotoImage(file=’./images/BlackStone.png’), PhotoImage(file=’./images/WhiteStone.png’)] turn = 0 MyTurn = -1 maps = [[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] for y in range(15)] cv = Canvas(root, bg=’green’, width=610, height=610) drawQiPan() cv.bind('<Button-1>', callPos) cv.pack() label1 = Label(root, text='服務器端...') label1.pack() button1 = Button(root, text='退出游戲') button1.bind('<Button-1>', callExit) button1.pack() # 創建UDP SOCKET s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((’localhost’, 8000)) addr = (’localhost’, 8000) startNewThread() root.mainloop()客戶端:

from tkinter import *from tkinter.messagebox import *import socketimport threadingimport os# 主程序root = Tk()root.title('網絡五子棋v2.0--UDP客戶端')imgs = [PhotoImage(file=’./images/BlackStone.png’), PhotoImage(file=’./images/WhiteStone.png’)]turn = 0MyTurn = -1# 畫對方棋子def drawOtherChess(x, y): global turn img1 = imgs[turn] cv.create_image((x * 40 + 20, y * 40 + 20), image=img1) cv.pack() maps[x][y] = str(turn) # 換下一方走棋 if turn == 0:turn = 1 else:turn = 0# 發送消息def sendMessage(position): global s s.sendto(position.encode(), (host, port))# 退出函數def callExit(event): position = 'exit|' sendMessage(position) os.exit()# 走棋函數def callback(event): global turn global MyTurn if MyTurn == -1:MyTurn = turn else:if MyTurn != turn: showinfo(title='提示', message='還沒輪到自己走棋') return # print('clicked at',event.x,event.y) x = event.x // 40 y = event.y // 40 print('clicked at', x, y, turn) if maps[x][y] != ' ':showinfo(title='提示', message='已有棋子') else:img1 = imgs[turn]cv.create_image((x * 40 + 20, y * 40 + 20), image=img1)cv.pack()maps[x][y] = str(turn)position = str(x) + ’,’ + str(y)sendMessage('move|' + position)print('客戶端走的位置', position)label1['text'] = '客戶端走的位置' + position# 輸出輸贏信息if win_lose(): if turn == 0:showinfo(title='提示', message='黑方你贏了')sendMessage('over|黑方你贏了!') else:showinfo(title='提示', message='白方你贏了!')sendMessage('over|白方你贏了!')# 換下一方走棋:if turn == 0: turn = 1else: turn = 0# 畫棋盤def drawQiPan(): # 畫棋盤 for i in range(0, 15):cv.create_line(20, 20 + 40 * i, 580, 20 + 40 * i, width=2) for i in range(0, 15):cv.create_line(20 + 40 * i, 20, 20 + 40 * i, 580, width=2) cv.pack()# 輸贏判斷def win_lose(): a = str(turn) print('a=', a) for i in range(0, 11):for j in range(0, 11): if maps[i][j] == a and maps[i + 1][j + 1] == a and maps[i + 2][j + 2] == a and maps[i + 3][j + 3] == a and maps[i + 4][j + 4] == a:print('x=y軸上形成五子連珠')return True for i in range(4, 15):for j in range(0, 11): if maps[i][j] == a and maps[i - 1][j + 1] == a and maps[i - 2][j + 2] == a and maps[i - 3][j + 3] == a and maps[i - 4][j + 4] == a:print('x=-y軸上形成五子連珠')return True for i in range(0, 15):for j in range(4, 15): if maps[i][j] == a and maps[i][j - 1] == a and maps[i][j - 2] == a and maps[i][j - 2] == a and maps[i][j - 4] == a:print('Y軸上形成了五子連珠')return True for i in range(0, 11):for j in range(0, 15): if maps[i][j] == a and maps[i + 1][j] == a and maps[i + 2][j] == a and maps[i + 3][j] == a and maps[i + 4][j] == a:print('X軸形成五子連珠')return True return False# 接受消息def receiveMessage(): # 接受消息 global s while True:data = s.recv(1024).decode(’utf-8’)a = data.split('|')if not data: print(’server has exited!’) breakelif a[0] == ’exit’: print(’對方退出!’) label1['text'] = ’對方退出!游戲結束!’elif a[0] == ’over’: print(’對方贏信息!’) label1['text'] = data.split('|')[0] showinfo(title='提示', message=data.split('|')[1])elif a[0] == ’move’: print(’received:’, data) p = a[1].split(',') x = int(p[0]) y = int(p[1]) print(p[0], p[1]) label1['text'] = '服務器走的位置' + p[0] + p[1] drawOtherChess(x, y) s.close()# 啟動線程接受客戶端消息def startNewThread(): thread = threading.Thread(target=receiveMessage, args=()) thread.setDaemon(True) thread.start()if __name__ == ’__main__’: # 主程序 maps = [[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] for y in range(15)] cv = Canvas(root, bg=’green’, width=610, height=610) drawQiPan() cv.bind('<Button-1>', callback) cv.pack() label1 = Label(root, text='客戶端...') label1.pack() button1 = Button(root, text='退出游戲') button1.bind('<Button-1>', callExit) button1.pack() # 創建UDP s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) port = 8000 host = ’localhost’ pos = ’join|’ sendMessage(pos) startNewThread() root.mainloop()

游戲執行頁面:

python實現網絡五子棋

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
久久青青视频| 麻豆91精品视频| 婷婷成人在线| 午夜av不卡| 午夜av成人| 99久久www免费| 激情五月色综合国产精品| 中文亚洲欧美| 久久精品国语| 亚洲啊v在线免费视频| 国产精品第一| 久久久精品久久久久久96| 亚洲欧美网站| 美日韩一区二区三区| 国产91久久精品一区二区| 在线成人直播| 国产亚洲欧美日韩精品一区二区三区 | 欧美sss在线视频| 久久精品亚洲欧美日韩精品中文字幕| 久久天堂成人| 亚洲人成亚洲精品| 91综合久久爱com| 你懂的国产精品永久在线| 国产精品麻豆成人av电影艾秋| 国产精品九九| 天堂√8在线中文| 日韩一级网站| 欧美日韩xxxx| 国产精品专区免费| 一级欧洲+日本+国产| 亚洲精品大全| 免费亚洲一区| 久久国产中文字幕| 蜜臀va亚洲va欧美va天堂 | 久久婷婷久久| 日韩激情啪啪| 免费毛片在线不卡| 国产精品男女| 午夜在线视频一区二区区别| 成人精品国产亚洲| 午夜久久av| 亚洲成av在线| 国产精品主播在线观看| 五月天激情综合网| 老司机免费视频一区二区| 蜜臀久久久久久久| 亚洲成人一区在线观看| 国产精品亚洲四区在线观看| 国产一级久久| 欧洲一区二区三区精品| 亚洲精品免费观看| 国产尤物精品| 免费在线亚洲欧美| 999国产精品| 国产一卡不卡| 午夜国产欧美理论在线播放| 国产香蕉精品| 日韩视频二区| 精品欠久久久中文字幕加勒比| 婷婷综合社区| 精品在线网站观看| 久久成人亚洲| 高清一区二区| 亚洲精选91| 日韩久久一区二区三区| 免费日本视频一区| а√天堂8资源在线| 中文字幕免费一区二区| 麻豆视频在线观看免费网站黄 | 成人国产精选| 蜜桃久久精品一区二区| 另类专区亚洲| 国产探花在线精品| 亚洲在线国产日韩欧美| 电影91久久久| 精品国产乱码久久久久久1区2匹| 黄色成人在线网址| 1024精品一区二区三区| 精品国产一区二区三区性色av| 欧美精品观看| 日韩免费精品| 少妇精品久久久一区二区| 鲁大师成人一区二区三区| 精品欧美激情在线观看| 天堂网av成人| 在线一区视频观看| 午夜欧美巨大性欧美巨大| 国产一区二区久久久久| 精品中文字幕一区二区三区 | 日本一区二区三区视频在线看| 国产视频亚洲| 日韩av免费大片| 亚洲精品在线国产| 日韩视频中文| 欧美在线观看视频一区| 精品久久美女| 国产精品亚洲片在线播放| 亚洲精品影视| 免播放器亚洲一区| 国产精品普通话对白| 日韩欧美在线中字| 精品三区视频| 欧美国产视频| 婷婷综合一区| 亚洲欧美不卡| 欧美在线亚洲| 一区免费在线| 亚洲精品一二三区区别| 国产在线成人| 国产一区亚洲| 午夜精品一区二区三区国产| 韩国三级一区| 欧美gv在线| 日韩欧美一区二区三区在线视频| 久久久久久夜| 91日韩欧美| 日韩欧美视频专区| 久久久噜噜噜| 久久婷婷一区| 亚洲网站视频| 国产高清一区二区| 亚洲精品2区| 石原莉奈一区二区三区在线观看| 五月天激情综合网| 亚洲免费网址| 亚洲精品一级| 日本综合视频| 国产探花一区| 精品久久网站| 日韩国产欧美一区二区| 另类专区亚洲| 亚洲高清激情| 国产亚洲一级| 视频精品一区二区| 亚洲欧美日本国产| 日本aⅴ亚洲精品中文乱码| 日韩欧美三区| 国产美女撒尿一区二区| 国产精品亚洲一区二区在线观看| 老牛国内精品亚洲成av人片 | 香蕉成人av| 国产99亚洲| 免费久久精品视频| 日韩高清一区二区| 国产精选久久| 日韩欧美一区二区三区免费看| 久久久一二三| 亚洲一区欧美二区| 日韩欧美精品一区二区综合视频| 国产日韩高清一区二区三区在线 | 尤物在线精品| 视频一区中文字幕精品| 久久99影视| 成人va天堂| 日韩午夜精品| 97久久亚洲| 在线天堂中文资源最新版| 一区二区三区视频免费观看| 日韩影院精彩在线| 国产精品久久国产愉拍| 日韩不卡在线| 综合在线一区| 精品深夜福利视频| 自由日本语亚洲人高潮| 91九色综合| 国产精品久久久久久久免费观看| 美女久久久久| 日本电影久久久| 鲁鲁在线中文| 国产精品试看| 国产精品日韩精品在线播放| 日韩国产网站| 日韩高清电影一区| 亚洲成人va| 日韩区一区二| 人人精品亚洲| 日韩中文字幕在线一区| 国产成人1区| 一区视频在线| 久久中文字幕一区二区| 性欧美69xoxoxoxo| 里番精品3d一二三区| 亚洲欧洲午夜| 国产一区2区| 亚洲一区观看| 91日韩在线| 亚洲精选av| 天堂8中文在线最新版在线| 午夜久久av | 欧美亚洲精品在线| 综合国产视频| 宅男噜噜噜66国产日韩在线观看| 精品一区二区三区亚洲| 日韩在线免费| 日韩精品视频网| 欧美日韩一区二区三区视频播放| 91精品尤物| 亚洲男女av一区二区| 婷婷亚洲精品| 亚洲国内精品|