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

Python Pygame制作简单五子棋游戏

代码参考自:https://blog.csdn.net/weixin_43918046/article/details/119521845
新增功能:1任意棋盘大小;2.任意棋子连线

# 棋盘大小 [670, 670] 
# 棋盘行列 15*15
import pygame
from pygame.locals import QUIT, KEYDOWN
import numpy as np
import timeline_space=44
screen_margin=27
row=col=17
# screen_size=(670, 670) # 27*2+44*14
screen_row=(row-1)*line_space+screen_margin*2
screen_col=(col-1)*line_space+screen_margin*2
screen_size=(screen_row,screen_col )
screen_color = [238, 154, 73]
line_color=[0,0,0]
prompt_box_color=[0, 229, 238]
stage = np.zeros((row, col))
white_color = [255, 255, 255]  # 白棋颜色
black_color = [0, 0, 0]  # 黑棋颜色
victory_color=[238,48,167]
line=5 # 五颗棋子
# 设置鼠标时延
flag = False
tim = 0def find_pos(x, y):x=int((x-screen_margin)/line_space+0.5)y=int((y-screen_margin)/line_space+0.5)x=x if x<=(row-1) else row-1y=y if y<=(col-1) else col-1return x,y
def find_line_consecutive_ones_v2(matrix, line):victory_list_v2=[]a=find_line_consecutive_ones(matrix, line)b=find_line_consecutive_ones(matrix*(-1), line)victory_list_v2.extend(a)victory_list_v2.extend(b)return victory_list_v2
def my_range(start,end):if start<end:return range(start,end+1)else:return range(start,end-1,-1)
def find_line_consecutive_ones(matrix, line):victory_list=[]w, h = matrix.shape# 水平方向for row_index, row in enumerate(matrix):for col_index in range(len(row) - (line - 1)):if np.all(row[col_index:col_index + line] == 1):r,c=row_index,col_indexvictory_list.append([[r,c+i] for i in range(line) ])# 垂直方向for col_index in range(matrix.shape[1]):for row_index in range(matrix.shape[0] - (line - 1)):if np.all(matrix[row_index:row_index + line, col_index] == 1):r, c = row_index, col_indexvictory_list.append([[r+i, c] for i in range(line )])# 正对角线方向for diag in range(-(w - line), (h - line) + 1):diagonal = np.diag(matrix, k=diag)# print(diagonal)if len(diagonal) >= line:for start in range(len(diagonal) - (line - 1)):if np.all(diagonal[start:start + line] == 1):start_row = max(0, -diag) + startstart_col = max(0, diag) + startend_row = start_row + line-1end_col = start_col + line-1r=[i for i in my_range(start_row,end_row)]c=[i for i  in my_range(start_col,end_col)]rc=[i for i in zip(r,c)]victory_list.append(rc)# 反对角线方向for diag in range(-(w - line), (h - line) + 1):diagonal = np.diag(np.fliplr(matrix), k=diag)# print(diagonal)if len(diagonal) >= line:for start in range(len(diagonal) - (line - 1)):if np.all(diagonal[start:start + line] == 1):start_row = max(0, -diag) + startstart_col = min(h - 1, h - 1 - diag) - startend_row = start_row + line-1end_col = start_col - line+1r=[i for i in my_range(start_row,end_row)]c=[i for i  in my_range(start_col,end_col)]rc=[i for i in zip(r,c)]victory_list.append(rc)return victory_list# 初始化pygame
pygame.init()
# 获取对显示系统的访问,并创建一个窗口screen
# 窗口大小为670x670
screen = pygame.display.set_mode(screen_size)
while True:  # 不断训练刷新画布for event in pygame.event.get():  # 获取事件,如果鼠标点击右上角关闭按钮,关闭if event.type in (QUIT, KEYDOWN):sys.exit()screen.fill(screen_color)  # 清屏col_lines = [[[screen_margin+i*line_space, screen_margin], [screen_margin +i*line_space, screen_margin+(col-1)*line_space]] for i in range(col)]for i, xy_xy in enumerate(col_lines):line_thickness = 2if i == 0 or i == len(col_lines)-1:line_thickness = 4pygame.draw.line(screen, line_color,xy_xy[0], xy_xy[1], line_thickness)row_lines = [[[screen_margin, screen_margin+i*line_space], [screen_margin +(row-1)*line_space, screen_margin+i*line_space]] for i in range(row)]for i, xy_xy in enumerate(row_lines):line_thickness = 2if i == 0 or i == len(row_lines)-1:line_thickness = 4pygame.draw.line(screen, line_color,xy_xy[0], xy_xy[1], line_thickness)pygame.draw.circle(screen, line_color, [screen_row/2, screen_col/2], 8, 0)for x in range(stage.shape[0]):  # 外层循环遍历行for y in range(stage.shape[1]):  # 内层循环遍历列if stage[x][y] == 0:continueplay_color = black_color if stage[x][y] == -1 else white_colorpygame.draw.circle(screen, play_color, [screen_margin+x*line_space, screen_margin+y*line_space], 20, 0)res = find_line_consecutive_ones_v2(stage, line)time.sleep(0.1)if len(res) > 0:for ret in res:for x, y in ret:pygame.draw.rect(screen, victory_color, [screen_margin + int((x-0.5)*line_space), screen_margin+int((y-0.5)*line_space), line_space, line_space], 2, 1)pygame.display.update()  # 刷新显示continue  # 游戏结束,停止下面的操作# 获取鼠标坐标信息x, y = pygame.mouse.get_pos()x, y = find_pos(x, y)if stage[x][y] == 0:pygame.draw.rect(screen, prompt_box_color, [screen_margin + int((x-0.5)*line_space), screen_margin+int((y-0.5)*line_space), line_space, line_space], 2, 1)if pygame.mouse.get_pressed()[0] and tim == 0:flag = Trueif stage[x][y] == 0:  # 判断是否可以落子,再落子if np.sum(stage == 0) % 2 == 0:  # 黑子stage[x][y] = -1else:stage[x][y] = 1# 鼠标左键延时作用if flag:tim += 1if tim % 10 == 0:  # 延时200msflag = Falsetim = 0pygame.display.update()  # 刷新显示

