gpt4 book ai didi

python - 在 Pygame 中创建多个交互式多边形按钮

转载 作者:行者123 更新时间:2023-12-04 07:31:28 29 4
gpt4 key购买 nike

我的目标是创建很多多边形按钮。例如,如果我输入一个数字 6,我想要有 6 个多边形。
例如:

import pygame
pygame.init()
SIZE_X = SIZE_Y = 600
screen = pygame.display.set_mode((SIZE_X, SIZE_Y), pygame.RESIZABLE )
red = (255, 0 , 0)

NB_POLYGONS = 6
for i in range(NB_POLYGONS):
polygons = [ ( x1 + i * 50,y1) , ( x2 + i * 50,y2), ( x3 + i * 50,y3)]#With xn and yn some random cords
pygame.draw.polygon(screen,red, polygons)
此示例工作并创建 6 个多边形。但是我怎样才能让他们使用react呢?例如,通过单击其中之一将其切换为蓝色。在我提供的代码中,我在多边形中放置了 3 个点,但我想知道是否可以使用任意多边形来执行此操作。

最佳答案

pygame.draw.polygon() 返回 pygame.Rect 限制改变的像素。测试鼠标是否在矩形中,并用不同的颜色再次绘制多边形:

for i in range(NB_POLYGONS):
points = [(x1 + i*50, y1), (x2 + i*50, y2), (x3 + i*50, y3)]
bounding_rect = pygame.draw.polygon(screen, 'red', points)
if bounding_rect.collidepoint(pygame.mouse.get_pos()):
pygame.draw.polygon(screen, 'blue', points)

关于python - 在 Pygame 中创建多个交互式多边形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67909718/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com