简体   繁体   English

在PyGame中获取1像素RGB颜色

[英]Get 1 pixel RGB color in pygame

How can i get one pixel RGB color in my 2D game? 如何在2D游戏中获得一种像素的RGB颜色? I need to compare it to red color(255,0,0), so i need it like this (R,G,B). 我需要将其与红色(255,0,0)进行比较,所以我需要像这样(R,G,B)。 I tried to use get_at() method like this: 我试图像这样使用get_at()方法:

if background.get_at(x,y) == (255,0,0):
    print("same")
else:
    print("not same")

But it didn't worked. 但这没有用。

code: 码:

import pygame
pygame.init()
background = pygame.display.set_mode([640,480])
background_colour = (3, 3, 251)
background.fill(background_colour)
pygame.display.flip()

print(background.get_at(2,2))
if background.get_at(background(2,2)) == background_colour:
   print("Same")
else:
   print("Not same:")

Error: 错误:

Traceback (most recent call last):
File "C:/Users/Kaspar/Desktop/proov13.py", line 8, in <module>
print(background.get_at(2,2))
TypeError: function takes exactly 1 argument (2 given)

Looking at Pygame docs: http://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_at You need to do: 查看Pygame文档: http ://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_at您需要执行以下操作:

background.get_at((2,2))

This is passing a single argument to the function, in this case the argument is the tuple (2,2) 这是将单个参数传递给函数,在这种情况下,参数为元组(2,2)

Also, if you want the colour in the form (255,0,0) then you need to call tuple(background.get_at((2,2))) (assuming you're using pygame version > 1.9.0). 另外,如果您想要颜色为(255,0,0)的形式,则需要调用tuple(background.get_at((2,2))) (假设您使用的是pygame版本> 1.9.0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM