python实现五子棋游戏
作者:ArthurCaoMH 发布时间:2021-09-24 13:52:37
标签:python,五子棋
本文实例为大家分享了python实现五子棋游戏的具体代码,供大家参考,具体内容如下
话不多说,直接上代码:
全部工程文件,在GitHub:五子棋
效果预览:
#!/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()
来源:https://blog.csdn.net/ArthurCaoMH/article/details/88376553


猜你喜欢
- MySQL使用环境变量TMPDIR的值作为保存临时文件的目录的路径名。如果未设置TMPDIR,MySQL将使用系统的默认值,通常为/tmp、
- 本文实例讲述了php查询whois信息的方法。分享给大家供大家参考。具体如下:这里使用php通过查询whois信息的网站列表进行查询func
- 背景本文主要给大家介绍了关于在Python一段程序中使用多次事件循环的相关内容,我们在Python异步程序编写中经常要用到如下的结构impo
- 安装完 anaconda运行如下代码执行不了import numpy as npimport os,sys#获取当前文件夹,并根据文件名de
- 新手,虽然比较简单的东西,但是弄了我很久。很多不完善的地方,比如锁定用户,同一用户输入错三次密码就会锁定,但是如果在第二第三次换了用户再输入
- 遇到了这个问题,意思是你的 CPU 支持AVX AVX2 (可以加速CPU计算),但你安装的 TensorFlow 版本不支持解决:1. 如
- 本文实例为大家分享了python图形用户接口实例的具体代码,供大家参考,具体内容如下运用tkinter图形库,模拟聊天应用界面,实现信息发送
- mysql 查看表空间主要是查看创建的库和系统自带的库大小 - SELECT table_schema,
- 本文实例讲述了GO语言筛选法求100以内的素数。分享给大家供大家参考。具体实现方法如下:思路:找出一个非素数就把它挖掉,最后剩下就是素数。下
- 当初我觉得一个网站上注册和登录这两个功能很神奇,后来自己研究一下发现其实道理很简单,接下来看一下怎么实现的吧。。。。我在我的电脑上建了几个文
- 目前,SQL Server数据库有几个版本都在使用中,比如 7.0, 2000和2005,那么,在现实的工作和学习中,你很有可能会需要从以前
- 本文主要讲解SQLite中时间函数进行分析与总结并给出使用案例。本文给出的例子都是经过测试。SQLite时间/日期函数种类:1、dateti
- $str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $s
- reflect.StructField 和 reflect.Method如果变量是一个结构体,我们还可以通过结构体域类型对象 reflect
- php获取 checkbox复选框值的方法 <html xmlns="https://www.aspxhome.net/19
- 1、GIL简介GIL的全称为Global Interpreter Lock,全局解释器锁。1.1 GIL设计理念与限制python的代码执行
- TEMPLATESDjango 1.8的新特性一个列表,包含所有在Django中使用的模板引擎的设置。列表中的每一项都是一个字典,包含某个引
- 我自己的一个项目,需要同时对65536个文件进行多次写操作。如果先全部打开所有的文件,然后重复写,最后关闭所有的文件。那么第一次写操作全部完
- CSS浮动一直是个比较让人郁闷的问题,很多的布局问题都出在浮动上,特别是当浮动的列数很多时,但其实只要理解了两列结构的浮动,面对多列数的浮动
- 前言:文件处理是任何 Web 应用程序的重要组成部分。Python 有几个用于创建、读取、更新和删除文件的函数。1.文件处理在 Python