[英]getting the current color of the visible pixel in the pygame module
I am using PYgame to create an environment with multiple cars.我正在使用 PYgame 创建一个有多辆车的环境。 etch car has its own radar where it can check the distance between obstacles.
蚀刻车有自己的雷达,可以检查障碍物之间的距离。 the problem that I am having is, how to get the color of a pixel.
我遇到的问题是,如何获取像素的颜色。 so if a moving car has the color purple and it is currently over the [x,y] Pixsle I will get the purple cooler not the surface color of the picture screen.
因此,如果移动中的汽车颜色为紫色,并且它当前位于 [x,y] Pixsle 上方,我将获得紫色冷却器,而不是图片屏幕的表面颜色。
current logic for the radars:雷达的当前逻辑:
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()
this code works great with the screen image.此代码适用于屏幕图像。
but not so great with other cars但对其他车来说不是很好
do there is a function in pygame that handles this case? pygame 中是否有 function 处理这种情况?
You have to options.你必须选择。
Do the opposite.反其道而行之。 Test if the color is the gray color of the street:
测试颜色是否是街道的灰色:
street_gray = (128, 128, 128) # use your gray street color here while WIN.get_at((x, y)) == street_gray and length < 80: # [...]
Use a pygame.mask.Mask
that is 1 at positions of obstacles and 0 elsewhere.使用
pygame.mask.Mask
,在障碍物位置为 1,在其他地方为 0。 When you create the mask you need to set the set_colorkey()
for the gray road.创建蒙版时,您需要为灰色道路设置
set_colorkey()
。 The mask can be created with pygame.mask.from_surface
.可以使用
pygame.mask.from_surface
创建掩码。 pygame.mask.Mask.get_at
returns either 0 or 1: 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) while street_mask.get_at((x, y)) == 0 and length < 80: # [...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.