gpt4 book ai didi

python - pyglet 绘制黑线而不是绿色像素的问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:40:28 24 4
gpt4 key购买 nike

所以我是一名 Python 初学者程序员,我一直在使用 pyglet,如果您运行此代码,我发现了一个问题:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

image =pyglet.resource.image("logo.png")

intt = None

def pixel(x1,y1):
color = []
size = []
for x in range(x1):
for y in range(y1):
size.append(x)
size.append(y)

color.append(0)
color.append(255)
color.append(0)

vertex_list = pyglet.graphics.vertex_list(x1 * y1,('v2i', size ),
('c3B', color ))
return vertex_list

@window.event
def on_draw():
window.clear()
#vertex_list = pixel(600,500)
#vertex_list.draw(GL_POINTS)
color = []
size = []
for x in range(100):
for y in range(500):
size.append(x+504)
size.append(y)

color.append(0)
color.append(255)
color.append(0)

vertex_list = pyglet.graphics.vertex_list(100 * 500, ('v2i', size),
('c3B', color))

vertex_list.draw(GL_POINTS)

pyglet.app.run()

你会发现显示的区域有黑线。我尝试修复它,但它不起作用,而且因为 pyglet 不太出名,所以没有描述该问题的视频。我希望你知道如何解决它,因为你是我最后的希望。

最佳答案

如果您想绘制填充区域,请绘制矩形 GL_POLYGON 图元,而不是多个 GL_POINT 图元。这会快得多并确保该区域完全填满:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

def pixel(x1, y1, x2, y2):
size = [x1, y1, x2, y1, x2, y2, x1, y2]
color = []
for _ in range(4):
color += [0, 255, 0]
vertex_list = pyglet.graphics.vertex_list(4, ('v2i', size ), ('c3B', color ))
return vertex_list

@window.event
def on_draw():
window.clear()
vertex_list = pixel(500, 0, 600, 500)
vertex_list.draw(GL_POLYGON)

pyglet.app.run()
<小时/>

如果你想画一个pyglet.image ,然后生成纹理和 pyglet.graphics.Batch :

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

def quadTexture(x, y, w, h, texture):

vertex = [x, y, x+w, y, x+w, y+h, x, y+h]
tex = [0, 0, 1, 0, 1, 1, 0, 1]

batch = pyglet.graphics.Batch()
batch.add(4, GL_QUADS, texture, ('v2i', vertex ), ('t2f', tex ))
return batch

@window.event
def on_draw():
window.clear()
batch.draw()

file = "logo.png"

image = pyglet.image.load(file)
tex = pyglet.graphics.TextureGroup(image.get_texture())
batch = quadTexture(20, 20, image.width, image.height, tex)
pyglet.app.run()
<小时/>

使用 pyglet.sprite 更容易。例如:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

@window.event
def on_draw():
window.clear()
sprite.draw()

file = "logo.png"
image = pyglet.image.load(file)
sprite = pyglet.sprite.Sprite(image, x=20, y=20)

pyglet.app.run()

关于python - pyglet 绘制黑线而不是绿色像素的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60460779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com