相关文章:

Python Pygame制作简单五子棋游戏

代码参考自&#xff1a;https://blog.csdn.net/weixin_43918046/article/details/119521845 新增功能&#xff1a;1任意棋盘大小&#xff1b;2.任意棋子连线 # 棋盘大小 [670, 670] # 棋盘行列 15*15 import pygame from pygame.locals import QUIT, KEYDOWN import numpy as…...

JS+H5在线文心AI聊天(第三方接口)

源码在最后面 调用的不是文心官方接口 可以正常聊天 有打字动画 效果图 源代码 <!DOCTYPE html> <html lang"zh"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-s…...

kafka源码阅读-ReplicaStateMachine(副本状态机)解析

概述 Kafka源码包含多个模块&#xff0c;每个模块负责不同的功能。以下是一些核心模块及其功能的概述&#xff1a; 服务端源码 &#xff1a;实现Kafka Broker的核心功能&#xff0c;包括日志存储、控制器、协调器、元数据管理及状态机管理、延迟机制、消费者组管理、高并发网络…...

【MetaGPT系列】【MetaGPT完全实践宝典——如何定义单一行为多行为Agent】

目录 前言一、智能体1-1、Agent概述1-2、Agent与ChatGPT的区别 二、多智能体框架MetaGPT2-1、安装&配置2-2、使用已有的Agent&#xff08;ProductManager&#xff09;2-3、拥有单一行为的Agent&#xff08;SimpleCoder&#xff09;2-3-1、定义写代码行为2-3-2、角色定义2-3…...

Kolla-Ansible的确是不支持CentOS-Stream系列产品了

看着OpenStack最新的 C 版本出来一段时间了&#xff0c;想尝个鲜、用Kolla-Ansible进行容器化部署&#xff0c;结果嘛。。。 根据实验结果&#xff0c;自OpenStack Bobcat版本开始&#xff0c;Kolla-Ansible就适合在CentOS系列产品上部署了&#xff0c;通过对 Bobcat和Caracal…...

IDEA启动C:\Users\badboy\.jdks\corretto-17.0.7\bin\java.exe -Xmx700m报错

