Example #1
0
 def __init__(self, hours, minutes):
     super(Alarm, self).__init__()
     print(hours, minutes)
     self.tl = TrafficLight()
     self.hours = int(hours)
     self.minutes = int(minutes)
     self.keep_running = True
Example #2
0
class Alarm(threading.Thread):
    def __init__(self, hours, minutes):
        super(Alarm, self).__init__()
        print(hours, minutes)
        self.tl = TrafficLight()
        self.hours = int(hours)
        self.minutes = int(minutes)
        self.keep_running = True

    def run(self):

        while self.keep_running:
            now = time.strftime("%H:%M", time.localtime())
            now = now.split(":")
            h = int(now[0])
            m = int(now[1])

            if (h == self.hours and m == self.minutes):
                print("ALARM NOW!")
                self.tl.flash_random(10, 0.05)
                return
        time.sleep(5)

    def just_die(self):
        self.keep_running = False
Example #3
0
    def generate_traffic_lights(self, traffic_light_data):
        for id, cardinal_direction in traffic_light_data.items():
            traffic_light_type = id[1]

            if traffic_light_type == 'B':
                self.traffic_lights[id] = TrafficLight(id, cardinal_direction, self.emitter, self.active_roads)
            elif traffic_light_type == 'V':
                self.traffic_lights[id] = TrafficLight(id, cardinal_direction, self.emitter, self.active_roads)
            elif traffic_light_type == 'F':
                self.traffic_lights[id] = TrafficLight(id, cardinal_direction, self.emitter, self.active_roads)
            else:
                self.traffic_lights[id] = TrafficLight(id, cardinal_direction, self.emitter, self.active_roads)
 def generate_traffic_lights(self):
     radius = constants.TRAFFIC_LIGHT_SIZE
     if self.blocks[0][0][1].width > self.blocks[0][0][1].height:
         radius *= (self.blocks[0][0][1].height / 2)
     else:
         radius *= (self.blocks[0][0][1].width / 2)
     for i in range(1, len(self.blocks) - 1):
         for j in range(1, len(self.blocks[i]) - 1):
             traffic_light_blocks = []
             traffic_light_blocks.append(self.blocks[i][j])
             traffic_light_blocks.append(self.blocks[i][j - 1])
             traffic_light_blocks.append(self.blocks[i - 1][j])
             traffic_light_blocks.append(self.blocks[i][j + 1])
             traffic_light_blocks.append(self.blocks[i + 1][j])
             left = traffic_light_blocks[1][2] != constants.BLACK
             up = traffic_light_blocks[2][2] != constants.BLACK
             right = traffic_light_blocks[3][2] != constants.BLACK
             down = traffic_light_blocks[4][2] != constants.BLACK
             count = sum([left, right, up, down])
             if count >= 3 and traffic_light_blocks[0][2] != constants.BLACK:
                 x = traffic_light_blocks[0][1].x + (
                     traffic_light_blocks[0][1].width / 2)
                 y = traffic_light_blocks[0][1].y + (
                     traffic_light_blocks[0][1].height / 2)
                 self.traffic_lights.append(
                     TrafficLight(x, y, traffic_light_blocks, radius))
 def __init__(self, name, max_q_size, x, y):
     super().__init__(name, "intersection", x, y)
     self.neighbours = [None, None, None, None]  # north, east, south, west
     self.qs = [
         CarQueue(max_q_size),
         CarQueue(max_q_size),
         CarQueue(max_q_size),
         CarQueue(max_q_size)
     ]
     self.traffic_lights = [
         TrafficLight(),
         TrafficLight(),
         TrafficLight(),
         TrafficLight()
     ]
     self.current_state = None
     self.reward = 0
