PROGRAMA DE LOS 4 SEMAFOROS EN PYTHON

from turtle import *
import random
import RPi.GPIO as gpio
import time
from time import sleep

#salidas
gpio.setmode(gpio.BOARD)

#T1
gpio.setup(40, gpio.OUT) #Rojo
gpio.setup(38, gpio.OUT) #Amarillo
gpio.setup(36, gpio.OUT) #Verde

#T1
gpio.setup(37, gpio.OUT) #Rojo
gpio.setup(35, gpio.OUT) #Amarillo
gpio.setup(33, gpio.OUT) #Verde

#T1
gpio.setup(26, gpio.OUT) #Rojo
gpio.setup(24, gpio.OUT) #Amarillo
gpio.setup(22, gpio.OUT) #Verde

#T1
gpio.setup(23, gpio.OUT) #Rojo
gpio.setup(21, gpio.OUT) #Amarillo
gpio.setup(19, gpio.OUT) #Verde

#Difiniendo Tortugas

t=Turtle()
t1=Turtle()
t2=Turtle()
t3=Turtle()
t4=Turtle()

#tortugas semaforo
t5=Turtle()
t6=Turtle()
t7=Turtle()
t8=Turtle()

#Figuras a las tortugas

t5.shape("turtle")
t6.shape("turtle")
t7.shape("turtle")
t8.shape("turtle")

#Ocultando tortugas

t5.hideturtle()
t6.hideturtle()
t7.hideturtle()
t8.hideturtle()

screen=t.getscreen()
colormode(255)
screensize(300,150)
t.hideturtle()

#fondo
t.speed(0)
t.fillcolor("black")
t.begin_fill()
t.penup()
t.goto(470,-370)
t.goto(-470,-370)
t.goto(-470,370)
t.goto(470, 370)
t.goto(470,-370)
t.end_fill()
t.penup()

#Primer cuadro
t.fillcolor("gray")
t.begin_fill()
t.goto(-450,-100)
t.pendown()
t.goto(-450,-360)
t.goto(-100,-360)
t.goto(-100,-100)
t.goto(-450,-100)
t.end_fill()
t.penup()

#Segundo cuadro
t.fillcolor("gray")
t.begin_fill()
t.goto(460,100)
t.pendown()
t.goto(460,360)
t.goto(100,360)
t.goto(100,100)
t.goto(460,100)
t.end_fill()
t.penup()

#Tercer Cuadro

t.fillcolor("gray")
t.begin_fill()
t.goto(460,-100)
t.pendown()
t.goto(460,-360)
t.goto(100,-360)
t.goto(100,-100)
t.goto(460,-100)
t.end_fill()
t.penup()

#Cuarto Cuadro

t.fillcolor("gray")
t.begin_fill()
t.goto(-460,100)
t.pendown()
t.goto(-460,360)
t.goto(-100,360)
t.goto(-100,100)
t.goto(-460,100)
t.end_fill()
t.penup()

#Lineas Horizontales

t.pencolor("white")
t.pensize(10)
t.goto(-470,0)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(250)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()


#Lineas Verticales

t.goto(0,370)
t.right(90)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(250)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)
t.pendown()
t.fd(20)
t.penup()
t.fd(20)


#Semaforo 1

t.goto(-100,100)
t.dot(20,255,150,150)
t.goto(-80,100)
t.dot(20,255,255,150)
t.goto(-60,100)
t.dot(20,150,255,150)

#Semaforo 2

t.goto(-100,-100)
t.dot(20,255,150,150)
t.goto(-100,-80)
t.dot(20,255,255,150)
t.goto(-100,-60)
t.dot(20,150,255,150)

#Semaforo 3

t.goto(100,-100)
t.dot(20,255,150,150)
t.goto(80,-100)
t.dot(20,255,255,150)
t.goto(60,-100)
t.dot(20,150,255,150)

#Semaforo 4

t.goto(100,100)
t.dot(20,255,150,150)
t.goto(100,80)
t.dot(20,255,255,150)
t.goto(100,60)
t.dot(20,150,255,150)
t.penup()

#TMMT

x=random.randint(1,4)
x2=random.randint(1,4)
x3=random.randint(1,4)
x4=random.randint(1,4)

t1.penup()
t2.penup()
t3.penup()
t4.penup()

t1.hideturtle()
t2.hideturtle()
t3.hideturtle()
t4.hideturtle()

t.shape("turtle")
Muestras=["red","green","yellow","blue","purple","white","brown","orange","gold","silver"]
a=[]
b=[]
c=[]
d=[]

#T4 entre cuadro 4 y 1
s=-370

t4.shape("turtle")
for i in range (x):
z=random.randint(0,4)
t4.color(Muestras[z])
a.append(Muestras[z])
t4.goto(s,-38)
t4.stamp()
s=s+70

