利用pygame 图形绘制函数绘制一个机器人

利用pygame 图形绘制函数绘制一个机器人

通过实践记录Pygame draw模块中以下函数的用法:

  1. pygame.draw.rect
  2. pygame.draw.circle
  3. pygame.draw.line
  4. pygame.draw.arc
import pygame, sys, math

vec = pygame.math.Vector2
pygame.init()

screen = pygame.display.set_mode((800, 800))

robot_border_color = (0, 0, 0)
robot_head_color = (182, 183, 241)
robot_body_color = (155, 227, 234)
robot_body_inner_color = (178, 178, 178)
robot_body_inner2_color = (217, 238, 178)
robot_body_circle_color = (245, 131, 131)
robot_body_circle2_color = (167, 231, 159)
robot_leg_color = (167, 231, 159)

robot_eye_color = (166, 174, 182)

robot_ear_color_small = (226, 167, 141)
robot_ear_color_big = (242, 207, 137)

robot_chujiao_color_rect = (250, 225, 115)
robot_chujiao_color_circle_big = (124, 215, 232)
robot_chujiao_color_circle_small = (97, 151, 153)

robot_head = pygame.Rect(98, 94, 268, 184)

robot_chujiao_left_rect = pygame.Rect(147, 78, 25, 25)
robot_chujiao_right_rect = pygame.Rect(297, 78, 25, 25)
robot_chujiao_left_circle = vec(159, 52)
robot_chujiao_right_circle = vec(309, 52)

robot_eye_left = vec(187, robot_head.y + robot_head.height // 2)
robot_eye_left_start = vec(robot_eye_left.x - 20, robot_eye_left.y)
robot_eye_left_end = vec(robot_eye_left.x + 20, robot_eye_left.y)

robot_eye_right = vec(277, robot_head.y + robot_head.height // 2)
robot_eye_right_start = vec(robot_eye_right.x - 20, robot_eye_right.y)
robot_eye_right_end = vec(robot_eye_right.x + 20, robot_eye_right.y)

robot_ear_left_small = pygame.Rect(73, 170, 30, 30)
robot_ear_left_big = pygame.Rect(47, 135, 30, 90)

robot_ear_right_small = pygame.Rect(362, 170, 30, 30)
robot_ear_right_big = pygame.Rect(388, 135, 30, 90)
robot_mouth_rect = pygame.Rect(210, 190, 40, 40)

robot_body = pygame.Rect(136, 274, 192, 215)
robot_body_inner = pygame.Rect(168, 305, 128, 153)
robot_body_inner2 = pygame.Rect(188, 323, 55, 116)
robot_body_circle = vec(265, 338)
robot_body_circle2 = vec(265, 380)

robot_arm_rect = (32, 300, 400, 280)
robot_arm_rect2 = (73, 325, 320, 224)

robot_leg_rect = (168, 484, 48, 60)
robot_leg_rect2 = (248, 484, 48, 60)
 
def draw_rect_with_border(surface, color, border_color, rect, border_radius=0):
	#当width为0时,会绘制一个填充色的矩形
    pygame.draw.rect(surface, color, rect, 0, border_radius=border_radius)
    #当width大于0时,会绘制一个只有边框的矩形
    pygame.draw.rect(surface, border_color, rect, 4, border_radius=border_radius)


def draw_circle_with_border(surface, center, radius, color, border_color):
    pygame.draw.circle(surface, color, center, radius)
    pygame.draw.circle(surface, border_color, center, radius, 4)


def draw_robot():
    surface = pygame.Surface((465, 580))

    surface.fill((255, 255, 255))
    # 耳朵
    draw_rect_with_border(surface, robot_ear_color_small, robot_border_color, robot_ear_left_small)
    draw_rect_with_border(surface, robot_ear_color_big, robot_border_color, robot_ear_left_big)

    draw_rect_with_border(surface, robot_ear_color_small, robot_border_color, robot_ear_right_small)
    draw_rect_with_border(surface, robot_ear_color_big, robot_border_color, robot_ear_right_big)
    # 触角
    draw_circle_with_border(surface, robot_chujiao_left_circle, 30, robot_chujiao_color_circle_big, robot_border_color)
    draw_circle_with_border(surface, robot_chujiao_left_circle, 15, robot_chujiao_color_circle_small,
                            robot_border_color)

    draw_circle_with_border(surface, robot_chujiao_right_circle, 30, robot_chujiao_color_circle_big, robot_border_color)
    draw_circle_with_border(surface, robot_chujiao_right_circle, 15, robot_chujiao_color_circle_small,
                            robot_border_color)

    draw_rect_with_border(surface, robot_chujiao_color_rect, robot_border_color, robot_chujiao_left_rect)
    draw_rect_with_border(surface, robot_chujiao_color_rect, robot_border_color, robot_chujiao_right_rect)
    # 头
    draw_rect_with_border(surface, robot_head_color, robot_border_color, robot_head, 20)

    # 嘴巴
    # arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
    start_angle = math.radians(210)
    end_angle = math.radians(325)
    pygame.draw.arc(surface, robot_border_color, robot_mouth_rect, start_angle, end_angle, 4)
    ##眼睛
    draw_circle_with_border(surface, robot_eye_left, 20, robot_eye_color, robot_border_color)
    pygame.draw.line(surface, robot_border_color, robot_eye_left_start, robot_eye_left_end, 4)
    draw_circle_with_border(surface, robot_eye_right, 20, robot_eye_color, robot_border_color)
    pygame.draw.line(surface, robot_border_color, robot_eye_right_start, robot_eye_right_end, 4)
    # 身体

    start_angle_arm = math.radians(10)
    end_angle_arm = math.radians(170)

    pygame.draw.arc(surface, robot_border_color, robot_arm_rect, start_angle_arm, end_angle_arm, 4)
    pygame.draw.arc(surface, robot_border_color, robot_arm_rect2, start_angle_arm, end_angle_arm, 4)

    draw_rect_with_border(surface, robot_leg_color, robot_border_color, robot_leg_rect)
    draw_rect_with_border(surface, robot_leg_color, robot_border_color, robot_leg_rect2)

    draw_rect_with_border(surface, robot_body_color, robot_border_color, robot_body)
    draw_rect_with_border(surface, robot_body_inner_color, robot_border_color, robot_body_inner)
    draw_rect_with_border(surface, robot_body_inner2_color, robot_border_color, robot_body_inner2)

    draw_circle_with_border(surface, robot_body_circle, 20, robot_body_circle_color, robot_border_color)
    draw_circle_with_border(surface, robot_body_circle2, 20, robot_body_circle2_color, robot_border_color)

    return surface


robot = draw_robot()

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                pass
    screen.fill((255, 255, 255))
    screen.blit(robot, (168, 90))
    pygame.display.update()

你可能感兴趣的:(Pygame实践,pygame,机器人,python)