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的Django框架中获取单个对象数据的简单方法
Jul 17 Python
Python爬虫中urllib库的进阶学习
Jan 05 Python
Python中常用的内置方法
Jan 28 Python
python SQLAlchemy的Mapping与Declarative详解
Jul 04 Python
python实现集中式的病毒扫描功能详解
Jul 09 Python
Django 全局的static和templates的使用详解
Jul 19 Python
Python使用scipy模块实现一维卷积运算示例
Sep 05 Python
python pygame实现挡板弹球游戏
Nov 25 Python
Python urllib.request对象案例解析
May 11 Python
你需要学会的8个Python列表技巧
Jun 24 Python
windows安装python超详细图文教程
May 21 Python
详细介绍python类及类的用法
May 31 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
如何使用PHP中的字符串函数
2006/11/24 PHP
php5.3 废弃函数小结
2010/05/16 PHP
PHP命名空间(Namespace)的使用详解
2013/05/04 PHP
PHP分页类集锦
2014/11/18 PHP
php中get_object_vars()方法用法实例
2015/02/08 PHP
检测codeigniter脚本消耗内存情况的方法
2015/03/21 PHP
简要剖析PHP的Yii框架的组件化机制的基本知识
2016/03/17 PHP
php实现保存周期为1天的购物车类
2017/07/07 PHP
开发跨浏览器javascript常见注意事项
2009/01/01 Javascript
javascript 面向对象全新理练之继承与多态
2009/12/03 Javascript
jquery里的each使用方法详解
2010/12/22 Javascript
javascript中indexOf技术详解
2015/05/07 Javascript
javascript检测移动设备横竖屏
2016/05/21 Javascript
jQuery图片渐变特效的简单实现
2016/06/25 Javascript
JS 动态加载js文件和css文件 同步/异步的两种简单方式
2016/09/23 Javascript
JS中用try catch对代码运行的性能影响分析
2016/12/26 Javascript
js实现简单的计算器功能
2017/01/16 Javascript
Javascript中绑定click事件的四种方式介绍
2018/10/26 Javascript
Vue常用的几个指令附完整案例
2018/11/06 Javascript
NodeJs 实现简单WebSocket即时通讯的示例代码
2019/08/05 NodeJs
微信小程序之数据绑定原理解析
2019/08/14 Javascript
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
2018/01/11 Python
Python基于Floyd算法求解最短路径距离问题实例详解
2018/05/16 Python
Pycharm2017版本设置启动时默认自动打开项目的方法
2018/10/29 Python
对PyQt5中的菜单栏和工具栏实例详解
2019/06/20 Python
Python使用GitPython操作Git版本库的方法
2020/02/29 Python
详解HTML5中的Communication API基本使用方法
2016/01/29 HTML / CSS
Photobook澳大利亚:制作相片书,婚礼卡,旅行相簿
2017/01/12 全球购物
final, finally, finalize的区别
2012/03/01 面试题
教学实验楼管理制度
2014/02/01 职场文书
网站创业计划书
2014/04/30 职场文书
开展批评与自我批评发言材料
2014/10/17 职场文书
台风停课通知
2015/04/24 职场文书
如何在Python中创建二叉树
2021/03/30 Python
python 实现定时任务的四种方式
2021/04/01 Python
Windows下用Nginx配置https服务器及反向代理的问题
2021/09/25 Servers