背景需求
以下的色块眼里挑战需要人工筛选图片,非常繁琐。
【教学类-64-01】20240607色块眼力挑战(一)-0-255随机底色-CSDN博客文章浏览阅读446次,点赞12次,收藏5次。【教学类-64-01】20240607色块眼力挑战(一)-0-255随机底色https://blog.csdn.net/reasonsummer/article/details/139522756?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139522756%22%2C%22source%22%3A%22reasonsummer%22%7D
目前没有AI对话大师,无法用它写程序了。
因此在网络上搜索相关的代码,找到moyuweiqing的【Python小游戏】使用tkinter制作不同的找不同色块小游戏。
【Python小游戏】使用tkinter制作不同的找不同色块小游戏-CSDN博客文章浏览阅读377次。测一测你的色差情况_找不同色块https://blog.csdn.net/moyuweiqing/article/details/134263628
原作者代码
'''
作者:moyuweiqing
https://blog.csdn.net/moyuweiqing/article/details/134263628
'''from tkinter import *
from tkinter import messagebox
import numpy as np
import randomstart_grid_num = 2 # 开始的每行格子数
grid_num = start_grid_num # 每次调用时候的每行格子数
size = 800 # 画布的大小
margin = 2 # 格子边缘大小
max_grid_num = 25 # 最大的每行格子数start_color_gap = 40 # 开始的色块差
min_color_gap = 5 # 最小色块差
color_gap = start_color_gap # 每次调用时候的色块差
color_list = [] # 颜色列表
each_size = 0 # 每块格子的大小
A = np.full((grid_num, grid_num), 0) # 矩阵score = 0 # 计算分数def get_random_position(length):random_x = random.randint(0, length - 1)random_y = random.randint(0, length - 1)return random_x, random_ydef change_color_from_rgb(rgb):return "#%02x%02x%02x" % (rgb[0], rgb[1], rgb[2])def color_change():global color_list, color_gapcolor_list = []rgb_set1 = [random.randint(0, 255 - color_gap), random.randint(0, 255 - color_gap), random.randint(0, 255 - color_gap)]rgb_set2 = list((i + color_gap for i in rgb_set1))color_list.append(change_color_from_rgb(rgb_set1))color_list.append(change_color_from_rgb(rgb_set2))if color_gap > min_color_gap:color_gap -= 1def draw():global grid_num, each_size, AA = np.full((grid_num, grid_num), 0)each_size = size // grid_nums_x, s_y = get_random_position(grid_num)A[s_x][s_y] = -1canvas.delete('all')color_change()# 画方块for i in range(0, grid_num):for j in range(0, grid_num):if A[i][j] == 0:canvas.create_rectangle(i * each_size + margin, j * each_size + margin, (i + 1) * each_size - margin,(j + 1) * each_size - margin,fill=color_list[0])elif A[i][j] == -1:canvas.create_rectangle(i * each_size + margin, j * each_size + margin, (i + 1) * each_size - margin,(j + 1) * each_size - margin,fill=color_list[1])if grid_num < max_grid_num:grid_num += 1def if_right(x, y):global scorexx = x // each_sizeyy = y // each_sizeif A[xx][yy] == -1:score += 1return Trueelse:return Falsedef click(event):if if_right(event.x, event.y):draw()else:messagebox.showinfo('游戏结束!', '您的分数是:' + str(score))tk.destroy()if __name__ == '__main__':tk = Tk()tk.title('找不同色块')canvas = Canvas(tk, width=size, height=size, background='lightcyan')canvas.pack()draw()canvas.bind("<Button -1>", click)tk.mainloop()
存在问题:
我的电脑上始终无法安装tkinter,所以我不想做成TK界面小程序,只想要图片就行。
解决方式1——使用文心一言修改代码
但是源代码字母超过2000字符,没有办法完整输入代码,并写入需求
解决方式2——使用讯飞星火
讯飞星火是免费的,可以输入超过2000字的代码,并输入需求
成果展示:
通过对moyuweiqing的【Python小游戏】使用tkinter制作不同的找不同色块小游戏。代码的修改,实现了快速获得“2-25宫格”“色差从10-100”“题卡和答案”“导入PPT”四种需求
'''
目的:不同的找不同色块小游戏(从2宫格-25宫格,色差从10,20^100)有提卡和答案,从难度1-10(难度1就是色差100,难度10是色彩10)
图片导入PPT
原作者:moyuweiqing
https://blog.csdn.net/moyuweiqing/article/details/134263628
改编:星火讯飞,阿夏
时间:2024年6月9日
'''print('------1、图片和答案----------')
from PIL import Image, ImageDraw # 导入PIL库中的Image和ImageDraw模块
import numpy as np # 导入numpy库并简写为np
import random ,os # 导入random库path = r'C:\Users\jg2yXRZ\OneDrive\桌面\辨认色彩图'
new_path=path+r'\01样式' # 图片保存路径
os.makedirs(new_path,exist_ok=True)start_grid_num = 2 # 开始的每行格子数
grid_num = start_grid_num # 每次调用时候的每行格子数
size = 800 # 画布的大小
margin = 2 # 格子边缘大小
max_grid_num = 25 # 最大的每行格子数# 难度
nd=list(range(10,0,-1))
print(nd)start_color_gap = list(range(10,110, 10)) # 开始的色块差,10的色块与其他底色相同,看不清清除,,100数字越大,对比越清晰,看的清除
# start_color_gap = list(range(100, 0, -10)) # 为了从简单(100)到难(10),数字翻过来
print(start_color_gap)
min_color_gap = 5 # 最小色块差
color_list = [] # 颜色列表
each_size = 0 # 每块格子的大小
A = np.full((grid_num, grid_num), 0) # 矩阵
max_color_gap = 25 def get_random_position(length):random_x = random.randint(0, length - 1) # 随机生成x坐标random_y = random.randint(0, length - 1) # 随机生成y坐标return random_x, random_ydef change_color_from_rgb(rgb):return "#%02x%02x%02x" % (rgb[0], rgb[1], rgb[2]) # 将RGB颜色转换为十六进制颜色def color_change():global color_list, color_gapcolor_list = [] # 清空颜色列表rgb_set1 = [random.randint(0, 255 - color_gap), random.randint(0, 255 - color_gap), random.randint(0, 255 - color_gap)] # 生成第一个颜色的RGB值rgb_set2 = list((i + color_gap for i in rgb_set1)) # 生成第二个颜色的RGB值color_list.append(change_color_from_rgb(rgb_set1)) # 将第一个颜色添加到颜色列表中color_list.append(change_color_from_rgb(rgb_set2)) # 将第二个颜色添加到颜色列表中if color_gap > min_color_gap:color_gap -= 1 # 如果当前颜色间隔大于最小颜色间隔,则减小颜色间隔if __name__ == '__main__': for zz in range(len(start_color_gap)): start_grid_num = 2 # 开始的每行格子数grid_num = start_grid_num # 每次调用时候的每行格子数 for yy in range(start_grid_num, max_grid_num + 1): color_gap = start_color_gap[zz]print(color_gap)A = np.full((grid_num, grid_num), 0) # 重置二维数组each_size = size // grid_num # 计算每个网格的尺寸s_x, s_y = get_random_position(grid_num) # 获取随机位置A[s_x][s_y] = -1 # 将随机位置的值设为-1img1 = Image.new('RGB', (size, size), 'lightcyan') # 创建一个新的图片对象img2 = Image.new('RGB', (size, size), 'lightcyan') # 创建一个新的图片对象draw1 = ImageDraw.Draw(img1) # 创建一个绘图对象draw2 = ImageDraw.Draw(img2) # 创建一个绘图对象color_change() # 改变颜色for i in range(0, grid_num):for j in range(0, grid_num):if A[i][j] == 0:draw1.rectangle([i * each_size + margin, j * each_size + margin, (i + 1) * each_size - margin, (j + 1) * each_size - margin], fill=color_list[0]) # 绘制矩形并填充颜色draw2.rectangle([i * each_size + margin, j * each_size + margin, (i + 1) * each_size - margin, (j + 1) * each_size - margin], fill=color_list[0]) # 绘制矩形并填充颜色elif A[i][j] == -1:draw1.rectangle([i * each_size + margin, j * each_size + margin, (i + 1) * each_size - margin, (j + 1) * each_size - margin], fill=color_list[1]) # 绘制矩形并填充颜色draw2.rectangle([i * each_size + margin, j * each_size + margin, (i + 1) * each_size - margin, (j + 1) * each_size - margin], fill=color_list[1], outline='white', width=10) # 绘制矩形并填充颜色,添加黑色边框if grid_num <= max_grid_num:grid_num += 1 # 如果当前网格数量小于最大网格数量,则增加网格数量img1.save(new_path+fr'\难度{nd[zz]:02d}色彩间距{start_color_gap[zz]:03d}色块差{min_color_gap:03d}格子数{grid_num-1:02d}_01试题.png') # 保存图片img2.save(new_path+fr'\难度{nd[zz]:02d}色彩间距{start_color_gap[zz]:03d}色块差{min_color_gap:03d}格子数{grid_num-1:02d}_02答案.png') # 保存图片print('------2、导入PPT----------')import os
from pptx import Presentation
from pptx.util import Inches# 创建一个新的PPT文件
ppt = Presentation()# 设置PPT页面大小为800x800
ppt.slide_width = Inches(8)
ppt.slide_height = Inches(8)# 获取123文件夹中的所有图片文件
image_folder = new_path
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg', '.gif'))]# 将图片批量导入到PPT中
for image_file in image_files:slide = ppt.slides.add_slide(ppt.slide_layouts[6]) # 添加一个空白幻灯片img_path = os.path.join(image_folder, image_file)left, top, width, height = Inches(0), Inches(0), Inches(8), Inches(8)slide.shapes.add_picture(img_path, left, top, width, height)# 保存PPT文件
ppt.save(path+r'\色块小游戏.pptx')
结果展示
以下是难度1,最简单的,色差100,所以看起来对比明显,最小色差5,从2宫格-25宫格,每个宫格有试题和答案两张
以下是难度10,最难的,色差10,几乎看不出色彩差异,最小色差5,从2宫格-25宫格,每个宫格有试题和答案两张
把所有2宫格图像提取出来,可以看,色差越大(100),辨认难度越小,所以放在难度是1,色差越小(10)辨认难度越高(几乎与周边颜色相同),所以难度10。
图片放大看,
这样生成速度很快。一共480图,实际240图
保存为PPT。
视频展示
202406106色块小游戏