def create_ball_image(size, color): chroma = (0, 255, 0) image = Surface(size.tuple()) image.fill(chroma) image.set_colorkey(chroma) ellipse(image, color, [0, 0, size.width(), size.height()]) return image
def RenderRoundedRectangle(surface, position, dimensions, color, radius=0.5): targetRectangle = Rect(tuple(position), tuple(dimensions)) alpha = color[3] color = (color[0], color[1], color[2], 0) position = targetRectangle.topleft targetRectangle.topleft = 0, 0 circle = Surface([min(targetRectangle.size) * 3] * 2, SRCALPHA) draw.ellipse(circle, (0, 0, 0), circle.get_rect(), 0) circle = transform.smoothscale( circle, [int(min(targetRectangle.size) * radius)] * 2) rectangle = Surface(targetRectangle.size, SRCALPHA) radius = rectangle.blit(circle, (0, 0)) radius.bottomright = targetRectangle.bottomright rectangle.blit(circle, radius) radius.topright = targetRectangle.topright rectangle.blit(circle, radius) radius.bottomleft = targetRectangle.bottomleft rectangle.blit(circle, radius) rectangle.fill((0, 0, 0), targetRectangle.inflate(-radius.w, 0)) rectangle.fill((0, 0, 0), targetRectangle.inflate(0, -radius.h)) rectangle.fill(color, special_flags=BLEND_RGBA_MAX) rectangle.fill((255, 255, 255, alpha), special_flags=BLEND_RGBA_MIN) return surface.blit(rectangle, position)
def draw(self, screen): # Draw bullet at asteroids position rect = Rect( (self.pos.x - self.size/2, self.pos.y - self.size/2), (self.size, self.size)) draw.ellipse(screen, cfg.white, rect) return
def budka_plus_cep(): ''' Draw the home for dogs :return: ''' draw.polygon(screen, (235, 191, 0), [[350, 450], [350, 650], [500, 700], [550, 650], [550, 450], [450, 300], [400, 350]]) draw.aalines(screen, BLACK, False, [[350, 450], [350, 650], [500, 700], [550, 650], [550, 450], [450, 300], [400, 350], [500, 500], [500, 700]]) draw.aalines(screen, BLACK, False, [[400, 350], [350, 450], [500, 500], [550, 450]]) draw.ellipse(screen, BLACK, (375, 500, 100, 150)) draw.arc(screen, YELLOW, (355, 610, 15, 30), 0, pi, 4) draw.arc(screen, YELLOW, (355, 610, 15, 30), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (357, 630, 15, 30), 0, pi, 4) draw.arc(screen, YELLOW, (357, 630, 15, 30), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (355, 650, 15, 30), 0, pi, 4) draw.arc(screen, YELLOW, (355, 650, 15, 30), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (345, 665, 30, 15), 0, pi, 4) draw.arc(screen, YELLOW, (345, 665, 30, 15), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (325, 663, 30, 15), 0, pi, 4) draw.arc(screen, YELLOW, (325, 663, 30, 15), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (305, 665, 30, 15), 0, pi, 4) draw.arc(screen, YELLOW, (305, 665, 30, 15), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (285, 663, 30, 15), 0, pi, 4) draw.arc(screen, YELLOW, (285, 663, 30, 15), pi, 2 * pi, 4) draw.arc(screen, YELLOW, (265, 665, 30, 15), 0, pi, 4) draw.arc(screen, YELLOW, (265, 665, 30, 15), pi, 2 * pi, 4)
def house(): house_parameters = ( (wall_color, ((x, y), (x+75, y+30), (x+100, y), (x+100, y+80), (x+75, y+110), (x, y+80)), 0), (roof_color, ((x, y), (x+75, y+30), (x+100, y), (x+70, y-70), (x+40, y-55)), 0), (bl_color, ((x, y), (x+75, y+30), (x+75, y+110), (x, y+80)), 1), (bl_color, ((x+75, y+30), (x+100, y), (x+100, y+80), (x+75, y+110)), 1), (bl_color, ((x, y), (x+75, y+30), (x+40, y-55)), 1), (bl_color, ((x+75, y+30), (x+100, y), (x+70, y-70), (x+40, y-55)), 1), (bl_color, (x+17, y+32, 40, 45), 0) ) ''' the sequence of drawing the house is: walls(1 polygon), roof(1 polygon), 2 walls' contours(each contour - 1 polygon), 2 roof's contours(each contour - 1 polygon), hole(1 ellipse) ''' k = 1 for elem in house_parameters: if k == 1: pgd.polygon(screen, elem[0], (elem[1][0], elem[1][1], elem[1][2], elem[1][3], elem[1][4], elem[1][5]), elem[2]) elif k == 2: pgd.polygon(screen, elem[0], (elem[1][0], elem[1][1], elem[1][2], elem[1][3], elem[1][4]), elem[2]) elif k == 5: pgd.polygon(screen, elem[0], (elem[1][0], elem[1][1], elem[1][2]), elem[2]) elif k == 7: pgd.ellipse(screen, elem[0], elem[1], elem[2]) else: pgd.polygon(screen, elem[0], (elem[1][0], elem[1][1], elem[1][2], elem[1][3]), elem[2]) k = k + 1
def draw_nose(screen, x, y, torso_length, torso_height, nose_color): '''the center(x,y) of the panda is its nose''' nose_coors = [x - 3 * torso_length // 50, y - 3 * torso_height // 55] nose_width = 3 * torso_length // 25 nose_height = 4 * torso_height // 55 pgd.ellipse(screen, nose_color, [nose_coors, [nose_width, nose_height]])
def get_aa_round_rect(size, radius, color): surface = Surface(size, flags=SRCALPHA).convert_alpha() rect = Rect((0, 0), size) color = Color(*color) alpha = color.a color.a = 0 rectangle = Surface(size, SRCALPHA) #here 5 is an arbitrary multiplier, we will just rescale later circle = Surface([min(size) * 5, min(size) * 5], SRCALPHA) draw.ellipse(circle, (0,0,0), circle.get_rect(), 0) circle = transform.smoothscale(circle, (2*radius, 2*radius)) #now circle is just a small circle of radius #blit topleft circle: radius_rect = rectangle.blit(circle, (0, 0)) #now radius_rect = Rect((0, 0), circle.size), rect=Rect((0, 0), size) #blit bottomright circle: radius_rect.bottomright = rect.bottomright #radius_rect is growing rectangle.blit(circle, radius_rect) #blit topright circle: radius_rect.topright = rect.topright rectangle.blit(circle, radius_rect) #blit bottomleft circle: radius_rect.bottomleft = rect.bottomleft rectangle.blit(circle, radius_rect) #black-fill of the internal rect rectangle.fill((0, 0, 0), rect.inflate(-radius_rect.w, 0)) rectangle.fill((0, 0, 0), rect.inflate(0, -radius_rect.h)) #fill with color using blend_rgba_max rectangle.fill(color, special_flags=BLEND_RGBA_MAX) #fill with alpha-withe using blend_rgba_min in order to make transparent #the rectangle.fill((255, 255, 255, alpha), special_flags=BLEND_RGBA_MIN) surface.blit(rectangle, rect.topleft) return surface
def test_color_validation(self): surf = pygame.Surface((10, 10)) colors = 123456, (1, 10, 100), RED # but not '#ab12df' or 'red' ... points = ((0, 0), (1, 1), (1, 0)) # 1. valid colors for col in colors: draw.line(surf, col, (0, 0), (1, 1)) draw.aaline(surf, col, (0, 0), (1, 1)) draw.aalines(surf, col, True, points) draw.lines(surf, col, True, points) draw.arc(surf, col, pygame.Rect(0, 0, 3, 3), 15, 150) draw.ellipse(surf, col, pygame.Rect(0, 0, 3, 6), 1) draw.circle(surf, col, (7, 3), 2) draw.polygon(surf, col, points, 0) # 2. invalid colors for col in ('invalid', 1.256, object(), None, '#ab12df', 'red'): with self.assertRaises(TypeError): draw.line(surf, col, (0, 0), (1, 1)) with self.assertRaises(TypeError): draw.aaline(surf, col, (0, 0), (1, 1)) with self.assertRaises(TypeError): draw.aalines(surf, col, True, points) with self.assertRaises(TypeError): draw.lines(surf, col, True, points) with self.assertRaises(TypeError): draw.arc(surf, col, pygame.Rect(0, 0, 3, 3), 15, 150) with self.assertRaises(TypeError): draw.ellipse(surf, col, pygame.Rect(0, 0, 3, 6), 1) with self.assertRaises(TypeError): draw.circle(surf, col, (7, 3), 2) with self.assertRaises(TypeError): draw.polygon(surf, col, points, 0)
def draw_legs(surf_2, surf_1, length, width): """ Функция рисует на дополнительных поверхностях ноги птицы :param surf_2: поверхность, на которой будет нарисована желтая часть лапок и черный контур вокруг лапок :param surf_1: поверхность, на которой будут нарисованы ноги без желтой части лапок :param length: длина белой части ноги :param width: ширина белой части ноги :return: функция ничего не возвращает """ draw.ellipse(surf_2, white, (45, 10, 20, 50)) draw.ellipse(surf_1, white, (0, 0, 30, 60)) draw.polygon(surf_2, black, [(width - 1, length), (width - 16, length + 10), (width - 31, length + 29), (width - 5, length + 11), (width - 1, length + 36), (width + 4, length + 19), (width + 1, length + 11), (width + 1, length + 9), (width + 19, length + 34), (width + 18, length + 19), (width + 11, length + 11), (width + 7, length + 4), (width + 36, length + 30), (width + 29, length + 14), (width + 11, length)]) draw.polygon(surf_2, (234, 211, 114), [(width + 0, length), (width - 15, length + 10), (width - 30, length + 28), (width - 5, length + 10), (width, length + 35), (width + 3, length + 20), (width, length + 10), (width + 2, length + 8), (width + 20, length + 33), (width + 17, length + 20), (width + 10, length + 10), (width + 7, length + 4), (width + 35, length + 29), (width + 28, length + 14), (width + 10, length)])
def draw(self): surface = Surface(self.size, flags=SRCALPHA).convert_alpha() rect = Rect((0, 0), self.size) color = Color(*self.color) alpha = color.a color.a = 0 rectangle = Surface(rect.size, SRCALPHA) #ex: [h*3, h*3] circle = Surface([min(rect.size) * 5] * 2, SRCALPHA) draw.ellipse(circle, (0, 0, 0), circle.get_rect(), 0) #ex: [h*0.5, h*.05] circle = transform.smoothscale(circle, [int(self.radius_value)] * 2) #now circle is just a small circle of radius self.radius*h (for example) #blit topleft circle: radius = rectangle.blit(circle, (0, 0)) #now radius = Rect((0, 0), circle.size), rect=Rect((0, 0), self.size) #blit bottomright circle: radius.bottomright = rect.bottomright #radius is growing rectangle.blit(circle, radius) #blit topright circle: radius.topright = rect.topright rectangle.blit(circle, radius) #blit bottomleft circle: radius.bottomleft = rect.bottomleft rectangle.blit(circle, radius) #black-fill of the internal rect rectangle.fill((0, 0, 0), rect.inflate(-radius.w, 0)) rectangle.fill((0, 0, 0), rect.inflate(0, -radius.h)) #fill with color using blend_rgba_max rectangle.fill(color, special_flags=BLEND_RGBA_MAX) #fill with alpha-withe using blend_rgba_min in order to make transparent #the rectangle.fill((255, 255, 255, alpha), special_flags=BLEND_RGBA_MIN) surface.blit(rectangle, rect.topleft) return surface
def body() : body_parameters = ( (x+75*n-55*abs(n), y+25*abs(n), 110*abs(n), 60*abs(n)), (x+115*n-35*abs(n), y+18*abs(n), 70*abs(n), 40*abs(n)), (x+65*n-15*abs(n), y+45*abs(n), 30*abs(n), 70*abs(n)), (x+25*n-15*abs(n), y+35*abs(n), 30*abs(n), 70*abs(n)), (x+118*n-8*abs(n), y+45*abs(n), 16*abs(n), 50*abs(n)), (x+158*n-8*abs(n), y+70*abs(n), 16*abs(n), 40*abs(n)), (x+55*n-15*abs(n), y+110*abs(n), 30*abs(n), 15*abs(n)), (x+15*n-15*abs(n), y+100*abs(n), 30*abs(n), 15*abs(n)), (x+110*n-10*abs(n), y+90*abs(n), 20*abs(n), 10*abs(n)), (x+150*n-10*abs(n), y+105*abs(n), 20*abs(n), 10*abs(n)), (x+int(100*n), y+int(35*abs(n)), int(20*abs(n)), 0), (x+int(145*n), y+int(60*abs(n)), int(20*abs(n)), 0), ) ''' the sequence of drawing the body is: chest, back, 4 legs, 4 feet , 2 thighs each part except thighs is drawn with the only ellipse thighs are drawn with circles ''' k = 1 for elem in body_parameters: if k > 10: pgd.circle(screen, dog_color, (elem[0], elem[1]), elem[2]) else: pgd.ellipse(screen, dog_color, elem) k = k + 1
def cloud(x, y, grayness=0.0, scale=1.0): """function that draws a cloud with left top corner (x, y) with color scaling by grayness parameter [0, 1]""" s = pygame.Surface((screen_width, screen_height), pygame.SRCALPHA) d.ellipse(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x, y, 120 * scale, 25 * scale)) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + int(120 * 0.8 * scale) // 3, y + int(25 * scale) // 3), 40) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + int(120 * 1.5 * scale) // 3, y + int(25 * 0.8 * scale) // 3), 40) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + int(120 * 2.4 * scale) // 3, y + int(25 * 1.2 * scale) // 3), 40) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + int(120 * 0.95 * scale) // 3, y + int(25 * 2 * scale) // 3), 40) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + int(120 * 1.7 * scale) // 3, y + int(25 * 1.7 * scale) // 3), 40) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + int(120 * 2.8 * scale) // 3, y + int(25 * 1.3 * scale) // 3), 40) d.circle(s, (255 * grayness, 255 * grayness, 255 * grayness, 100), (x + 25, y + int(12 * scale)), 45) screen.blit(s, (0, 0))
def desenhar(self, tela): x = self.x y = self.y # desenhar uma bolinha no ponto draw.ellipse(tela, (255, 255, 255), ((x + self.translate[0]) - 2, (y + self.translate[1]) - 2, 4, 4))
def car(x, y): d.rect(surface_car, black, (x - 35, y + 30, 10, 5, 255)) d.rect(surface_car, blue, (x, y, 80, 30, 255)) d.rect(surface_car, blue, (x - 30, y + 20, 150, 20)) d.rect(surface_car, white, (x + 5, y + 5, 20, 10)) d.rect(surface_car, white, (x + 50, y + 5, 20, 10)) d.ellipse(surface_car, black, (x - 15, y + 30, 30, 25)) d.ellipse(surface_car, black, (x + 80, y + 30, 30, 25))
def draw_torso(screen, x, y, torso_length, torso_height, torso_color): '''the center(x,y) of the panda is its nose''' torso_coords = [x - 5 * torso_length // 53, y - 25 * torso_height // 60] pgd.ellipse( screen, torso_color, [torso_coords, [47 * torso_length // 53, 27 * torso_height // 60]])
def draw(self, surface): for edge in self.edges: start_node, end_node = self.nodes[edge[0]], self.nodes[edge[1]] draw.line(surface, self.edge_color, (start_node[0] + self.node_size // 2, start_node[1] + self.node_size // 2), (end_node[0] + self.node_size // 2, end_node[1] + self.node_size // 2)) for node in self.nodes: draw.ellipse(surface, self.node_color, (node[0], node[1], self.node_size, self.node_size))
def car(color, x, y, car_width, car_length): d.rect(screen, black, (x - 35, y + 30, car_width, car_length)) d.rect(screen, color, (x, y, 8 * car_width, 6 * car_length)) d.rect(screen, color, (x - 30, y + 20, 15 * car_width, 4 * car_length)) d.rect(screen, white, (x + 5, y + 5, 2 * car_width, 2 * car_length)) d.rect(screen, white, (x + 50, y + 5, 2 * car_width, 2 * car_length)) d.ellipse(screen, black, (x - 15, y + 30, 3 * car_width, 5 * car_length)) d.ellipse(screen, black, (x + 80, y + 30, 3 * car_width, 5 * car_length))
def test_ellipse(self): """ |tags: ignore| """ # __doc__ (as of 2008-08-02) for pygame.draw.ellipse: # pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect # draw a round shape inside a rectangle # # Draws an elliptical shape on the Surface. The given rectangle is the # area that the circle will fill. The width argument is the thickness # to draw the outer edge. If width is zero then the ellipse will be # filled. # sizes = [(4, 4), (5, 4), (4, 5), (5, 5)] color = (1, 13, 24, 255) for width, height in sizes: for border_width in (0, 1): surface = pygame.Surface((width, height)) draw.ellipse(surface, color, (0, 0, height, width), border_width) # Get all positions of the surface's borders border_top = [] border_left = [] border_right = [] border_bottom = [] for x in range(width): for y in range(height): try: surface.get_at((x, y - 1)) except IndexError: border_top.append((x, y)) try: surface.get_at((x - 1, y)) except IndexError: border_left.append((x, y)) try: surface.get_at((x + 1, y)) except IndexError: border_right.append((x, y)) try: surface.get_at((x, y + 1)) except IndexError: border_bottom.append((x, y)) # For each of the four borders check if it contains the color borders = [ border_top, border_left, border_right, border_bottom ] for border in borders: colors = [surface.get_at(position) for position in border] self.assertTrue(color in colors)
def update(self): if self.__status == StatusHealth.contaminated: if self.__time_to_sick: self.__time_to_sick -= 1 else: p = randint(1, 100) if p <= 5: self.set_status(StatusHealth.sick_icu) self.set_speed(0) elif p <= 20: self.set_status(StatusHealth.sick) self.set_speed(10) else: self.set_status(StatusHealth.recovered) self.set_speed(100) if self.__status in [StatusHealth.sick, StatusHealth.sick_icu]: if self.__time_after_sick: self.__time_after_sick -= 1 else: if self.__status == StatusHealth.sick: self.set_status(StatusHealth.recovered) elif self.__status == StatusHealth.sick_icu: if randint(1, 5) <= 2: self.set_status(StatusHealth.dead) else: self.set_status(StatusHealth.recovered) self.set_speed(100) if self.__pause: self.__pause -= 1 return self.__pause = 100 - self.__speed screen_witdh, screen_height = 500, 500 if ( self.rect.x + self.direction_x > screen_witdh or self.rect.x + self.direction_x < 0 ): self.direction_x = self.direction_x * (-1) self.direction_y = randint(5 * (-1), 5) self.rect.x += self.direction_x if ( self.rect.y + self.direction_y > screen_height or self.rect.y + self.direction_y < 0 ): self.direction_y = self.direction_y * (-1) self.direction_x = randint(5 * (-1), 5) self.rect.y += self.direction_y draw.ellipse( self.image, self.__status.color, [0, 0, 4, 4], 0 )
def draw(self, surface): radius = self.mass * self.density draw.ellipse( surface, self.color, ( self.position[0] - radius, self.position[1] - radius, radius * 2, radius * 2 ) )
def same_size(width, height, border_width): """Test for ellipses with the same size as the surface.""" surface = pygame.Surface((width, height)) draw.ellipse(surface, color, (0, 0, width, height), border_width) # For each of the four borders check if it contains the color borders = get_border_values(surface, width, height) for border in borders: self.assertTrue(color in border)
def test_hollow_ellipses(test_surf): for cent_x, cent_y, color in ((70, 130, (255, 0, 0, 255)), (150, 450, (255, 255, 255, 255)), (200, 200, (0, 255, 0, 255)), (500, 500, (255, 128, 128, 255))): for r1, r2 in ((30, 20), (50, 10), (10, 40), (15, 90)): for thickness in range(1, 9, 3): e_rect = (cent_x - r1 + 30 * thickness, cent_y - r2 - 30 * thickness, 2 * r1, 2 * r2) draw.ellipse(test_surf, color, e_rect, thickness)
def yurt(x1, y1, length, height): """ функция рисует юрту заданных размеров в заданном месте :param x1: координата по х верхнего левого угла прямоугольника, в который будет вписана юрта :param y1: координата по у верхнего левого угла прямоугольника, в который будет вписана юрта :param length: ширина юрты :param height: высота юрты, умноженная на 2 :return: функция ничего не возвращает """ draw.ellipse(screen, (230, 230, 230), (x1, y1, length, height)) draw.arc(screen, (0, 0, 0), (x1, y1, length, height), math.pi * 2, math.pi, 3) draw.rect(screen, (255, 255, 255), (x1, y1 + height / 2, length, height / 2)) draw.line(screen, (0, 0, 0), (x1, y1 + height / 2), (x1 + length, y1 + height / 2), 1) x2 = x1 + length / 8 xx2 = x2 - x1 - length / 2 l2 = length / 2 y2 = -(height / 2) * math.sqrt(1 - (xx2**2) / (l2**2)) + y1 + height / 2 draw.arc(screen, (0, 0, 0), (x2, y2 - 5, 0.75 * length, 10), math.pi, math.pi * 2, 2) x3 = x1 + length / 32 xx3 = x3 - x1 - length / 2 y3 = -(height / 2) * math.sqrt(1 - (xx3**2) / (l2**2)) + y1 + height / 2 draw.arc(screen, (0, 0, 0), (x3, y3 - 5, 0.9375 * length, 10), math.pi, math.pi * 2, 2) x4 = x1 + length / 4 xx4 = x4 - x1 - length / 2 y4 = -(height / 2) * math.sqrt(1 - (xx4**2) / (l2**2)) + y1 + height / 2 draw.arc(screen, (0, 0, 0), (x4, y4 - 5, 0.5 * length, 10), math.pi, math.pi * 2, 2) draw.line(screen, (0, 0, 0), (x3 + length / 5, y3), (x1 + length / 5, y1 + height / 2), 1) draw.line(screen, (0, 0, 0), (x3 + 2 * length / 5, y3), (x1 + 3 * length / 7, y1 + height / 2), 1) draw.line(screen, (0, 0, 0), (x3 + 3 * length / 5, y3), (x1 + 4 * length / 6, y1 + height / 2), 1) draw.line(screen, (0, 0, 0), (x3 + 4 * length / 5, y3), (x1 + 7 * length / 8, y1 + height / 2), 1) draw.line(screen, (0, 0, 0), (x2 + length / 4, y2), (x3 + 2 * length / 7, y3 + 5), 1) draw.line(screen, (0, 0, 0), (x2 + 4 * length / 7, y2), (x3 + 5 * length / 7, y3 + 5), 1) draw.line(screen, (0, 0, 0), (x4 + length / 10, y4), (x2 + length / 8, y2 + 5), 1) draw.line(screen, (0, 0, 0), (x4 + length / 4, y4), (x2 + 3 * length / 7, y2 + 5), 1) draw.line(screen, (0, 0, 0), (x4 + 5 * length / 11, y4), (x2 + 8 * length / 12, y2 + 5), 1) draw.line(screen, (0, 0, 0), (x1 + length / 2, y1), (x4 + 4 * length / 12, y4 + 5), 1)
def not_same_size(width, height, border_width, left, top): """Test for ellipses that aren't the same size as the surface.""" surface = pygame.Surface((width, height)) draw.ellipse(surface, color, (left, top, width - 1, height - 1), border_width) borders = get_border_values(surface, width, height) # Check if two sides of the ellipse are touching the border sides_touching = [ color in border for border in borders].count(True) self.assertEqual(sides_touching, 2)
def draw_sun(screen): width = screen.get_width() srf = pygame.Surface((width // 2, width // 2)) srf.set_colorkey((0, 0, 0)) dr.ellipse(srf, (255, 255, 255), [0, 0, width // 2, width // 2], 20) dr.ellipse(srf, (255, 255, 255), [width // 4 - 20, width // 4 - 20, 40, 40]) dr.rect(srf, (255, 255, 255), [width // 4 - 5, 0, 10, width // 2]) dr.rect(srf, (255, 255, 255), [0, width // 4 - 5, width // 2, 10]) screen.blit(srf, (width // 2, 0))
def test_filled_ellipses_2(test_surf): """Draw several filled circles""" for cent_x, color in ((100, (0, 0, 255, 255)), (400, (0, 255, 255, 255)), (600, (255, 0, 255, 255))): cent_y = 10 div = 9 for radius in range(10, 100, 10): cent_y += radius + 1 if div > 3: div = div // 3 else: div = div * 3 e_rect = rect.Rect(cent_x - radius // div, cent_y - radius, div * radius, radius) draw.ellipse(test_surf, color, e_rect)
def draw_tree(screen, x, y, size_x, size_y, color): #the center(x,y) of the tree is the point in the middle of its trunk #bottom part of trunk pgd.line(screen, color, [x, y + int(size_y/60)], [x, y + int(size_y/4)], int(size_x/18)) pgd.line(screen, color, [x, y + int(17*size_y/60)], [x, y + int(size_y/2)], int(size_x/18)) #right and bottom branch pgd.arc(screen, tree_color, [[x, y - int(5*size_y/57)], [int(14*size_x/35), int(size_y/2 + 5*size_y/57)]], 6*m.pi/14, 4*m.pi/5, 4) #top part of trunk pgd.polygon(screen, tree_color, [[x, y], [x - int(size_x/22), y - int(size_y/80)], [x, y - int(17.5*size_y/116)], [x + int(size_x/22), y - int(16*size_y/116)]]) pgd.polygon(screen, tree_color, [[x + int(size_x/80), y - int(size_y/6 + 0.5*size_y/100)], [x - int(size_x/80), y - int(17*size_y/96)], [x + int(size_x/18), y - int(size_y/2 + 1*size_y/100 )], [x + int(7*size_x/86), y - int(size_y/2 + 0.5*size_y/100)]]) #leaves on right and bottom branch delta_1 = int(5*size_x/37) for i in range(3): pgd.ellipse(screen, tree_color, [[x + delta_1, y - int(5*size_y/70)], [int(size_x/40), int(5*size_y/35)]]) delta_1 += int(3*size_x/60) #right and top branch and leaves on it pgd.arc(screen, tree_color, [[x + int(5*size_x/80), y - int(size_y/2)], [int(size_x - 5*size_x/40), int(30*size_y/35)]], m.pi/2, 9*m.pi/10, 4) delta_2x = int(8*size_x/24) delta_2y = int(32*size_y/70) for i in range(5): pgd.ellipse(screen, tree_color, [[x + delta_2x, y - delta_2y], [int(size_x/40), int(5*size_y/35)]]) delta_2x += int(3*size_x/80) delta_2y += int(size_y/70) #left top branch and leaves pgd.arc(screen, tree_color, [[x - size_x, y - int(size_y/2)], [size_x, size_y]], m.pi/10, m.pi/2, 4) delta_3x = int(2.5*size_x/8) delta_3y = int(3.5*size_y/8) for i in range(5): pgd.ellipse(screen, tree_color, [[x - delta_3x, y - delta_3y], [int(size_x/40), int(5*size_y/35)]]) delta_3x += int(3*size_x/80) delta_3y += int(size_y/70) #left top branch and leaves pgd.arc(screen, tree_color, [[x - int(22*size_x/48), y], [int(22*size_x/48), int(size_y/2)]], m.pi/6, 2.5*m.pi/4, 4) delta_4 = int(size_x/6) for i in range(3): pgd.ellipse(screen, tree_color, [[x - delta_4, y], [int(size_x/40), int(5*size_y/35)]]) delta_4 += int(3*size_x/60)
def draw_maze(maze): '''Create a surface big enough to draw the entire maze. We need "0" -- room with atleast one corridor "-" -- horizontal corridor "|" -- vertical corridor ''' ROOM_DIAMETER = 10 CORRIDOR_LENGTH = 5 CORRIDOR_THICKNESS = 3 MARGIN_LR = 30 MARGIN_TB = 30 ROOM_DELTA = ROOM_DIAMETER + CORRIDOR_LENGTH CORR_E_YD = (ROOM_DIAMETER - CORRIDOR_THICKNESS) / 2 CORR_S_XD = (ROOM_DIAMETER - CORRIDOR_THICKNESS) / 2 from pygame import Rect from pygame.draw import ellipse, rect black = (0, 0, 0) white = (255,255,255) surf = pygame.display.set_mode( ((maze.width * ROOM_DELTA) - CORRIDOR_LENGTH + 2 * MARGIN_LR, (maze.height * ROOM_DELTA) - CORRIDOR_LENGTH + 2 * MARGIN_TB)) surf.fill(black) # draw rooms rooms = maze.visited curr_sy = MARGIN_TB - ROOM_DELTA for y in range(maze.height): curr_sy += ROOM_DELTA curr_sx = MARGIN_LR - ROOM_DELTA for x in range(maze.width): curr_sx += ROOM_DELTA rmask = rooms[(x, y)] if rmask == 0: continue ellipse(surf, white, Rect(curr_sx, curr_sy, ROOM_DIAMETER, ROOM_DIAMETER)) if rmask & 4: rect(surf, white, Rect(curr_sx + ROOM_DIAMETER, curr_sy + CORR_E_YD, CORRIDOR_LENGTH, CORRIDOR_THICKNESS)) if rmask & 1: rect(surf, white, Rect(curr_sx + CORR_S_XD, curr_sy + ROOM_DIAMETER, CORRIDOR_THICKNESS, CORRIDOR_LENGTH))
def get_aa_ellipsis(size, color): surface = Surface(size, flags=SRCALPHA).convert_alpha() rect = Rect((0, 0), size) color = Color(*color) alpha = color.a color.a = 0 #here 5 is an arbitrary multiplier, we will just rescale later circle = Surface([size[0] * 5, size[1] * 5], SRCALPHA) draw.ellipse(circle, (0,0,0), circle.get_rect(), 0) circle = transform.smoothscale(circle, size) #fill with color using blend_rgba_max circle.fill(color, special_flags=BLEND_RGBA_MAX) #fill with alpha-withe using blend_rgba_min in order to make transparent circle.fill((255, 255, 255, alpha), special_flags=BLEND_RGBA_MIN) return circle
def puts(x1, y1, length, height, angle): """" функция рисует ноги и хвост кота :param x1: координата по х верхнего левого угла прямоугольника, в который вписан эллипс - конечность кота :param y1: координата по н верхнего левого угла прямоугольника, в который вписан эллипс - конечность кота :param length: длина части тела :param height: ширина части тела :param angle: угол, на который нужно повернуть лапу.хвост(против часовой стрелки) :return: функция ничего не возвращает """ tale = pygame.Surface((length, height)) tale.set_colorkey('BLACK') draw.ellipse(tale, (204, 204, 204), (0, 0, length, height)) tale2 = pygame.transform.rotate(tale, angle) screen.blit(tale2, (x1, y1))
def test_filled_ellipses_1(test_surf): """Draw several filled circles""" for cent_x, color in ((100, (0, 0, 255, 255)), (300, (0, 255, 255, 255)), (500, (255, 0, 255, 255))): cent_y = 10 div = 8 offset = 0 for radius in range(10, 100, 10): if div > 2: div = div // 2 else: div = div * 2 cent_y += radius // div + 1 offset += 35 e_rect = rect.Rect(cent_x - radius + offset, cent_y - radius // div, radius, div * radius) draw.ellipse(test_surf, color, e_rect)
def draw_eyes(screen, x, y, torso_length, torso_height, eyes_color): '''the center(x,y) of the panda is its nose''' right_eye_coords = [ x - 5 * torso_length // 53, y - 15 * torso_height // 60 ] right_eye_width = 6 * torso_length // 53 right_eye_height = 7 * torso_height // 60 pgd.ellipse(screen, eyes_color, [right_eye_coords, [right_eye_width, right_eye_height]]) left_eye_coords = (x + 10 * torso_length // 53, y - 10 * torso_height // 60) left_eye_radius = 4 * torso_length // 53 pgd.circle(screen, eyes_color, left_eye_coords, left_eye_radius)
def printNet(self, network): x = START_OF_NET[0] last_layer = [] this_layer = [] index = 0 draw.ellipse(self.screen, GREY, [x, START_OF_NET[1] + 105, 50, 500]) this_layer.append((START_OF_NET[0] + 25, 350)) x += int(SIZE[0] / network.number_of_layers) w_maxes = [] b_maxes = [] for wm in network.weights: w_maxes.append(np.max(np.abs(wm))) for bm in network.biases: b_maxes.append(np.max(np.abs(bm))) w_adj = 255 / max(w_maxes) b_adj = 255 / max(b_maxes) for size, biases in zip(network.sizes[1:], network.biases): last_layer = this_layer.copy() this_layer.clear() y = START_OF_NET[1] radius = 10 y_step = int(SIZE[1] / size) for n, bias in zip(range(size), biases): if bias < 0: draw.circle(self.screen, (-b_adj * bias, 0, 0), (x, y), radius) else: draw.circle(self.screen, (0, b_adj * bias, 0), (x, y), radius) this_layer.append((x, y)) y += y_step x += int((SIZE[0] - START_OF_NET[0]) / network.number_of_layers) if last_layer and this_layer: weights_matr = network.weights[index] index += 1 for right, weights in zip(this_layer, weights_matr): for left, weight in zip(last_layer, weights): if weight < 0: draw.line(self.screen, (-w_adj * weight, 0, 0), left, right) else: draw.line(self.screen, (0, w_adj * weight, 0), left, right) display.flip() for event in p.event.get(): pass
def draw(self, surface): super(Player, self).draw(surface) angle = self.baseChainAngle radius = self.mass * self.density baseRadius = self.baseMass * self.density for i in range(self.chain): x = self.position[0] + cos(angle) * (radius + baseRadius / 2) y = self.position[1] + sin(angle) * (radius + baseRadius / 2) size = baseRadius / 3 draw.ellipse( surface, self.color, [x - size, y - size, size * 2, size * 2] ) angle += pi * 2 / (self.maxChain - 1) angle %= pi * 2
def mouth(surface, mod, pos): pos = list(pos) if mod["mouthPoison"] < 2: color0 = (255,0,0) color1 = (200,0,0) else: color0 = (255,0,200) color1 = (200,0,200) # adjust for body size radius = ((mod["bodySize"] + 1) * 2) + 6 t = mod["mouthType"] if t == 0: draw.circle(surface, color0, pos, radius) draw.circle(surface, color1, pos, radius, 2) return Rect(pos[0] - radius, pos[1] - radius, radius * 2, radius * 2) else: rect = Rect((pos[0] - radius, pos[1] - radius / 2), (radius * 2, radius)) draw.ellipse(surface, color0, rect) draw.ellipse(surface, color0, rect, 2) return rect
def update(self, time): self.delta = time for comp in [x for x in self.components if x.active]: # prepare style options color = comp.style.get('color', common.BLACK) width = comp.style.get('width', 1) # draw debug objects if comp.rect: from pdb import set_trace set_trace() trans_rect = Rect(comp.rect.rect.x + comp.rect.anchor.body.x, comp.rect.rect.y + comp.rect.anchor.body.y, comp.rect.rect.w, comp.rect.rect.h) draw.rect(self.surface, color, trans_rect, width) if comp.circle: draw.circle(self.surface, color, comp.circle.pos, comp.circle.radius, width) if comp.ellipse: draw.ellipse(self.surface, color, comp.ellipse.rect, width) if comp.arc: draw.arc(self.surface, color, comp.arc.start, comp.arc.end, width) if comp.line: draw.line(self.surface, color, comp.line.closed, comp.line.points, width) if comp.get_value: text_val = str(comp.get_value()) if text_val != comp.last_value: # fire event to update rendered graphic for this comp.last_value = text_val tu_event = GameEvent(UPDATETEXT, component=comp.text, text=text_val) self.delegate(tu_event)
# draw board screen.fill(background) draw.line(screen, gridcolor, (boardpos.left, boardpos.top + 2*gridsize), (boardpos.right, boardpos.top + 2*gridsize), 3) draw.rect(screen, outlinecolor, boardpos, 4) # board outline #colored blockies for level in range(1, currentcolor): r = pygame.Rect(0,0,0,0) r.top = 10 r.height = gridsize/2 r.left = boardwidth * gridsize - gridsize/2 * len(combinecolors) + gridsize/2 * level r.width = gridsize/2 draw.ellipse(screen, combinecolors[level], pygame.Rect(r.left, r.top, r.width, r.height), 0) piece = pygame.Rect(boardpos) # to be reused piece.width = piece.height = gridsize if state == 'idle': # draw drop for i in range(0, len(drop)): for j in range(0, len(drop[0])): piece.top = boardpos.top + (1 - i)*gridsize offset = min(dropindex, boardwidth - len(drop[0])) piece.left = boardpos.left + (j + offset)*gridsize draw.ellipse(screen, combinecolors[drop[i][j]], piece, 0) # draw pieces
def body(surface, mod, pos): # adjust for body size size = ((mod["bodySize"] + 1) * 6) + 30 # get the correct skin color color0 = skinColor(mod) t = mod["bodyType"] # wide ellipse if t == 0: rect = Rect(pos,(size, size + size / 3)) draw.ellipse(surface, color0, rect) return rect # wide curved elif t == 1: radius = size / 2 x = pos[0] + radius y = pos[1] + radius draw.circle(surface, color0, (x - radius / 2, y), radius) draw.circle(surface, color0, (x + radius / 2, y), radius) rect = Rect((pos[0] - radius / 2, pos[1]),(size + radius, radius * 2)) return rect # wide 3-segmented elif t == 2: segments = 3 radius = size / 3 dx = size / segments x = pos[0] + radius y = pos[1] + radius for i in range(0,3): draw.circle(surface, color0, (x + dx * i, y), radius) rect = Rect((pos[0] - dx, pos[1]), (size + radius, radius * 2)) return rect # tall ellipse if t == 3: rect = Rect(pos,(size + size / 3, size)) draw.ellipse(surface, color0, rect) return rect # tall curved elif t == 4: radius = size / 2 x = pos[0] + radius y = pos[1] + radius draw.circle(surface, color0, (x, y - radius / 2), radius) draw.circle(surface, color0, (x, y + radius / 2), radius) rect = Rect((pos[0], pos[1] - radius / 2),(radius * 2, size + radius)) return rect # tall 3-segmented elif t == 5: segments = 3 radius = size / 3 dy = size / segments x = pos[0] + radius y = pos[1] + radius for i in range(0,3): draw.circle(surface, color0, (x + dx * i, y + dy * i), radius) rect = Rect((pos[0], pos[1]- dy), (radius * 2, radius + size)) return rect # block else: rect = Rect(pos,(size,size)) draw.rect(surface, color0, rect) return rect
def draw_ellipse(top_left_x, top_left_y, width, height, thickness = 1): draw.ellipse(screen, color, pygame.Rect(int(top_left_x), int(WINDOW_HEIGHT-top_left_y), int(width), int(height)), thickness)