这篇文章写的就很详细了(IDEA启动C:\Users\badboy\.jdks\corretto-17.0.7\bin\java.exe -Xmx700m报错_error occurred during initialization of vm failed -CSDN博客)...

ctfshow298-300(java信息泄露,代码审计)

Web298 代码审计 这里看到getVipStatus方法&#xff0c;获得了获取flag的条件就是user等于admin&#xff0c;password等于ctfshow Poc: https://d036a90d-ac1c-4de1-9b0b-86f52d2586b9.challenge.ctf.show/ctfshow/login?usernameadmin&passwordctfshow Web299 打开页面…...

Java 基础 and 进阶面试知识点(超详细)

一个 Java 文件中是否可以存在多个类&#xff08;修饰类除外&#xff09;&#xff1f; 一个 Java 文件中是可以存在多个类的&#xff0c;但是一个 Java 文件中只能存在一个 public 所修饰的类&#xff0c;而且这个 Java 文件的文件名还必须和 public 所修饰类的类名保持一致&a…...

【LabVIEW作业篇 - 5】:水仙花数、数组与for循环的连接

文章目录 水仙花数数组与for循环的连接 水仙花数 水仙花数&#xff0c;是指一个3位数&#xff0c;它的每个位上的数字的3次幂之和等于它本身。如371 3^3 7^3 1^3&#xff0c;则371是一个水仙花数。 思路&#xff1a;水仙花数是一个三位数&#xff0c;通过使用for循环&#xf…...

Kafka系列之如何提高消费者消费速度

前言 在实际开发过程中&#xff0c;如果使用Kafka处理超大数据量(千万级、亿级)的场景&#xff0c;Kafka消费者的消费速度可能决定系统性能瓶颈。 实现方案 为了提高消费者的消费速度&#xff0c;我们可以采取以下措施&#xff1a; 将主题的分区数量增大&#xff0c;如 20&…...

mac安装Whisper

Whisper 官方git https://github.com/openai/whisper?tabreadme-ov-file 基本上参考官方的安装流程 pip3 install -U openai-whisper pip3 install githttps://github.com/openai/whisper.git pip3 install --upgrade --no-deps --force-reinstall githttps://github.com/…...

Linux:进程概述(什么是进程、进程控制块PCB、并发与并行、进程的状态、进程的相关命令)

进程概述 (1)What&#xff08;什么是进程&#xff09; 程序&#xff1a;磁盘上的可执行文件&#xff0c;它占用磁盘、是一个静态概念 进程&#xff1a;程序执行之后的状态&#xff0c;占用CPU和内存&#xff0c;是一个动态概念&#xff1b;每一个进程都有一个对应的进程控制块…...

Unity UGUI 之 坐标转换

本文仅作学习笔记与交流&#xff0c;不作任何商业用途 本文包括但不限于unity官方手册&#xff0c;唐老狮&#xff0c;麦扣教程知识&#xff0c;引用会标记&#xff0c;如有不足还请斧正 本文在发布时间选用unity 2022.3.8稳定版本&#xff0c;请注意分别 前置知识&#xff1a;…...

使用 uPlot 在 Vue 中创建交互式图表

本文由ScriptEcho平台提供技术支持 项目地址&#xff1a;传送门 使用 uPlot 在 Vue 中创建交互式图表 应用场景介绍 uPlot 是一个轻量级、高性能的图表库&#xff0c;适用于创建各种交互式图表。它具有丰富的功能&#xff0c;包括可自定义的轴、网格、刻度和交互性。本篇博…...

SpringBoot 项目配置文件注释乱码的问题解决方案

一、问题描述 在项目的配置文件中&#xff0c;我们写了一些注释&#xff0c;如下所示&#xff1a; 但是再次打开注释会变成乱码&#xff0c;如下所示&#xff1a; 那么如何解决呢&#xff1f; 二、解决方案 1. 点击” File→Setting" 2. 搜索“File Encodings”, 将框…...

TTS如何正确读AI缩写、金额和数字

案例&#xff1a;Tell me whats AI(a i), you need pay $186.30, your card Number is 1 2 3, your work Number is 5 6 7 8...

python基础知识点(蓝桥杯python科目个人复习计划75)

