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中list循环语句用法实例
Nov 10 Python
python计算牛顿迭代多项式实例分析
May 07 Python
python写日志封装类实例
Jun 28 Python
Python数据结构与算法之链表定义与用法实例详解【单链表、循环链表】
Sep 28 Python
python中的随机函数random的用法示例
Jan 27 Python
利用python如何处理百万条数据(适用java新手)
Jun 06 Python
linux 下selenium chrome使用详解
Apr 02 Python
Python实现迪杰斯特拉算法过程解析
Sep 18 Python
GitHub上值得推荐的8个python 项目
Oct 30 Python
python3中确保枚举值代码分析
Dec 02 Python
python从ftp获取文件并下载到本地
Dec 05 Python
python实现学员管理系统(面向对象版)
Jun 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
linux实现php定时执行cron任务详解
2013/12/24 PHP
PHP+iframe图片上传实现即时刷新效果
2016/11/18 PHP
浅谈PHP接入(第三方登录)QQ登录 OAuth2.0 过程中遇到的坑
2017/10/13 PHP
PHP rmdir()函数的用法总结
2019/07/02 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
2020/03/24 PHP
Prototype中dom对象方法汇总
2008/09/17 Javascript
JS实现标签页效果(配合css)
2013/04/03 Javascript
将HTML格式的String转化为HTMLElement的实现方法
2014/08/07 Javascript
js全选实现和判断是否有复选框选中的方法
2015/02/17 Javascript
javascript学习笔记整理(概述、变量、数据类型简介)
2015/10/25 Javascript
AngularJS使用ngMessages进行表单验证
2015/12/27 Javascript
JS实现拖拽的方法分析
2016/12/20 Javascript
Vue组件选项props实例详解
2017/08/18 Javascript
使用vue实现简单键盘的示例(支持移动端和pc端)
2017/12/25 Javascript
详解从0开始搭建微信小程序(前后端)的全过程
2019/04/15 Javascript
vue和better-scroll实现列表左右联动效果详解
2019/04/29 Javascript
详解Vue template 如何支持多个根结点
2020/02/10 Javascript
从零开始在vue-cli4配置自适应vw布局的实现
2020/06/08 Javascript
python使用urllib2模块获取gravatar头像实例
2013/12/18 Python
python基于xml parse实现解析cdatasection数据
2014/09/30 Python
Python 给屏幕打印信息加上颜色的实现方法
2019/04/24 Python
python和mysql交互操作实例详解【基于pymysql库】
2019/06/04 Python
解决pycharm上的jupyter notebook端口被占用问题
2019/12/17 Python
PyCharm中如何直接使用Anaconda已安装的库
2020/05/28 Python
python 基于卡方值分箱算法的实现示例
2020/07/17 Python
html5 兼容IE6结构的实现代码
2012/05/14 HTML / CSS
英国大码女性时装零售商:Evans
2018/08/29 全球购物
银行办公室岗位职责
2014/03/10 职场文书
质量保证书范本
2014/04/29 职场文书
干部作风建设年活动剖析材料
2014/10/23 职场文书
给客户的感谢信
2015/01/21 职场文书
清明节扫墓活动总结
2015/02/09 职场文书
超市主管竞聘书
2015/09/15 职场文书
银行服务理念口号
2015/12/25 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书
Vue.js中v-for指令的用法介绍
2022/03/13 Vue.js