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 14 Python
Python2.x中文乱码问题解决方法
Jun 02 Python
Python中的列表生成式与生成器学习教程
Mar 13 Python
Python提取Linux内核源代码的目录结构实现方法
Jun 24 Python
在python的类中动态添加属性与生成对象
Sep 17 Python
使用Python通过win32 COM实现Word文档的写入与保存方法
May 08 Python
Python动态参数/命名空间/函数嵌套/global和nonlocal
May 29 Python
Python3 tkinter 实现文件读取及保存功能
Sep 12 Python
Python异常模块traceback用法实例分析
Oct 22 Python
python matplotlib 绘图 和 dpi对应关系详解
Mar 14 Python
TensorFLow 数学运算的示例代码
Apr 21 Python
python学习笔记之多进程
Aug 06 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
Javascript常用运算符(Operators)-javascript基础教程
2007/12/14 Javascript
jQuery EasyUI 开源插件套装 完全替代ExtJS
2010/03/24 Javascript
JQuery最佳实践之精妙的自定义事件
2010/08/11 Javascript
JavaScript版DateAdd和DateDiff函数代码
2012/03/01 Javascript
Js 冒泡事件阻止实现代码
2013/01/27 Javascript
drag-and-drop实现图片浏览器预览
2015/08/06 Javascript
JQuery用户名校验的具体实现
2016/03/18 Javascript
Angularjs使用指令做表单校验的方法
2017/03/31 Javascript
Vue官方文档梳理之全局配置
2017/11/22 Javascript
Vuejs 2.0 子组件访问/调用父组件的方法(示例代码)
2018/02/08 Javascript
redux.js详解及基本使用
2019/05/24 Javascript
2分钟实现一个Vue实时直播系统的示例代码
2020/06/05 Javascript
JS pushlet XMLAdapter适配器用法案例解析
2020/10/16 Javascript
一起深入理解js中的事件对象
2021/02/06 Javascript
vue前端和Django后端如何查询一定时间段内的数据
2021/02/28 Vue.js
[01:35]2014DOTA2西雅图邀请赛 专访狐狸妈青春献给刀塔
2014/07/08 DOTA
[01:09:19]DOTA2-DPC中国联赛 正赛 VG vs Aster BO3 第二场 2月28日
2021/03/11 DOTA
python错误处理详解
2014/09/28 Python
python中的__slots__使用示例
2015/02/26 Python
Python学习小技巧之列表项的拼接
2017/05/20 Python
Python探索之SocketServer详解
2017/10/28 Python
django定期执行任务(实例讲解)
2017/11/03 Python
解决python中遇到字典里key值为None的情况,取不出来的问题
2018/10/17 Python
树莓派使用python-librtmp实现rtmp推流h264的方法
2019/07/22 Python
python tkinter实现屏保程序
2019/07/30 Python
Python pandas RFM模型应用实例详解
2019/11/20 Python
python实现拼图小游戏
2020/02/22 Python
python:解析requests返回的response(json格式)说明
2020/04/30 Python
英国花园、DIY、电器和家居用品商店:Robert Dyas
2019/03/18 全球购物
通信工程专业女生个人求职信
2013/09/21 职场文书
研究生自荐信
2013/10/09 职场文书
党的群众路线教育实践活动领导班子整改措施
2014/09/30 职场文书
酒店销售经理岗位职责
2015/04/02 职场文书
党支部创先争优公开承诺书
2015/04/30 职场文书
Python List remove()实例用法详解
2021/08/02 Python
nginx常用配置conf的示例代码详解
2022/03/21 Servers