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环境下搭建属于自己的pip源的教程
May 05 Python
Python numpy 常用函数总结
Dec 07 Python
Django框架的使用教程路由请求响应的方法
Jul 03 Python
在python中使用xlrd获取合并单元格的方法
Dec 26 Python
Python之列表实现栈的工作功能
Jan 28 Python
python多线程同步实例教程
Aug 11 Python
Python异常继承关系和自定义异常实现代码实例
Feb 20 Python
解决IDEA 的 plugins 搜不到任何的插件问题
May 04 Python
python如何实时获取tcpdump输出
Sep 16 Python
Python实现迪杰斯特拉算法过程解析
Sep 18 Python
Python实现简单的猜单词小游戏
Oct 28 Python
用python对oracle进行简单性能测试
Dec 05 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实现的功能是显示8条基色色带
2006/10/09 PHP
php 显示指定路径下的图片
2009/10/29 PHP
Can't create/write to file 'C:\WINDOWS\TEMP\...MYSQL报错解决方法
2011/06/30 PHP
PHP数组及条件,循环语句学习
2012/11/11 PHP
Referer原理与图片防盗链实现方法详解
2019/07/03 PHP
php高性能日志系统 seaslog 的安装与使用方法分析
2020/02/29 PHP
如何运行/调试你的PHP代码
2020/10/23 PHP
通过js动态操作table(新增,删除相关列信息)
2012/05/23 Javascript
纯javascript模仿微信打飞机小游戏
2015/08/20 Javascript
javascript每日必学之封装
2016/02/23 Javascript
JavaScript模拟push
2016/03/06 Javascript
JavaScript学习小结之使用canvas画“哆啦A梦”时钟
2016/07/24 Javascript
项目实践一图片上传之form表单还是base64前端图片压缩(前端图片压缩)
2016/07/28 Javascript
如何使用angularJs
2017/05/08 Javascript
jQuery Pagination分页插件_动力节点Java学院整理
2017/07/17 jQuery
浅谈KOA2 Restful方式路由初探
2019/03/14 Javascript
Vue中全局变量的定义和使用
2019/06/05 Javascript
怎么理解wx.navigateTo的events参数使用详情
2020/05/18 Javascript
vue键盘事件点击事件加native操作
2020/07/27 Javascript
JS绘图Flot应用图形绘制异常解决方案
2020/10/16 Javascript
[01:15:15]VG VS EG Supermajor小组赛B组胜者组第一轮 BO3第二场 6.2
2018/06/03 DOTA
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2016/06/07 Python
Python检测生僻字的实现方法
2016/10/23 Python
python实现斐波那契数列的方法示例
2017/01/12 Python
python利用微信公众号实现报警功能
2018/06/10 Python
浅谈pycharm使用及设置方法
2019/09/09 Python
appium+python adb常用命令分享
2020/03/06 Python
python操作yaml说明
2020/04/08 Python
Python 实现 T00ls 自动签到脚本代码(邮件+钉钉通知)
2020/07/06 Python
Hotels.com南非:酒店预订
2017/11/02 全球购物
高三自我鉴定怎么写
2013/10/19 职场文书
静心口服夜广告词
2014/03/20 职场文书
法语专业求职信
2014/07/20 职场文书
2015年村党支部工作总结
2015/04/30 职场文书
gojs实现蚂蚁线动画效果
2022/02/18 Javascript
python实现学生信息管理系统(面向对象)
2022/06/05 Python