Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyglet
- from math import *
- from pyglet.gl import *
- class Polygon(object):
- def __init__(self, batch: pyglet.graphics.Batch, x_coor, y_coor, radius, sides, r, g, b, a):
- """
- Tuple 1:
- Because vertex data can be supplied in several forms,
- a “format string” is required. In this case, the format
- string is "v2i", meaning the vertex position data has
- two components (2D) that are of int type.
- Tuple 2:
- Specifies color of the polygon.
- c = color
- 4 = with transparency (3 is without transparency, "alpha" in programming terms)
- """
- vertex_coor_list = []
- central_angle = 360 / sides
- i = 0
- while i < sides:
- xv = floor(cos(radians(central_angle * i))*radius + x_coor)
- vertex_coor_list.append(xv)
- yv = floor(sin(radians(central_angle * i))*radius + y_coor)
- vertex_coor_list.append(yv)
- i += 1
- print(vertex_coor_list)
- self.vertex_list = batch.add(sides, pyglet.gl.GL_POLYGON, None,
- ('v2i', vertex_coor_list),
- ('c4B', [r, g, b, a] * 4)
- )
- class Window(pyglet.window.Window):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.batch = pyglet.graphics.Batch()
- self.polygons = []
- def on_draw(self):
- self.clear()
- self.batch.draw()
- def draw_polygon(self, x_coor, y_coor, radius, sides, r=255, g=255, b=255, a=255):
- self.polygons.append(Polygon(self.batch, x_coor, y_coor, radius, sides, r, g, b, a))
- window = Window()
- x = window.width // 2
- y = window.width // 2
- window.draw_polygon(x, y, radius=25, sides=4)
- pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement