我正在使用PYgame创建一个具有多辆汽车的环境。蚀刻车有自己的雷达,可以检查障碍物之间的距离。我面临的问题是,如何获得像素的颜色。因此,如果一辆行驶中的汽车有紫色,并且它现在在x上,y Pixsle,我会得到紫色冷却器,而不是图片屏幕的表面颜色。
雷达的当前逻辑:
while not WIN.get_at((x, y)) == pygame.Color(48, 48, 48, 255) and length < 80:
length += 1
x = int(xcenter + math.sin(-math.radians(self.angle + radar_angle)) * length)
y = int(ycenter - math.cos(-math.radians(self.angle + radar_angle)) * length)
pygame.draw.line(WIN, (255, 255, 255, 0), (xcenter,ycenter), (x, y), 1)
pygame.draw.circle(WIN, (0, 255, 0, 0), (x, y), 2)
pygame.display.flip()这段代码很适合屏幕图像。

但对其他汽车来说没那么好

游戏中有处理这种情况的函数吗?
发布于 2022-05-08 06:14:53
你必须有选择。
street_gray = (128,128,128) #在这里使用灰色街道颜色,而WIN.get_at((x,y)) == street_gray和长度< 80:#.
pygame.mask.Mask,在障碍物位置为1,在其他地方为0。创建掩码时,需要为灰色道路设置set_colorkey()。可以使用 pygame.mask.from_surface创建掩码。pygame.mask.Mask.get_at返回0或1:win_surf = WIN.copy() win_surf.set_colorkey(street_gray) street_mask = pygame.mask.from_surface(win_surf)而street_mask.get_at((x,y)) == 0且长度< 80:#.
https://stackoverflow.com/questions/72158201
复制相似问题