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中比较特别的除法运算和幂运算介绍
Apr 05 Python
Python中文竖排显示的方法
Jul 28 Python
python正则表达式的使用
Jun 12 Python
Python自动化运维_文件内容差异对比分析
Dec 13 Python
在Python中分别打印列表中的每一个元素方法
Nov 07 Python
Python 面试中 8 个必考问题
Nov 16 Python
Python基础之函数的定义与使用示例
Mar 23 Python
python中pygame安装过程(超级详细)
Aug 04 Python
Django 实现前端图片压缩功能的方法
Aug 07 Python
Python Pickle 实现在同一个文件中序列化多个对象
Dec 30 Python
PyTorch 随机数生成占用 CPU 过高的解决方法
Jan 13 Python
python实现sm2和sm4国密(国家商用密码)算法的示例
Sep 26 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面向对象全攻略 (十五) 多态的应用
2009/09/30 PHP
php 调用远程url的六种方法小结
2009/11/02 PHP
PHP中使用CURL伪造来路抓取页面或文件
2011/05/04 PHP
PHP表单验证内容是否为空的实现代码
2016/11/14 PHP
PHP堆栈调试操作简单示例
2018/06/15 PHP
javascript 实现父窗口引用弹出窗口的值的脚本
2007/08/07 Javascript
jQuery判断元素是否是隐藏的代码
2011/04/24 Javascript
javascript动态添加、修改、删除对象的属性与方法详解
2014/01/27 Javascript
详解JavaScript中undefined与null的区别
2014/03/29 Javascript
Google官方支持的NodeJS访问API,提供后台登录授权
2014/07/29 NodeJs
JavaScript+CSS实现仿天猫侧边网页菜单效果
2015/08/25 Javascript
AngularJS中实现动画效果的方法
2016/07/28 Javascript
JavaScript在form表单中使用button按钮实现submit提交方法
2017/01/23 Javascript
Vue.js路由vue-router使用方法详解
2017/03/20 Javascript
原生javascript上传图片带进度条【实例分享】
2017/04/06 Javascript
vue中如何引入jQuery和Bootstrap
2017/04/10 jQuery
vue+vue-router转场动画的实例代码
2018/09/01 Javascript
[01:02]DOTA2辉夜杯决赛日 CDEC.Y对阵VG赛前花絮
2015/12/27 DOTA
[01:58]最残酷竞争 2016国际邀请赛中国区预选赛积分循环赛回顾
2016/06/28 DOTA
Python编程入门之Hello World的三种实现方式
2015/11/13 Python
python密码错误三次锁定(实例讲解)
2017/11/14 Python
python 请求服务器的实现代码(http请求和https请求)
2018/05/25 Python
Python 调用PIL库失败的解决方法
2019/01/08 Python
带你认识Django
2019/01/15 Python
python删除列表元素的三种方法(remove,pop,del)
2019/07/22 Python
解决Pycharm 包已经下载,但是运行代码提示找不到模块的问题
2019/08/31 Python
python交互模式基础知识点学习
2020/06/18 Python
伦敦高级内衣品牌:Agent Provocateur(大内密探)
2016/08/23 全球购物
JSF界面控制层技术
2013/06/17 面试题
公司委托书范本
2014/04/04 职场文书
小学生三分钟演讲稿
2014/08/18 职场文书
党员干部民主生活会议批评与自我批评材料
2014/09/20 职场文书
2014副镇长民主生活会个人对照检查材料思想汇报
2014/09/30 职场文书
培训通知书模板
2015/04/17 职场文书
Mysql数据库表中为什么有索引却没有提高查询速度
2022/02/24 MySQL
Windows Server 修改远程桌面端口的实现
2022/06/25 Servers