亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

python實現網絡五子棋

瀏覽:9日期: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 編程
相關文章:
主站蜘蛛池模板: 手机看片欧美日韩 | 久久www成人看片 | 亚洲成人日韩 | 日韩黄色中文字幕 | 美国特级a毛片免费网站 | 一级毛片aa高清免费观看 | 欧美毛片免费观看 | 自拍偷拍视频网站 | 欧美黑人巨大白妞出浆 | 69交性视频 | 成人18网址在线观看 | 成人国产精品免费视频 | 曰本又色又爽又黄三级视频 | 成年午夜性视频免费播放 | 欧美视频中文字幕 | 亚洲国产精品第一区二区 | 欧美日韩国产片 | 国产精品久久免费观看 | 国产不卡一区二区视频免费 | 91最新免费地址入口 | 1024手机在线播放 | 黄色a视频在线观看 | 久久国产精品久久久久久久久久 | 亚洲一区二区三区高清网 | 我要看黄色一级片 | 日韩精品首页 | 黄视频网站免费看 | a级成人毛片久久 | 日韩一二区 | 久久免费国产精品一区二区 | 亚洲欧美综合国产精品一区 | 国内国内在线精品视频 | 国产伦理久久精品久久久久 | 国产成人激烈叫床视频 | 成人 日韩 在线 | 嫩操影院 | 欧美黑人换爱交换乱理伦片 | 国产精品视频一区二区三区经 | 日韩中文字幕网站 | 噜噜噜噜精品视频在线观看 | 国产黄片毛片 |