python|python pygame实现五子棋双人联机
本文实例为大家分享了python pygame实现五子棋双人联机的具体代码,供大家参考,具体内容如下
同一局域网内,服务端开启时,另一机器将IP地址HOST改为服务端对应的IP地址、端口号与服务端的保持一致即可实现双人联机。(IP地址查询方式:菜单栏输入cmd,cmd窗口输入ipconfig找到无线网络下的IPv4地址)
服务端:
# -*- coding: utf-8 -*-"""Created on Tue Jun8 14:03:09 2021@author: Administrator"""import pygameimport sysfrom pygame.locals import *from collections import Counterfrom socket import *from time import ctimeimport jsonimport selectimport socket#界面初始化screen=pygame.display.set_mode((400,450))pygame.display.set_caption('五子棋')pygame.init()#图片导入img_board=pygame.image.load('F:/images/五子棋/chess_board.png')img_bchess=pygame.image.load('F:/images/五子棋/black_chess.jpg')img_wchess=pygame.image.load('F:/images/五子棋/white_chess.jpg')#颜色white=(255,255,255)black=(0,0,0)#用于传送的数据msg=[]#棋盘定义chess_board=[[]]def set_chess_board():x,y=0,0while True:if x==400:x=0y+=40if y<400:chess_board.append([])if y==400:breakchess_board[-1].append([x,y])x+=40set_chess_board()#棋盘格子是否落子chess_exist=[[0 for i in range(10)]for j in range(10)]#黑白棋子初始化black_chess,white_chess=[],[]#棋子类型chess_kind=1#1为黑棋,0为白棋wcx,wcy,bcx,bcy=[],[],[],[]#white_chess_xdef draw_board():for i in chess_board:for j in i:screen.blit(img_board,(j[0],j[1]))pygame.display.update()#默认棋子类型为1def set_chess():if event.type==MOUSEBUTTONDOWN:pos=pygame.mouse.get_pos()for i in range(len(chess_board)):for j in range(len(chess_board[i])):if chess_board[i][j][0]=5:xy=[]for j in chess:if j[m]==i:xy.append(j[n])xy.sort()count=0for j in range(len(xy)-1):if xy[j]+1==xy[j+1]:count+=1else:count=0if count>=4:return 1def xiejiao_win(chess):x,y=[],[]chess.sort()for i in chess:x.append(i[0])y.append(i[1])c,first,last=0,0,0for i in range(len(x)-1):if x[i+1]!=x[i]:if x[i]+1==x[i+1]:c+=1last=i+1else:if c<4:first=i+1c=0else:last=iprint(last)breakelse:last=i+1if c>=4:dis=[]for i in range(first,last+1):dis.append(x[i]-y[i])count=Counter(dis)for i in count:if count[i]>=5:return 1dis=[]x2=[i*(-1) for i in x]for i in range(first,last+1):dis.append(x2[i]-y[i])count=Counter(dis)for i in count:if count[i]>=5:return 1def gameover():wcx_count,wcy_count,bcx_count,bcy_count=Counter(wcx),Counter(wcy),Counter(bcx),Counter(bcy)if row_column_win(wcx_count,0,1,white_chess)==1:return 0elif row_column_win(bcx_count,0,1,black_chess)==1:return 1elif row_column_win(wcy_count,1,0,white_chess)==1:return 0elif row_column_win(bcy_count,1,0,black_chess)==1:return 1elif xiejiao_win(white_chess)==1:return 0elif xiejiao_win(black_chess)==1:return 1def draw_text(text,x,y,size):pygame.font.init()fontObj=pygame.font.SysFont('SimHei',size )textSurfaceObj=fontObj.render(text, True, white,black)textRectObj=textSurfaceObj.get_rect()textRectObj.center=(x,y)screen.blit(textSurfaceObj, textRectObj)pygame.display.update()#定义服务器名称HOST = '0.0.0.0'PORT = 400BUFSIZE = 1024ADDR = (HOST,PORT)#定义服务器属性tcpsersock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)tcpsersock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)# 对socket的配置重用ip和端口号tcpsersock.bind(ADDR)tcpsersock.listen(1)inputs=[tcpsersock]print(inputs)draw_board()settable=1link=Falsewhile True:rs,ws,es=select.select(inputs, [], [],0)for r in rs:if r is tcpsersock:link=Trueprint('new ser')tcpcliscock, addr = tcpsersock.accept()inputs.append(tcpcliscock)else:data,addr=r.recvfrom(BUFSIZE)disconnected=not datadraw_text('你的回合',200,420,15)if disconnected:inputs.remove(r)draw_text('对手掉线',200,420,15)while True:for event in pygame.event.get():if event.type==QUIT:pygame.quit()sys.exit()else:data=https://www.it610.com/article/json.loads(data)settable=1white_chess.append(data)wcx.append(data[0])wcy.append(data[1])for event in pygame.event.get():if event.type==QUIT:pygame.quit()sys.exit()tcpsersock.close()if link==True:if settable==1:if set_chess()==1:draw_text('对手回合',200,420,15)settable=0msg1=json.dumps(msg)tcpcliscock.sendto(msg1.encode(),ADDR)msg=[]draw_chess()if gameover()==1:draw_text('你赢了!',200,420,15)while True:for event in pygame.event.get():if event.type==QUIT:pygame.quit()sys.exit()elif gameover()==0:draw_text('你输了!',200,420,15)while True:for event in pygame.event.get():if event.type==QUIT:pygame.quit()sys.exit()tcpcliscock.close()tcpsersock.close()
客户端:
# -*- coding: utf-8 -*-"""Created on Tue Jun8 14:03:09 2021@author: Administrator"""import pygameimport sysfrom pygame.locals import *from collections import Counterfrom socket import *from time import ctimeimport jsonimport selectimport socketimport time#界面初始化screen=pygame.display.set_mode((400,450))pygame.display.set_caption('五子棋')pygame.init()#图片导入img_board=pygame.image.load('F:/images/五子棋/chess_board.png')img_bchess=pygame.image.load('F:/images/五子棋/black_chess.jpg')img_wchess=pygame.image.load('F:/images/五子棋/white_chess.jpg')#颜色white=(255,255,255)black=(0,0,0)#用于传送的数据msg=[]#棋盘定义chess_board=[[]]def set_chess_board():x,y=0,0while True:if x==400:x=0y+=40if y<400:chess_board.append([])if y==400:breakchess_board[-1].append([x,y])x+=40set_chess_board()#棋盘格子是否落子chess_exist=[[0 for i in range(10)]for j in range(10)]#黑白棋子初始化black_chess,white_chess=[],[]#棋子类型chess_kind=1#1为黑棋,0为白棋wcx,wcy,bcx,bcy=[],[],[],[]#white_chess_xdef draw_board():for i in chess_board:for j in i:screen.blit(img_board,(j[0],j[1]))pygame.display.update()#默认棋子类型为0def set_chess():if event.type==MOUSEBUTTONDOWN:pos=pygame.mouse.get_pos()for i in range(len(chess_board)):for j in range(len(chess_board[i])):if chess_board[i][j][0]=5:xy=[]for j in chess:if j[m]==i:xy.append(j[n])xy.sort()count=0for j in range(len(xy)-1):if xy[j]+1==xy[j+1]:count+=1else:count=0if count>=4:return 1def xiejiao_win(chess):x,y=[],[]chess.sort()for i in chess:x.append(i[0])y.append(i[1])c,first,last=0,0,0for i in range(len(x)-1):if x[i+1]!=x[i]:if x[i]+1==x[i+1]:c+=1last=i+1else:if c<4:first=i+1c=0else:last=iprint(last)breakelse:last=i+1if c>=4:dis=[]for i in range(first,last+1):dis.append(x[i]-y[i])count=Counter(dis)for i in count:if count[i]>=5:return 1dis=[]x2=[i*(-1) for i in x]for i in range(first,last+1):dis.append(x2[i]-y[i])count=Counter(dis)for i in count:if count[i]>=5:return 1def gameover():wcx_count,wcy_count,bcx_count,bcy_count=Counter(wcx),Counter(wcy),Counter(bcx),Counter(bcy)if row_column_win(wcx_count,0,1,white_chess)==1:return 1elif row_column_win(bcx_count,0,1,black_chess)==1:return 0elif row_column_win(wcy_count,1,0,white_chess)==1:return 1elif row_column_win(bcy_count,1,0,black_chess)==1:return 0elif xiejiao_win(white_chess)==1:return 1elif xiejiao_win(black_chess)==1:return 0def draw_text(text,x,y,size):pygame.font.init()fontObj=pygame.font.SysFont('SimHei',size )textSurfaceObj=fontObj.render(text, True, white,black)textRectObj=textSurfaceObj.get_rect()textRectObj.center=(x,y)screen.blit(textSurfaceObj, textRectObj)pygame.display.update()#定义客户端名称HOST = '10.203.111.180'PORT = 400BUFSIZE = 1024ADDR = (HOST,PORT)#连接服务器tcpCliSock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)tcpCliSock.connect(ADDR)inputs=[tcpCliSock]draw_board()settable=0while True:rs,ws,es=select.select(inputs,[],[],0)for r in rs:if r is tcpCliSock:data,addr = r.recvfrom(BUFSIZE)draw_text('你的回合',200,420,15)data=https://www.it610.com/article/json.loads(data)settable=1black_chess.append(data)bcx.append(data[0])bcy.append(data[1])for event in pygame.event.get():if event.type==QUIT:tcpCliSock.close()pygame.quit()sys.exit()if settable==1:if set_chess()==1:draw_text('对手回合',200,420,15)settable=0msg1=json.dumps(msg)tcpCliSock.sendto(msg1.encode(),ADDR)msg=[]draw_chess()if gameover()==1:draw_text('你赢了!',200,420,15)while True:for event in pygame.event.get():if event.type==QUIT:pygame.quit()sys.exit()elif gameover()==0:draw_text('你输了!',200,420,15)while True:for event in pygame.event.get():if event.type==QUIT:pygame.quit()sys.exit()
背景:
文章图片
黑棋:
文章图片
白棋:
文章图片
效果演示:
【python|python pygame实现五子棋双人联机】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- python神经网络学习使用Keras进行回归运算
- 数据结构(c语言实现)|<数据结构>还不会写单向链表(我手把手教你)
- 数据结构(c语言实现)|<数据结构>刷题笔记——链表篇(一)(有动图详解)
- Android 桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果
- Android 类似360悬浮窗口实现源码
- 安卓手机端实现点击电话号码,出现拨打复制保存到通讯录
- 数据结构|排序算法之希尔排序——c++实现
- 算法|希尔排序——JS实现
- 手撕常用排序算法|希尔排序——C语言实现
- 数据结构|C语言实现插入排序——希尔排序算法