Advertisement
Aikiro42

Polygon Draw

Nov 23rd, 2019
1,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pyglet
  2. from math import *
  3. from pyglet.gl import *
  4.  
  5.  
  6. class Polygon(object):
  7.     def __init__(self, batch: pyglet.graphics.Batch, x_coor, y_coor, radius, sides, r, g, b, a):
  8.         """
  9.        Tuple 1:
  10.        Because vertex data can be supplied in several forms,
  11.        a “format string” is required. In this case, the format
  12.        string is "v2i", meaning the vertex position data has
  13.        two components (2D) that are of int type.
  14.  
  15.        Tuple 2:
  16.        Specifies color of the polygon.
  17.        c = color
  18.        4 = with transparency (3 is without transparency, "alpha" in programming terms)
  19.        """
  20.         vertex_coor_list = []
  21.         central_angle = 360 / sides
  22.         i = 0
  23.         while i < sides:
  24.             xv = floor(cos(radians(central_angle * i))*radius + x_coor)
  25.             vertex_coor_list.append(xv)
  26.             yv = floor(sin(radians(central_angle * i))*radius + y_coor)
  27.             vertex_coor_list.append(yv)
  28.             i += 1
  29.         print(vertex_coor_list)
  30.  
  31.         self.vertex_list = batch.add(sides, pyglet.gl.GL_POLYGON, None,
  32.                                      ('v2i', vertex_coor_list),
  33.                                      ('c4B', [r, g, b, a] * 4)
  34.                                      )
  35.  
  36.  
  37. class Window(pyglet.window.Window):
  38.     def __init__(self, *args, **kwargs):
  39.         super().__init__(*args, **kwargs)
  40.         self.batch = pyglet.graphics.Batch()
  41.         self.polygons = []
  42.  
  43.     def on_draw(self):
  44.         self.clear()
  45.         self.batch.draw()
  46.  
  47.     def draw_polygon(self, x_coor, y_coor, radius, sides, r=255, g=255, b=255, a=255):
  48.         self.polygons.append(Polygon(self.batch, x_coor, y_coor, radius, sides, r, g, b, a))
  49.  
  50.  
  51. window = Window()
  52. x = window.width // 2
  53. y = window.width // 2
  54. window.draw_polygon(x, y, radius=25, sides=4)
  55.  
  56. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement