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计算三角函数之asin()方法的使用
May 15 Python
python简单获取本机计算机名和IP地址的方法
Jun 03 Python
在Linux系统上部署Apache+Python+Django+MySQL环境
Dec 24 Python
Python3.X 线程中信号量的使用方法示例
Jul 24 Python
Python实现解析Bit Torrent种子文件内容的方法
Aug 29 Python
浅谈flask中的before_request与after_request
Jan 20 Python
Python numpy 点数组去重的实例
Apr 18 Python
Python实现购物评论文本情感分析操作【基于中文文本挖掘库snownlp】
Aug 07 Python
Python实现的矩阵转置与矩阵相乘运算示例
Mar 26 Python
python3的数据类型及数据类型转换实例详解
Aug 20 Python
最新2019Pycharm安装教程 亲测
Feb 28 Python
Mysql数据库反向生成Django里面的models指令方式
May 18 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检测图片木马多进制编程实践
2013/04/11 PHP
ThinkPHP之用户注册登录留言完整实例
2014/07/22 PHP
PHP中使用php5-ffmpeg撷取视频图片实例
2015/01/07 PHP
PHP中捕获超时事件的方法实例
2015/02/12 PHP
php实现支付宝当面付(扫码支付)功能
2018/05/30 PHP
详解php协程知识点
2018/09/21 PHP
JavaScript实际应用:innerHTMl和确认提示的使用
2006/06/22 Javascript
JS获取浏览器版本及名称实现函数
2013/04/02 Javascript
2014最热门的JavaScript代码高亮插件推荐
2014/11/25 Javascript
javaScript的函数对象的声明详解
2015/02/06 Javascript
javascript 对象数组根据对象object key的值排序
2015/03/09 Javascript
JavaScript过滤字符串中的中文与空格方法汇总
2016/03/07 Javascript
Vue.js 2.0 移动端拍照压缩图片预览及上传实例
2017/04/27 Javascript
jsTree事件和交互以及插件plugins详解
2017/08/29 Javascript
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
解决bootstrap-select 动态加载数据不显示的问题
2018/08/10 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
浅谈一个webpack构建速度优化误区
2019/06/24 Javascript
Python中的匿名函数使用简介
2015/04/27 Python
Python的math模块中的常用数学函数整理
2016/02/04 Python
Python 专题二 条件语句和循环语句的基础知识
2017/03/19 Python
python引用(import)某个模块提示没找到对应模块的解决方法
2019/01/19 Python
html5 Canvas画图教程(3)—canvas出现1像素线条模糊不清的原因
2013/01/09 HTML / CSS
HTML5 canvas基本绘图之绘制五角星
2016/06/27 HTML / CSS
美国男女折扣服饰百货连锁店:Stein Mart
2017/05/02 全球购物
Omio中国:全欧洲低价大巴、火车和航班搜索和比价
2018/08/09 全球购物
美国快时尚彩妆品牌:Winky Lux(透明花瓣润唇膏)
2018/11/06 全球购物
地理教师岗位职责
2014/03/16 职场文书
小学生学雷锋演讲稿
2014/04/25 职场文书
2014年扶贫帮困工作总结
2014/12/09 职场文书
2015年安全教育月活动总结
2015/03/26 职场文书
停电通知范文
2015/04/16 职场文书
老乡聚会通知
2015/04/23 职场文书
单位更名证明
2015/06/18 职场文书
三严三实·严以修身心得体会
2016/01/15 职场文书
PHP控制循环操作的时间
2021/04/01 PHP