Python通過類的組合模擬街道紅綠燈
一,紅綠燈揭示板案例思路
1. 建立Traffic_light紅綠燈類
(1)靜態屬性 :
<1> 綠燈時間,<2> 黃燈時間,<3> 紅燈時間, <4> 兩塊顯示時間的電子屏
(2)動態屬性
<1> 輸入紅黃綠時間函式(靜態函式),<2> 紅黃綠時間倒計時函式 ,
<3> 構造電子屏數字的顯示函式,<4> 顯示兩塊電子屏繫結兩位數的顯示函式
<5> 例項化物件展示電子屏函式
2. 電子屏類的建立(Light):
python中沒有陣列,因此自己建立函式把獲取到的值存放到陣列中
(存放內容: 20行,10列的布林值)
3. input_time(color:str)函式的建立
<1> 匯入colorama包並初始化實現windows命令列下顏色字型列印效果
<2> 輸入紅黃綠時間的字型成對應的顏色
<3> 通過colorama類方法實現輸入的紅黃綠時間為對應的顏色展示
<4> 對輸入的數字進行校驗(必須為1-99之間的正數。因為一塊電子屏只記錄一位數字)
<5> 返回相應的值
4. Countdown數字倒計時函式的建立
<1> 通過while迴圈讓三個燈的狀態一直迴圈持續
<2> 對於紅黃綠燈輸入的數字進行遞減列印流程如下#流程: 清屏-->列印完後 -->暫停1秒鐘-->清屏 -->數字減一後再列印-->再暫停1秒鐘-->清屏-->再數字減一列印
<3> 匯入time,os,colorama等需要的包
5.build_LED_number函式的建立
之前建立的電子屏是預設False的狀態。分別構造0-9的狀態在電子屏中True的狀態的顯示
6.print_LED函式的建立
兩塊電子屏,分別顯示輸入時間的第一位和第二位數字.如果數字為單數則前面用零補齊的方法顯示。兩塊屏並排顯示每一位數字,從而展示電子版的效果
7.注意事項:
因為我們用到了os,及colorama類。所以最終效果的展示不是在pycharm中展示。而是在windows的cmd命令列中展示。
原因是因為我們程式碼中呼叫了os.system("cls")這個清屏命令。在pycharm中是很難做到清屏的效果。
另外在pycharm中對於電子屏的展示效果也不如windows cmd中展示的效果俱佳。因此執行程式是請在windows命令列中執行。
二,紅綠燈揭示板程式碼的呈現
import time import os from colorama import init,Fore,Back,Style #命令列模式字型顏色初始化 init(autoreset=True) #電子屏類 class Light: #建構函式 def __init__(self): self.light = [] #儲存行列陣列的集合 #自動初始化 self.prepare_light() def prepare_light(self): """ 電子屏的建立 python中沒有陣列.因此通過類,函式來建立陣列得到一個20行10列的陣列 :return: """ for row in range(20): #20行 temp = [] # 臨時儲存每行10個圈 for col in range(10): #10列 temp.append(False) #預設燈都是不亮的因此通過布林型別的False表示不亮的狀態 #把行列排的200個燈的狀態存入到light集合中 self.light.append(temp) #紅綠燈類 class Traffic_light: #建構函式,靜態屬性 def __init__(self,green_time,yellow_time,rea_time): self.green_time = green_time #綠燈時間 self.yellow_time = yellow_time #黃燈時間 self.red_time = rea_time #紅燈時間 #通過類的組合呼叫Light類函式 self.number01 = Light() #建立第一個電子屏 self.number02 = Light() #建立第二個電子屏 #紅黃綠等時間倒計時函式 def countdown(self): while True: #流程: 清屏-->列印完後 -->暫停1秒鐘-->清屏 -->數字減一後再列印-->再暫停1秒鐘-->清屏-->再數字減一列印 for number in range(self.green_time,-1,-1): #第一個-1代表取值到0,如果設定0則取值取不到0.第二個-1代表數字減一 os.system("cls") #清屏 self.start_display(number,"green") #呼叫start_display函式傳數字及顏色 time.sleep(1) #停止一秒鐘 # 黃燈倒計時 for number in range(self.yellow_time,-1): os.system("cls") #清屏 self.start_display(number,"yellow") time.sleep(1) #停止一秒鐘 # 紅燈倒計時 for number in range(self.red_time,-1):#第一個-1代表取值到0,"red") time.sleep(1) #停止一秒鐘 @staticmethod #靜態方法不需要初始化 def input_time(color:str): # 設定全域性變數(便於靜態方法使用) time = "" while True: if color.lower() in ["green","綠色","綠","綠燈"]: print(Fore.GREEN + "請輸入綠燈的時間:",end="") #實現列印字型呈現顏色效果 time = input() if color.lower() in ["yellow","黃色","黃","黃燈"]: print(Fore.YELLOW + "請輸入黃燈的時間:",end="") time = input() if color.lower() in ["red","紅色","紅","紅燈"]: print(Fore.RED + "請輸入紅燈的時間:",end="") time = input() #校驗輸入的是否合規 if not time.isdigit(): print("輸入的值不符合要求。【要求:必須是1-99之間的正數。】") continue else: time_number = int(time) # 因為time是字串.拿到數字後轉成Int型別再判斷 if time_number < 1 or time_number > 99: print("輸入的值不符合要求。【要求:必須是1-99之間的正數。】") continue else: return time_number def build_LED_number(self,char:str): """ :param char: LED燈數字的構造 :return: 返回temp_LED這個陣列 """ temp_LED = Light() #臨時建立新的陣列 if char == "0": #構造0 for row in range(20): for col in range(10): if row < 2: #最上面兩列 temp_LED.light[row][col] = True if row > 17: #最下面兩列 temp_LED.light[row][col] = True if col < 2:#最左邊兩列 temp_LED.light[row][col] = True if col > 7: #最後面兩列 temp_LED.light[row][col] = True elif char == "1": #構造1 for row in range(20): for col in range(10): if col > 7: #最後面兩列 temp_LED.light[row][col] = True elif char == "2": #構造2 for row in range(20): for col in range(10): if row < 2: # 最上面兩列 temp_LED.light[row][col] = True if col > 7 and row < 9: # 最後面兩列 temp_LED.light[row][col] = True if row == 9 or row == 10: # 中間兩行 temp_LED.light[row][col] = True if col < 2 and row >10: #左邊列 temp_LED.light[row][col] = True if row > 17: # 最下面兩列 temp_LED.light[row][col] = True elif char == "3": #構造3 for row in range(20): for col in range(10): if row < 2: # 最上面兩列 temp_LED.light[row][col] = True if col > 7 : # 最後面兩列 temp_LED.light[row][col] = True if row == 9 or row == 10: # 中間兩行 temp_LED.light[row][col] = True if row > 17: # 最下面兩列 temp_LED.light[row][col] = True elif char == "4": # 構造4 for row in range(20): for col in range(10): if col < 2 and row <9: # 最上面兩列 temp_LED.light[row][col] = True if col > 7: # 最後面兩列 temp_LED.light[row][col] = True if row == 9 or row == 10: # 中間兩行 temp_LED.light[row][col] = True elif char == "5": # 構造5 for row in range(20): for col in range(10): if row < 2: temp_LED.light[row][col] = True if col < 2 and row < 9: temp_LED.light[row][col] = True if row == 9 or row == 10: temp_LED.light[row][col] = True if col > 7 and row > 10: temp_LED.light[row][col] = True if row > 17: temp_LED.light[row][col] = True elif char == "6": # 構造6 for row in range(20): for col in range(10): if row < 2: temp_LED.light[row][col] = True if col < 2: temp_LED.light[row][col] = True if row == 9 or row == 10: temp_LED.light[row][col] = True if col > 7 and row > 10: temp_LED.light[row][col] = True if row > 17: temp_LED.light[row][col] = True elif char == "7": # 構造7 for row in range(20): for col in range(10): if row < 2: temp_LED.light[row][col] = True if col > 7: temp_LED.light[row][col] = True elif char == "8": #構造8 for row in range(20): for col in range(10): if row < 2: #最上面兩列 temp_LED.light[row][col] = True if row > 17: #最下面兩列 temp_LED.light[row][col] = True if row == 9 or row == 10: # 中間兩行 temp_LED.light[row][col] = True if col < 2:#最左邊兩列 temp_LED.light[row][col] = True if col > 7: #最後面兩列 temp_LED.light[row][col] = True elif char == "9": # 構造9 for row in range(20): for col in range(10): if row < 2: # 最上面兩列 temp_LED.light[row][col] = True if col < 2 and row < 9: temp_LED.light[row][col] = True if row > 17: # 最下面兩列 temp_LED.light[row][col] = True if row == 9 or row == 10: # 中間兩行 temp_LED.light[row][col] = True if col > 7: # 最後面兩列 temp_LED.light[row][col] = True #返回值 return temp_LED def print_LED(self,color:str): for row in range(20): #列印第一個數 for col01 in range(10): if self.number01.light[row][col01] == True: if color == "green": print(Fore.GREEN + "●",end="") elif color == "yellow": print(Fore.YELLOW + "●",end="") elif color == "red": print(Fore.RED + "●",end="") else: print(" ",end="") # 兩個全形空格 註釋:○佔用的字元相當於兩個全形空格的佔位 print("\t",end="") #列印第二個數 for col02 in range(10): if self.number02.light[row][col02] == True: if color == "green": print(Fore.GREEN + "●",end="") #換行 print() def start_display(self,number:int,color:str): """ 電子屏展示 :param number:電子屏上展示的數字 :param color: 電子屏上展示的顏色 :return: """ number_str = "%02d" % number #傳進來的數字2位顯示 self.number01 = self.build_LED_number(number_str[0]) #把數字的第一位給第一個電子屏 self.number02 = self.build_LED_number(number_str[1]) #把數字的第二位給第二個電子屏 #在電子屏上顯示 self.print_LED(color) if __name__ == "__main__": green_time = Traffic_light.input_time("綠燈") yellow_time = Traffic_light.input_time("黃燈") red_time = Traffic_light.input_time("紅燈") #例項化 traffic01 = Traffic_light(green_time,red_time) traffic01.countdown()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。