python+pygame实现五子棋网络对战之二 上篇我们对python的Socket编程有了一个基本的认识,接下来就可以完成五子棋的网络对战的实现。步骤如下:1、服务端开始侦听2、客户端通过tcp连接上服务端,开局3、判断输赢后,一方提出是否开新局4、对方同意后,开新局。接下来就简单了,只要把Socket编程的通信部分弄到以前的代码就可以了。五子棋人机对战版:(https://blog.csdn.net/cxhold/article/details/140126002https://blog.csdn.net/cxhold/article/details/140133224https://blog.csdn.net/cxhold/article/details/140136176https://blog.csdn.net/cxhold/article/details/140142940https://blog.csdn.net/cxhold/article/details/140159258)五子棋人机代码三、代码结构:服务端和客户端的代码区别就在main.py中3.1服务端main.py# encoding:utf-8 import pygame from pygame.locals import * import sys import os from socket import * import select import piece from tkinter import * from params import Params from utils import * pygame.init() pygame.mixer.init() WHITE = (255, 255, 255) BLACK = (0, 0, 0) rows = int(Params.get('ROWS')) blocksize = int(Params.get('blockSize')) width = int(Params.get('WIDTH')) height = int(Params.get('HEIGHT')) game_bgsize = width, height # 棋盘 x = [] #x轴 y = [] #y轴 for i in range(0, rows): #棋盘的所有交叉点坐标(x,y) x.append(28 + i * blocksize) y.append(28 + i * blocksize) # 记录棋盘每个坐标的属性,没有棋子为0,白棋为1,黑棋为2 chess_map = {} #结构是chess_map[28,28] = 0 ... # 字体 fontpath = os.path.join(Params.get('resourcePath'), Params.get('fontFolder'), 'simkai.ttf') myfont = pygame.font.Font(fontpath, 38) # 起始选项 txtplayerandplayer = myfont.render("玩家与玩家", True, WHITE) txtplayerandplayer_rect = txtplayerandplayer.get_rect() txtplayerandplayer_rect.left, txtplayerandplayer_rect.top = (game_bgsize[0] - txtplayerandplayer_rect.width) // 2, \ (game_bgsize[1] - txtplayerandplayer_rect.height) // 2 - 100 txtplayerandcomputer = myfont.render('玩家与电脑', True, WHITE) txtplayerandcomputer_rect = txtplayerandcomputer.get_rect() txtplayerandcomputer_rect.left, txtplayerandcomputer_rect.top = (game_bgsize[0] - txtplayerandplayer_rect.width) // 2, \ (game_bgsize[1] - txtplayerandplayer_rect.height) // 2 txtplaysound = myfont.render('音效: 开', True, WHITE) txtplaysound_rect = txtplaysound.get_rect() txtplaysound_rect.left, txtplaysound_rect.top = (game_bgsize[0] - txtplaysound_rect.width) // 2, \ (game_bgsize[1] - txtplaysound_rect.height) // 2 + 100 txtclosesound = myfont.render('音效: 关', True, WHITE) txtclosesound_rect = txtclosesound.get_rect() txtclosesound_rect.left, txtclosesound_rect.top = (game_bgsize[0] - txtclosesound_rect.width) // 2, \ (game_bgsize[1] - txtclosesound_rect.height) // 2 + 100 # 输赢 successtext = myfont.render("你有点屌哦!!!", True, WHITE) successtext_rect = successtext.get_rect() successtext_rect.left, successtext_rect.top = (game_bgsize[0] - successtext_rect.width) // 2, \ (game_bgsize[1] - successtext_rect.height) // 2 failuretext = myfont.render("小趴菜...", True, WHITE) failuretext_rect = failuretext.get_rect() failuretext_rect.left, failuretext_rect.top = (game_bgsize[0] - failuretext_rect.width) // 2, \ (game_bgsize[1] - failuretext_rect.height) // 2 playagaintext = myfont.render("再玩一局", True, WHITE) menutext = myfont.render('主菜单', True, WHITE) #连接 txtwatting = myfont.render('等待连接', True, WHITE) txtwatting_rect = txtclosesound.get_rect() txtwatting_rect.left, txtwatting_rect.top = (game_bgsize[0] - txtwatting_rect.width) // 2, \ (game_bgsize[1] - txtwatting_rect.height) // 2 txtclosed = myfont.render('对方不愿和你玩并断开了连接', True, WHITE) txtclosed_rect = txtclosed.get_rect() txtclosed_rect.left, txtclosed_rect.top = (game_bgsize[0] - txtclosed_rect.width) // 2, \ (game_bgsize[1] - txtclosed_rect.height) // 2 txtchallenge = myfont.render('对方再次向你发起挑战', True, WHITE) txtchallenge_rect = txtchallenge.get_rect() txtchallenge_rect.left, txtchallenge_rect.top = (game_bgsize[0] - txtchallenge_rect.width) // 2, \ (game_bgsize[1] - txtchallenge_rect.height) // 2 txtrecive = myfont.render('接受挑战 不接受挑战', True, WHITE) txtrecive_rect = txtrecive.get_rect() txtrecive_rect.left, txtrecive_rect.top = (game_bgsize[0] - txtrecive_rect.width) // 2, \ (game_bgsize[1] - txtrecive_rect.height) // 2 + 100 # TCP服务 HOST = '' # 地址 POST = 23338 # 端口 BUFSIZE = 2048 #缓冲区 HOSTADDR = (HOST, POST) tcpserversocket = socket(AF_INET, SOCK_STREAM) #创建套接字 tcpserversocket.bind(HOSTADDR) tcpserversocket.listen(1) #listen(n)的n值表示socket的“排队个数”,表示的是服务器拒绝(超过限制数量的)连接之前,操作系统可以挂起的最大连接数量。 #一般情况下,一个进程只有一个主线程(也就是单线程),那么socket允许的最大连接数为: n + 1 \ # 如果服务器是多线程,比如开了2个线程,那么socket允许的最大连接数就是: n + 2 \ # 换句话说:排队的人数(就是那个n) + 正在就餐的人数(服务器正在处理的socket连接数) = 允许接待的总人数(socket允许的最大连接数)\ # 因此listen(1),最大连接数是2,就是1个处理,1个“排队” inputs = [tcpserversocket] # 判断棋盘某位置上是否有棋子 def isempty(theposition): global chess_map existpiece = False if chess_map[str(theposition[0]) + ',' + str(theposition[1])]: existpiece = True return existpiece # 电脑选取落子的位置 def computerdecision(): value = max1 = max2 = 0 pos1 = pos2 = () # 进攻 for i in range(0, rows): row = 28 + i * blocksize for j in range(0, rows): col = 28 + j * blocksize pos = (row, col) if isempty(pos): continue value = pointvalue(chess_map,pos, 1, 2) if value max1: max1 = value pos1 = (row, col) # 防守 for i in range(0, rows): for j in range(0, rows): row = 28 + i * blocksize col = 28 + j * blocksize if isempty((row, col)): continue value = pointvalue(chess_map,(row, col), 2, 1) if value max2: max2 = value pos2 = (row, col) if max1 max2: return pos1 else: return pos2 # 初始化棋盘 def init(): global chess_map for i in x: for j in y: chess_map[str(i) + ',' + str(j)] = 0 #清空 def main(): os.environ['SDL_VIDEO_CENTERED'] = '1' #居中 screen = pygame.display.set_mode(game_bgsize) pygame.display.set_caption('五子棋服务端') bgimagepath = os.path.join(Params.get('resourcePath'), Params.get('imgFolder'), 'bg.png') bg_image = pygame.image.load(bgimagepath).convert_alpha() # 背景图片加载 zzimagepath = os.path.join(Params.get('resourcePath'), Params.get('imgFolder'), 'zz.png') #遮罩图片 zz_image = pygame.image.load(zzimagepath).convert_alpha() # 背景音乐 bgsoundpath = os.path.join(Params.get('resourcePath'), Params.get('soundFolder'), '齐豫 - 莲花处处开.mp3') bg_sound = pygame.mixer.music.load(bgsoundpath) # 背景音乐加载 pygame.mixer.music.set_volume(0.08) pygame.mixer.music.play(-1) clicksoundpath =