接口测试 java 线程池 unity cassandra menu cocos2d html5 vue路由 oracle修改字段默认值 mysql重新初始化 python程序界面 quartz配置 mysql或者条件 mysql函数 python数据 windows搭建python开发环境 简单python脚本实例 javaindexof java中string java入门教程 java类型 java开发环境配置 java运算 java中new java写入txt javastringbuilder linux安装教程 linux文档 sql语句大全实例教程 脚本之家 简体中文语言包 圆形截图 python的用途 pr转场特效下载 心理学与生活pdf 按键精灵脚本教程 cfqq网吧任务 网络适配器下载 电脑基础 js刷新页面 mysql使用教程
当前位置: 首页 > 学习教程  > 编程语言

芮瑶学编程-绘制红绿灯

2020/10/8 20:25:36 文章标签:

import turtleturtle.speed(0) # 绘制灯箱 turtle.pensize(5) turtle.forward(100) turtle.left(90) turtle.forward(200) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(200) turtle.left(90)# 绘制第一个灯,红灯 turtle.penup() turtle.go…

import turtle

turtle.speed(0)
# 绘制灯箱
turtle.pensize(5)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)

# 绘制第一个灯,红灯
turtle.penup()
turtle.goto(50,200-35)
turtle.pendown()
turtle.dot(50,"red")

# 绘制第二个灯,黄灯
turtle.penup()
turtle.goto(50,200-95)
turtle.pendown()
turtle.dot(50,"yellow")

# 绘制第三个灯,绿灯
turtle.penup()
turtle.goto(50,200-155)
turtle.pendown()
turtle.dot(50,"green")

# 写字
turtle.penup()
turtle.goto(0,-60)
turtle.pendown()
turtle.write("交通灯",font=("Arial",20,"normal"))

turtle.hideturtle()

turtle.done()

 

继续优化,可以加上倒计时,模拟十字路口,红灯停绿灯行黄灯等一等的效果

import turtle
import time

turtle.speed(0)
turtle.hideturtle()

# 写字
turtle.penup()
turtle.goto(0,-60)
turtle.pendown()
turtle.write("交通灯",font=("Arial",20,"normal"))
turtle.penup()
turtle.goto(0,0)
turtle.pendown()



# 绘制灯箱
turtle.pensize(5)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(200)
turtle.left(90)

# 绘制第一个灯,红灯
turtle.penup()
turtle.goto(50,200-35)
turtle.pendown()
turtle.dot(50,"red")

# 等待5秒,红灯灭
time.sleep(5)
turtle.penup()
turtle.goto(50,200-35)
turtle.pendown()
turtle.dot(50,"white")


# 绘制第二个灯,黄灯
turtle.penup()
turtle.goto(50,200-95)
turtle.pendown()
turtle.dot(50,"yellow")

# 等待2秒,黄灯灭
time.sleep(3)
turtle.penup()
turtle.goto(50,200-95)
turtle.pendown()
turtle.dot(50,"white")


# 绘制第三个灯,绿灯
turtle.penup()
turtle.goto(50,200-155)
turtle.pendown()
turtle.dot(50,"green")



turtle.done()

 


本文链接: http://www.dtmao.cc/news_show_250289.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?