Example #1
0
    def Start(self):
        running = True
        moves = []
        for move_num in range(len(self.controllers_alive)):
            moves.append(common.get_move(self.controllers_alive[move_num], move_num))

        serials = self.controllers_alive
        processes = []
        
        for num_try, serial in enumerate(serials):
            starting_bullets = 0
            #starting_bullets = random.choice([0, 1])
            opts = Array('i', [0, 0, 0, 1, starting_bullets, 1, 1])
            p = Process(target=track_controller, args=(serial, num_try, opts))
            p.start()
            processes.append(p)
            self.controller_opts[serial] = opts
            self.humans.append(serial)
            

        human_victory = Audio('audio/Zombie/sound_effects/human_victory.wav')
        zombie_victory = Audio('audio/Zombie/sound_effects/zombie_victory.wav')
        death = Audio('audio/Zombie/sound_effects/zombie_death.wav')
        pistol = Audio('audio/Zombie/sound_effects/pistol.wav')
        shotgun = Audio('audio/Zombie/sound_effects/shotgun.wav')
        molotov = Audio('audio/Zombie/sound_effects/molotov.wav')
        try:
            music = Audio('audio/Zombie/music/' + random.choice(os.listdir('audio/Zombie/music/')))
            music.start_effect_music()
        except:
            print('no music in audio/Zombie/music/')

        start_kill = time.time() + 5
        while time.time() < start_kill:
            pass

        #kill first humans
        for i in range(2):
            random_human = random.choice(self.humans)
            self.controller_opts[random_human][3] = 0
        
        while running:
            self.audio_cue()
            #human update, loop through the different human controllers
            for serial in self.humans:
                #human is dead and now a zombie
                if self.controller_opts[serial][3] == 0:
                    self.controller_opts[serial][0] = 1
                    self.dead_zombies[serial] = time.time() + self.get_kill_time()
                    
                #pistol fired(1 bullet 1 random alive zombie)
                elif self.controller_opts[serial][1] == 2:
                    pistol.start_effect()
                    self.kill_zombies(1, [0, 0, 0, 0, 1, 1, 1])
                    self.controller_opts[serial][1] = 0
                            

                #shotgun fired(2 bullets 3 random alive zombies)
                #elif self.controller_opts[serial][1] == 3:
                #    shotgun.start_effect()
                #    self.kill_zombies(3, [ 0, 0, 1, 1, 2])
                #    self.controller_opts[serial][1] = 0


                #molotov fired(5 bullets all alive zombies)
                elif self.controller_opts[serial][1] == 4:
                    molotov.start_effect()
                    self.kill_zombies(20, [0, 0, 1, 1, 2, 3])
                    self.controller_opts[serial][1] = 0

                    
            for serial, spawn_time in self.dead_zombies.items():
                if serial in self.humans:
                    self.humans.remove(serial)
                if spawn_time < time.time():
                    #set zombie to alive
                    self.controller_opts[serial][3] = 1
                    self.alive_zombies.append(serial)

            #loop through dead zombies
            for serial in self.alive_zombies:
                if serial in self.dead_zombies:
                    del self.dead_zombies[serial]

                #melee
                if self.controller_opts[serial][3] == 0:
                    self.controller_opts[serial][0] = 1
                    self.dead_zombies[serial] = time.time() + self.get_kill_time()
                    self.alive_zombies.remove(serial)
                    self.reward([0, 0, 1, 1, 2])
                    death.start_effect()

            #win scenario
            if len(self.humans) <= 0 or (time.time() - self.start_time) > self.win_time:
                for proc in processes:
                    proc.terminate()
                    proc.join()
                pause_time = time.time() + 3
                HSV = [(x*1.0/(50*len(self.controllers_alive)), 0.9, 1) for x in range(50*len(self.controllers_alive))]
                colour_range = [[int(x) for x in common.hsv2rgb(*colour)] for colour in HSV]
                win_controllers = []
                if len(self.humans) <= 0:
                    zombie_victory.start_effect()
                    self.alive_zombies.extend(self.dead_zombies.keys())
                    win_controllers = self.alive_zombies
                if (time.time() - self.start_time) > self.win_time:
                    human_victory.start_effect()
                    win_controllers = self.humans
                #This needs to go in it's own function
                while time.time() < pause_time:
                    for win_move in moves:
                        if win_move.get_serial() in win_controllers:
                            win_move.set_leds(*colour_range[0])
                            colour_range.append(colour_range.pop(0))
                            win_move.update_leds()
                        else:
                            win_move.set_rumble(100)
                            win_move.poll()
                            win_move.set_leds(0, 0, 0)
                            win_move.update_leds()
                    time.sleep(0.01)
                running = False
                try:
                    music.stop_effect_music()
                except:
                    print('no audio loaded')