这次我们做的是各种各样的汽车,再让他们跑起来,所以创造一个车的class,因为所有的车都有很多共同点。比如说,他们都有颜色,品牌和形状。

我们先画静态的,用绝对坐标画。绝对坐标是不能改变的坐标,像157, -200。然后为了让车子动起来,我们需要把绝对坐标变成相对坐标。首先选定一个绝对坐标当作画这个图形的初始点。相对坐标是能改变的坐标,像x,y。然后再用初始值表示剩下的坐标用来画图。改完以后,我们又把初始坐标x加比如说10,然后再把屏幕清空,去下一个点在画一遍。这样,如果画得够快,就可以向动画一样,车就可以走了。

但是画完以后出现了一个问题,车是一边转圈一边走的,因为画完一个图形以后,需要调整往哪画,因为画完后turtle箭头有可能是朝右,但是再画的时候有可能需要向左画,这时候就得用 t.left() 或者t.right()来重置光标的方向,车就不会翻来翻去。

P.S.这次写的有点长,因为画图形的时候需要更多的程序

import turtle

class Car(object): #我们定义了一个类,叫Car

 def __init__(self, owner, color, brand, shape): #定义了一个构造方法,只有init是构造方法

   self.owner = owner #self指这个类自己,owner,color,brand,和shape都是公共属性

   self.color = color

   self.brand = brand

   self.shape = shape

 def description(self): #定义了一个方法,描述自己的方法

   print("I'm a %s %s car and I shaped like a %s." % (self.color, self.brand, self.shape))

 def whose(self):

   print("And I'm %s's car. " % (self.owner))

 def draw(self): #定义了一个方法,画自己的方法

   t = turtle.Turtle()

   t.speed(0) #画的速度

   t.delay(1) #延迟的时间

   if self.shape == "rectangle": #如果形状长方形:

     x = -350 #给x一个初始值

     y = 300 #给y一个初始值

     while x <= 400: #如果x小于等于400:

       t.penup()

       t.goto(x,y)

       t.pendown()

       t.fill(True)

       t.fillcolor(self.color)

       t.backward(100)

       t.right(90)

       t.forward(40)

       t.left(90)

       t.forward(100)

       t.left(90)

       t.forward(40)

       t.fill(False)

       t.penup()

       t.goto(x - 63,y - 50)

       t.pendown()
       t.fill(True)

       t.circle(10)

       t.fill(False)

       t.penup()

       t.goto(x - 23,y - 50)

       t.pendown()

       t.fill(True)

       t.circle(10)

       t.fill(False)

       t.penup()

       t.goto(x - 65,y - 90)

       t.write( self.owner + "'s " + self.brand, font=("Arial", 20, "normal")) #写self.owner,然后再写's ,最后再写self.brand

       t.clear() #把画的东西清空

       x = x + 10 #把x + 10的值给x

       t.right(90) #调整角度,向右转90度

   elif self.shape == "triangle": #如果形状三角形:

     x = -450#给x一个初始值

     y = 0 #给y一个初始值

     while x <= 400: #如果x小于等于400:

       t.penup()

       t.goto(x,y)

       t.pendown()

       t.fill(True)

       t.fillcolor(self.color)

       t.forward(100)

       t.left(120)

       t.forward(100)

       t.left(120)

       t.forward(100)

       t.fill(False)

       t.penup()

       t.goto(x + 20,y - 5)

       t.pendown()

       t.fill(True)

       t.fillcolor(self.color)

       t.circle(10)

       t.fill(False)

       t.penup()

       t.goto(x + 60,y - 5)

       t.pendown()

       t.fill(True)

       t.fillcolor(self.color)

       t.circle(10)

       t.fill(False)

       t.penup()

       t.goto(x,y - 50)

       t.write( self.owner + "'s " + self.brand, font=("Arial", 20, "normal"))#写self.owner,然后再写's ,最后再写self.brand

       t.clear() #把画的东西清空

       x = x + 25 #把x + 25的值给x

       t.left(120)#调整角度,向左转0度

   elif self.shape == 'circle':

     x = -400

     y = -250

     while x <= 400:

       t.penup()

       t.goto(x,y)

       t.pendown()

       t.fill(True)

       t.fillcolor('red')

       t.circle(43)

       t.fill(False)

       t.penup()

       t.goto(x - 18,y - 18)

       t.pendown()

       t.fill(True)

       t.fillcolor('red')

       t.circle(10)

       t.fill(False)

       t.penup()

       t.goto(x + 18,y - 18)

       t.pendown()

       t.fill(True)

       t.fillcolor('red')

       t.circle(10)

       t.fill(False)

       t.penup()

       t.goto(x - 50,y - 50)

       t.write( self.owner + "'s " + self.brand, font=("Arial", 20, "normal"))

       t.clear()

       x = x + 100

candy = Car('Candy', 'blue', 'BMW', 'rectangle')

jerry = Car('Jerry', 'red', 'Ford', 'circle')

tina = Car('Tina', 'green', 'Toyota', 'triangle')

candy.description()

candy.whose()

candy.draw()

jerry.description()

jerry.whose()

jerry.draw()

tina.description()

tina.whose()

tina.draw()