Python基于pygame实现单机版五子棋对战


Posted in Python onDecember 26, 2019

python实现的五子棋,能够自动判断输赢,没有是实现电脑对战功能

源码下载:pygame五子棋

# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
 
initChessList = []
initRole = 1 # 代表白子下 2:代表当前是黑子下
resultFlag = 0
userFlag = True
 
class StornPoint():
 def __init__(self, x, y, value = 0):
  '''
  :param x: 代表x轴坐标
  :param y: 代表y轴坐标
  :param value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子
  '''
  self.x = x
  self.y = y
  self.value = value
  pass
 
 
def initChessSquare(x, y):
 '''
 初始化棋盘的坐标
 :param x:
 :param y:
 :return:
 '''
 # 使用二维列表保存了棋盘是的坐标系,和每个落子点的数值
 for i in range(15):  # 每一行的交叉点坐标
  rowList = []
  for j in range(15): # 每一列的交叉点坐标
   pointX = x + j*40
   pointY = y + i*40
   # value = 0
   sp = StornPoint(pointX, pointY, 0)
   rowList.append(sp)
   pass
  initChessList.append(rowList)
 pass
 
# 处理事件
def eventHandler():
 global userFlag
 '''
 监听各种事件
 :return:
 '''
 for event in pygame.event.get():
 
  global initRole
  # 监听点积退出按钮事件
  if event.type == QUIT:
   pygame.quit()
   sys.exit()
   pass
  # 监听鼠标点积事件
  if event.type == MOUSEBUTTONDOWN:
   x, y = pygame.mouse.get_pos() #
   print((x, y))
   i = j = 0
   for temp in initChessList:
    for point in temp:
     if x >= point.x - 15 and x <= point.x + 15 \
      and y >= point.y - 15 and y <= point.y + 15:
      # 当前区域没有棋子,并且是白子下
      if point.value == 0 and initRole == 1 and userFlag:
       point.value = 1
       judgeResult(i, j, 1)
       initRole = 2 # 切换棋子颜色
       pass
      elif point.value == 0 and initRole == 2 and userFlag:
       point.value = 2
       judgeResult(i, j, 2)
       initRole = 1 # 切换棋子颜色
       pass
      break
      pass
     j += 1
     pass
    i += 1
    j = 0
   pass
  pass
 
 pass
 
# 判断输赢函数
def judgeResult(i, j, value):
 global resultFlag
 
 flag = False # 用于判断是否已经判决出输赢
 for x in range(j - 4, j + 5): # 水平方向有没有出现5连
  if x >= 0 and x + 4 < 15 :
   if initChessList[i][x].value == value and \
    initChessList[i][x + 1].value == value and \
    initChessList[i][x + 2].value == value and \
    initChessList[i][x + 3].value == value and \
    initChessList[i][x + 4].value == value :
    flag = True
    break
    pass
 for x in range(i - 4, i + 5): # 垂直方向有没有出现5连
  if x >= 0 and x + 4 < 15:
   if initChessList[x][j].value == value and \
     initChessList[x + 1][j].value == value and \
     initChessList[x + 2][j].value == value and \
     initChessList[x + 3][j].value == value and \
     initChessList[x + 4][j].value == value:
    flag = True
    break
    pass
 
 # 判断东北方向的对角线是否出现了5连
 for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):
  if x >= 0 and x+4 < 15 and y + 4 >= 0 and y < 15:
   if initChessList[y][x].value == value and \
     initChessList[y - 1][x + 1].value == value and \
     initChessList[y - 2][x + 2].value == value and \
     initChessList[y - 3][x + 3].value == value and \
     initChessList[y - 4][x + 4].value == value:
    flag = True
    break
    pass
   pass
  pass
 
 # 判断西北方向的对角是否出现了五连
 for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):
  if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:
   if initChessList[y][x].value == value and \
     initChessList[y + 1][x + 1].value == value and \
     initChessList[y + 2][x + 2].value == value and \
     initChessList[y + 3][x + 3].value == value and \
     initChessList[y + 4][x + 4].value == value:
    flag = True
    break
    pass
   pass
  pass
 
 if flag:
  resultFlag = value
  pass
 pass
 
