def __init__(self, start_loc, size, speed, rot_speed, dir, depth, screen_size): self.loc = start_loc self.size = size self.radius = math.sqrt(3) * self.size / 3 # the radius of the circle the snowflake is inscribed in self._velocity = (0.0, 0.0) self._rot_speed = rot_speed self._dir = dir self.theta = 0 self.depth = depth self._velocity = [math.sin(self.theta) * self._dir, speed] self.wind = [0, 0] self._damping = 0.0001 # damping factor so wind dies down self.debug = False self._stroke_color = (0, 0, 0, 200) self._screen_size = screen_size blue = random.randrange(20) # how blue the snowflakes should be self._fill_color = (255 - blue, 255 - blue, 255, 240) points = self.generate_snowflake() self.surface = pygame.Surface((self.radius * 2, self.radius * 2), pygame.SRCALPHA, 32).convert_alpha() gfxdraw.aapolygon(self.surface, points, self._stroke_color) # border gfxdraw.filled_polygon(self.surface, points, self._fill_color) # fill gfxdraw.polygon(self.surface, points, self._stroke_color) # fill
def draw(self, pressed, shifted): if pressed is None: # Unused state (overwritten if binding exists) key_color = (50, 50, 50) txt_color = (80, 80, 80) elif pressed: key_color = (150, 150, 150) txt_color = (0, 0, 0) else: key_color = (50, 50, 50) txt_color = (250, 250, 250) points = [ [self.x, self.y], [self.x, self.y+self.height], [self.x+self.width, self.y+self.height], [self.x+self.width, self.y] ] points = [[int(round(x)) for x in xy] for xy in points] gfxdraw.filled_polygon(screen, points, key_color) gfxdraw.aapolygon(screen, points, key_color) font = pg.font.SysFont("monospace", 12) if shifted: name = self.shift else: name = self.name label = font.render(name, 1, txt_color) r = label.get_rect() center = [self.x+self.width/2, self.y+self.height/2] corner = [center[0]-r.width/2, center[1]-r.height/2] corner = [int(round(x)) for x in corner] screen.blit(label, corner)
def set_image(self, active): self.text_surface = self.font.render(self.caption, True, black if not active else gray) self.text = (self.text_surface, self.text_rect) self.image = pygame.Surface([self.w, self.h]) self.fill_with_outline(white, black if not active else gray) self.rect = self.image.get_rect() self.rect.center = (self.x, self.y) self.text[1].center = (self.rect.width // 2, self.rect.height // 2) if self.type == 'right_arrow': gfx.aapolygon( self.image, [[3 * self.w // 4, self.h // 2], [self.w // 4, self.h // 4], [self.w // 4, 3 * self.h // 4]], gray if active else black) gfx.filled_polygon( self.image, [[3 * self.w // 4, self.h // 2], [self.w // 4, self.h // 4], [self.w // 4, 3 * self.h // 4]], gray if active else black) elif self.type == 'left_arrow': gfx.aapolygon( self.image, [[self.w // 4, self.h // 2], [3 * self.w // 4, self.h // 4], [3 * self.w // 4, 3 * self.h // 4]], gray if active else black) gfx.filled_polygon( self.image, [[self.w // 4, self.h // 2], [3 * self.w // 4, self.h // 4], [3 * self.w // 4, 3 * self.h // 4]], gray if active else black) else: self.image.blit(self.text[0], self.text[1])
def _draw_colored_polygon(self, surface, poly, color, zoom, translation, angle, clip=True): import pygame from pygame import gfxdraw poly = [pygame.math.Vector2(c).rotate_rad(angle) for c in poly] poly = [(c[0] * zoom + translation[0], c[1] * zoom + translation[1]) for c in poly] # This checks if the polygon is out of bounds of the screen, and we skip drawing if so. # Instead of calculating exactly if the polygon and screen overlap, # we simply check if the polygon is in a larger bounding box whose dimension # is greater than the screen by MAX_SHAPE_DIM, which is the maximum # diagonal length of an environment object if not clip or any( (-MAX_SHAPE_DIM <= coord[0] <= WINDOW_W + MAX_SHAPE_DIM) and (-MAX_SHAPE_DIM <= coord[1] <= WINDOW_H + MAX_SHAPE_DIM) for coord in poly): gfxdraw.aapolygon(self.surf, poly, color) gfxdraw.filled_polygon(self.surf, poly, color)
def draw_colored_polygon(self, surface, poly, color, zoom, translation, angle): poly = [pygame.math.Vector2(c).rotate_rad(angle) for c in poly] poly = [(c[0] * zoom + translation[0], c[1] * zoom + translation[1]) for c in poly] gfxdraw.aapolygon(self.surf, poly, color) gfxdraw.filled_polygon(self.surf, poly, color)
def draw(self, ctx): if self.isActive(): gfxdraw.filled_polygon(ctx, self.activeAngleCoords, self.color) gfxdraw.aapolygon(ctx, self.activeAngleCoords, self.color) else: gfxdraw.filled_polygon(ctx, self.angleCoords, self.color) gfxdraw.aapolygon(ctx, self.angleCoords, self.color)
def update_light_surf(self, world): """ This creates the light and bloom surfaces Parameters ---------- world : world.World The current world Returns ------- None. """ self.light_surfs = np.array([]) self.bloom_surfs = np.array([]) # to determine the size of each light surface lowest_val = None highest_val = None for chunk in world.loaded_chunks: if not lowest_val or int(chunk) < lowest_val: lowest_val = int(chunk) if not highest_val or int(chunk) > highest_val: highest_val = int(chunk) for polygon in self.polygons: light_surf = pg.Surface(((highest_val + 2) * constants.BLOCK_SIZE * constants.CHUNK_SIZE, 800)).convert() light_surf.set_alpha(50) # render the polygon # TODO: fix no color with bloom if self.shadows: light_surf.fill((0, 0, 0)) if self.antialiasing: gfxdraw.aapolygon(light_surf, polygon, self.color) gfxdraw.textured_polygon(light_surf, polygon, self.color_surf, 0, 0) # set up a bloom surf for each main surface if self.bloom: bloom_surf = copy.copy(light_surf) start_size = bloom_surf.get_size() small_size = (start_size[0] // 10, start_size[1] // 10) # Shrink the surface then put it back to the normal size, which blurs it bloom_surf = pg.transform.smoothscale(bloom_surf, small_size) bloom_surf = pg.transform.smoothscale(bloom_surf, start_size) light_surf.blit(bloom_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT) self.light_surfs = np.append(self.light_surfs, light_surf)
def draw_polygon(polygon, color, filled=False, global_coords=True): if len(polygon) > 2: if global_coords: polygon = list(map(lambda x: world_to_win_pt(x, c.player.center), polygon)) if filled: gfxdraw.filled_polygon(screen, polygon, color) else: gfxdraw.polygon(screen, polygon, color) gfxdraw.aapolygon(screen, polygon, color)
def draw_arrow_head(x, y, angle, color, thickness): head_angle = math.pi / 6. width = 8 + thickness x -= width / 2. * math.cos(angle) y -= width / 2. * math.sin(angle) x1 = x + width * math.cos(angle + head_angle) y1 = y + width * math.sin(angle + head_angle) x2 = x + width * math.cos(angle - head_angle) y2 = y + width * math.sin(angle - head_angle) gfxdraw.aapolygon(surface, ((x, y), (x1, y1), (x2, y2)), color) gfxdraw.filled_polygon(surface, ((x, y), (x1, y1), (x2, y2)), color)
def polygon(surf, points, color): """Draw an antialiased filled polygon on a surface""" gfxdraw.aapolygon(surf, points, color) gfxdraw.filled_polygon(surf, points, color) x = min([x for (x, y) in points]) y = min([y for (x, y) in points]) xm = max([x for (x, y) in points]) ym = max([y for (x, y) in points]) return pygame.Rect(x, y, xm - x, ym - y)
def thick_line(surface, center, thickness, angle, color): x1, y1 = -_THICKLINE_WIDTH, thickness / 2 x2, y2 = _THICKLINE_WIDTH, thickness / 2 x3, y3 = -_THICKLINE_WIDTH, -thickness / 2 x4, y4 = _THICKLINE_WIDTH, -thickness / 2 x1, y1 = _transform(x1, y1, center, angle) x2, y2 = _transform(x2, y2, center, angle) x3, y3 = _transform(x3, y3, center, angle) x4, y4 = _transform(x4, y4, center, angle) gfxdraw.aapolygon(surface, [(x1, y1), (x2, y2), (x4, y4), (x3, y3)], color) pygame.draw.polygon(surface, color, [(x1, y1), (x2, y2), (x4, y4), (x3, y3)])
def render(self, screen, scale=None, anti_alias=False): if scale is None: scale = 1 if anti_alias: pointlist = [scale * self.flipy(x) for x in self.pointlist] gfxdraw.filled_polygon(screen, pointlist, pg.Color(self.color)) gfxdraw.aapolygon(screen, pointlist, pg.Color(self.color)) else: p1 = scale * self.flipy(self.start_pos) p2 = scale * self.flipy(self.end_pos) pg.draw.lines(screen, pg.Color(self.color), False, (p1, p2), int(scale * self.thickness))
def draw(self, surface, points, colors): color = colors[self.color_id] if color is None: return vertices = [points[v] for v in self.vertices] # pygame.draw # pygame.draw.polygon(surface, self.color, vertices, self.width) # pygame.gfxdraw aapolygon(surface, vertices, color) filled_polygon(surface, vertices, color)
def draw(self): #Draws the centre hexagon gfxdraw.filled_polygon(screen,self.hexagonPoints,(15,75,0)) gfxdraw.aapolygon(screen,self.hexagonPoints,(25,220,0)) #environment.explosionSurface = Surface((960,540),SRCALPHA) #re declare the explosion surface for clearing (do to per-pixel alpha) environment.explosionSurface.fill((0,0,0,0)) for e in environment.explosionList: e.draw() #draws explosions e.manage() #makes explosions cool for a in self.asteroidList: a.draw() screen.blit(self.timeTxt(),(2,2))
def draw(self, screen): h = screen.get_height() s = self.scale_sim_to_vis if self.shape == 'quad': verts = self.fixture.shape.vertices verts = [self.body.transform * vert for vert in verts] verts = [(s * x, h - s * y) for (x, y) in verts] gfxdraw.aapolygon(screen, verts, self.object_color) elif self.shape == 'circle': x = self.body.position[0] y = self.body.position[1] gfxdraw.aacircle(screen, int(s * x), int(h - s * y), int(s * self.HALF_H * self.scale_real_to_sim), self.object_color)
def render(self,screen): point1=screen.convertCoords(self.pos) #checks if planet is off screen rad=self.radius/screen.zoom if (point1[0]+rad)<-screen.margin or (point1[1]+rad)<-screen.margin or (point1[0]-rad)>screen.size[0]+screen.margin or (point1[1]-rad)>screen.size[1]+screen.margin: pass else: gfx.filled_circle(screen.display,point1[0],point1[1],int(self.radius/screen.zoom),self.color) gfx.aacircle(screen.display,point1[0],point1[1],int(self.radius/screen.zoom),self.color) for i,feature in enumerate(self.surfaceFeatAsset): for j,point2 in enumerate(feature): self.surfaceFeatDisplay[i][j][0]=point1[0]+point2[0]*rad self.surfaceFeatDisplay[i][j][1]=point1[1]+point2[1]*rad gfx.filled_polygon(screen.display,self.surfaceFeatDisplay[i],self.secondColor) gfx.aapolygon(screen.display,self.surfaceFeatDisplay[i],self.secondColor)
def _drawTiles(self, surface: pygame.Surface) -> None: """ Draws the tiles on the given surface Args: surface: The surface on which the tiles will be drawn """ for i in range(len(self._drawMatrix)): for j in range(len(self._drawMatrix[0])): points = self._drawMatrix[i][j] internal_color = self._colorMatrix[i][j][self._INTERNAL_COLOR] border_color = self._colorMatrix[i][j][self._BORDER_COLOR] if internal_color is not None: gfxdraw.filled_polygon(surface, points, internal_color) gfxdraw.aapolygon(surface, points, border_color)
def render(self, screen, scale=None, anti_alias=False): if scale is None: scale = 1 pointlist = [] for v in self.vertices: x, y = pm.Vec2d(v).rotated(self.shape.body.angle) + self.shape.body.position point = scale * self.flipy([x, y]) pointlist.append([int(point[0]), int(point[1])]) if anti_alias: gfxdraw.filled_polygon(screen, pointlist, pg.Color(self.color)) gfxdraw.aapolygon(screen, pointlist, pg.Color(self.color)) else: pg.draw.polygon(screen, pg.Color(self.color), pointlist)
def rotating_bones(surface, progress, mood, count=4): width, height = surface.get_size() w = width // 40 r = min(width - 9 * w, height - 9 * w) // 2 for i in range(count): alpha = progress * 2 * pi + i * pi * 8 / count pos_x = width // 2 + sin(alpha/4) * r * (count > 1) pos_y = height // 2 + cos(alpha/4) * r * (count > 1) if i % 2 == 0: color = mood.primary_color else: color = mood.secondary_color filled_circle(surface, int(pos_x + sin(alpha) * 3.5 * w), int(pos_y + cos(alpha) * 3.5 * w), w, color) aacircle(surface, int(pos_x + sin(alpha) * 3.5 * w), int(pos_y + cos(alpha) * 3.5 * w), w, color) filled_circle(surface, int(pos_x - sin(alpha) * 3.5 * w), int(pos_y - cos(alpha) * 3.5 * w), w, color) aacircle(surface, int(pos_x - sin(alpha) * 3.5 * w), int(pos_y - cos(alpha) * 3.5 * w), w, color) filled_polygon(surface, [ (int(pos_x - sin(alpha) * 3.5 * w + sin(alpha + pi / 2) * w / 2), int(pos_y - cos(alpha) * 3.5 * w + cos(alpha + pi / 2) * w / 2)), (int(pos_x - sin(alpha) * 3.5 * w - sin(alpha + pi / 2) * w / 2), int(pos_y - cos(alpha) * 3.5 * w - cos(alpha + pi / 2) * w / 2)), (int(pos_x + sin(alpha) * 3.5 * w - sin(alpha + pi / 2) * w / 2), int(pos_y + cos(alpha) * 3.5 * w - cos(alpha + pi / 2) * w / 2)), (int(pos_x + sin(alpha) * 3.5 * w + sin(alpha + pi / 2) * w / 2), int(pos_y + cos(alpha) * 3.5 * w + cos(alpha + pi / 2) * w / 2)) ], color) aapolygon(surface, [ (int(pos_x - sin(alpha) * 3.5 * w + sin(alpha + pi / 2) * w / 2), int(pos_y - cos(alpha) * 3.5 * w + cos(alpha + pi / 2) * w / 2)), (int(pos_x - sin(alpha) * 3.5 * w - sin(alpha + pi / 2) * w / 2), int(pos_y - cos(alpha) * 3.5 * w - cos(alpha + pi / 2) * w / 2)), (int(pos_x + sin(alpha) * 3.5 * w - sin(alpha + pi / 2) * w / 2), int(pos_y + cos(alpha) * 3.5 * w - cos(alpha + pi / 2) * w / 2)), (int(pos_x + sin(alpha) * 3.5 * w + sin(alpha + pi / 2) * w / 2), int(pos_y + cos(alpha) * 3.5 * w + cos(alpha + pi / 2) * w / 2)) ], color)
def draw_hex_grid_element(surface, colour, size, point): h = 1.7320508 * size w = 2 * size x = point[0] * w * 3 / 4 + w y = point[1] * h if point[0] % 2: y = y + h / 2 p1 = (x, y) p2 = (p1[0] + w / 2, p1[1]) p3 = (p2[0] + w / 4, p2[1] + h / 2) p4 = (p3[0] - w / 4, p3[1] + h / 2) p5 = (p4[0] - w / 2, p4[1]) p6 = (p5[0] - w / 4, p5[1] - h / 2) gfxdraw.filled_polygon(surface, (p1, p2, p3, p4, p5, p6), next()) gfxdraw.aapolygon(surface, (p1, p2, p3, p4, p5, p6), colour)
def polygon(self, pos_list, color=WHITE, fill=True, width=1): if not len(pos_list) > 0: return if isinstance(pos_list[0], Vector2): polygon = tuple(map(lambda vec: vec.pos, pos_list)) else: polygon = pos_list gfxdraw.aapolygon(self.screen, polygon, color) if fill: gfxdraw.filled_polygon(self.screen, polygon, color) elif width != 1: pygame.draw.polygon(self.screen, color, polygon, width)
def polygon(points, color, edge_color=None): """ PyGame polygon printing helper function Prints a polygon with an antialiased edge, handling conversion from game units to pixels Inputs: points: 2x2 numpy array of line end-points [[x1, y1], [x2, y2]] color: a 3 or 4 tuple, RGB(A), of integers in [0, 255] edge_color: a 3 or 4 tuple, RGB(A), of integers in [0, 255]; if ommitted, edge_color matches color """ if edge_color is None: edge_color = color # Invert Y coordinate so up is positive (PyGame uses down as positive) points[:, 1] = screen_height - points[:, 1] gfxdraw.filled_polygon(screen, (px*points).round().astype(int), color) gfxdraw.aapolygon(screen, (px*points).round().astype(int), edge_color)
def screen(self): if not self.first_opening: self.bg = self.scr.subsurface(self).copy() self.first_opening = True self.scr.fill(0x4d4c47,self) if self.item_index+self.offset>=0 and self.item_index+self.offset not in self.exc: self.scr.fill(0xe25817,(self.x,self.item_index*(self.item_h)+self.y-(self.top-self.itemsrect.top)%self.item_h,self.w,self.item_h)) b = self.itemsrect.y+self.margin//2 for e,i in enumerate(self.items): self.scr.blit(self.font.render(' %s '%(i.label if type(i)==Menu else i),1,(250,250,250)if e not in self.exc else(10,10,10)),(self.x,b)) if type(i)==Menu: r = pg.Rect(self.right-self.item_h,b-self.margin//2,self.item_h,self.item_h).inflate(-self.margin*3,-self.margin*3) filled_polygon(self.scr,(r.topleft,r.midright,r.bottomleft),(250,250,250)if e not in self.exc else(10,10,10)) aapolygon(self.scr,(r.topleft,r.midright,r.bottomleft),(250,250,250)if e not in self.exc else(10,10,10)) b+=self.item_h s = self.inflate(-2,-2) pg.draw.rect(self.scr,(100,100,100),self,1) pg.draw.lines(self.scr,(0,0,0),0,(s.topright,s.bottomright,s.bottomleft),1)
def show(self, walls, triangles=True, rays=False): self.surface.fill(consts.WHITE) # calculted the rays to the verticies of all the walls if self.autocp: for wall in walls: self._calc_rays_to_edges(wall, walls) # updating the intersection_pt attribute for the rays and drawing them if the option is activated for ray in self.rays: ray.check_intersections(walls) # sorts and filters the similar rays self._sort_rays() # print("Original Num of Rays is ", len(self.rays)) self.rays = self._filter_rays(self.rays) # print("New Num of Rays is ", len(self.rays)) self._sort_rays() if rays: for ray in self.rays: ray.draw(self.surface) if triangles: # drawing the triangles if len(self.rays): for i in range(len(self.rays) - 1): r1, r2 = self.rays[i], self.rays[i + 1] pt_list = (self.pos, r1.intersection_pt, r2.intersection_pt) if None in pt_list: self.surface.fill(consts.WHITE) continue gfx.aapolygon(self.surface, pt_list, consts.LIGHT_RED) gfx.aapolygon(self.surface, (self.pos, self.rays[0].intersection_pt, self.rays[-1].intersection_pt), consts.LIGHT_BLUE) pygame.display.update() if self.autocp: self.rays = [] pygame.draw.circle(self.surface, self.color, self.pos, self.rad)
def render_raw(self, res: Tuple[int], frame: int) -> pygame.Surface: surface = pygame.Surface(res, pygame.SRCALPHA) border = self.border(frame) color = self.color(frame) border_color = self.border_color(frame) offset = self.offset(frame) antialias = self.antialias(frame) verts = [(vx + offset[0], vy + offset[1]) for v in self.verts for vx, vy in v(frame)] if antialias: gfxdraw.aapolygon(surface, verts, color) gfxdraw.filled_polygon(surface, verts, color) else: pygame.draw.polygon(surface, color, verts) if border > 0: pygame.draw.polygon(surface, border_color, verts, border) return surface
def draw(self, update_screen=True): """ Uses standardized self.map_data from load function and displays it on the screen. Be sure to run self.load() in your init function before calling this draw function, or their will be no data to work with """ # Scale all of our data before displaying it, if it hasn't been done already if self.track_draw_info is None: self._pre_draw() # Draw our background, and all our track segments self.screen.fill((247, 247, 247)) for corner in self.track_draw_info["scaled_corners"]: gfxdraw.aapolygon(self.screen, corner, (204, 204, 204)) gfxdraw.filled_polygon(self.screen, corner, (204, 204, 204)) self.draw_window_trimmings() if update_screen: pygame.display.flip()
def draw_hex(screen, pos, color=BLACK, fill=True, info=None): screen_pos = to_screen_coord(pos) points = [] for i in range(6): angle_deg = 60 * i + 30 angle_rad = pi / 180 * angle_deg point = (screen_pos[0] + GRID_SIZE * cos(angle_rad), screen_pos[1] + GRID_SIZE * sin(angle_rad)) points.append(point) if fill: gfxdraw.filled_polygon(screen, points, color) gfxdraw.aapolygon(screen, points, color) if info is not None: # dist to (5, 5) dist_text = FONT.render(str(info), True, BLACK) dist_rect = dist_text.get_rect() dist_rect.center = screen_pos screen.blit(dist_text, dist_rect)
def draw(self, screen): self.dPoints = rtPoints(self.subdivPts) draw.polygon(screen, (0, 100, 0), self.dPoints, 0) gfxdraw.aapolygon(screen, self.dPoints, (0, 150, 0)) draw.circle(screen, (255, 255, 255), (int(self.center[0]), int(self.center[1])), 5) # for p in range(len(self.dPoints)): # pt = self.dPoints[p] # angle = self.normals[p]+cameraRotation # normal = pt[0]+cos(angle)*15,pt[1]+sin(angle)*15 # draw.line(screen,(255,0,0),self.dPoints[p],normal,1) ins = rtPoints(self.inside) draw.polygon(screen, (112, 71, 71), ins, 0) gfxdraw.aapolygon(screen, ins, (112, 71, 71)) for p in self.spikes: t = rtPoint(p) draw.lines(screen, (255, 0, 0), False, self.dPoints[p[0]:p[1]], 4)
def draw_arc(surface, x, y, r, th, start, stop, color): # from https://stackoverflow.com/a/57457571 stop = min(stop, tau) points_outer = [] points_inner = [] n = round(r * abs(stop - start) / 20) if n < 2: n = 2 for i in range(n): delta = i / (n - 1) phi0 = start + (stop - start) * delta x0 = round(x + r * cos(phi0)) y0 = round(y + r * sin(phi0)) points_outer.append([x0, y0]) phi1 = stop + (start - stop) * delta x1 = round(x + (r - th) * cos(phi1)) y1 = round(y + (r - th) * sin(phi1)) points_inner.append([x1, y1]) points = points_outer + points_inner aapolygon(surface, points, color) filled_polygon(surface, points, color)
def draw(self, surface): if self.status == 'dead': return c_inner, c_outer = self.colouring() for i in range(min(self.power, 3)): bsc = rn.uniform(0, 1) b = (self.back - self.position) * bsc s = (self.port_corner - self.back) * rn.uniform(-1, 1) * bsc p = self.back + b + s # gfx.filled_circle(surface, int(p[0]), int(p[1]), 3, c_inner) gfx.aacircle(surface, int(p[0]), int(p[1]), 3, c_outer) # if not c_inner is None: gfx.filled_polygon( surface, [self.position, self.port_corner, self.starboard_corner], c_inner) # print "drew inner at %s, %s, %s" % (self.position, self.port_corner, self.starboard_corner) # if not c_outer is None: gfx.aapolygon(surface, [self.position, self.port_corner, self.starboard_corner], c_outer)
def calc_aa_rect(self): # https://stackoverflow.com/questions/30578068/pygame-draw-anti-aliased-thick-line # P2----P3 # | | # P1----P4 # https://en.wikipedia.org/wiki/Rotation_matrix # Might be possible to make this faster # Precompute things ct = cos(self.theta) st = sin(self.theta) pre1 = self.height + self.r pre2 = self.width - self.width pre3 = self.center[0] + ct * self.width pre4 = self.center[1] + st * self.width pre5 = self.center[0] + ct * pre2 pre6 = self.center[1] + st * pre2 pre7 = st * self.r pre8 = ct * self.r pre9 = st * pre1 pre10 = ct * pre1 P1 = (pre3 - pre7, pre4 + pre8) P2 = (pre3 - pre9, pre4 + pre10) P3 = (pre5 - pre9, pre6 + pre10) P4 = (pre5 - pre7, pre6 + pre8) gfxdraw.aapolygon(self.surface, (P1, P2, P3, P4), self.colour) gfxdraw.filled_polygon(self.surface, (P1, P2, P3, P4), self.colour)
def aapolygon(self, points, color): """複数の頂点からなるアンチエイリアス処理のなされた多角形を描画します. Parameters ---------- points : array-like 頂点の座標を格納した2次元配列. 配列の2次元目の要素数は2で, 少なくとも3点以上の座標を設定する必要があります. color : tuple of int 描画に使用される色を指定します. """ return gfx.aapolygon(self.pg.screen, points, color)
def draw(self, surface): if self.status == 'dead': return c_inner, c_outer = self.colouring() for row in self.branches: for segment in row: sp = segment.p1 ep = segment.p2 d, l = (sp - ep).as_polar() p1 = sp + 1 * np.cos(np.radians(d + 90)) p2 = sp + 1 * np.cos(np.radians(d - 90)) p3 = ep + 1 * np.cos(np.radians(d + 90)) p4 = ep + 1 * np.cos(np.radians(d - 90)) gfx.aapolygon(surface, [p1, p2, p3, p4], c_outer) gfx.filled_polygon(surface, [p1, p2, p3, p4], c_outer) for row in self.branches: for segment in row: sp = segment.p1 ep = segment.p2 pdraw.aaline(surface, c_inner, sp, ep, 1)
def draw_cell(cell): font_size = 16 virus_sizes = {100:1, 106:2, 113:3, 119:4, 125:5, 131:6, 136:7} cx,cy = world_to_win_pt(cell.pos,c.player.center) try: mov_ang = cell.movement_angle p2 = cell.pos + Vec( math.cos(mov_ang + mechanics.eject_delta*math.pi/180), math.sin(mov_ang + mechanics.eject_delta*math.pi/180) ) * (cell.size+700) p3 = cell.pos + Vec( math.cos(mov_ang - mechanics.eject_delta*math.pi/180), math.sin(mov_ang - mechanics.eject_delta*math.pi/180) ) * (cell.size+700) cx2,cy2 = world_to_win_pt(p2,c.player.center) cx3,cy3 = world_to_win_pt(p3,c.player.center) except (AttributeError, TypeError): cx2,cy2=cx,cy cx3,cy3=cx,cy radius = world_to_win_length(cell.size) if cell.is_virus: color = (0,255,0) color2 = (100,255,0) polygon = generate_virus(int(cell.size*0.3), 10*zoom, radius, (cx, cy)) polygon2 = generate_virus(int(cell.size*0.3), 10*zoom, radius-10, (cx, cy)) gfxdraw.filled_polygon(screen, polygon, color2) gfxdraw.polygon(screen, polygon, (0,0,0)) gfxdraw.aapolygon(screen, polygon, (0,0,0)) gfxdraw.filled_polygon(screen, polygon2, color) gfxdraw.aapolygon(screen, polygon2, color) draw_text((cx, cy), "%s / 7" % virus_sizes.get(cell.size, "?"), (64,0,0), font_size*2, False, True) draw_text((cx, cy + radius + 10), str(cell.cid), (0,0,0), font_size, False, True) else: color=(int(cell.color[0]*255), int(cell.color[1]*255), int(cell.color[2]*255)) if not (cell.is_ejected_mass or cell.is_food): gfxdraw.aapolygon(screen, [(cx,cy),(cx2,cy2),(cx3,cy3),(cx,cy)] ,(255,127,127)) gfxdraw.filled_circle(screen, cx, cy, radius, color) gfxdraw.aacircle(screen, cx, cy, radius, (0,0,0)) gfxdraw.aacircle(screen, cx, cy, int(radius/2), (255,255,255)) gfxdraw.circle(screen, cx, cy, int(radius/2), (255,255,255)) draw_text((cx, cy + radius + 10), cell.name, (0, 0, 0), font_size, False, True) draw_text((cx, cy + radius + 10 + font_size), str(cell.cid), (0,0,0), font_size, False, True) # surface = draw_text(cell.name, (0, 0, 0), font_size) # screen.blit(surface, (cx - (surface.get_width()/2), cy + radius + 5)) draw_text((cx, cy), str(int(cell.mass))+"/"+str(int(cell.size)), (255, 255, 255), font_size, False, True) # surface = draw_text(str(int(cell.mass)), (255, 255, 255), font_size) # screen.blit(surface, (cx - (surface.get_width()/2), cy - (surface.get_height()/2))) else: gfxdraw.aacircle(screen, cx, cy, radius, color) gfxdraw.filled_circle(screen, cx, cy, radius, color)
def __init__(self, surface): """ Инициализация. surface - поверхность на которой будем рисовать """ self.surface = surface self.surface.set_colorkey( C_BLACK, pygame.RLEACCEL ) self.rect = self.surface.get_rect() self.area = hex.Hex(self.hex_size, self.hex_dist) # Закрашенный гекс подсветки. self.solid = pygame.Surface( (self.area.S.size[0]+1, self.area.S.size[1]+1) ) self.solid.set_colorkey( C_BLACK, pygame.RLEACCEL ) self.solid.set_alpha(200, pygame.RLEACCEL) self.solid_rect = self.solid.get_rect() self.solid_rect.center = (-100,-100) points = self.area.polygon( (0,0) ) gfx.filled_polygon(self.solid, points, C_FILL) gfx.aapolygon(self.solid, points, C_WHITE) # Выбранный гекс. self.select = self.solid.copy() self.select_rect = self.select.get_rect() points = self.area.polygon( (0,0) ) gfx.filled_polygon(self.select, points, C_SELECT) gfx.aapolygon(self.select, points, C_WHITE) # отрисовка задника line = pygame.draw.line font = pygame.font.Font(None, 20) for i in range(self.hex_col): for j in range(self.hex_row): x, y = self.area.center( (i,j) ) points = self.area.polygon( (i,j) ) gfx.aapolygon(surface, points, C_WHITE) line(surface, C_WHITE, (x,y), (x,y), 1)#центральная точка text = font.render(str(i)+":"+str(j), 1, C_WHITE) surface.blit(text, (x,y))
def drawAHat(surf, face): pointsInHat = [ (face.left, face.top), (face.left + face.width, face.top), (face.left + face.width/2, face.top - face.height/2 )] gfxdraw.filled_polygon(surf, pointsInHat, Color("red")) gfxdraw.aapolygon(surf, pointsInHat, Color("black"))