Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改pyglet窗口的颜色_Python_Pyglet - Fatal编程技术网

Python 如何更改pyglet窗口的颜色

Python 如何更改pyglet窗口的颜色,python,pyglet,Python,Pyglet,我正在创建一个程序,它必须改变pyglet窗口中单个像素的颜色。我无法在文档中找到任何方法来执行此操作。有没有办法做到这一点?您可以将像素点嵌入背景“图像”中。您可以查看堆栈溢出问题。您可以将像素点嵌入背景“图像”。您可以查看堆栈溢出问题。您可以使用神奇的函数SolidColorImagePattern并修改所需的数据 R,G,B,A = 255,255,255,255 pyglet.image.SolidColorImagePattern((R,G,B,A).create_image(widt

我正在创建一个程序,它必须改变pyglet窗口中单个像素的颜色。我无法在文档中找到任何方法来执行此操作。有没有办法做到这一点?

您可以将像素点嵌入背景“图像”中。您可以查看堆栈溢出问题。

您可以将像素点嵌入背景“图像”。您可以查看堆栈溢出问题。

您可以使用神奇的函数
SolidColorImagePattern
并修改所需的数据

R,G,B,A = 255,255,255,255
pyglet.image.SolidColorImagePattern((R,G,B,A).create_image(width,height)
这是一个
.blit()
:able图像。它是白色的,可能不是你想要的。
因此,我们将进行更多的魔法,将所有像素替换为随机像素(蚂蚁战争):

出于教育目的,我将把它分解成更简单的部分

image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)
如前所述,创建纯白色图像。它的宽度和高度与窗口大小相匹配

然后我们获取图像数据:

data = image.get_image_data().get_data('RGB', width*3)
字节
字符串将包含
宽度*高度*
,这意味着
20x20
图像将
1200
字节大,因为RGB每像素占用3个字节

new_image = b''

for i in range(0, len(data), 3):
    pixel = bytes([randint(0,255)]) + bytes([randint(0,255)]) + bytes([randint(0,255)])
    new_image += pixel
整个块在所有像素上循环(len(数据)只是一件方便的事情,你也可以做
range(0,width*height*3,3)
,但是meh.
像素包含3个randint(255)字节的对象,组合成一个字符串,如下所示:

pixel = b'xffxffxff'
这也是为什么我们在
范围(0,len(data),3)
中选择
3
的原因。因为一个像素的宽度是3字节

一旦我们生成了所有像素(由于某种原因,字节对象
图像
无法修改..我可以发誓我以前修改过字节“字符串”..I'm wear tho,所以这可能是一个乌托邦式的梦想或其他东西。
总之,一旦所有这些甜美的图像构建完成,我们通过以下操作为图像对象提供新数据:

image.set_data('RGB', width*3, new_image)
就是这样。就像冬天零下45度阳光下的黄油一样容易

文件:


你也可以选择加入获取一个区域,只需修改一个区域。但是我将把修补工作留给你:)

你可以使用神奇的函数
SolidColorImagePattern
修改你需要的数据

R,G,B,A = 255,255,255,255
pyglet.image.SolidColorImagePattern((R,G,B,A).create_image(width,height)
这是一个
.blit()
:able图像。它是白色的,可能不是您想要的。
因此,我们将进行更多的魔法,将所有像素替换为随机像素(蚂蚁战争):

出于教育目的,我将把它分解成更简单的部分

image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)
如前所述,创建纯白色图像。其宽度和高度与窗口大小匹配

然后我们获取图像数据:

data = image.get_image_data().get_data('RGB', width*3)
字节
字符串将包含
宽度*高度*
,这意味着
20x20
图像将
1200
字节大,因为RGB每像素占用3个字节

new_image = b''

for i in range(0, len(data), 3):
    pixel = bytes([randint(0,255)]) + bytes([randint(0,255)]) + bytes([randint(0,255)])
    new_image += pixel
整个块在所有像素上循环(len(数据)只是一件方便的事情,你也可以做
range(0,width*height*3,3)
,但是meh.
像素包含3个randint(255)字节的对象,组合成一个字符串,如下所示:

pixel = b'xffxffxff'
这也是为什么我们在
范围(0,len(data),3)
中选择
3
的原因。因为一个像素的宽度是3字节

一旦我们生成了所有像素(由于某种原因,字节对象
图像
无法修改..我可以发誓我以前修改过字节“字符串”..I'm wear tho,所以这可能是一个乌托邦式的梦想或其他东西。
总之,一旦所有这些甜美的图像构建完成,我们通过以下操作为图像对象提供新数据:

image.set_data('RGB', width*3, new_image)
就是这样。就像冬天零下45度阳光下的黄油一样容易

文件:


你也可以选择加入获取一个区域,只需修改一个区域。但我将把修补工作留给你:)

为了好玩,我将添加另一个更符合你可能需要的答案。因为窗口本身将是你通过以下方式决定的“清晰”颜色缓冲区:

window = pyglet.window.Window(width=width, height=height)
pyglet.gl.glClearColor(0.5,0,0,1) # Note that these are values 0.0 - 1.0 and not (0-255).
因此,改变背景几乎是不可能的,因为这“没什么”。
但是,您可以通过
.draw()
函数在背景上绘制像素

import pyglet
from random import randint

width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)

@window.event
def on_draw():
    window.clear()
    for i in range(10):
        x = randint(0,width)
        y = randint(0,height)
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                ('v2i', (x, y)),
                ('c3B', (255, 255, 255))
            )

pyglet.app.run()
这将在背景上创建10个随机放置的白点。

要添加上面的任何内容,只需将您的
.blit()
.draw()
功能放在
pyglet.graphics.draw()
行之后。

为了增加趣味性,我将添加另一个更符合您可能需要的答案。因为窗口本身将是您通过以下方式决定的“清晰”颜色缓冲区:

window = pyglet.window.Window(width=width, height=height)
pyglet.gl.glClearColor(0.5,0,0,1) # Note that these are values 0.0 - 1.0 and not (0-255).
因此,改变背景几乎是不可能的,因为这“没什么”。
但是,您可以通过
.draw()
函数在背景上绘制像素

import pyglet
from random import randint

width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)

@window.event
def on_draw():
    window.clear()
    for i in range(10):
        x = randint(0,width)
        y = randint(0,height)
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                ('v2i', (x, y)),
                ('c3B', (255, 255, 255))
            )