第一题&#xff1a;ip补充 题目描述&#xff1a; 小蓝的ip地址为192.168.*.21&#xff0c;其中*是一个数字&#xff0c;请问这个数字最大可能是多少&#xff1f; import os import sys# 请在此输入您的代码 print("255") 第二题&#xff1a;出现最多的字符 题目描…...

小技巧:如何在已知PDF密码情况下去掉PDF的密码保护

第一步&#xff0c;用Edge打开你的pdf&#xff0c;输入密码进去 第二步&#xff0c;点击打印 第三步&#xff0c;选择导出PDF&#xff0c;选择彩印 第四步&#xff0c;选择导出位置&#xff0c;导出成功后打开发现没有密码限制了&#xff01;...

Java泛型的介绍和基本使用

什么是泛型 ​ 泛型就是将类型参数化&#xff0c;比如定义了一个栈&#xff0c;你必须在定义之前声明这个栈中存放的数据的类型&#xff0c;是int也好是double或者其他的引用数据类型也好&#xff0c;定义好了之后这个栈就无法用来存放其他类型的数据。如果这时候我们想要使用这…...

【C++】动态内存管理与模版

目录 1、关键字new&#xff1a; 1、用法&#xff1a; 2、理解&#xff1a; 3、与malloc的相同与不同&#xff1a; 1、相同&#xff1a; 2、不同&#xff1a; 2、模版初阶&#xff1a; 1、函数模版&#xff1a; 1、概念&#xff1a; 2、关键字&#xff1a;template&…...

地震勘探——干扰波识别、井中地震时距曲线特点

目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波&#xff1a;可以用来解决所提出的地质任务的波&#xff1b;干扰波&#xff1a;所有妨碍辨认、追踪有效波的其他波。 地震勘探中&#xff0c;有效波和干扰波是相对的。例如&#xff0c;在反射波…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》

引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...

【论文笔记】若干矿井粉尘检测算法概述

总的来说&#xff0c;传统机器学习、传统机器学习与深度学习的结合、LSTM等算法所需要的数据集来源于矿井传感器测量的粉尘浓度&#xff0c;通过建立回归模型来预测未来矿井的粉尘浓度。传统机器学习算法性能易受数据中极端值的影响。YOLO等计算机视觉算法所需要的数据集来源于…...

C++ 基础特性深度解析

目录 引言 一、命名空间&#xff08;namespace&#xff09; C 中的命名空间​ 与 C 语言的对比​ 二、缺省参数​ C 中的缺省参数​ 与 C 语言的对比​ 三、引用&#xff08;reference&#xff09;​ C 中的引用​ 与 C 语言的对比​ 四、inline&#xff08;内联函数…...

LLM基础1_语言模型如何处理文本

基于GitHub项目&#xff1a;https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken&#xff1a;OpenAI开发的专业"分词器" torch&#xff1a;Facebook开发的强力计算引擎&#xff0c;相当于超级计算器 理解词嵌入&#xff1a;给词语画"…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

AspectJ 在 Android 中的完整使用指南

一、环境配置&#xff08;Gradle 7.0 适配&#xff09; 1. 项目级 build.gradle // 注意&#xff1a;沪江插件已停更&#xff0c;推荐官方兼容方案 buildscript {dependencies {classpath org.aspectj:aspectjtools:1.9.9.1 // AspectJ 工具} } 2. 模块级 build.gradle plu…...

MySQL 索引底层结构揭秘:B-Tree 与 B+Tree 的区别与应用

文章目录 一、背景知识&#xff1a;什么是 B-Tree 和 BTree&#xff1f; B-Tree&#xff08;平衡多路查找树&#xff09; BTree&#xff08;B-Tree 的变种&#xff09; 二、结构对比&#xff1a;一张图看懂 三、为什么 MySQL InnoDB 选择 BTree&#xff1f; 1. 范围查询更快 2…...

DeepSeek越强,Kimi越慌?

被DeepSeek吊打的Kimi&#xff0c;还有多少人在用&#xff1f; 去年&#xff0c;月之暗面创始人杨植麟别提有多风光了。90后清华学霸&#xff0c;国产大模型六小虎之一&#xff0c;手握十几亿美金的融资。旗下的AI助手Kimi烧钱如流水&#xff0c;单月光是投流就花费2个亿。 疯…...