本文主要是介绍【教学类-63-01】20240606专注力测试6*6图形箭头,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景需求:
就是在6*6单元格里随机出现“圆形、方形、上箭头、下箭头、左箭头、右箭头。
用AI对话大师书写
我用了7种几何图形(几何形)+4类箭头(文字符号)
代码展示
'''
箭头专注力
AI对话大师 阿夏
2024年6月6日
'''from PIL import Image, ImageDraw, ImageFont
import random
import mathpath = r'C:\Users\jg2yXRZ\OneDrive\桌面\箭头专注力'for xx in range(10):# 定义画布大小和边距canvas_width = 800canvas_height = 800margin = 40# 计算单元格大小cell_width = (canvas_width - 2 * margin) // 6cell_height = (canvas_height - 2 * margin) // 6# 创建画布image = Image.new("RGB", (canvas_width, canvas_height), "white")draw = ImageDraw.Draw(image)# 定义字体和文字大小font = ImageFont.truetype(r"C:\Windows\Fonts\simsun.ttc", 60)# 绘制单元格for i in range(7):x = margin + i * cell_widthy = margin + i * cell_heightdraw.line([(x, margin), (x, canvas_height - margin)], fill="black")draw.line([(margin, y), (canvas_width - margin, y)], fill="black")# 绘制图形for i in range(6):for j in range(6):x = margin + i * cell_widthy = margin + j * cell_height# 随机选择一个图形shape = random.choice(["circle", "triangle", "square", "diamond", "rectangle","ellipse", "trapezoid", "up_arrow", "down_arrow", "left_arrow", "right_arrow"])if shape == "circle":# 绘制圆形circle_diameter = min(cell_width, cell_height) // 4 * 2center_x = x + cell_width // 2center_y = y + cell_height // 2draw.ellipse([(center_x - circle_diameter / 2, center_y - circle_diameter / 2),(center_x + circle_diameter / 2, center_y + circle_diameter / 2)],outline="black", width=5)elif shape == "triangle":# 绘制等边三角形side_length = min(cell_width, cell_height) // 2triangle_height = side_length * math.sqrt(3) / 2top_point = (x + cell_width // 2, y + (cell_height - triangle_height) // 2)left_point = (top_point[0] - side_length / 2, top_point[1] + triangle_height)right_point = (top_point[0] + side_length / 2, top_point[1] + triangle_height)draw.polygon([top_point, left_point, right_point, top_point], outline="black", width=5)elif shape == "square":# 绘制正方形square_side = min(cell_width, cell_height) // 2left = x + cell_width // 2 - square_side // 2top = y + cell_height // 2 - square_side // 2right = x + cell_width // 2 + square_side // 2bottom = y + cell_height // 2 + square_side // 2draw.rectangle([(left, top), (right, bottom)], outline="black", width=5)elif shape == "diamond":# 绘制菱形diamond_width = min(cell_width, cell_height) // 2diamond_height = diamond_widthcenter_x = x + cell_width // 2center_y = y + cell_height // 2draw.polygon([(center_x, center_y - diamond_height // 2),(center_x + diamond_width // 2, center_y),(center_x, center_y + diamond_height // 2),(center_x - diamond_width // 2, center_y)],outline="black", width=5)elif shape == "rectangle":# 绘制长宽比为2:1的长方形rectangle_width = min(cell_width, cell_height) // 2rectangle_height = rectangle_width // 2left = x + cell_width // 2 - rectangle_width // 2top = y + cell_height // 2 - rectangle_height // 2right = x + cell_width // 2 + rectangle_width // 2bottom = y + cell_height // 2 + rectangle_height // 2draw.rectangle([(left, top), (right, bottom)], outline="black", width=5)elif shape == "ellipse":# 绘制2:1椭圆形ellipse_width = min(cell_width, cell_height) // 2ellipse_height = ellipse_width // 2left = x + cell_width // 2 - ellipse_width // 2top = y + cell_height // 2 - ellipse_height // 2right = x + cell_width // 2 + ellipse_width // 2bottom = y + cell_height // 2 + ellipse_height // 2draw.ellipse([(left, top), (right, bottom)], outline="black", width=5)elif shape == "trapezoid":# 绘制下边长、上边短的梯形trapezoid_bottom_width = min(cell_width, cell_height) // 2trapezoid_top_width = trapezoid_bottom_width - 40trapezoid_height = trapezoid_bottom_width // 2center_x = x + cell_width // 2center_y = y + cell_height // 2draw.polygon([(center_x - trapezoid_bottom_width // 2, center_y + trapezoid_height // 2),(center_x + trapezoid_bottom_width // 2, center_y + trapezoid_height // 2),(center_x + trapezoid_top_width // 2, center_y - trapezoid_height // 2),(center_x - trapezoid_top_width // 2, center_y - trapezoid_height // 2)],outline="black", width=5)# elif shape == "sector":# # 绘制扇形# sector_radius = min(cell_width, cell_height) // 2# center_x = x + cell_width // 2# center_y = y + cell_height // 2# start_angle = random.randint(0, 360)# end_angle = start_angle + random.randint(30, 300)# draw.arc([(center_x - sector_radius, center_y - sector_radius),# (center_x + sector_radius, center_y + sector_radius)],# start=start_angle, end=end_angle, fill=None, width=5)if shape == "up_arrow":draw.text((x + cell_width // 2 - 30, y + cell_height // 2 - 30), "↑", fill="black", font=font)elif shape == "down_arrow":draw.text((x + cell_width // 2 - 30, y + cell_height // 2 - 30), "↓", fill="black", font=font)elif shape == "left_arrow":draw.text((x + cell_width // 2 - 30, y + cell_height // 2 - 30), "←", fill="black", font=font)elif shape == "right_arrow":draw.text((x + cell_width // 2 - 30, y + cell_height // 2 - 30), "→", fill="black", font=font)# 保存图片image.save(path + fr"\{xx:02d}.png")
还有一个平行四边形,始终无法符合我想要的效果。,就放弃了。
这篇关于【教学类-63-01】20240606专注力测试6*6图形箭头的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!