当前位置: 首页 > news >正文

五子棋纯python手写,需要的拿去

 

import pygame,sys
from pygame import *
pygame.init()game = pygame.display.set_mode((600,600))
gameover = False
circlebox = []
# 棋盘坐标点存储
box = []
def xy():for x in range(0,800//40): for y in range(0,800//40): box.append((x*40,y*40))
xy()
defaultColor = 'white'
def gameWrite(word,size,color,x,y):# 创建一个帮我们写字的对象writer = pygame.font.SysFont('kaiti',size)# 将内容书写到给定的位置即可text = writer.render(word, True, color)# 将书写好的text传送到画布上game.blit(text,(x,y))
tmpColor = (255,255,255)
def success(color):global gameover,tmpColorgameover = TruetmpColor = color# 左斜对角验证
def L(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往上走x1 = x1-40y1 = y1-40if x1>=0 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakprint("左上斜对角值:",n)while True:#往下走x = x+40y = y+40if x<=800 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakprint("左下斜对角值:",n)if n>=4:success(color)def R(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往上走x1 = x1+40y1 = y1-40if x1<=800 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakwhile True:#往下走x = x-40y = y+40if x>=0 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakif n==4:success(color)
def S(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往上走y1 = y1-40if x1>=0 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakwhile True:#往下走y = y+40if x<=800 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakif n==4:success(color)
def H(x,y,color):n = 0# 如果斜对角能凑足4个则即为成功x1 = xy1 = ywhile True:#往左走x1 = x1-40if x1>=0 and y1>=0:f = Falsefor i in circlebox:if i[0] == x1 and i[1] == y1 and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakwhile True:#往右走x = x+40if x<=800 and y<=800:f = Falsefor i in circlebox:if i[0] == x and i[1] == y and i[2] == color:n = n+1f = Truebreakif not f:breakelse:breakif n==4:success(color)
# 画棋盘
def chess():start = 0for i in range(800//40): pygame.draw.polygon(game,(255,255,255),[(0,start), (800,start)],1)pygame.draw.polygon(game,(255,255,255),[(start,0), (start,800)],1)start = start+40def E():global defaultColorfor e in pygame.event.get():if e.type == QUIT:pygame.quit()sys.exit()if e.type == MOUSEBUTTONDOWN:x,y = e.posprint(e.button)if e.button == 1 and not gameover:# for xy in box:if abs(xy[0]-x)<20 and abs(xy[1]-y)<20 :f = Falsefor mark in circlebox:if xy[0]==mark[0] and xy[1]==mark[1]:f = Trueif not f:if defaultColor=='white':# 判断原来这个点是否已经存在circlebox.append((xy[0],xy[1],(255,255,255)))L(xy[0],xy[1],(255,255,255))R(xy[0],xy[1],(255,255,255))S(xy[0],xy[1],(255,255,255))H(xy[0],xy[1],(255,255,255))defaultColor = 'red'else:circlebox.append((xy[0],xy[1],(255,0,0)))L(xy[0],xy[1],(255,0,0))R(xy[0],xy[1],(255,0,0))S(xy[0],xy[1],(255,0,0))H(xy[0],xy[1],(255,0,0))defaultColor = 'white'def drawCircle():for i in circlebox:pygame.draw.circle(game,i[2],(i[0],i[1]),15)               
while True:chess()drawCircle()if gameover:if tmpColor == (255,255,255):gameWrite("恭喜白方获取胜利",60,(0,0,255),70,180)else:gameWrite("恭喜红方获取胜利",60,(0,0,255),70,180)E()pygame.display.update()

http://www.lryc.cn/news/383922.html

相关文章:

  • C# Winform按钮避免重复点击以及解决WinForm中设置Enabled=False为什么还会响应Click事件
  • 谷歌SEO是什么意思?
  • IPFoxy Tips:匿名海外代理IP的使用方法及注意事项
  • 【MySQL进阶之路 | 小结篇】MySQL键约束KEY与索引INDEX
  • 【中学教资科目二】02中学课程
  • Stable Diffusion 亲测这几个SDXL大模型,真的非常好用!
  • DLS策略洞察:如何应对AI数据中心网络交换机市场的爆发式增长?
  • 数据仓库架构设计
  • EasyExcel动态表头多sheet录入,单元格操作样式,自动修改单元格格式
  • Linux的设备模型
  • 初始化一个Android项目时,Android Studio会自动生成一些文件和目录结构,以帮助你快速上手开发
  • 社区团购小程序开发
  • 数据分析python基础实战分析
  • 英语笔记-专升本
  • 什么野指针(c++)
  • 【编译原理】绪论
  • 优化Docker部署:解决Java应用ExcelGenerateException并提速镜像构建
  • 你了解RabbitMQ、RocketMQ和Kafka吗?
  • python实现可视化大屏(django+pyechars)
  • Leetcode 力扣 125. 验证回文串 (抖音号:708231408)
  • Java程序递归及mybatis递归查询
  • 苹果电脑安装双系统步骤 教你苹果电脑如何装双系统
  • Axios-入门
  • Python22 Pandas库
  • 不同表格式下的小文件治理方式(开源RC file/ORC/Text非事务表、事务表、Holodesk表格式..)
  • 0.7 模拟电视标准 PAL 简介
  • vue项目中封装element分页组件
  • linux下docker安装与镜像容器管理
  • 【Unity】RPG2D龙城纷争(六)关卡编辑器之角色编辑
  • 【鸿蒙】鸿蒙的Stage和 FA 有什么区别