First joy with Python – Turtle Graphics


If I can still remember, Turtle Graphics has been taught by my D&T (Design and Technology) teacher at secondary school. I thought it was stupid and the GUI is ugly. I knew nothing about programming, and certainly got very little thing to play with Turtle Graphics. I moved it “forward", “rotate" and nothing else.

In Python, Turtle is in 2D space and represented by a Triangle (What?)

But now, I am studying Python and find it rocks. It rocks with its easy-to-program feature, it rocks with tons of modules, and it rocks with Turtle! I have absolutely no pain in studying Python and programming with it. It’s not a language for programming geeks, but also for the newbies. Every statements are just like natural language, (Python is a so-high level programming language). Among its differences from other common programming language in the aspect of statements, its indention-as-structure feature is the most astounding.

Indentation can be used as building up the program structure? I thought it was just for easy reading for programmers, but in Python, it replaces the “BEGIN…END" in Pascal, and “{…}" brackets in C language. WOW! What’s more? I only need the colon to tell Python where I starts a statement section.

Turtle Graphics – Drawing an eye

2 Eyes actually →.→

(Very simple program with no efficiency taken into consideration)

As I have just began my study in Python, this post will be follow by fun updates in Python.

Great Sites for learning Python

  1. http://learnpythonthehardway.org/book/
  2. http://docs.python.org/3.3/tutorial/index.html

And, the codes, I have almost forgotten ~

import turtle
turtle.reset()

turtle.speed(0) #var
px = 4 #var
turtle.color("purple")

#####This is the first enevelop
x_bot = -400
y_bot = -400 #constant
x_up = -400 #constant
y_up = 410
while ((x_bot < 400) and (y_up > -400)):
x_bot += px
y_up -= px
turtle.up()
turtle.goto(x_bot, y_bot)
turtle.down()
turtle.goto(x_up, y_up)
x_bot += px
y_up -= px
turtle.up()
turtle.goto(x_up, y_up)
turtle.down()
turtle.goto(x_bot, y_bot)

turtle.goto(400, -400)

#####This is the second enevelop
x_rgt = 400 #constant
y_rgt = -400 
x_top = 400 
y_top = 400 #constant
while ((y_rgt < 400) and (x_top > -400)):
x_bot += px
y_up -= px
turtle.up()
turtle.goto(x_rgt, y_rgt)
turtle.down()
turtle.goto(x_top, y_top)
y_rgt += px
x_top -= px
turtle.up()
turtle.goto(x_top, y_top)
turtle.down()
turtle.goto(x_rgt, y_rgt)

#####Drawing the eyeball
turtle.up()

turtle.goto(0, -200)
turtle.begin_fill()
turtle.fillcolor("blue")
turtle.circle(200)
turtle.end_fill()

turtle.goto(-90, 70)
turtle.begin_fill()
turtle.fillcolor("white")
turtle.circle(30)
turtle.end_fill()

turtle.goto(50, -180)
turtle.begin_fill()
turtle.fillcolor("white")
turtle.circle(120)
turtle.end_fill()

塗牆

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s