Python วาดรูปซานต้า

Santa claus using Python turtle

เรียนโค้ดดิ้งให้สนุก การเรียนเขียนโปรแกรมด้วยศิลปะ “Python Turtle” วันนี้จะมาแบ่งปั่นบทเรียน
ที่ได้สอนเด็ก ๆ กันครับ วันนี้จะ “ไพทอน วาดรูปซานตาคลอส ” กันครับ

ขันตอนแรก ต้อง Import Turtle เข้ามาในโปรแกรมก่อน

Python
import turtlent = turtle.Turtle()
screen = turtle.Screen ()
t.title('Python Santa Claus')
t.setup(430,680, startx=450, starty=300)

ในขั้นตอนนี้ เราอิมพอร์มโมดูล Turtle เข้ามาแล้ว ต้องตั้งชื่อว่า “t” ให้เต่า เพื่อความง่าย สั้นและกระชับ
เราเรียกขั้นตอนนี้ว่า instance object หรือ การสร้างตัวแปรจากคลาส Turtle (Class)
กำหนดขนาดของหน้าต่าง window ขนาด 430X680 จุดเริ่มต้นคือตำแหน่ง (x,y) 450,300

ขันตอนที่ 2: วาดหน้า Face

Python
# face
t.goto(-3,0)
t.pendown()
for n in range(18):
    t.width(40)
    t.right(20)
    t.color('white')
    t.forward(90)
    t.backward(90)
t.penup()
t.goto(80,-5)
t.color(234,214,207)
t.width(1)
t.pendown()
t.begin_fill()
t.seth(90)
t.circle(80,180)
t.end_fill()

ขันตอนที่ 3: วาดตา eyes

Python
#left
t.penup()
t.goto(-15,15)
t.seth(270)
t.color('black')
t.pendown()
t.dot(12)
#right
t.penup()
t.goto(15,15)
t.seth(270)
t.color('black')
t.pendown()
t.dot(12)

ขันตอนที่ 3: วาดจมูก

Python
#note
t.penup()
t.color(243, 195, 173)
t.goto(0,-8)
t.seth(0)
t.dot(35)

ขันตอนที่ 3: วาดหมวก

Python
#shadow
t.goto(-70,35)
t.color('gainsboro')
for i in range(7):
    t.begin_fill()
    t.seth(0)
    t.circle(20)
    t.forward(25)
    t.end_fill()

#Trim on Hat
t.penup()
t.goto(-70,40)
t.color('white')
for i in range(7):
    t.begin_fill()
    t.seth(0)
    t.circle(20)
    t.forward(25)
    t.end_fill()

#Red Part of Hat
t.penup()
t.goto(-90, 65)
t.pendown()
t.color("red")
t.begin_fill()
for i in range(3):
    t.forward(190)
    t.left(120)
t.end_fill()

#Pom Pom
t.penup()
t.goto(5, 240)
t.pendown()
t.color("white")
t.dot(25)

โค้ดทั้งหมด

Python
# santa Cluse
import turtle as t
t.Screen().bgcolor('cyan')
t.hideturtle()
t.title('Python Santa Cluase')
t.setup(430,680, startx=450, starty=300)
t.colormode(255)

# face
t.goto(-3,0)
t.pendown()
for n in range(18):
    t.width(40)
    t.right(20)
    t.color('white')
    t.forward(90)
    t.backward(90)
t.penup()
t.goto(80,-5)
t.color(234,214,207)
t.width(1)
t.pendown()
t.begin_fill()
t.seth(90)
t.circle(80,180)
t.end_fill()

#eye
t.penup()
t.goto(-15,15)
t.seth(270)
t.color('black')
t.pendown()
t.dot(12)
#right
t.penup()
t.goto(15,15)
t.seth(270)
t.color('black')
t.pendown()
t.dot(12)

#note
t.penup()
t.color(243, 195, 173)
t.goto(0,-8)
t.seth(0)
t.dot(35)

#shadow
t.goto(-70,35)
t.color('gainsboro')
for i in range(7):
    t.begin_fill()
    t.seth(0)
    t.circle(20)
    t.forward(25)
    t.end_fill()

#Trim on Hat
t.penup()
t.goto(-70,40)
t.color('white')
for i in range(7):
    t.begin_fill()
    t.seth(0)
    t.circle(20)
    t.forward(25)
    t.end_fill()

#Red Part of Hat
t.penup()
t.goto(-90, 65)
t.pendown()
t.color("red")
t.begin_fill()
for i in range(3):
    t.forward(190)
    t.left(120)
t.end_fill()

#Pom Pom
t.penup()
t.goto(5, 240)
t.pendown()
t.color("white")
t.dot(25)