python实现红绿灯

import threading,time
import randomdef light():if not event.isSet(): #如果没有设定eventevent.set() #wait就不阻塞 #绿灯状态count = 0while True:if count < 10:print("\033[42;1m--green light on--\033[0m")elif count <13:print("\033[43;1m--yellow light on --\033[0m")elif count <20:if event.isSet():event.clear()print("\033[41;1m--red light on-- \033[0m")else:count = 0event.set() #打开绿灯time.sleep(1)count +=1
# def car(n):
#     while 1:
#         time.sleep(random.randrange(10))
#         if event.isSet(): #绿灯
#             print("car [%s] is running.." %n)
#         else:
#             print("car [%s] is waiting for the red light.." %n)
def car(n):    #no bug versionwhile 1:time.sleep(1) #让车慢点if event.isSet(): #绿灯print("car [%s] is running.." %n)else:print("car [%s] is waiting for the red light.." %n)event.wait() #不断检查flag有没有被设定,如果没有设定就等着,这个是输入event等待时间,可以精确到毫秒级
if __name__ == "__main__":event = threading.Event()Light = threading.Thread(target=light)Light.start()for i in range(3):t = threading.Thread(target=car,args=(i,))t.start()

转载于:https://www.cnblogs.com/Presley-lpc/p/9998128.html

效果图

python实现红绿灯相关推荐

  1. python程序红绿灯识别_【2020】Python语言编程实现简易红绿灯功能

    作为树莓派编程的"黄金搭档",Python语言是目前正在广泛使用的通用高级编程语言,使用Python语言编程进行创客实验已经成为树莓派最为强势的应用之一. "简易红绿灯& ...

  2. python turtle红绿灯_【Python】类的组合案例(模拟街道红绿灯)

    importtimeimportosfrom colorama importinit,Fore,Back,Style#命令行模式字体颜色初始化 init(autoreset=True)#电子屏类 cl ...

  3. 用Python识别红绿灯

    安装包 import cv2 import numpy as np import time 导入视频,并且自定义 cor_x, cor_y = -1, -1 # fourcc = cv2.VideoW ...

  4. 一起学Python——自助红绿灯程序设计

    一.按键监测程序代码: import turtle import time #绘制圆,等待2秒,自动清除 def cs(): t.fd(200) t.circle(50) time.sleep(2) ...

  5. 用python做红绿灯_Python利用Event类模拟红绿灯

    #利用Event类模拟红绿灯 import threading import time event = threading.Event() def lighter(): count = 0 event ...

  6. python识别红绿灯

    1.导入所需要的库 import cv2 import numpy as np 2.视频读取 cap = cv2.VideoCapture('红绿灯.mp4') while True:     ret ...

  7. python程序红绿灯识别_使用python创建红绿灯

    例如,函数被分配给Radiobutton,但您可以将函数分配给self.colorfrom Tkinter import * class TrafficLights: def __init__(sel ...

  8. Python识别红绿灯代码

    先导入数据所需要的库 import cv2 import numpy as np cap = cv2.VideoCapture('honglvdeng.mp4') # 读取视频 fps = cap.g ...

  9. python判断红绿灯

    import cv2 import numpy as npcap = cv2.VideoCapture(r"C:\Users\吴金松\Videos.mp4") # 读取视频 fps ...

最新文章

  1. 奥巴马竟被「去马赛克 AI」洗白,CVPR 新研究惹上种族歧视大麻烦,LeCun 也被卷入其中...
  2. java文件分割合并_java实现文件分割与合并 类示例源码
  3. Linux下使用expect实现跳板机自动跳转/免密登录/自动登录(转)
  4. chardet坑——比蜗牛还慢
  5. C和C++中的整形数据类型
  6. [linux] sftp/ssh异常:ssh_selinux_copy_context getcon failed with No such file or directory
  7. C++ map注意事项
  8. [html] iframe父页面如何获取子页面的元素?
  9. python csv 排序_python数据清洗
  10. 店铺如何用视觉走出差异化?
  11. 软考信息系统项目管理师2021_信息化与信息系统_项目管理阶段_方法_网络协议---软考高级之信息系统项目管理师003
  12. 中国农业种植施肥机械行业市场供需与战略研究报告
  13. PHP数组json占用内存,php - 从json“连接”多个数组 - 堆栈内存溢出
  14. 幽冥问答录:兼职阴间判官介绍阴间是什么样子
  15. 计算机模拟学,深度学习是让计算机模拟
  16. ios 启动图一键生成工具_APP启动图生成
  17. 有向图和无向图转化为邻接表后链表中结点个数
  18. ubuntu16.04 安装hustoj 最新【亲测,附带常见问题解决】
  19. [ NeurIPS 2020 ] 一叶知秋 —— 基于“单目标域样本”的领域自适应方法
  20. Leaflet中Maker碰撞监测

热门文章

  1. java识别不了欧元 符号_java – 使用unicode显示欧元符号并将字符更改为大写
  2. Android手机显示的4G信号强度的格子数
  3. 关于QQ音乐的音乐下载
  4. Kali Linux渗透测试 106 离线密码破解
  5. IMBF170R1K0M1XTMA1晶体管 1700V N-CH SICFET
  6. 发那科点焊机器人故障处理方法
  7. Android炫酷效果集锦
  8. Window 7 查看电脑网卡是千兆还是百兆
  9. 记一次管道流的问题定位过程
  10. 小程序技术始于微信?来看看移动端小程序技术的前世今生!