大家好,我是复仇流浪者,我有回来了!!!

今天我给大家当来一个Python绘画;

好了废话不多说,上代码吧!!!

# 直接点击“运行”即可

# 礼花蛋
import turtle
import math
import random
turtle.speed(0)
turtle.ht()
body = turtle.Turtle()
body.ht()
hand = turtle.Turtle()
hand.ht()
face = turtle.Turtle()
face.ht()
eye1 = turtle.Turtle()
eye1.ht()
eye2 = turtle.Turtle()
eye2.ht()
eyesball = turtle.Turtle()
eyesball.ht()
mouth = turtle.Turtle()
mouth.ht()
cheek = turtle.Turtle()
cheek.ht()

def ellipspos(a, b, angle, steps, rotateAngle, x, y, t=turtle):
    rotateAngle = rotateAngle / 360 * 2 * math.pi
    t.penup()
    t.setpos(x + b * math.sin(rotateAngle), y - b * math.cos(rotateAngle))
    
def ellips(a, b, angle, steps, rotateAngle, x, y, up=False, down=False, t=turtle):
    x = -x
    t.penup()
    minAngle = (2 * math.pi / 360) * angle / steps
    rotateAngle = rotateAngle / 360 * 2 * math.pi
    halfsteps = steps // 2
    for i in range(steps + 1):
        nextPoint = [y + a * math.sin((i + 1) * minAngle),
                     x - b * math.cos((i + 1) * minAngle)]
        nextPoint = [nextPoint[0] * math.cos(rotateAngle) - nextPoint[1] * math.sin(rotateAngle),
                     nextPoint[0] * math.sin(rotateAngle) + nextPoint[1] * math.cos(rotateAngle)]
        t.setpos(nextPoint)
        if up and i <= halfsteps:
            t.pendown()
        elif up and i > halfsteps:
            break
        elif down and i > halfsteps:
            t.pendown()
        elif down and i < halfsteps:
            t.penup()
        else:
            t.pendown()
    t.end_fill()


# 画身体
def draw_body(color):
    body.fillcolor(color) 
    ellipspos(200, 150, 360, 160, 90, 0, 0, body)
    body.begin_fill()
    ellips(200, 150, 360, 160, 90, 0, 0, True, False, body)
    body.end_fill()
    body.fillcolor(color)
    ellipspos(140, 150, 360, 160, 90, 0, 0, body)
    body.begin_fill()
    ellips(140, 150, 360, 160, 90, 0, 0, False, True, body)
    body.end_fill()


# 画脸
def draw_face(color):
    face.fillcolor(color)
    face.penup()
    face.goto(-75,-80)      #蓝色脸位置正常-75,-80
    face.pendown()
    face.begin_fill()
    face.forward(145)
    face.circle(50, 60)
    face.circle(100, 30)
    face.circle(150, 30)
    face.circle(180, 45)
    face.left(30)
    face.circle(180, 45)
    face.circle(150, 30)
    face.circle(100, 30)
    face.circle(50, 70)
    face.end_fill()
    face.penup()


# 画手
def draw_hands(color):
    hand.fillcolor(color)
    hand.penup()
    ellipspos(56, 12, 360, 150, 90, 166, -8, hand)
    hand.begin_fill()
    ellips(56, 12, 360, 150, 90, 166, -8, False, False, hand)
    hand.end_fill()
    hand.penup()
    ellipspos(56, 12, 360, 150, 90, -166, -8, hand)
    hand.begin_fill()
    ellips(56, 12, 360, 150, 90, -166, -8, False, False, hand)
    hand.end_fill()

# 画眼球
def draw_eyesball(color1,color2,color3):
    eyesball.fillcolor(color1)
    eyesball.penup()
    eyesball.goto(65, 15)
    eyesball.pendown()
    eyesball.begin_fill()
    eyesball.circle(20)
    eyesball.end_fill()
    
    eyesball.penup()
    eyesball.goto(-65, 15)
    eyesball.pendown()
    eyesball.begin_fill()
    eyesball.circle(20)
    eyesball.end_fill()
    
    eyesball.fillcolor(color2)
    eyesball.penup()
    eyesball.goto(65, 25)
    eyesball.pendown()
    eyesball.begin_fill()
    eyesball.circle(10)
    eyesball.end_fill()
    
    eyesball.penup()
    eyesball.goto(-65, 25)
    eyesball.pendown()
    eyesball.begin_fill()
    eyesball.circle(10)
    eyesball.end_fill()
    
    eyesball.fillcolor(color3)
    eyesball.penup()
    eyesball.goto(58, 38)
    eyesball.pendown()
    eyesball.begin_fill()
    eyesball.circle(8)
    eyesball.end_fill()
    
    eyesball.penup()
    eyesball.goto(-72, 38)
    eyesball.pendown()
    eyesball.begin_fill()
    eyesball.circle(8)
    eyesball.end_fill()

