简体   繁体   English

在 pyxel 复古游戏中获取像素的颜色

[英]Get the color of a pixel on a pyxel retro game

I am writing a simple shooting game using Pyxel, where an enemy shows up randomly on the screen.我正在使用 Pyxel 编写一个简单的射击游戏,其中一个敌人随机出现在屏幕上。 I already implemented a strategy to detect if a given shot hit the enemy or not, but a much simpler solution for me would be to check the color of a pixel on the screen under the mouse coordinates.我已经实施了一种策略来检测给定的射击是否击中了敌人,但对我来说一个更简单的解决方案是检查鼠标坐标下屏幕上像素的颜色。 I did not find an API function to do that.我没有找到一个 API 函数来做到这一点。

I checked the source code of the Renderer module ( https://github.com/kitao/pyxel/blob/master/pyxel/renderer.py ) and found a call to the OpenGL lib:我检查了 Renderer 模块的源代码( https://github.com/kitao/pyxel/blob/master/pyxel/renderer.py )并找到了对 OpenGL 库的调用:

capture_image = gl.glReadPixels(
            0, 0, self._width, self._height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)

I tried to do the same on my script like so:我试图在我的脚本上做同样的事情:

capture_image = gl.glReadPixels(
            pyxel.mouse_x, pyxel.mouse_y, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)

but no matter when I call the gl.glReadPixels(..) I always get the background color.但无论何时我调用 gl.glReadPixels(..) 我总是得到背景颜色。

Is there a way to get the color of a single pixel on the screen on a pyxel script?有没有办法在pyxel脚本上获取屏幕上单个像素的颜色?

Have you tried:你有没有尝试过:

capture_image = gl.glReadPixels(
        pyxel.mouse_x, 
        window_height - pyxel.mouse_y,
        1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)

获取 ( x , y ) 处像素的颜色:

pget(x, y)

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

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