#T1 entre cuadro 4 y 2

t1.penup()
t1.right(90)
t1.shape("turtle")
q=340

for i in range (x2):
z=random.randint(0,4)
t1.color(Muestras[z])
b.append(Muestras[z])
t1.goto(-30,q)
t1.stamp()
q=q-70

#T2 entre cuadro 2 y 3

t2.penup()
t2.left(180)
t2.shape("turtle")
r=430

for i in range (x3):
z=random.randint(0,4)
t2.color(Muestras[z])
c.append(Muestras[z])
t2.goto(r,25)
t2.stamp()
r=r-70

#T3 entre cuadro 3 y 1

t3.penup()
t3.left(90)
t3.shape("turtle")
y=-350

for i in range (x4):
z=random.randint(0,4)
t3.color(Muestras[z])
d.append(Muestras[z])
t3.goto(40,y)
t3.stamp()
y=y+70


t.speed(5)
t1.speed(5)
t2.speed(5)
t3.speed(5)
t4.speed(5)
t5.speed(0)
t6.speed(0)
t7.speed(0)
t8.speed(0)
t.hideturtle()


#EJECUCION
try:
while True:

#PRUEBA


gpio.output(40,gpio.HIGH) #T1 - ROJO
gpio.output(38,gpio.HIGH) #T1 - AMARILLO
gpio.output(36,gpio.HIGH) #T1 - VERDE

gpio.output(37,gpio.HIGH) #T2 - ROJO
gpio.output(35,gpio.HIGH) #T2 - AMARILLO
gpio.output(33,gpio.HIGH) #T2 - VERDE

gpio.output(26,gpio.HIGH) #T3 - ROJO
gpio.output(24,gpio.HIGH) #T3 - AMARILLO
gpio.output(22,gpio.HIGH) #T3 - VERDE

gpio.output(23,gpio.HIGH) #T4 - ROJO
gpio.output(21,gpio.HIGH) #T4 - AMARILLO
gpio.output(19,gpio.HIGH) #T4 - VERDE

time.sleep(1)

# ROJOS

t5.penup()
t6.penup()
t7.penup()
t8.penup()
t5.goto(-100,100)
t5.dot(20,255,0,0) #rojo 1
t6.goto(-100,-100)
t6.dot(20,255,0,0) #rojo 2
t7.goto(100,-100)
t7.dot(20,255,0,0) #rojo 3
t8.goto(100,100)
t8.dot(20,255,0,0) #rojo 4

gpio.output(40,gpio.HIGH) #T1 - ROJO
gpio.output(38,gpio.LOW) #T1 - AMARILLO
gpio.output(36,gpio.LOW) #T1 - VERDE

gpio.output(37,gpio.HIGH) #T2 - ROJO
gpio.output(35,gpio.LOW) #T2 - AMARILLO
gpio.output(33,gpio.LOW) #T2 - VERDE

gpio.output(26,gpio.HIGH) #T3 - ROJO
gpio.output(24,gpio.LOW) #T3 - AMARILLO
gpio.output(22,gpio.LOW) #T3 - VERDE

gpio.output(23,gpio.HIGH) #T4 - ROJO
gpio.output(21,gpio.LOW) #T4 - AMARILLO
gpio.output(19,gpio.LOW) #T4 - VERDE

time.sleep(1)

#Primer semaforo

#Se enciende el rojo por 4 segundos
gpio.output(40,gpio.HIGH) #T1 - ROJO
gpio.output(38,gpio.LOW) #T1 - AMARILLO
gpio.output(36,gpio.LOW) #T1 - VERDE

time.sleep(1)


t5.dot(20,255,150,150) #rojo 4
t5.goto(-60,100)
t5.dot(20,0,255,0) #verde 4

for i in range(x2):
t1.goto(-30,q)
t1.pendown()
t1.showturtle()
t1.color(b[x3-i-1])
t1.clearstamps(-1)
t1.penup()
t1.fd(800)
t1.hideturtle()
q=q+70

     
#Se enciende el verde por 3 segundos
gpio.output(40,gpio.LOW) #T1 - ROJO
gpio.output(38,gpio.LOW) #T1 - AMARILLO
gpio.output(36,gpio.HIGH) #T1 - VERDE

time.sleep(3)

t5.goto(-60,100)
t5.dot(20,150,255,150) #verde 1

#parpadea del verde
gpio.output(40,gpio.LOW) #T1 - ROJO
gpio.output(38,gpio.LOW) #T1 - AMARILLO
gpio.output(36,gpio.LOW) #T1 - VERDE

time.sleep(.5)

t5.goto(-60,100)
t5.dot(20,0,255,0) #verde 1

gpio.output(36,gpio.HIGH) #T1 - VERDE

time.sleep(.5)

t5.goto(-60,100)
t5.dot(20,150,255,150) #verde 1