# 画眼睛
def draw_eyes():
    eye1.fillcolor("#ffffff")
    eye1.penup()
    ellipspos(60, 40, 360, 150, 98, 70, 30, eye1)
    
    eye1.begin_fill()
    ellips(60, 40, 360, 150, 98, 70, 30, False, False, eye1)
    eye1.end_fill()
    
    eye2.fillcolor("#ffffff")
    ellipspos(60, 40, 360, 150, 82, -70, 30, eye2)
    
    eye2.begin_fill()
    eye2.penup()
    ellips(60, 40, 360, 150, 82, -70, 30, False, False, eye2)
    eye2.end_fill()
    draw_eyesball("#003333","#000000","#ffffff")    # 眼白,瞳孔,反光


# 画嘴
def draw_mouth(color):
    mouth.fillcolor(color)
    ellipspos(30, 8, 360, 150, 90, 0, -40, mouth)
    mouth.begin_fill()
    ellips(30, 8, 360, 150, 90, 0, -40, False, False, mouth)
    mouth.end_fill()
    
    mouth.penup()
    mouth.goto(-6, -19)
    mouth.pendown()
    mouth.fillcolor("#ffffff")
    mouth.begin_fill()
    for i in range(3):
        mouth.forward(12)
        mouth.left(120)
    mouth.end_fill()



# 画脸颊
def draw_cheeks(color):
    cheek.fillcolor(color)
    cheek.pencolor("#ffcccc")
    cheek.penup()
    cheek.goto(-90, -52)
    cheek.pendown()
    cheek.begin_fill()
    cheek.circle(17)
    cheek.end_fill()
    
    cheek.penup()
    cheek.goto(90, -52)
    cheek.pendown()
    cheek.begin_fill()
    cheek.circle(17)
    cheek.end_fill()


# 画眉毛
def draw_eyebrows():
    turtle.color("#000000")
    turtle.pensize(3)
    turtle.penup()
    turtle.goto(-75, 100)
    turtle.pendown()
    turtle.seth(35)
    turtle.forward(40)
    turtle.seth(-80)
    turtle.forward(10)
    
    turtle.penup()
    turtle.goto(75, 100)
    turtle.pendown()
    turtle.seth(145)
    turtle.forward(40)
    turtle.seth(260)
    turtle.forward(10)

# 画星星
x = turtle.window_width() / 3
y = turtle.window_height() / 2
def goto_place():
    turtle.penup()
    x_1 = random.uniform(-x, x)
    y_1 = random.uniform(0, y)
    turtle.goto(x_1, y_1)
    turtle.pendown()

def place_of_star():
    y = turtle.ycor()
    return y

def change_place():
    turtle.penup()
    x_1 = random.uniform(-x, x)
    y_1 = random.uniform(200, y)
    turtle.goto(x_1, y_1)
    turtle.pendown()

def draw_star(c):
    step = random.randint(20, 30)
    turtle.color(c)
    turtle.setheading(random.randint(1, 180))
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(step)
        turtle.right(144)
    turtle.end_fill()


# 画可爱的专属蛋君
# 画身体
draw_body("white")
# 画脸
draw_face("royalblue")
# 画手
draw_hands("white")
# 画嘴
draw_mouth("brown")
# 画脸颊
draw_cheeks("pink")
# 画眼睛
draw_eyes()
# 画眉毛
draw_eyebrows()


好了,这就是主要代码,还有一个附赠的小MOD,可以画星星!!!

for _ in range(100):
    # 去某一个位置
    goto_place()
    # 如果星星的位置低于200
    if place_of_star() < 200:
        # 那么就换个位置
        change_place()
    # 画星星
    draw_star("yellow")


好啦同志们,这一期文章就到这里了,我们下期见啦!!!

本站作者已申明原创,禁止转载!

文章内容属作者个人观点,不代表本站立场,如有侵权立删。

   口袋儿题库-青少儿编程自测题库