Example #6
0
    def __init__(self, cfg, res, fsm):
        self.cfg = cfg
        self.res = res
        self.fsm = fsm

        self.__is_finished = False
        self.level_num = None # set in set_level called from init
        self.register = Register(cfg, res, (350, 350))

        self.background = Sprite("background", self.res, None, ORIGIN_TOP_LEFT)
        self.hud = Sprite("hud", self.res, None, ORIGIN_TOP_LEFT)

        self.points = 0
        self.deaths = 0
        self.bonuses = []
        self.blood_sprite = Sprite("blood", self.res)
        self.blood_positions = []
        self.remote_gather = False

        self.stopped = True # the game is not playing right now (characters don't move etc)

        self.answer = Sprite("answer", self.res)
        self.pause_sprite = [ Sprite("tutorial-level-1", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("tutorial-level-1", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("tutorial-level-2", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("tutorial-level-3", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("tutorial-level-4", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("tutorial-level-5", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("paused", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("paused", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("paused", self.res, None, ORIGIN_TOP_LEFT),
                              Sprite("paused", self.res, None, ORIGIN_TOP_LEFT) ]

        self.teleport_sprite = Sprite("teleport_area", self.res, None, ORIGIN_TOP_LEFT)
        self.teleporting = False

        self.trafficLights = [TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 160, 160),
                              TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 380, 160),
                              TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 220, 380),
                              TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 440, 380),
                              TrafficLight(traffic_light.YELLOW_BEFORE_RED, 160, 220),
                              TrafficLight(traffic_light.YELLOW_BEFORE_RED, 160, 440),
                              TrafficLight(traffic_light.YELLOW_BEFORE_RED, 380, 160),
                              TrafficLight(traffic_light.YELLOW_BEFORE_RED, 380, 380)]
        for i in range(8):
            self.trafficLights[i].setPosition(290,270)
        # self.trafficLights[0].setPosition(610, 300)
        # self.trafficLights[1].setPosition(390, 160)
        # self.trafficLights[2].setPosition(220, 300)
        # self.trafficLights[3].setPosition(440, 300)
        # self.trafficLights[4].setPosition(140, 440)
        # self.trafficLights[6].setPosition(360, 160)
        # self.trafficLights[7].setPosition(360, 380)

        crossings =  [(160, 160, 80, 80), (380, 160, 80, 80), (160, 380, 80, 80), (380, 380, 80, 80)]

        self.lanes = [Lane(50, [self.trafficLights[0], self.trafficLights[1]], const.RIGHT, 3, (0, 160), [(140, 140, 80, 80), (380, 140, 80, 80)], self.cfg, res),
                 Lane(100, [self.trafficLights[0], self.trafficLights[1]], const.RIGHT, 3, (0, 181), [(140, 140, 80, 80), (380, 140, 80, 80)], self.cfg, res),
                 Lane(70, [self.trafficLights[0], self.trafficLights[1]], const.RIGHT, 3, (0, 202), [(140, 140, 80, 80), (380, 140, 80, 80)], self.cfg, res),
                 Lane(60, [self.trafficLights[2], self.trafficLights[3]], const.LEFT, 3, (self.cfg.board_size[0] - 40, 380), [(140, 380, 80, 80), (380, 380, 80, 80)], self.cfg, res),
                 Lane(120, [self.trafficLights[2], self.trafficLights[3]], const.LEFT, 3, (self.cfg.board_size[0] - 40, 401), [(140, 380, 80, 80), (380, 380, 80, 80)], self.cfg, res),
                 Lane(80, [self.trafficLights[2], self.trafficLights[3]], const.LEFT, 3, (self.cfg.board_size[0] - 40, 422), [(140, 380, 80, 80), (380, 380, 80, 80)], self.cfg, res),
                 Lane(40, [self.trafficLights[4], self.trafficLights[5]], const.UP, 3, (162, self.cfg.board_size[1] - 40), [(140, 140, 80, 80), (160, 380, 80, 80)], self.cfg, res),
                 Lane(80, [self.trafficLights[4], self.trafficLights[5]], const.UP, 3, (182, self.cfg.board_size[1] - 40), [(140, 140, 80, 80), (160, 380, 80, 80)], self.cfg, res),
                 Lane(60, [self.trafficLights[4], self.trafficLights[5]], const.UP, 3, (203, self.cfg.board_size[1] - 40), [(140, 140, 80, 80), (160, 380, 80, 80)], self.cfg, res),
                 Lane(60, [self.trafficLights[6], self.trafficLights[7]], const.DOWN, 3, (381, 0), [(380, 140, 80, 80), (380, 360, 80, 80)], self.cfg, res),
                 Lane(100, [self.trafficLights[6], self.trafficLights[7]], const.DOWN, 3, (403, 0), [(380, 140, 80, 80), (380, 360, 80, 80)], self.cfg, res),
                 Lane(80, [self.trafficLights[6], self.trafficLights[7]], const.DOWN, 3, (423, 0), [(380, 140, 80, 80), (380, 360, 80, 80)], self.cfg, res)]
        self.cars = []
        for lane in self.lanes:
            self.cars += lane.getCars()

        self.allsprites = pygame.sprite.RenderPlain([self.trafficLights[0]])

        self.time = 0
        self.lasttime = 0
        self.timeoffset = [10, 3]
        self.currentOffset = 1
        self.last_keys = []

        self.cops = []
        self.cop_sprites = pygame.sprite.Group()

        self.inited = False
Example #7
0
from flask import Flask, render_template, request
from traffic_light import TrafficLight, RED, YELLOW, GREEN
import time 
from alarm import Alarm

app = Flask(__name__)

tl = TrafficLight()

# @app.route("/")
# def hello():
#     return "Hello World!"

@app.route("/", methods=['GET', 'POST'])
def home():

    return render_template("home.html")

@app.route("/alarm", methods=['GET', 'POST'])
def alarm():
    return render_template("alarm.html")

@app.route("/show", methods=['GET', 'POST'])
def show():
    return render_template("show.html")

@app.route("/start_alarm", methods=['GET', 'POST'])
def start_alarm():

    if request.method == "POST":
        t = request.form["time"]
Example #8
0
from traffic_light import TrafficLight, RED, YELLOW, GREEN
import time

tl = TrafficLight()

R_active = False
Y_active = False
G_active = False


def activate(channel, R_active, Y_active, G_active):
    if channel == 1:
        if R_active:
            tl.kill(RED)
            R_active = False
        else:
            tl.start(RED)
            R_active = True

    elif channel == 2:
        if Y_active:
            tl.kill(YELLOW)
            Y_active = False
        else:
            tl.start(YELLOW)
            Y_active = True

    elif channel == 3:
        if G_active:
            tl.kill(GREEN)
            G_active = False
Example #9
0
from traffic_light import TrafficLight, RED, YELLOW, GREEN
import time

tl = TrafficLight()
try:

    i = 0.2

    tl.flash_random(5, i)
    tl.kill_all()
    time.sleep(1)

    tl.cycle_down(5, i)
    tl.kill_all()
    time.sleep(1)

    tl.cycle_up(5, i)
    tl.kill_all()
    time.sleep(1)

    tl.build_up(5, i)
    tl.kill_all()
    time.sleep(1)

    tl.build_down(5, i)
    tl.kill_all()
    time.sleep(1)

except Exception as e:
    tl.end()
    raise e
Example #10
0
    def __init__(self, cfg, res):
        self.cfg = cfg
        self.res = res

        random.init()

        self.__is_finished = False
        self.level_num = None  # set in set_level called from init
        self.register = Register(cfg, res)

        self.background = Sprite("background", self.res, None, ORIGIN_TOP_LEFT)
        self.hud = Sprite("hud", self.res, None, ORIGIN_TOP_LEFT)

        self.points = 0
        self.deaths = 0
        self.bonuses = []

        self.stopped = True  # the game is not playing right now (characters don't move etc)

        self.trafficLights = [
            TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 160, 160),
            TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 380, 160),
            TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 220, 380),
            TrafficLight(traffic_light.YELLOW_BEFORE_GREEN, 440, 380),
            TrafficLight(traffic_light.YELLOW_BEFORE_RED, 160, 220),
            TrafficLight(traffic_light.YELLOW_BEFORE_RED, 160, 440),
            TrafficLight(traffic_light.YELLOW_BEFORE_RED, 380, 160),
            TrafficLight(traffic_light.YELLOW_BEFORE_RED, 380, 380)
        ]
        self.trafficLights[0].setPosition(610, 300)
        self.trafficLights[1].setPosition(390, 160)
        self.trafficLights[2].setPosition(220, 300)
        self.trafficLights[3].setPosition(440, 300)
        self.trafficLights[4].setPosition(140, 440)
        self.trafficLights[6].setPosition(360, 160)
        self.trafficLights[7].setPosition(360, 380)

        crossings = [(160, 160, 80, 80), (380, 160, 80, 80),
                     (160, 380, 80, 80), (380, 380, 80, 80)]

        self.lanes = [
            Lane(0.5, [self.trafficLights[0], self.trafficLights[1]],
                 const.RIGHT, 3, (0, 160), [(140, 140, 80, 80),
                                            (380, 140, 80, 80)], self.cfg,
                 res),
            Lane(1, [self.trafficLights[0], self.trafficLights[1]],
                 const.RIGHT, 3, (0, 181), [(140, 140, 80, 80),
                                            (380, 140, 80, 80)], self.cfg,
                 res),
            Lane(0.7, [self.trafficLights[0], self.trafficLights[1]],
                 const.RIGHT, 3, (0, 202), [(140, 140, 80, 80),
                                            (380, 140, 80, 80)], self.cfg,
                 res),
            Lane(0.6, [self.trafficLights[2], self.trafficLights[3]],
                 const.LEFT, 3, (self.cfg.board_size[0] - 40, 380),
                 [(140, 380, 80, 80), (380, 380, 80, 80)], self.cfg, res),
            Lane(1.2, [self.trafficLights[2], self.trafficLights[3]],
                 const.LEFT, 3, (self.cfg.board_size[0] - 40, 401),
                 [(140, 380, 80, 80), (380, 380, 80, 80)], self.cfg, res),
            Lane(0.8, [self.trafficLights[2], self.trafficLights[3]],
                 const.LEFT, 3, (self.cfg.board_size[0] - 40, 422),
                 [(140, 380, 80, 80), (380, 380, 80, 80)], self.cfg, res),
            Lane(0.4, [self.trafficLights[4], self.trafficLights[5]], const.UP,
                 3, (162, self.cfg.board_size[1] - 40), [(140, 140, 80, 80),
                                                         (160, 380, 80, 80)],
                 self.cfg, res),
            Lane(0.8, [self.trafficLights[4], self.trafficLights[5]], const.UP,
                 3, (182, self.cfg.board_size[1] - 40), [(140, 140, 80, 80),
                                                         (160, 380, 80, 80)],
                 self.cfg, res),
            Lane(0.6, [self.trafficLights[4], self.trafficLights[5]], const.UP,
                 3, (203, self.cfg.board_size[1] - 40), [(140, 140, 80, 80),
                                                         (160, 380, 80, 80)],
                 self.cfg, res),
            Lane(0.6, [self.trafficLights[6], self.trafficLights[7]],
                 const.DOWN, 3, (381, 0), [(380, 140, 80, 80),
                                           (380, 360, 80, 80)], self.cfg, res),
            Lane(1, [self.trafficLights[6], self.trafficLights[7]],
                 const.DOWN, 3, (403, 0), [(380, 140, 80, 80),
                                           (380, 360, 80, 80)], self.cfg, res),
            Lane(0.8, [self.trafficLights[6], self.trafficLights[7]],
                 const.DOWN, 3, (423, 0), [(380, 140, 80, 80),
                                           (380, 360, 80, 80)], self.cfg, res)
        ]
        self.cars = []
        for lane in self.lanes:
            self.cars += lane.getCars()

        self.allsprites = pygame.sprite.RenderPlain([self.trafficLights[0]])

        self.time = 0
        self.lasttime = 0
        self.timeoffset = [10, 3]
        self.currentOffset = 1
        self.cops = [Cop(260, 170, 390, 170)]
        self.cop_sprites = pygame.sprite.Group()
        for cop in self.cops:
            self.cop_sprites.add(cop.GetSprite())
Example #11
0
from change_state_thread import StateThread
from traffic_light import TrafficLight
from web_gui import MyGuy

if __name__ == '__main__':
    guy = MyGuy()
    thread1 = StateThread(guy.greenState, guy.yellowState, guy.redState,
                          TrafficLight())
    thread1.start()
    guy.root.mainloop()
Example #12
0
from traffic_light import TrafficLight, RED, YELLOW, GREEN
import time

tl = TrafficLight()

tl.flash_random(1, 0.1)
# tl.cycle_down(2, .5)

tl.end()