pyglet.app.run()
这将在背景上创建10个随机放置的白点。

要添加上面的任何内容,只需将您的
.blit()
.draw()
功能放在
pyglet.graphics.draw()
行之后。

如果您指的是背景色,我可以提供帮助。我知道有一个选项,pyglet.gl.glClearColor函数

import pyglet
from random import randint

width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)

@window.event
def on_draw():
    window.clear()
    for i in range(10):
        x = randint(0,width)
        y = randint(0,height)
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                ('v2i', (x, y)),
                ('c3B', (255, 255, 255))
            )

pyglet.app.run()
例如:

import pyglet
from pyglet.gl import glClearColor

win = pyglet.window.Window(600, 600, caption = "test")

glClearColor(255, 255, 255, 1.0) # red, green, blue, and alpha(transparency)
def on_draw():
    win.clear()

这将创建一个白色背景的窗口(与默认的黑色相反)

如果你是指背景色,我可以提供帮助。我知道有一个选项,pyglet.gl.glClearColor函数

import pyglet
from random import randint

width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)

@window.event
def on_draw():
    window.clear()
    for i in range(10):
        x = randint(0,width)
        y = randint(0,height)
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                ('v2i', (x, y)),
                ('c3B', (255, 255, 255))
            )

pyglet.app.run()
例如:

import pyglet
from pyglet.gl import glClearColor

win = pyglet.window.Window(600, 600, caption = "test")

glClearColor(255, 255, 255, 1.0) # red, green, blue, and alpha(transparency)
def on_draw():
    win.clear()
这将创建一个背景为白色的窗口(与默认背景相反,为黑色)