gpio.output(36,gpio.LOW) #T1 - VERDE

time.sleep(.5)

t5.goto(-60,100)
t5.dot(20,0,255,0) #verde 1

gpio.output(36,gpio.HIGH) #T1 - VERDE

time.sleep(.5)

t5.goto(-60,100)
t5.dot(20,150,255,150) #verde 1

gpio.output(36,gpio.LOW) #T1 - VERDE

time.sleep(.5)

t5.goto(-60,100)
t5.dot(20,0,255,0) #verde 1

gpio.output(36,gpio.HIGH) #T1 - VERDE

time.sleep(.5)

t5.goto(-60,100)
t5.dot(20,150,255,150) #verde 1
t5.goto(-80,100)

gpio.output(36,gpio.LOW) #T1 - VERDE

time.sleep(.5)

#SE ENCIENDE EL AMARILLO POR 2 SEG

t5.goto(-80,100)
t5.dot(20,255,255,0) #amarillo 1

gpio.output(40,gpio.LOW) #T1 - ROJO
gpio.output(38,gpio.HIGH) #T1 - AMARILLO
gpio.output(36,gpio.LOW) #T1 - VERDE

time.sleep(2)

t5.dot(20,255,255,150) #amarillo 1
t5.goto(-100,100)
t5.dot(20,255,0,0) #rojo 1
t6.goto(-100,-100)

#Se enciende el rojo
gpio.output(40,gpio.HIGH) #T1 - ROJO
gpio.output(38,gpio.LOW) #T1 - AMARILLO
gpio.output(36,gpio.LOW) #T1 - VERDE

#Segundo semaforo

t6.dot(20,255,150,150) #rojo 2
t6.goto(-100,-60) 
t6.dot(20,0,255,0) # verde 2



#Se enciende el verde por 3 segundos
gpio.output(37,gpio.LOW) #T2 - ROJO
gpio.output(35,gpio.LOW) #T2 - AMARILLO
gpio.output(33,gpio.HIGH) #T2 - VERDE

for i in range(x):
t4.goto(s,-38)
t4.pendown()
t4.showturtle()
t4.color(a[x3-i-1])
t4.clearstamps(-1)
t4.penup()
t4.fd(800)
t4.hideturtle()
s=s+70

time.sleep(3)

t6.dot(20,150,255,150) # verde 2

#parpadea del verde
gpio.output(37,gpio.LOW) #T2 - ROJO
gpio.output(35,gpio.LOW) #T2 - AMARILLO
gpio.output(33,gpio.LOW) #T2 - VERDE

time.sleep(.5)

t6.dot(20,0,255,0) # verde 2

gpio.output(33,gpio.HIGH) #T2 - VERDE

time.sleep(.5)

t6.dot(20,150,255,150) # verde 2

gpio.output(33,gpio.LOW) #T2 - VERDE

time.sleep(.5)

t6.dot(20,0,255,0) # verde 2

gpio.output(33,gpio.HIGH) #T2 - VERDE

time.sleep(.5)

t6.dot(20,150,255,150) # verde 2

gpio.output(33,gpio.LOW) #T2 - VERDE

time.sleep(.5)

t6.dot(20,0,255,0) # verde 2

gpio.output(33,gpio.HIGH) #T2 - VERDE

time.sleep(.5)

t6.dot(20,150,255,150) # verde 2
t6.goto(-100,-80)

gpio.output(33,gpio.LOW) #T2 - VERDE

time.sleep(.5)

t6.dot(20,255,255,0) # amarillo 2

#SE ENCIENDE EL AMARILLO POR 2 SEG
gpio.output(37,gpio.LOW) #T2 - ROJO
gpio.output(35,gpio.HIGH) #T2 - AMARILLO
gpio.output(33,gpio.LOW) #T2 - VERDE

time.sleep(2)

t6.dot(20,255,255,150) # amarillo 2
t6.goto(-100,-100)
t6.dot(20,255,0,0) # rojo 2
t7.goto(100,-100)

#Se enciende el rojo 
gpio.output(37,gpio.HIGH) #T2 - ROJO
gpio.output(35,gpio.LOW) #T2 - AMARILLO
gpio.output(33,gpio.LOW) #T2 - VERDE)

#Tercer semaforo

t7.dot(20,255,150,150) #rojo 3
t7.goto(60,-100)
t7.dot(20,0,255,0) # verde 3

#Se enciende el verde por 3 segundos
gpio.output(26,gpio.LOW) #T3 - ROJO
gpio.output(24,gpio.LOW) #T3 - AMARILLO
gpio.output(22,gpio.HIGH) #T3 - VERDE

for i in range(x4):
t3.goto(40, y)
t3.pendown()
t3.showturtle()
t3.color(d[x3-i-1])
t3.clearstamps(-1)
t3.penup()
t3.fd(800)
t3.hideturtle()
y=y+70


