美文网首页从零开始学Python
用Python编个海龟赛跑小程序

用Python编个海龟赛跑小程序

作者: iLester | 来源:发表于2018-06-05 12:33 被阅读0次

    源代码:

    #!/bin/python3
    from turtle import *
    from random import randint
    
    speed(100)
    
    penup()
    goto(-140,140)
    for step in range(15):
      write(step,align="center")
      right(90)
      forward(10)
      pendown()
      trigger = -1
      for step2 in range(15):
        trigger = trigger * -1
        if trigger == 1:
          pendown()
        else:
          penup()
        forward(10)
      penup()
      backward(160)
      left(90)
      forward(20)
    
    #ada the red turtle  
    ada = Turtle()
    ada.color('red')
    ada.shape('turtle')
    
    ada.penup()
    ada.goto(-160, 100)
    ada.pendown()
    
    
    # bob the blue turtle
    bob = Turtle()
    bob.color('blue')
    bob.shape('turtle')
    
    bob.penup()
    bob.goto(-160, 70)
    bob.pendown()
    
    # carl the yellow turtle
    carl = Turtle()
    carl.color('yellow')
    carl.shape('turtle')
    
    carl.penup()
    carl.goto(-160, 40)
    carl.pendown()
    
    # david the green turtle
    david = Turtle()
    david.color('green')
    david.shape('turtle')
    
    david.penup()
    david.goto(-160, 10)
    david.pendown()
    
    for turn in range(10):
      ada.right(36)
      bob.right(36)
      carl.right(36)
      david.right(36)
    
    for turn in range(100):
      ada.forward(randint(1,5))
      bob.forward(randint(1,5))
      carl.forward(randint(1,5))
      david.forward(randint(1,5))  
    
    

    效果图:


    相关文章

      网友评论

        本文标题:用Python编个海龟赛跑小程序

        本文链接:https://www.haomeiwen.com/subject/glupsftx.html