最近我在學習Python,今天我寫了一段代碼,可以讓小黃雞打球,非常有趣呢!
import turtle # 設置小黃雞 def set_chicken(): turtle.speed(0) turtle.penup() turtle.goto(0, -150) turtle.pendown() turtle.color("yellow") turtle.begin_fill() turtle.circle(150) turtle.end_fill() # 設置網(wǎng)和球 def set_ground(): turtle.speed(0) turtle.penup() turtle.goto(-300, -200) turtle.pendown() turtle.forward(600) turtle.penup() turtle.goto(0, -200) turtle.pendown() turtle.circle(100) turtle.penup() turtle.goto(0, 0) turtle.color("white") turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 小黃雞打球 def chicken_play_ball(): turtle.penup() turtle.goto(0, -50) turtle.pendown() turtle.right(45) turtle.speed(20) for i in range(30): turtle.right(12) turtle.forward(20) turtle.right(180) for i in range(30): turtle.right(12) turtle.forward(20) turtle.right(135) turtle.forward(200) set_chicken() set_ground() chicken_play_ball() turtle.done()
在代碼中,我使用了turtle模塊,設置小黃雞的位置和形狀,然后畫出了球和網(wǎng)。最后,我讓小黃雞打起了球,能夠非常好地練習其轉彎能力。運行代碼后,就可以看到小黃雞靈活地打起了球,非常有趣呢!