I've drawn a rectangle (4 line segments), but they're too thin. How can I change the thickness of each line?
The following example of mine gives me this:
if __name__ == '__main__':
app = App()
game_area = None
labels = [pyglet.text.Label("0"*8,
font_name = "Times New Roman",
font_size=18,
color = (255, 0, 0, 255),
x = app.width // 2,
y = app.height // 2 - n,
anchor_x = "center",
anchor_y = "center") for n in range(0, 100, 18)]
@app.event
def on_draw():
app.clear()
[label.draw() for label in labels]
pyglet.graphics.draw(4, pyglet.gl.GL_LINES,
("v2f", (0, 0, 0, app.height, app.width / 2, app.height, app.width / 2, 0))
)
pyglet.app.run()
You can change the width of lines by calling glLineWidth . This is accomplished through pyglet like so:
pyglet.gl.glLineWidth(desired_line_size)
If you require lines of varying thickness you'll have to call the function again with the new thickness before drawing each line. Otherwise, you can just set the thickness in your initialization and leave it be.
I've realized that actually I have to draw triangles in order to achieve "thickness":
def draw_game_area():
pyglet.graphics.draw(24, pyglet.gl.GL_TRIANGLES,
("v2f", (ORIGIN, ORIGIN, ORIGIN, app.height, LINE_SEGMENT_THICKNESS, ORIGIN,
ORIGIN, app.height, LINE_SEGMENT_THICKNESS, app.height, LINE_SEGMENT_THICKNESS, ORIGIN,
ORIGIN, ORIGIN, ORIGIN, LINE_SEGMENT_THICKNESS, app.width / 2, ORIGIN,
ORIGIN, LINE_SEGMENT_THICKNESS, app.width / 2, LINE_SEGMENT_THICKNESS, app.width / 2, ORIGIN,
app.width / 2 - LINE_SEGMENT_THICKNESS, app.height, app.width / 2, app.height, app.width / 2, ORIGIN,
app.width / 2 - LINE_SEGMENT_THICKNESS, app.height, app.width / 2, ORIGIN, app.width / 2 - LINE_SEGMENT_THICKNESS, ORIGIN,
ORIGIN, app.height, app.width / 2, app.height, app.width / 2, app.height - LINE_SEGMENT_THICKNESS,
ORIGIN, app.height, ORIGIN, app.height - LINE_SEGMENT_THICKNESS, app.width / 2, app.height - LINE_SEGMENT_THICKNESS))
)
Create points and draw a polygon between them to create the desired thickness. I created a function on desmos which takes two points and finds new points to create the desired thickness T
.
I created a function to do this with each point. This may not be the most efficient way, but it works.
This function expects pyglet.graphics.vertex_list().vertices
as input to vertices
and any number as input to thickness
.
def add_thickness(vertices,thickness):
from math import atan,sin,cos,pi
output = list()
i = 2
while i < len(vertices):
a = vertices[i-2:i]
b = vertices[i:i+2]
dy = b[1]-a[1]
dx = b[0]-a[0]
try:
a = -(dx/dy)
t = atan(a)
except:
t = pi/2
output.append((thickness/2)*cos(t)+b[0]);output.append((thickness/2)*sin(t)+b[1])
output.append(-(thickness/2)*cos(t)+b[0]);output.append(-(thickness/2)*sin(t)+b[1])
i+=2
return output
Note that this function does not create points for the first vertex in the vertices list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.