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 相关文章推荐
Perl中著名的Schwartzian转换问题解决实现
Jun 02 Python
Bottle框架中的装饰器类和描述符应用详解
Oct 28 Python
python抓取文件夹的所有文件
Feb 27 Python
画pytorch模型图,以及参数计算的方法
Aug 17 Python
Python Scrapy框架第一个入门程序示例
Feb 05 Python
通过实例简单了解python yield使用方法
Aug 06 Python
python selenium xpath定位操作
Sep 01 Python
python判断变量是否为列表的方法
Sep 17 Python
Python基础之pandas数据合并
Apr 27 Python
python文本处理的方案(结巴分词并去除符号)
May 26 Python
Python实战之OpenCV实现猫脸检测
Jun 26 Python
python 学习GCN图卷积神经网络
May 11 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
Session服务器配置指南与使用经验的深入解析
2013/06/17 PHP
Thinkphp实现自动验证和自动完成
2015/12/19 PHP
PHP的几个常用加密函数
2016/02/03 PHP
JavaScript中yield实用简洁实现方式
2010/06/12 Javascript
JS画5角星方法介绍
2013/09/17 Javascript
javascript中typeof的使用示例
2013/12/19 Javascript
javascript 闭包详解
2015/07/02 Javascript
基于Jquery实现表单验证
2020/07/20 Javascript
javascript实现base64 md5 sha1 密码加密
2015/09/09 Javascript
JavaScript+CSS实现仿Mootools竖排弹性动画菜单效果
2015/10/14 Javascript
浅谈JavaScript中的分支结构
2016/07/01 Javascript
老生常谈js-react组件生命周期
2017/05/02 Javascript
微信小程序 wx.request方法的异步封装实例详解
2017/05/18 Javascript
ReactNative实现Toast的示例
2017/12/31 Javascript
js中火星坐标、百度坐标、WGS84坐标转换实现方法示例
2020/03/02 Javascript
python和pyqt实现360的CLable控件
2014/02/21 Python
详解python单元测试框架unittest
2018/07/02 Python
python利用百度AI实现文字识别功能
2018/11/27 Python
Python3实现取图片中特定的像素替换指定的颜色示例
2019/01/24 Python
Python生成器的使用方法和示例代码
2019/03/04 Python
详解python-图像处理(映射变换)
2019/03/22 Python
Python HTML解析器BeautifulSoup用法实例详解【爬虫解析器】
2019/04/05 Python
Windows10下 python3.7 安装 facenet的教程
2019/09/10 Python
Python搭建HTTP服务过程图解
2019/12/14 Python
利用matplotlib为图片上添加触发事件进行交互
2020/04/23 Python
python 实现分组求和与分组累加求和代码
2020/05/18 Python
django教程如何自学
2020/07/31 Python
搭建pypi私有仓库实现过程详解
2020/11/25 Python
荷兰美妆护肤品海淘网站:Beautinow(中文)
2020/11/22 全球购物
我与祖国共奋进演讲稿
2014/09/13 职场文书
2014年作风建设心得体会
2014/10/22 职场文书
工作表扬信范文
2015/01/17 职场文书
2015年志愿者服务工作总结
2015/04/20 职场文书
2015初中团支部工作总结
2015/07/21 职场文书
JS实现数组去重的11种方法总结
2022/04/04 Javascript
python实现双链表
2022/05/25 Python