Python基于pygame实现单机版五子棋对战
作者:Jason Niu 发布时间:2021-02-26 05:53:54
标签:pygame,五子棋
python实现的五子棋,能够自动判断输赢,没有是实现电脑对战功能
源码下载:pygame五子棋
# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
initChessList = []
initRole = 1 # 代表白子下 2:代表当前是黑子下
resultFlag = 0
userFlag = True
class StornPoint():
def __init__(self, x, y, value = 0):
'''
:param x: 代表x轴坐标
:param y: 代表y轴坐标
:param value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子
'''
self.x = x
self.y = y
self.value = value
pass
def initChessSquare(x, y):
'''
初始化棋盘的坐标
:param x:
:param y:
:return:
'''
# 使用二维列表保存了棋盘是的坐标系,和每个落子点的数值
for i in range(15): # 每一行的交叉点坐标
rowList = []
for j in range(15): # 每一列的交叉点坐标
pointX = x + j*40
pointY = y + i*40
# value = 0
sp = StornPoint(pointX, pointY, 0)
rowList.append(sp)
pass
initChessList.append(rowList)
pass
# 处理事件
def eventHandler():
global userFlag
'''
监听各种事件
:return:
'''
for event in pygame.event.get():
global initRole
# 监听点积退出按钮事件
if event.type == QUIT:
pygame.quit()
sys.exit()
pass
# 监听鼠标点积事件
if event.type == MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos() #
print((x, y))
i = j = 0
for temp in initChessList:
for point in temp:
if x >= point.x - 15 and x <= point.x + 15 \
and y >= point.y - 15 and y <= point.y + 15:
# 当前区域没有棋子,并且是白子下
if point.value == 0 and initRole == 1 and userFlag:
point.value = 1
judgeResult(i, j, 1)
initRole = 2 # 切换棋子颜色
pass
elif point.value == 0 and initRole == 2 and userFlag:
point.value = 2
judgeResult(i, j, 2)
initRole = 1 # 切换棋子颜色
pass
break
pass
j += 1
pass
i += 1
j = 0
pass
pass
pass
# 判断输赢函数
def judgeResult(i, j, value):
global resultFlag
flag = False # 用于判断是否已经判决出输赢
for x in range(j - 4, j + 5): # 水平方向有没有出现5连
if x >= 0 and x + 4 < 15 :
if initChessList[i][x].value == value and \
initChessList[i][x + 1].value == value and \
initChessList[i][x + 2].value == value and \
initChessList[i][x + 3].value == value and \
initChessList[i][x + 4].value == value :
flag = True
break
pass
for x in range(i - 4, i + 5): # 垂直方向有没有出现5连
if x >= 0 and x + 4 < 15:
if initChessList[x][j].value == value and \
initChessList[x + 1][j].value == value and \
initChessList[x + 2][j].value == value and \
initChessList[x + 3][j].value == value and \
initChessList[x + 4][j].value == value:
flag = True
break
pass
# 判断东北方向的对角线是否出现了5连
for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):
if x >= 0 and x+4 < 15 and y + 4 >= 0 and y < 15:
if initChessList[y][x].value == value and \
initChessList[y - 1][x + 1].value == value and \
initChessList[y - 2][x + 2].value == value and \
initChessList[y - 3][x + 3].value == value and \
initChessList[y - 4][x + 4].value == value:
flag = True
break
pass
pass
pass
# 判断西北方向的对角是否出现了五连
for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):
if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:
if initChessList[y][x].value == value and \
initChessList[y + 1][x + 1].value == value and \
initChessList[y + 2][x + 2].value == value and \
initChessList[y + 3][x + 3].value == value and \
initChessList[y + 4][x + 4].value == value:
flag = True
break
pass
pass
pass
if flag:
resultFlag = value
pass
pass
# 加载素材
def main():
global resultFlag, initChessList
initChessSquare(27, 27) # 初始化棋牌
pygame.init() # 初始化游戏环境
# 创建游戏窗口
screen = pygame.display.set_mode((620,620), 0, 0) # 第一个参数是元组:窗口的长和宽
# 添加游戏标题
pygame.display.set_caption("五子棋小游戏")
# 图片的加载
background = pygame.image.load('images/bg.png')
blackStorn = pygame.image.load('images/storn_black.png')
whiteStorn = pygame.image.load('images/storn_white.png')
winStornW = pygame.image.load('images/white.png')
winStornB = pygame.image.load('images/black.png')
rect = blackStorn.get_rect()
while True:
screen.blit(background, (0, 0))
# 更新棋盘棋子
for temp in initChessList:
for point in temp:
if point.value == 1:
screen.blit(whiteStorn, (point.x - 18, point.y - 18))
pass
elif point.value == 2:
screen.blit(blackStorn, (point.x - 18, point.y - 18))
pass
pass
pass
# 如果已经判决出输赢
if resultFlag > 0:
initChessList = [] # 清空棋盘
initChessSquare(27, 27) # 重新初始化棋盘
if resultFlag == 1:
screen.blit(winStornW, (50,100))
else:
screen.blit(winStornB, (50,100))
pass
pygame.display.update()
if resultFlag >0:
time.sleep(3)
resultFlag = 0
pass
eventHandler()
pass
pass
if __name__ == "__main__":
main()
pass
来源:https://blog.csdn.net/nosprings/article/details/100137383


