python实现五子棋游戏


Posted in Python onJune 18, 2019

本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下

话不多说,直接上代码:

全部工程文件,在GitHub:五子棋

效果预览:

python实现五子棋游戏

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
import pygame
from pygame.locals import *
from sys import exit
import numpy
background_image = 'qipan.png'
white_image = 'white.png'
black_image = 'black.png'
 
def WhoWin(x,y,darray):
 num1,num2,num3,num4 = 0,0,0,0
 #判断上下左右左上右上左下右下8个方向
 i = x-1
 while(i>=0):
 if darray[i][y] == 1:
  num1+=1
  i -= 1
 else:
  break
 i = x+1
 while i<19:
 if darray[i][y] == 1:
  num1+=1
  i += 1
 else:
  break
 j =y-1
 while (j >= 0):
 if darray[x][j] == 1:
  num2 += 1
  j -= 1
 else:
  break
 j = y + 1
 while j < 19:
 if darray[x][j] == 1:
  num2 += 1
  j += 1
 else:
  break
 
 i,j = x-1,y-1
 while(i>=0 and j>=0):
 if darray[i][j] == 1:
  num3 += 1
  i -= 1
  j -= 1
 else :
  break
 i, j = x + 1, y + 1
 while (i < 19 and j < 19):
 if darray[i][j] == 1:
  num3 += 1
  i += 1
  j += 1
 else:
  break
 
 i, j = x + 1, y - 1
 while (i >= 0 and j >= 0):
 if darray[i][j] == 1:
  num4 += 1
  i += 1
  j -= 1
 else:
  break
 i, j = x - 1, y + 1
 while (i < 19 and j < 19):
 if darray[i][j] == 1:
  num4 += 1
  i -= 1
  j += 1
 else:
  break
 
#五子胜
 if num1>=4 or num2>=4 or num3 >= 4 or num4 >= 4:
 return True
 else:
 return False
 
#初始化
pygame.init()
#屏幕、背景图、白黑子转换
screen = pygame.display.set_mode((584, 584), RESIZABLE, 32)
background = pygame.image.load(background_image).convert()
white = pygame.image.load(white_image).convert_alpha()
black = pygame.image.load(black_image).convert_alpha()
#标题画图字体
screen.blit(background, (0,0))
font = pygame.font.SysFont("arial", 40);
pygame.display.set_caption('五子棋')
 
#zeros()返回19行19列的数组
white_luodian = numpy.zeros((19,19))
black_luodian = numpy.zeros((19,19))
 
#设置棋盘的所有点的坐标
qipan_list = [(30+i*29-12,30+j*29-12) for i in range(19) for j in range(19)]
#默认黑子先手,转换下棋
transW_B = True
#游戏主循环
while True:
 
 for event in pygame.event.get():
 if event.type == QUIT:
  exit()
 if event.type == MOUSEBUTTONDOWN:
  x,y = pygame.mouse.get_pos()
  if 30 <= x <= 554 and 30 <= y <= 554 and ((x - 30) % 29 <= 12 or (x - 30) % 29 >= 17) and (
   (y - 30) % 29 <= 12 or (y - 30) % 29 >= 17):
  #四舍五入
  m = int(round((x-30)/29))
  n = int(round((y-30)/29))
  #结果分析
  if transW_B:
   transW_B = not transW_B
   screen.blit(black, qipan_list[19*m+n])
   black_luodian[n][m] = 1
   if WhoWin(n,m,black_luodian):
   screen.blit(font.render('Black chess player wins!', True, (0, 0, 0),(0,229,238)), (120, 280))
 
  else:
   transW_B = not transW_B
   screen.blit(white, qipan_list[19 * m + n])
   white_luodian[n][m] = 1
   if WhoWin(n,m,white_luodian):
   screen.blit(font.render('White chess player wins!', True, (255, 255, 255),(0,229,238)), (120, 280))
 
  qipan_list[19*m+n] = ''
 
 pygame.display.update()

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