time.sleep(3)

t7.dot(20,150,255,150) #verde 3

#parpadea del verde
gpio.output(26,gpio.LOW) #T3 - ROJO
gpio.output(24,gpio.LOW) #T3 - AMARILLO
gpio.output(22,gpio.LOW) #T3 - VERDE

time.sleep(.5)

t7.dot(20,0,255,0) # verde 3

gpio.output(22,gpio.HIGH) #T3 - VERDE

time.sleep(.5)

t7.dot(20,150,255,150) # verde 3

gpio.output(22,gpio.LOW) #T3 - VERDE

time.sleep(.5)

t7.dot(20,0,255,0) #verde 3

gpio.output(22,gpio.HIGH) #T3 - VERDE

time.sleep(.5)

t7.dot(20,150,255,150) # verde 3

gpio.output(22,gpio.LOW) #T3 - VERDE

time.sleep(.5)

t7.dot(20,0,255,0) # verde 3

gpio.output(22,gpio.HIGH) #T3 - VERDE

time.sleep(.5)

t7.dot(20,150,255,150) # verde 3
t7.goto(80,-100)

gpio.output(22,gpio.LOW) #T3 - VERDE

time.sleep(.5)

t7.dot(20,255,255,0) # amarillo 3
 
#SE ENCIENDE EL AMARILLO POR 2 SEG
gpio.output(26,gpio.LOW) #T3 - ROJO
gpio.output(24,gpio.HIGH) #T3 - AMARILLO
gpio.output(22,gpio.LOW) #T3 - VERDE

time.sleep(2)

t7.dot(20,255,255,150) # amarillo 3
t7.goto(100,-100)
t7.dot(20,255,0,0) # rojo 3
t8.goto(100,100)


#Se enciende el rojo
gpio.output(26,gpio.HIGH) #T3 - ROJO
gpio.output(24,gpio.LOW) #T3 - AMARILLO
gpio.output(22,gpio.LOW) #T3 - VERDE

#Cuarto semaforo

t8.dot(20,255,150,150) # rojo 4
t8.goto(100,60)
t8.dot(20,0,255,0) # verde 4

#Se enciende el verde por 3 segundos
gpio.output(23,gpio.LOW) #T4 - ROJO
gpio.output(21,gpio.LOW) #T4 - AMARILLO
gpio.output(19,gpio.HIGH) #T4 - VERDE

for i in range(x3):
t2.goto(r,25)
t2.pendown()
t2.showturtle()
t2.color(c[x3-i-1])
t2.clearstamps(-1)
t2.penup()
t2.fd(800)
t2.hideturtle()
r=r+70

time.sleep(3)

t8.dot(20,150,255,150) # verde 4

#parpadea del verde
gpio.output(23,gpio.LOW) #T4 - ROJO
gpio.output(21,gpio.LOW) #T4 - AMARILLO
gpio.output(19,gpio.LOW) #T4 - VERDE

time.sleep(.5)

t8.dot(20,0,255,0) # verde 4

gpio.output(19,gpio.HIGH) #T4 - VERDE

time.sleep(.5)

t8.dot(20,150,255,150) # verde 4

gpio.output(19,gpio.LOW) #T4 - VERDE

time.sleep(.5)

t8.dot(20,0,255,0) # verde 4

gpio.output(19,gpio.HIGH) #T4 - VERDE

time.sleep(.5)

t8.dot(20,150,255,150) # verde 4

gpio.output(19,gpio.LOW) #T4 - VERDE

time.sleep(.5)

t8.dot(20,0,255,0) # verde 4

gpio.output(19,gpio.HIGH) #T4 - VERDE

time.sleep(.5)

t8.dot(20,150,255,150) # verde 4
t8.goto(100,80)

gpio.output(19,gpio.LOW) #T4 - VERDE

time.sleep(.5)

t8.dot(20,255,255,0) # amarillo 4

#SE ENCIENDE EL AMARILLO POR 2 SEG
gpio.output(23,gpio.LOW) #T4 - ROJO
gpio.output(21,gpio.HIGH) #T4 - AMARILLO
gpio.output(19,gpio.LOW) #T4 - VERDE

time.sleep(2)

t8.dot(20,255,255,150) # amarillo 4
t8.goto(100,100)
t8.dot(20,255,0,0) #rojo 4

#Se enciende el rojo por 4 segundos
gpio.output(23,gpio.HIGH) #T4 - ROJO
gpio.output(21,gpio.LOW) #T4 - AMARILLO
gpio.output(19,gpio.LOW) #T4 - VERDE

gpio.cleanup()

except KeyboardInterrupt:
gpio.cleanup() #LIMPIA PUERTOS

gpio.cleanup()


Video de Prueva:

https://www.youtube.com/watch?v=BaiZYuqtr2g

Comentarios