# 加载素材
def main():
 global resultFlag, initChessList
 initChessSquare(27, 27) # 初始化棋牌
 pygame.init()   # 初始化游戏环境
 # 创建游戏窗口
 screen = pygame.display.set_mode((620,620), 0, 0) # 第一个参数是元组:窗口的长和宽
 # 添加游戏标题
 pygame.display.set_caption("五子棋小游戏")
 # 图片的加载
 background = pygame.image.load('images/bg.png')
 blackStorn = pygame.image.load('images/storn_black.png')
 whiteStorn = pygame.image.load('images/storn_white.png')
 winStornW = pygame.image.load('images/white.png')
 winStornB = pygame.image.load('images/black.png')
 rect = blackStorn.get_rect()
 
 while True:
  screen.blit(background, (0, 0))
  # 更新棋盘棋子
  for temp in initChessList:
   for point in temp:
    if point.value == 1:
     screen.blit(whiteStorn, (point.x - 18, point.y - 18))
     pass
    elif point.value == 2:
     screen.blit(blackStorn, (point.x - 18, point.y - 18))
     pass
    pass
   pass
  # 如果已经判决出输赢
  if resultFlag > 0:
   initChessList = []  # 清空棋盘
   initChessSquare(27, 27) # 重新初始化棋盘
   if resultFlag == 1:
    screen.blit(winStornW, (50,100))
   else:
    screen.blit(winStornB, (50,100))
   pass
  pygame.display.update()
 
  if resultFlag >0:
   time.sleep(3)
   resultFlag = 0
   pass
  eventHandler()
  pass
 
 pass
 
if __name__ == "__main__":
 main()
 pass

Python基于pygame实现单机版五子棋对战

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中用Decorator来简化元编程的教程
Apr 13 Python
python清理子进程机制剖析
Nov 23 Python
浅谈使用Python内置函数getattr实现分发模式
Jan 22 Python
tensorflow实现图像的裁剪和填充方法
Jul 27 Python
Python延时操作实现方法示例
Aug 14 Python
Django中使用第三方登录的示例代码
Aug 20 Python
Python过滤txt文件内重复内容的方法
Oct 21 Python
pygame实现雷电游戏雏形开发
Nov 20 Python
python实现AES和RSA加解密的方法
Mar 28 Python
Tensorflow中的dropout的使用方法
Mar 13 Python
python如何将图片转换素描画
Sep 08 Python
OpenCV+Python3.5 简易手势识别的实现
Dec 21 Python
用python3读取python2的pickle数据方式
Dec 25 #Python
python文件绝对路径写法介绍(windows)
Dec 25 #Python
python 实现将list转成字符串,中间用空格隔开
Dec 25 #Python
python 输出列表元素实例(以空格/逗号为分隔符)
Dec 25 #Python
python 定义类时,实现内部方法的互相调用
Dec 25 #Python
Python:type、object、class与内置类型实例
Dec 25 #Python
使用python实现希尔、计数、基数基础排序的代码
Dec 25 #Python
You might like
zf框架的db类select查询器join链表使用示例(zend框架)
2014/03/14 PHP
ThinkPHP提交表单时默认自动转义的解决方法
2014/11/25 PHP
php实现utf-8转unicode函数分享
2015/01/06 PHP
php读取文件内容到数组的方法
2015/03/16 PHP
php约瑟夫问题解决关于处死犯人的算法
2015/03/23 PHP
使用php转义输出HTML到JavaScript
2015/03/27 PHP
详解PHP中websocket的使用方法
2016/09/15 PHP
javascript 随机展示头像实现代码
2011/12/06 Javascript
图片无缝滚动代码(向左/向下/向上)
2013/04/10 Javascript
利用js制作html table分页示例(js实现分页)
2014/04/25 Javascript
jQuery.holdReady()使用方法
2014/05/20 Javascript
深入理解js函数的作用域与this指向
2016/05/28 Javascript
基于javascript实现数字英文验证码
2017/01/25 Javascript
详解基于Bootstrap+angular的一个豆瓣电影app
2017/06/26 Javascript
详解从Vue.js源码看异步更新DOM策略及nextTick
2017/10/11 Javascript
用node-webkit把web应用打包成桌面应用(windows环境)
2018/02/01 Javascript
微信小程序input框中加入小图标的实现方法
2018/06/19 Javascript
发布Angular应用至生产环境的方法
2018/12/10 Javascript
vue如何获取自定义元素属性参数值的方法
2019/05/14 Javascript
深入了解Vue.js 混入(mixins)
2020/07/23 Javascript
vue+elementUI动态增加表单项并添加验证的代码详解
2020/12/17 Vue.js
Python中获取网页状态码的两个方法
2014/11/03 Python
跟老齐学Python之使用Python操作数据库(1)
2014/11/25 Python
详解Python中的多线程编程
2015/04/09 Python
在Django同1个页面中的多表单处理详解
2017/01/25 Python
Python基于plotly模块实现的画图操作示例
2019/01/23 Python
python调试神器PySnooper的使用
2019/07/03 Python
django框架F&amp;Q 聚合与分组操作示例
2019/12/12 Python
从零开始的TensorFlow+VScode开发环境搭建的步骤(图文)
2020/08/31 Python
pycharm进入时每次都是insert模式的解决方式
2021/02/05 Python
新西兰第一的行李箱网站:luggage.co.nz
2019/07/22 全球购物
本科生求职信
2014/06/17 职场文书
有关九一八事变的演讲稿
2014/09/14 职场文书
民事起诉书范本
2015/05/19 职场文书
Python使用protobuf序列化和反序列化的实现
2021/05/19 Python
简单且有用的Python数据分析和机器学习代码
2021/07/02 Python