Python 相关文章推荐
python计数排序和基数排序算法实例
Apr 25 Python
Python类方法__init__和__del__构造、析构过程分析
Mar 06 Python
详细分析python3的reduce函数
Dec 05 Python
Python hashlib模块用法实例分析
Jun 12 Python
Python向excel中写入数据的方法
May 05 Python
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
May 28 Python
深入了解Python在HDA中的应用
Sep 05 Python
Python3 使用map()批量的转换数据类型,如str转float的实现
Nov 29 Python
python有序查找算法 二分法实例解析
Feb 18 Python
浅谈keras中自定义二分类任务评价指标metrics的方法以及代码
Jun 11 Python
Python爬虫破解登陆哔哩哔哩的方法
Nov 17 Python
BeautifulSoup中find和find_all的使用详解
Dec 07 Python
解决python中使用PYQT时中文乱码问题
Jun 17 #Python
pyqt5 tablewidget 利用线程动态刷新数据的方法
Jun 17 #Python
PyQt4 treewidget 选择改变颜色,并设置可编辑的方法
Jun 17 #Python
python3.6根据m3u8下载mp4视频
Jun 17 #Python
python如何实现视频转代码视频
Jun 17 #Python
python批量爬取下载抖音视频
Jun 17 #Python
python批量下载抖音视频
Jun 17 #Python
You might like
php设计模式 Bridge (桥接模式)
2011/06/26 PHP
解密ThinkPHP3.1.2版本之模板继承
2014/06/19 PHP
thinkphp3.2中Lite文件替换框架入口文件或应用入口文件的方法
2015/05/21 PHP
基于thinkPHP3.2实现微信接入及查询token值的方法
2017/04/18 PHP
js onclick事件传参讲解
2013/11/06 Javascript
js控制当再次点击按钮时的间隔时间
2014/06/03 Javascript
Jquery修改页面标题title其它JS失效的解决方法
2014/10/31 Javascript
跟我学习javascript创建对象(类)的8种方法
2015/11/20 Javascript
Node.js服务器环境下使用Mock.js拦截AJAX请求的教程
2016/05/23 Javascript
模拟javascript中的sort排序(简单实例)
2016/08/17 Javascript
jQuery购物网页经典制作案例
2016/08/19 Javascript
轻松掌握JavaScript代理模式
2016/08/26 Javascript
学习vue.js表单控件绑定操作
2016/12/05 Javascript
canvas绘制的直线动画
2017/01/23 Javascript
js仿微信公众平台打标签功能
2017/04/08 Javascript
EasyUI Datebox 日期验证之开始日期小于结束时间
2017/05/19 Javascript
NodeJS使用Range请求实现下载功能的方法示例
2018/10/12 NodeJs
jQuery实现的简单歌词滚动功能示例
2019/01/07 jQuery
详解vue路由
2020/08/05 Javascript
Antd表格滚动 宽度自适应 不换行的实例
2020/10/27 Javascript
如何使用RoughViz可视化Vue.js中的草绘图表
2021/01/30 Vue.js
Python实现简单的HttpServer服务器示例
2017/09/25 Python
Python后台开发Django会话控制的实现
2019/04/15 Python
Django url,从一个页面调到另个页面的方法
2019/08/21 Python
python3.7通过thrift操作hbase的示例代码
2020/01/14 Python
解决安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示no Qt platform plugin的问题
2020/04/24 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
2020/05/19 Python
CSS3实现头像旋转效果
2017/03/13 HTML / CSS
瑞典领先的汽车零部件网上零售商:bildelaronline24.se
2017/01/12 全球购物
abstract是什么意思
2012/02/12 面试题
新闻工作者先进事迹
2014/05/26 职场文书
科技节口号
2014/06/19 职场文书
基于Redis的List实现特价商品列表功能
2021/08/30 Redis
mysql配置SSL证书登录的实现
2021/09/04 MySQL
解析python中的jsonpath 提取器
2022/01/18 Python
如何优化vue打包文件过大
2022/04/13 Vue.js