我想创建一个类似于Geometry Dash的游戏。我有立方体的所有图像,但它们都是灰色和白色 - 这是为了让用户选择颜色。
我有两个变量colour_1
和colour_2
。 colour_1
应为灰色,colour_2
应为白色。如果我说变量是什么,我将如何修改图像以获得正确的颜色?
图像上的颜色并不完全相同,边缘混合,因此图像更平滑。这可能会导致并发症。
答案 0 :(得分:1)
这是基于它的工作代码
img_surface = pygame.image.load("image.gif") # Load image on a surface
img_array = pygame.surfarray.array3d(img_surface) # Convert it into an 3D array
colored_img = numpy.array(img_array) # Array thing
colored_img[:, :, 0] = 255 # <-- Red
colored_img[:, :, 1] = 128 # <-- Green
colored_img[:, :, 2] = 0 # <-- Blue
img_surface = pygame.surfarray.make_surface(colored_img) # Convert it back to a surface
screen.blit(img_surface, (0, 0)) # Draw it on the screen
这会改变每个像素的颜色值。如果将红色设置为255,则会为所有像素将红色加255。但如果将255设置为所有颜色,则图像将为白色。
请注意,您需要安装NumPy才能使用它,您可以这样做:
pip install numpy
您也可以尝试替换最后两行
pygame.surfarray.blit_array(screen, colored_img)
哪个应该更快,但它对我没有用,所以我将数组转换为表面然后在屏幕上显示它。
如果这不能回答您的问题,可能会:
答案 1 :(得分:0)
编写类并使用颜色代码变量实例化对象。
您可以编写一个方法,将结合状态特定数据绘制形状/图像。