def gl_rect_corners_outline(p1: Vec2D, p2: Vec2D, col: Union[Iterable, int]): if isinstance(col, Iterable): c = len(col) if c == 1: col = col * 12 elif c == 3: col = col * 4 elif c == 12: col = col else: raise ValueError( "Color data incorrect length. Expected: {}, got: {}".format( (1, 3, 12), c)) elif isinstance(col, (int, float)): col = (col, ) * 12 else: return tl = p1 br = p2 l, t = tl r, b = br gl_draw(4, GL_LINE_LOOP, ("v2f", (l, t, r, t, r, b, l, b)), ("c3B", col))
def draw(self): "draw the rectangle" for coord in self.coords: if isinstance(coord, int): coord = float(coord) elif not isinstance(coord, float): raise TypeError( f"coordinate '{coord}' type incorrect ({type(coord)})") # if there's no batch, draw the shape, if there is a batch, add the shape to the batch if not self.batch: gl_draw( 4, GL_QUADS, ("v2f", self.coords), ("c3B", self.color.get_colors(4, 'c3B')), ) # pprint(rectargs) else: self.batch.add( 4, GL_QUADS, None, ("v2f", self.coords), ("c3B", self.color.get_colors(4, 'c3B')), )
def gl_quad( p1: Union[Tuple[float, float], Vec2D], p2: Union[Tuple[float, float], Vec2D], p3: Union[Tuple[float, float], Vec2D], p4: Union[Tuple[float, float], Vec2D], col: Iterable, ): gl_draw(4, GL_QUADS, (("v2f", (*p1, *p2, *p3, *p4)), ("c3B", col)))
def draw(self, timestamp): dt = timestamp - self.timestamp shape = self.shape transform = self._get_transform(dt) n_points = len(shape) flatten_array = [] for vertice in shape: flatten_array.extend(vertice * transform) gl_draw(n_points, GL_LINE_LOOP, ('v2f', flatten_array))
def draw_player(): px, py = PLAYER_POS a = PLAYER_ANGLE mag = PLAYER_RENDER_DISTANCE gl_draw(4, GL_QUADS, ('v2f', [px - 5, py + 5, px + 5, py + 5, px + 5, py - 5, px - 5, py - 5])) gl_draw(2, GL_LINES, ('v2f', [px, py, px + (mag * cos(a)), py + (mag * sin(a))]))
def gl_line(p1: Vec2D, p2: Vec2D, col: Union[Iterable, int]): if isinstance(col, Iterable): c = len(col) if c == 1: col = col * 6 elif c == 3: col = col * 2 elif c == 6: col = col else: raise ValueError( "Color data incorrect length. Expected: {}, got: {}".format( (1, 3, 6), c)) elif isinstance(col, (int, float)): col = (col, ) * 6 else: return gl_draw(2, GL_LINES, ("v2f", [p1.x, p1.y, p2.x, p2.y]), ("c3B", col))
def main(delta): window.clear() draw_player() draw_walls() update_player() # Middle lines gl_draw(2, GL_LINES, ('v2f', (0, (window.height / 2) - 1, window.width, (window.height / 2) - 1))) gl_draw(2, GL_LINES, ('v2f', (window.width / 2, 0, window.width / 2, window.height / 2))) # render the scene render(window.width, window.height / 2) if keys[ESCAPE]: exit()
def gl_rect(p1: Vec2D, p2: Vec2D, p3: Vec2D, p4: Vec2D, col: Union[Iterable, int]): if isinstance(col, Iterable): c = len(col) if c == 1: col = col * 12 elif c == 3: col = col * 4 elif c == 12: col = col else: raise ValueError( "Color data incorrect length. Expected: {}, got: {}".format( (1, 3, 12), c)) elif isinstance(col, (int, float)): col = (col, ) * 12 else: return gl_draw(4, GL_QUADS, ("v2f", (*p1, *p2, *p3, *p4)), ("c3B", col))
def draw_collision(wall_no, angle, color=(255, 255, 255)): wall = WALLS[wall_no] a, b, c, d = wall wall = ((a, b), (c, d)) px, py = PLAYER_POS cx, cy = ( px + PLAYER_RENDER_DISTANCE * cos(radians(angle)), py + PLAYER_RENDER_DISTANCE * sin(radians(angle)), ) i = lines_intersect(((px, py), (cx, cy)), wall) if i: rx, ry = i gl_draw(4, GL_QUADS, ('v2f', ( rx - 5, ry + 5, rx + 5, ry + 5, rx + 5, ry - 5, rx - 5, ry - 5, )), ('c3B', color * 4))
def draw(self): gl_draw(4, GL_QUADS, ("v2i", self.coords), ("c3B", self.color))
def render(width, height): # get the angles to check degs = degrees(PLAYER_ANGLE) p_fov_minmax = (degs - PLAYER_FOV / 2, degs + PLAYER_FOV / 2) ## DEBUG ## # fov edges gl_draw(2, GL_LINES, ('v2f', (PLAYER_POS[0], PLAYER_POS[1], PLAYER_POS[0] + PLAYER_RENDER_DISTANCE * cos(radians(p_fov_minmax[0])), PLAYER_POS[1] + PLAYER_RENDER_DISTANCE * sin(radians(p_fov_minmax[0]))))) gl_draw(2, GL_LINES, ('v2f', (PLAYER_POS[0], PLAYER_POS[1], PLAYER_POS[0] + PLAYER_RENDER_DISTANCE * cos(radians(p_fov_minmax[1])), PLAYER_POS[1] + PLAYER_RENDER_DISTANCE * sin(radians(p_fov_minmax[1]))))) walls = [] cur, lim = p_fov_minmax while cur < lim: px, py = PLAYER_POS cx, cy = ( px + PLAYER_RENDER_DISTANCE * cos(radians(cur)), py + PLAYER_RENDER_DISTANCE * sin(radians(cur)), ) collisions = {} for wall_no, wall in enumerate(WALLS): a, b, c, d = wall wall = ((a, b), (c, d)) i = lines_intersect(((px, py), (cx, cy)), wall) if i: rx, ry = i collisions[wall_no] = dist(px, py, rx, ry) draw_collision(wall_no, cur, (255, 0, 0)) # print("Wall no: {} -> {}".format(wall_no, wall)) # else: # print("nop") if len(collisions): minv = 1000000 for k, v in collisions.items(): if v < minv: minv = v mink = k draw_collision(mink, cur, (0, 255, 0)) # if minv < 500: walls.append((minv, mink)) # else: # walls.append((0, None)) else: walls.append((0, None)) cur += RESOLUTION # The top of the render window RTOP = window.height / 2 pixel_width = window.width / len(walls) for index, wall in enumerate(reversed(walls)): wall, wall_no = wall wall_color = get_wall_color(wall_no) wall_color = wall_color if wall_color else (255, 255, 255) wallsmin, wallsmax = min([w[0] for w in walls]), max([w[0] for w in walls]) if not (wallsmax == 0 or wall > RTOP): # get the height of the wall # from min -> max => 0 -> RTOP - dist wh = remap(wall, RTOP, 0, 0, RTOP) # draw the wall # amount above and below the wall # the total height of the render window (RTOP) - the height of the wall all over 2 yoffset = (RTOP - wh) / 2 # distance the current segment is from the left side of the window # width of each rendered `pixel` * the current pixel's index xoffset = pixel_width * index gl_draw(4, GL_QUADS, ('v2f', ( xoffset, yoffset, xoffset, yoffset + wh, xoffset + pixel_width, yoffset + wh, xoffset + pixel_width, yoffset, )), ('c3B', wall_color * 4))
def draw_walls(): for wall_no, wall in enumerate(WALLS): gl_draw(2, GL_LINES, ('v2f', wall), ('c3B', get_wall_color(wall_no) * 2))