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 24 Python
从零学python系列之教你如何根据图片生成字符画
May 23 Python
Python随机生成数据后插入到PostgreSQL
Jul 28 Python
Python 网页解析HTMLParse的实例详解
Aug 10 Python
Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
Sep 14 Python
解决python3捕获cx_oracle抛出的异常错误问题
Oct 18 Python
对Python 语音识别框架详解
Dec 24 Python
简单了解python关系(比较)运算符
Jul 08 Python
python批量处理txt文件的实例代码
Jan 13 Python
基于python爬取有道翻译过程图解
Mar 31 Python
python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警
Jan 30 Python
Python 的演示平台支持 WSGI 接口的应用
Apr 20 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 计划任务 检测用户连接状态
2012/03/29 PHP
destoon实现调用热门关键字的方法
2014/07/15 PHP
PHP的Socket网络编程入门指引
2015/08/11 PHP
PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
2015/09/22 PHP
CodeIgniter框架常见用法工作总结
2017/03/16 PHP
ThinkPHP 3.2.3实现页面静态化功能的方法详解
2017/08/03 PHP
PHP中单例模式的使用场景与使用方法讲解
2019/03/18 PHP
ECMAScript 基础知识
2007/06/29 Javascript
由document.body和document.documentElement想到的
2009/04/13 Javascript
Javascript 面向对象 重载
2010/05/13 Javascript
jquery ui dialog ie8出现滚动条的解决方法
2010/12/06 Javascript
jquery乱码与contentType属性设置问题解决方案
2013/01/07 Javascript
eclipse如何忽略js文件报错(附图)
2013/10/30 Javascript
Node.js配合node-http-proxy解决本地开发ajax跨域问题
2016/08/31 Javascript
jquery 判断div show的状态实例
2016/12/03 Javascript
Vue中fragment.js使用方法详解
2017/03/09 Javascript
angular动态删除ng-repaeat添加的dom节点的方法
2017/07/20 Javascript
JS实现的简单分页功能示例
2018/08/23 Javascript
微信小程序之判断页面滚动方向的示例代码
2018/08/30 Javascript
Python交换变量
2008/09/06 Python
Python实现复杂对象转JSON的方法示例
2017/06/22 Python
放弃 Python 转向 Go语言有人给出了 9 大理由
2017/10/20 Python
sklearn+python:线性回归案例
2020/02/24 Python
简单了解Python write writelines区别
2020/02/27 Python
Python + selenium + crontab实现每日定时自动打卡功能
2020/03/31 Python
keras导入weights方式
2020/06/12 Python
Footshop乌克兰:运动鞋的最大选择
2019/12/01 全球购物
Internet主要有哪些网络群组成
2015/12/24 面试题
幼儿教师自我剖析材料
2014/09/29 职场文书
2014年团支书工作总结
2014/11/14 职场文书
就业意向协议书
2015/01/29 职场文书
大学生个人简历自我评价
2015/03/11 职场文书
警告通知
2015/04/25 职场文书
幽默口才训练经典句子(48句)
2019/08/19 职场文书
Mac M1安装mnmp (Mac+Nginx+MySQL+PHP) 开发环境
2021/03/29 PHP
JS前端canvas交互实现拖拽旋转及缩放示例
2022/08/05 Javascript