猜你喜欢
- 推荐go学习书籍,点击链接跳转京东官方商城购买。服务端经常需要返回一个列表,里面包含很多用户数据,常规做法当然是遍历然后读缓存。使用Go语言
- 一位资深的设计师曾经向我抱怨,说老板不仅让他做“设计”工作,还让他做“制作”工作,真是很烦。言下之意,“制作”还要一个资深设计师亲自上阵,未
- 一、什么是系统调用In computing, a system call is the programmatic way in which
- cookielib是一个自动处理cookies的模块,如果我们在使用爬虫等技术的时候需要保存cookie,那么cookielib会让你事半功
- 一、Python 的 IDE —— PyCharm1.1 集成开发环境(IDE)集成开发环境(IDE,Integrated Developm
- 我的读者知道我是一个喜欢痛骂Python3 unicode的人。这次也不例外。我将会告诉你用unicode有多痛苦和为什么我不能闭嘴。我花了
- 今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显。关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们
- 以前大家谈了很多有关打开数据库连接安全的问题,现在我再提出一种思路:使用activex dll来保护你的代码。(既可以不用为使用共享的加密软
- 创建 NumPy ndarray 对象NumPy 用于处理数组,NumPy 中的数组对象称为 ndarray。我们可以使用 array()
- //测试函数 function test(str){ alert(str); } // 方法一 window["test"
- 先来看一下该方法的说明create_image(position, **options) [#]Draws an image on the
- JavaScript 语法约定1、大小写的区分1). JavaScript的关键字,永远都是小写的;2). 内置对象,如Math和Date是
- Dreamweaver出现乱码,大致为两种情况:一是没有标明主页制作所用的文字,这种情况下很简单就可以
- 本文将教会我们如何使用PyQt5控件的工具提示功能。#!/usr/bin/python3# -*- coding: utf-8 -*-&qu
- 我们都知道在9i之前,要想获得建表和索引的语句是一件很麻烦的事。我们通常的做法都是通过export with rows=no来得到,但它的输
- 本文实例讲述了Python实现获取汉字偏旁部首的方法。分享给大家供大家参考,具体如下:功能介绍传入一个汉字,返回其偏旁部首字典分为本地字典与
- tkinter禁用(只读)下拉列表Comboboxtkinter将下拉列表框Combobox控件的状态设置为只读,也就是不可编辑状态:# 定
- 在使用ionic开发IOS系统微信的时候会有一个苦恼的问题,填写表单的时候键盘会挡住输入框,其实并不算什么大问题,只要用户输入一个字就可以立
- 文章中有不正确的或者说辞不清的地方,麻烦大家指出了~~~与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_runtim
- 前言在之前的面试过程中,问到执行计划,有很多童鞋不知道是什么?甚至将执行计划与执行时间认为是同一个概念。今天我们就一起来了解一下执行计划到底