当前位置: 首页 > 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()  # 刷新显示
http://www.lryc.cn/news/407921.html

相关文章:

  • JS+H5在线文心AI聊天(第三方接口)
  • kafka源码阅读-ReplicaStateMachine(副本状态机)解析
  • 【MetaGPT系列】【MetaGPT完全实践宝典——如何定义单一行为多行为Agent】
  • Kolla-Ansible的确是不支持CentOS-Stream系列产品了
  • IDEA启动C:\Users\badboy\.jdks\corretto-17.0.7\bin\java.exe -Xmx700m报错
  • ctfshow298-300(java信息泄露,代码审计)
  • Java 基础 and 进阶面试知识点(超详细)
  • 【LabVIEW作业篇 - 5】:水仙花数、数组与for循环的连接
  • Kafka系列之如何提高消费者消费速度
  • mac安装Whisper
  • Linux:进程概述(什么是进程、进程控制块PCB、并发与并行、进程的状态、进程的相关命令)
  • Unity UGUI 之 坐标转换
  • 使用 uPlot 在 Vue 中创建交互式图表
  • SpringBoot 项目配置文件注释乱码的问题解决方案
  • TTS如何正确读AI缩写、金额和数字
  • python基础知识点(蓝桥杯python科目个人复习计划75)
  • 小技巧:如何在已知PDF密码情况下去掉PDF的密码保护
  • Java泛型的介绍和基本使用
  • 【C++】动态内存管理与模版
  • MongoDB - 组合聚合阶段:$group、$match、$limit、$sort、$skip、$project、$count
  • vue element-ui日期控件传参
  • MacOS安装SDKMan管理Java版本
  • 【网络安全的神秘世界】文件包含漏洞
  • 并发编程--volatile
  • 记录unraid docker更新的域名
  • SpringCloud+Vue3多对多,多表联查
  • 麒麟系统信创改造
  • 【Android】ListView和RecyclerView知识总结
  • 泛域名绑定到wordpress网站二级目录
  • 8、从0搭建企业门户网站——网站部署