Python系列 | Turtle绘图学习之羽毛球随机点训练场

发布于:2022-12-22 ⋅ 阅读:(190) ⋅ 点赞:(0)

绘图之前先要安装turtle模块:
python 2:

pip install turtle

python 3:

pip3 install turtle

绘图思路:
首先绘制出外正方形和内线,然后使用dot绘制出九个跑位的点,记录每个点的x和y轴,写入死循环和if判断条件判断是否为中圆点并闪烁。

效果图展示:

请添加图片描述

效果图代码:

#woshinsy
import turtle
import time
import random
turtle.title("woshinsy python homework")
# 窗口大小
turtle.bgcolor("#367847")
# 大圈
def bcir():
    turtle.dot(65,"yellow")

# 小圈
def cir():
    turtle.dot(45, "white")

# 横纵坐标
def tp(x,y):
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()

# 球场设置
def square():
    turtle.color("white")
    for i in range(4):
        turtle.pensize(10)
        turtle.forward(500)
        turtle.left(90)

#隐藏箭头
turtle.speed(9)
turtle.hideturtle()
tp(-250,-250)
square()
tp(-250,125)
turtle.forward(500)
tp(0,120)
turtle.left(-90)
turtle.forward(370)
tp(0,0)
bcir()
# 上1
tp(-250,250)
cir()
# 上2
tp(0,250)
cir()
# 上3
tp(250,250)
cir()
# 中1
tp(-250,0)
cir()
# 中3
tp(250,0)
cir()
# 下1
tp(-250,-250)
cir()
# 下2
tp(0,-250)
cir()
# 下3
tp(250,-250)
cir()

def gogo():
    arr = [[0,0],[-250,250],[0,250],[250,250],[-250,0],[250,0],[-250,-250],[0,-250],[250,-250]]
    while True:
        ran = random.randint(0,8)
        if ran ==0:
            tp(0,0)
            turtle.dot(65, "red")
            time.sleep(2)
            turtle.undo()
        else:
            tp(arr[ran][0],arr[ran][1])
            turtle.dot(45,"red")
            time.sleep(2)
            turtle.undo()

gogo()

# 等待用户关闭
turtle.exitonclick()

网站公告

今日签到

点亮在社区的每一天
去签到