Пример #1
0
 def end_game_sound(self, winning_team):
     #if self.game_mode == common.Games.JoustTeams:
     if winning_team == Team.red.value:
         team_win = Audio('audio/Commander/sounds/red winner.wav')
     if winning_team == Team.blue.value:
         team_win = Audio('audio/Commander/sounds/blue winner.wav')
     team_win.start_effect()
Пример #2
0
 def end_game_sound(self, winning_team):
     if self.game_mode == common.Games.WereJoust:
         if winning_team == -1:
             team_win = Audio('audio/Joust/sounds/werewolf win.wav')
         else:
             team_win = Audio('audio/Joust/sounds/human win.wav')
         team_win.start_effect()
     elif self.game_mode != common.Games.JoustFFA:
         win_team_name = self.team_colors[winning_team].name
         if win_team_name == 'Pink':
             team_win = Audio('audio/Joust/sounds/human win.wav')
         if win_team_name == 'Magenta':
             team_win = Audio('audio/Joust/sounds/magenta team win.wav')
         if win_team_name == 'Orange':
             team_win = Audio('audio/Joust/sounds/human win.wav')
         if win_team_name == 'Yellow':
             team_win = Audio('audio/Joust/sounds/yellow team win.wav')
         if win_team_name == 'Green':
             team_win = Audio('audio/Joust/sounds/green team win.wav')
         if win_team_name == 'Turquoise':
             team_win = Audio('audio/Joust/sounds/cyan team win.wav')
         if win_team_name == 'Blue':
             team_win = Audio('audio/Joust/sounds/blue team win.wav')
         if win_team_name == 'Purple':
             team_win = Audio('audio/Joust/sounds/human win.wav')
         team_win.start_effect()
Пример #3
0
    def commander_intro_audio(self):
        # print 'BOOOP'
        intro_sound = Audio("audio/Commander/sounds/commander intro.wav")
        intro_sound.start_effect()
        # need while loop here
        play_last_one = True
        commander_select_time = time.time() + 50
        battle_ready_time = time.time() + 40
        while time.time() < commander_select_time:
            self.check_commander_select()
            if self.check_everyone_in():
                break

            if time.time() > battle_ready_time and play_last_one:
                play_last_one = False
                Audio("audio/Commander/sounds/10 seconds begins.wav").start_effect()
        intro_sound.stop_effect()

        if self.current_commander[Team.red.value] == "":
            self.change_random_commander(Team.red.value)
        if self.current_commander[Team.blue.value] == "":
            self.change_random_commander(Team.blue.value)

        Audio("audio/Commander/sounds/commanders chosen.wav").start_effect()
        time.sleep(4)
        self.reset_power(Team.red.value)
        self.reset_power(Team.blue.value)
        self.commander_intro.value = 0
Пример #4
0
 def end_game_sound(self, winning_team):
     # if self.game_mode == common.Games.JoustTeams:
     if winning_team == Team.red.value:
         team_win = Audio("audio/Commander/sounds/red winner.wav")
     if winning_team == Team.blue.value:
         team_win = Audio("audio/Commander/sounds/blue winner.wav")
     team_win.start_effect()
Пример #5
0
    def commander_intro_audio(self):
        intro_sound = Audio('audio/Commander/vox/' + self.voice +
                            '/commander intro.wav')
        intro_sound.start_effect()
        #need while loop here
        play_last_one = True
        commander_select_time = time.time() + 50
        battle_ready_time = time.time() + 40
        while time.time() < commander_select_time:
            self.check_commander_select()
            if self.check_everyone_in():
                break

            if time.time() > battle_ready_time and play_last_one:
                play_last_one = False
                Audio('audio/Commander/vox/' + self.voice +
                      '/10 seconds begins.wav').start_effect()
        intro_sound.stop_effect()

        if self.current_commander[Team.alpha.value] == '':
            self.change_random_commander(Team.alpha.value)
        if self.current_commander[Team.bravo.value] == '':
            self.change_random_commander(Team.bravo.value)

        Audio('audio/Commander/vox/' + self.voice +
              '/commanders chosen.wav').start_effect_and_wait()
        self.reset_power(Team.alpha.value)
        self.reset_power(Team.bravo.value)
        self.commander_intro.value = 0
Пример #6
0
    def commander_intro_audio(self):
        #print 'BOOOP'
        intro_sound = Audio('audio/Commander/sounds/commander intro.wav')
        intro_sound.start_effect()
        #need while loop here
        play_last_one = True
        commander_select_time = time.time() + 50
        battle_ready_time = time.time() + 40
        while time.time() < commander_select_time:
            self.check_commander_select()
            if self.check_everyone_in():
                break

            if time.time() > battle_ready_time and play_last_one:
                play_last_one = False
                Audio('audio/Commander/sounds/10 seconds begins.wav'
                      ).start_effect()
        intro_sound.stop_effect()

        if self.current_commander[Team.red.value] == '':
            self.change_random_commander(Team.red.value)
        if self.current_commander[Team.blue.value] == '':
            self.change_random_commander(Team.blue.value)

        Audio('audio/Commander/sounds/commanders chosen.wav').start_effect()
        time.sleep(4)
        self.reset_power(Team.red.value)
        self.reset_power(Team.blue.value)
        self.commander_intro.value = 0
Пример #7
0
 def end_game_sound(self, winning_team):
     if self.game_mode == common.Games.WereJoust:
         if winning_team == -1:
             team_win = Audio('audio/Joust/vox/' + self.voice +
                              '/werewolf win.wav')
         else:
             team_win = Audio('audio/Joust/vox/' + self.voice +
                              '/human win.wav')
         team_win.start_effect()
     elif self.game_mode != common.Games.JoustFFA and self.game_mode != common.Games.NonStop:
         win_team_name = self.team_colors[winning_team].name
         if winning_team == -1:
             team_win = Audio('audio/Joust/vox/' + self.voice +
                              '/traitor win.wav')
         else:
             if win_team_name == 'Pink':
                 if self.voice == 'aaron':
                     os.popen(
                         'espeak -ven -p 70 -a 200 "And the winner is ...Pink Team'
                     )
                 else:
                     team_win = Audio('audio/Joust/vox/' + self.voice +
                                      '/pink team win.wav')
             if win_team_name == 'Magenta':
                 team_win = Audio('audio/Joust/vox/' + self.voice +
                                  '/magenta team win.wav')
             if win_team_name == 'Orange':
                 if self.voice == 'aaron':
                     os.popen(
                         'espeak -ven -p 70 -a 200 "And the winner is ...Orange Team'
                     )
                 else:
                     team_win = Audio('audio/Joust/vox/' + self.voice +
                                      '/orange team win.wav')
             if win_team_name == 'Yellow':
                 team_win = Audio('audio/Joust/vox/' + self.voice +
                                  '/yellow team win.wav')
             if win_team_name == 'Green':
                 team_win = Audio('audio/Joust/vox/' + self.voice +
                                  '/green team win.wav')
             if win_team_name == 'Turquoise':
                 team_win = Audio('audio/Joust/vox/' + self.voice +
                                  '/cyan team win.wav')
             if win_team_name == 'Blue':
                 team_win = Audio('audio/Joust/vox/' + self.voice +
                                  '/blue team win.wav')
             if win_team_name == 'Purple':
                 if self.voice == 'aaron':
                     os.popen(
                         'espeak -ven -p 70 -a 200 "And the winner is ...Purple Team'
                     )
                 else:
                     team_win = Audio('audio/Joust/vox/' + self.voice +
                                      '/purple team win.wav')
         try:
             team_win.start_effect()
         except:
             pass
Пример #8
0
 def end_game_sound(self, winning_team):
     #if self.game_mode == common.Games.JoustTeams:
     if winning_team == Team.alpha.value:
         team_win = Audio('audio/Commander/vox/' + self.voice +
                          '/red winner.wav')
     if winning_team == Team.bravo.value:
         team_win = Audio('audio/Commander/vox/' + self.voice +
                          '/blue winner.wav')
     team_win.start_effect()
Пример #9
0
 def end_game_sound(self, winning_team):
     win_team_name = self.team_colors[winning_team].name
     if win_team_name == 'Pink':
         team_win = Audio('audio/Joust/sounds/human win.wav')
     if win_team_name == 'Magenta':
         team_win = Audio('audio/Joust/sounds/magenta team win.wav')
     if win_team_name == 'Orange':
         team_win = Audio('audio/Joust/sounds/human win.wav')
     if win_team_name == 'Yellow':
         team_win = Audio('audio/Joust/sounds/yellow team win.wav')
     if win_team_name == 'Green':
         team_win = Audio('audio/Joust/sounds/green team win.wav')
     if win_team_name == 'Turquoise':
         team_win = Audio('audio/Joust/sounds/cyan team win.wav')
     if win_team_name == 'Blue':
         team_win = Audio('audio/Joust/sounds/blue team win.wav')
     if win_team_name == 'Purple':
         team_win = Audio('audio/Joust/sounds/human win.wav')
     team_win.start_effect()
Пример #10
0
 def end_game_sound(self, winning_team):
     win_team_name = self.team_colors[winning_team].name
     if win_team_name == 'Pink':
         if self.voice == 'aaron':
             os.popen(
                 'espeak -ven -p 70 -a 200 "And the winner is ...Pink Team')
         else:
             team_win = Audio('audio/Joust/vox/' + self.voice +
                              '/pink team win.wav')
     if win_team_name == 'Magenta':
         team_win = Audio('audio/Joust/vox/' + self.voice +
                          '/magenta team win.wav')
     if win_team_name == 'Orange':
         if self.voice == 'aaron':
             os.popen(
                 'espeak -ven -p 70 -a 200 "And the winner is ...Orange Team'
             )
         else:
             team_win = Audio('audio/Joust/vox/' + self.voice +
                              '/orange team win.wav')
     if win_team_name == 'Yellow':
         team_win = Audio('audio/Joust/vox/' + self.voice +
                          '/yellow team win.wav')
     if win_team_name == 'Green':
         team_win = Audio('audio/Joust/vox/' + self.voice +
                          '/green team win.wav')
     if win_team_name == 'Turquoise':
         team_win = Audio('audio/Joust/vox/' + self.voice +
                          '/cyan team win.wav')
     if win_team_name == 'Blue':
         team_win = Audio('audio/Joust/vox/' + self.voice +
                          '/blue team win.wav')
     if win_team_name == 'Purple':
         if self.voice == 'aaron':
             os.popen(
                 'espeak -ven -p 70 -a 200 "And the winner is ...Purple Team'
             )
         else:
             team_win = Audio('audio/Joust/vox/' + self.voice +
                              '/purple team win.wav')
     team_win.start_effect()
Пример #11
0
 def end_game_sound(self, winning_team):
     if self.game_mode == common.Games.JoustTeams.value:
         if winning_team == 0:
             team_win = Audio("audio/Joust/sounds/yellow team win.wav")
         if winning_team == 1:
             team_win = Audio("audio/Joust/sounds/green team win.wav")
         if winning_team == 2:
             team_win = Audio("audio/Joust/sounds/cyan team win.wav")
         if winning_team == 3:
             team_win = Audio("audio/Joust/sounds/blue team win.wav")
         if winning_team == 4:
             team_win = Audio("audio/Joust/sounds/magenta team win.wav")
         if winning_team == 5:
             team_win = Audio("audio/Joust/sounds/red team win.wav")
         team_win.start_effect()
     if self.game_mode == common.Games.JoustRandomTeams.value:
         if winning_team == 0:
             team_win = Audio("audio/Joust/sounds/yellow team win.wav")
         if winning_team == 1:
             team_win = Audio("audio/Joust/sounds/cyan team win.wav")
         if winning_team == 2:
             team_win = Audio("audio/Joust/sounds/magenta team win.wav")
         if winning_team == 3:
             team_win = Audio("audio/Joust/sounds/red team win.wav")
         team_win.start_effect()
     if self.game_mode == common.Games.WereJoust.value:
         if winning_team == -1:
             team_win = Audio("audio/Joust/sounds/werewolf win.wav")
         else:
             team_win = Audio("audio/Joust/sounds/human win.wav")
         team_win.start_effect()
Пример #12
0
 def end_game_sound(self, winning_team):
     if self.game_mode == common.Games.JoustTeams.value:
         if winning_team == 0:
             team_win = Audio('audio/Joust/sounds/yellow team win.wav')
         if winning_team == 1:
             team_win = Audio('audio/Joust/sounds/green team win.wav')
         if winning_team == 2:
             team_win = Audio('audio/Joust/sounds/cyan team win.wav')
         if winning_team == 3:
             team_win = Audio('audio/Joust/sounds/blue team win.wav')
         if winning_team == 4:
             team_win = Audio('audio/Joust/sounds/magenta team win.wav')
         if winning_team == 5:
             team_win = Audio('audio/Joust/sounds/red team win.wav')
         team_win.start_effect()
     if self.game_mode == common.Games.JoustRandomTeams.value:
         if winning_team == 0:
             team_win = Audio('audio/Joust/sounds/yellow team win.wav')
         if winning_team == 1:
             team_win = Audio('audio/Joust/sounds/cyan team win.wav')
         if winning_team == 2:
             team_win = Audio('audio/Joust/sounds/magenta team win.wav')
         if winning_team == 3:
             team_win = Audio('audio/Joust/sounds/red team win.wav')
         team_win.start_effect()
     if self.game_mode == common.Games.WereJoust.value:
         if winning_team == -1:
             team_win = Audio('audio/Joust/sounds/werewolf win.wav')
         else:
             team_win = Audio('audio/Joust/sounds/human win.wav')
         team_win.start_effect()
Пример #13
0
class Joust():
    def __init__(self, game_mode, moves, teams, speed, command_queue, status_ns, audio_toggle, music):

        print("speed is {}".format(speed))
        global SLOW_MAX
        global SLOW_WARNING
        global FAST_MAX
        global FAST_WARNING

        self.audo_toggle = audio_toggle
        
        SLOW_MAX = common.SLOW_MAX[speed]
        SLOW_WARNING = common.SLOW_WARNING[speed]
        FAST_MAX = common.FAST_MAX[speed]
        FAST_WARNING = common.FAST_WARNING[speed]

        print("SLOWMAX IS {}".format(SLOW_MAX))

        

        #Sensitivity of the werewolf contollers
        WERE_SLOW_MAX = common.WERE_SLOW_MAX[speed]
        WERE_SLOW_WARNING = common.WERE_SLOW_WARNING[speed]
        WERE_FAST_MAX = common.WERE_FAST_MAX[speed]
        WERE_FAST_WARNING = common.WERE_FAST_WARNING[speed]

        self.move_serials = moves
        self.game_mode = game_mode
        self.tracked_moves = {}
        self.dead_moves = {}
        self.music_speed = Value('d', SLOW_MUSIC_SPEED)
        self.running = True
        self.force_move_colors = {}
        self.teams = teams
        self.team_num = 6
        self.game_mode = game_mode
        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_dead = 0
        self.show_team_colors = Value('i', 0)

        self.command_queue = command_queue
        self.status_ns = status_ns
        self.update_time = 0
        self.alive_moves = []

        #self.update_status('starting')
        
        self.werewolf_reveal = Value('i', 2)
        if game_mode == common.Games.JoustFFA.value:
            self.team_num = len(moves)
        if game_mode == common.Games.JoustRandomTeams.value:
            if len(moves) <= 5:
                self.team_num = 2
            elif len(moves) in [6,7]:
                self.team_num = 3
            else: #8 or more
                self.team_num = 4
        if game_mode == common.Games.Traitor.value:
            if len(moves) <= 8:
                self.team_num = 2
            else: #9 or more
                self.team_num = 3
            self.werewolf_reveal.value = 0
            
        if game_mode == common.Games.WereJoust.value:
            self.werewolf_reveal.value = 0
            self.team_num = 1
        if game_mode != common.Games.JoustTeams.value:
            self.generate_random_teams(self.team_num)

        if game_mode == common.Games.WereJoust.value:
            #were_num = int((len(moves)+2)/4)
            were_num = int((len(moves)*7)/16)
            if were_num <= 0:
                were_num = 1
            self.choose_werewolf(were_num)
        if self.audo_toggle:
            #music = random.choice(glob.glob("audio/Joust/music/*"))
            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            end = False
            #self.audio = Audio(music, end)
            self.audio = music
        fast_resample = False
        
        
        #self.change_time = self.get_change_time(speed_up = True)
        
        self.speed_up = False
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []        
        
        self.game_loop()


    def choose_werewolf(self, were_num):
        for were in range(were_num):
            werewolf = random.choice(self.move_serials)
            while self.teams[werewolf] < 0:
                werewolf = random.choice(self.move_serials)
            self.teams[werewolf] = (self.teams[werewolf] * -1) - 1

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        print (str(team_pick))
        traitor_pick = True
        copy_serials = self.move_serials[:]

        while len(copy_serials) >= 1:
        #for serial in self.move_serials:
            serial = random.choice(copy_serials)
            copy_serials.remove(serial)
            random_choice = random.choice(team_pick)
            if self.game_mode == common.Games.Traitor.value and traitor_pick:
                self.teams[serial] = (random_choice * -1) - 1
            else:
                self.teams[serial] = random_choice
##            print("doing random choice")
##            print(random_choice)
            team_pick.remove(random_choice)
            if not team_pick:
                traitor_pick = False
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            self.alive_moves.append(move_serial)
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            proc = Process(target=track_move, args=(move_serial,
                                                    move_num,
                                                    self.game_mode,
                                                    self.teams[move_serial],
                                                    self.team_num,
                                                    dead_move,
                                                    force_color,
                                                    self.music_speed,
                                                    self.werewolf_reveal,
                                                    self.show_team_colors))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            
    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.audo_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.audo_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.audo_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.audo_toggle:
            self.start_game.start_effect()

    def get_change_time(self, speed_up):
        min_moves = len(self.move_serials) - 2
        if min_moves <= 0:
            min_moves = 1
        
        game_percent = (self.num_dead/min_moves)
        if game_percent > 1.0:
            game_percent = 1.0
        min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME, END_MIN_MUSIC_FAST_TIME, game_percent)
        max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME, END_MAX_MUSIC_FAST_TIME, game_percent)

        min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME, END_MIN_MUSIC_SLOW_TIME, game_percent)
        max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME, END_MAX_MUSIC_SLOW_TIME, game_percent)
        if speed_up:
            added_time = random.uniform(min_music_fast, max_music_fast)
        else:
            added_time = random.uniform(min_music_slow, max_music_slow)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
            self.audio.change_chunk_size(True)
        elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up =  not self.speed_up
            self.change_time = self.get_change_time(speed_up = self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False
            self.audio.change_chunk_size(False)

    def get_real_team(self, team):
        if team < 0:
            return -1
        else:
            return team

    def reveal(self):
        self.werewolf_reveal.value = 2

    def werewolf_audio_cue(self):
        if self.game_mode == common.Games.WereJoust.value:
            #print self.werewolf_timer - (time.time() - self.start_timer)
            if self.werewolf_timer - (time.time() - self.start_timer) <= 30 and self.audio_cue == 0:
                Audio('audio/Joust/sounds/30 werewolf.wav').start_effect()
                self.audio_cue = 1
            if self.werewolf_timer - (time.time() - self.start_timer) <= 10 and self.audio_cue == 1:
                Audio('audio/Joust/sounds/10 werewolf.wav').start_effect()
                self.audio_cue = 2
            if self.werewolf_timer - (time.time() - self.start_timer) <= 0 and self.audio_cue == 2:
                Audio('audio/Joust/sounds/werewolf reveal 2.wav').start_effect()
                self.reveal()
                self.audio_cue = 3
                self.change_time = time.time()-0.001
            elif self.audio_cue == 3:
                self.check_music_speed()
            
                

    def check_end_game(self):
        winning_team = -100
        team_win = True
        for move_serial, dead in self.dead_moves.items():
            #if we are alive
            if dead.value == 1:
                if winning_team == -100:
                    winning_team = self.get_real_team(self.teams[move_serial])
                elif self.get_real_team(self.teams[move_serial]) != winning_team:
                    team_win = False
            if dead.value == 0:
                #This is to play the sound effect
                self.num_dead += 1
                dead.value = -1
                if self.audo_toggle:
                    self.explosion.start_effect()
                
        if team_win:
            self.update_status('ending',winning_team)
            if self.audo_toggle:
                self.end_game_sound(winning_team)
            for move_serial in self.teams.keys():
                if self.get_real_team(self.teams[move_serial]) == winning_team:
                    self.winning_moves.append(move_serial)
            self.game_end = True

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        if self.audo_toggle:
            self.audio.stop_audio()
        end_time = time.time() + END_GAME_PAUSE
                
        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        #if self.game_mode != common.Games.Traitor.value:
        

        if self.game_mode == common.Games.JoustTeams.value:
            if winning_team == 0:
                team_win = Audio('audio/Joust/sounds/yellow team win.wav')
            if winning_team == 1:
                team_win = Audio('audio/Joust/sounds/green team win.wav')
            if winning_team == 2:
                team_win = Audio('audio/Joust/sounds/cyan team win.wav')
            if winning_team == 3:
                team_win = Audio('audio/Joust/sounds/blue team win.wav')
            if winning_team == 4:
                team_win = Audio('audio/Joust/sounds/magenta team win.wav')
            if winning_team == 5:
                team_win = Audio('audio/Joust/sounds/red team win.wav')
            team_win.start_effect()
        if self.game_mode == common.Games.JoustRandomTeams.value:
            if self.team_num == 2:
                if winning_team == 0:
                    team_win = Audio('audio/Joust/sounds/cyan team win.wav')
                if winning_team == 1:
                    team_win = Audio('audio/Joust/sounds/red team win.wav')
            if self.team_num == 3:
                if winning_team == 0:
                    team_win = Audio('audio/Joust/sounds/green team win.wav')
                if winning_team == 1:
                    team_win = Audio('audio/Joust/sounds/blue team win.wav')
                if winning_team == 2:
                    team_win = Audio('audio/Joust/sounds/red team win.wav')
            else:
                if winning_team == 0:
                    team_win = Audio('audio/Joust/sounds/yellow team win.wav')
                if winning_team == 1:
                    team_win = Audio('audio/Joust/sounds/cyan team win.wav')
                if winning_team == 2:
                    team_win = Audio('audio/Joust/sounds/magenta team win.wav')
                if winning_team == 3:
                    team_win = Audio('audio/Joust/sounds/red team win.wav')
            team_win.start_effect()

        if self.game_mode == common.Games.Traitor.value:
            if self.team_num == 2:
                if winning_team == -1:
                    team_win = Audio('audio/Joust/sounds/traitor win.wav')
                if winning_team == 0:
                    team_win = Audio('audio/Joust/sounds/cyan team win.wav')
                if winning_team == 1:
                    team_win = Audio('audio/Joust/sounds/red team win.wav')
            else:
                if winning_team == -1:
                    team_win = Audio('audio/Joust/sounds/traitor win.wav')
                if winning_team == 0:
                    team_win = Audio('audio/Joust/sounds/green team win.wav')
                if winning_team == 1:
                    team_win = Audio('audio/Joust/sounds/blue team win.wav')
                if winning_team == 2:
                    team_win = Audio('audio/Joust/sounds/red team win.wav')
            team_win.start_effect()
            
        if self.game_mode == common.Games.WereJoust.value:
            if winning_team == -1:
                team_win = Audio('audio/Joust/sounds/werewolf win.wav')
            else:
                team_win = Audio('audio/Joust/sounds/human win.wav')
            team_win.start_effect()
        #self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        
    def werewolf_intro(self):
        Audio('audio/Joust/sounds/werewolf intro.wav').start_effect()
        time.sleep(3)
        self.change_all_move_colors(80, 0, 0)
        time.sleep(2)
        self.change_all_move_colors(30, 0, 0)
        time.sleep(14)
        self.change_all_move_colors(20, 20, 20)
        time.sleep(6)
        self.start_timer = time.time()
        

    def game_loop(self):
        self.track_moves()
        if self.game_mode == common.Games.WereJoust.value:
            self.werewolf_intro()
        self.werewolf_reveal.value = 1
        if self.game_mode == common.Games.JoustRandomTeams.value:
            if self.audo_toggle:
                Audio('audio/Joust/sounds/teams_form.wav').start_effect()
            self.show_team_colors.value = 1
            time.sleep(6)
        self.show_team_colors.value = 0
        self.count_down()
        self.change_time = time.time() + 6
        time.sleep(0.02)
        if self.audo_toggle:
            self.audio.start_audio_loop()
            self.audio.change_ratio(self.music_speed.value)
        else:
            #when no audio is playing set the music speed to middle speed
            self.music_speed.value = (FAST_MUSIC_SPEED + SLOW_MUSIC_SPEED) / 2

            
        time.sleep(0.8)
        if self.game_mode == common.Games.WereJoust.value:
            self.music_speed.value = SLOW_MUSIC_SPEED
            self.audio.change_ratio(self.music_speed.value)
            self.speed_up = False
        
        while self.running:
            #I think the loop is so fast that this causes 
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            if self.game_mode != common.Games.WereJoust.value and self.audo_toggle:
                self.check_music_speed()
            self.check_end_game()
            if self.audo_toggle:
                self.werewolf_audio_cue()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
         
    def check_command_queue(self):
        package = None
        while not(self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not(package == None):
            if command == 'killgame':
                self.kill_game()

    def update_status(self,game_status,winning_team=-1):
        data ={'game_status' : game_status,
               'game_mode' : common.game_mode_names[self.game_mode],
               'winning_team' : winning_team}
        if self.game_mode == common.Games.JoustFFA.value:
            data['total_players'] = len(self.move_serials)
            data['remaining_players'] = len([x[0] for x in self.dead_moves.items() if x[1].value==1])
        else:
            if self.game_mode in [common.Games.WereJoust.value, common.Games.Traitor.value]:
                num = self.team_num + 1
                data['winning_team'] += 1
            else:
                num = self.team_num
            team_alive = [0]*num
            team_total = [0]*num
            for move in self.move_serials:
                team = self.teams[move]
                if self.game_mode in [common.Games.WereJoust.value, common.Games.Traitor.value]:
                        team += 1 #shift so bad guy team is 0
                        if team < 0:
                            team = 0
                team_total[team] += 1
                if self.dead_moves[move].value == 1:
                    team_alive[team] += 1
            team_comp = list(zip(team_total,team_alive))
            data['team_comp'] = team_comp
        if self.game_mode == common.Games.WereJoust.value:
            thyme = int(self.werewolf_timer - (time.time() - self.start_timer))
            if thyme < 0:
                data['time_to_reveal'] = 0
            else:
                data['time_to_reveal'] = thyme

        self.status_ns.status_dict = data

    def kill_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')        
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE     
        
        h_value = 0
        while (time.time() < end_time):
            time.sleep(0.01)
            color = common.hsv2rgb(h_value, 1, 1)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                common.change_color(color_array, *color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0

        # bright = 255
        # while (time.time() < end_time):
        #     time.sleep(0.01)
        #     color = (bright,bright,bright)
        #     for move in all_moves:
        #         color_array = self.force_move_colors[move]
        #         common.change_color(color_array, *color)
        #     bright = bright - 1
        #     if bright < 10:
        #         bright = 10

        self.running = False
                
                
        
        

            
        

            
Пример #14
0
class Joust():
    def __init__(self, moves, command_queue, ns, music, teams, game_mode,
                 controller_teams, controller_colors, dead_moves,
                 force_move_colors, music_speed, werewolf_reveal,
                 show_team_colors, red_on_kill, restart):
        self.command_queue = command_queue
        self.ns = ns
        self.voice = self.ns.settings['menu_voice']
        print(self.ns.settings)

        self.game_mode = game_mode

        #save locally in case settings change from web
        self.play_audio = self.ns.settings['play_audio']
        self.sensitivity = self.ns.settings['sensitivity']
        self.color_lock = self.ns.settings['color_lock']
        self.color_lock_choices = self.ns.settings['color_lock_choices']
        self.random_teams = self.ns.settings['random_teams']
        self.red_on_kill = self.ns.settings['red_on_kill']

        self.move_serials = moves
        self.restart = restart
        self.dead_moves = dead_moves
        self.music_speed = music_speed
        self.music_speed.value = SLOW_MUSIC_SPEED

        self.controller_teams = controller_teams
        self.controller_colors = controller_colors

        self.running = True
        self.force_move_colors = force_move_colors
        self.teams = teams
        self.num_teams = len(colors.team_color_list)

        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_dead = 0
        self.show_team_colors = show_team_colors
        self.show_team_colors.value = 0

        self.non_stop_deaths = {}
        for move in self.move_serials:
            self.non_stop_deaths[move] = 0
        self.non_stop_time = time.time() + 150

        self.update_time = 0
        self.alive_moves = []

        #self.update_status('starting')

        self.werewolf_reveal = werewolf_reveal
        self.werewolf_reveal.value = 2
        if game_mode == common.Games.JoustFFA or game_mode == common.Games.NonStop:
            self.num_teams = len(moves)
        if game_mode == common.Games.JoustRandomTeams:
            if len(moves) <= 5:
                self.num_teams = 2
            elif len(moves) in [6, 7]:
                self.num_teams = 3
            else:  #8 or more
                self.num_teams = 4
        if game_mode == common.Games.Traitor:

            if len(moves) <= 8:
                self.num_teams = 2
            else:  #9 or more
                self.num_teams = 3
            self.werewolf_reveal.value = 0

        if self.game_mode == common.Games.WereJoust:
            self.werewolf_reveal.value = 0
            self.num_teams = 1

        if self.game_mode == common.Games.JoustTeams:
            self.team_colors = colors.team_color_list
        else:
            self.team_colors = colors.generate_team_colors(
                self.num_teams, self.color_lock, self.color_lock_choices)
            self.generate_random_teams(self.num_teams)

        if self.game_mode == common.Games.WereJoust:

            #were_num = int((len(moves)+2)/4)
            were_num = int((len(moves) * 7) / 16)
            if were_num <= 0:
                were_num = 1
            self.choose_werewolf(were_num)
        if self.play_audio:
            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            self.revive = Audio('audio/Commander/sounds/revive.wav')
            end = False
            #self.audio = Audio(music, end)
            self.audio = music
        fast_resample = False

        #self.change_time = self.get_change_time(speed_up = True)

        self.speed_up = False
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []

        self.game_loop()

    def choose_werewolf(self, were_num):
        for were in range(were_num):
            werewolf = random.choice(self.move_serials)
            while self.teams[werewolf] < 0:
                werewolf = random.choice(self.move_serials)
            self.teams[werewolf] = (self.teams[werewolf] * -1) - 1

    def generate_random_teams(self, num_teams):
        if self.random_teams == False and self.game_mode != common.Games.Traitor:
            players_per_team = (len(self.move_serials) // num_teams) + 1
            team_num = [x for x in range(num_teams)] * players_per_team
            for num, move in zip(team_num, self.move_serials):
                self.teams[move] = num
        else:
            team_pick = list(range(num_teams))
            traitor_pick = True
            copy_serials = self.move_serials[:]

            while len(copy_serials) >= 1:
                #for serial in self.move_serials:
                serial = random.choice(copy_serials)
                copy_serials.remove(serial)
                random_choice = random.choice(team_pick)
                if self.game_mode == common.Games.Traitor and traitor_pick:
                    self.teams[serial] = (random_choice * -1) - 1
                    #Turn this off for 3 traitors vs 1
                    traitor_pick = False
                else:
                    self.teams[serial] = random_choice
                team_pick.remove(random_choice)
                if not team_pick:
                    traitor_pick = False
                    team_pick = list(range(num_teams))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            self.alive_moves.append(move_serial)
            time.sleep(0.1)
            self.controller_teams[move_serial].value = self.teams[move_serial]
            self.controller_colors[move_serial][0] = self.team_colors[
                self.teams[move_serial]].value[0]
            self.controller_colors[move_serial][1] = self.team_colors[
                self.teams[move_serial]].value[1]
            self.controller_colors[move_serial][2] = self.team_colors[
                self.teams[move_serial]].value[2]
            self.dead_moves[move_serial].value = 1
            self.force_move_colors[move_serial][0] = 1
            self.force_move_colors[move_serial][1] = 1
            self.force_move_colors[move_serial][2] = 1

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.play_audio:
            self.start_game.start_effect()

    def get_change_time(self, speed_up):
        min_moves = len(self.move_serials) - 2
        if min_moves <= 0:
            min_moves = 1

        game_percent = (self.num_dead / min_moves)
        if game_percent > 1.0:
            game_percent = 1.0
        min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME,
                                     END_MIN_MUSIC_FAST_TIME, game_percent)
        max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME,
                                     END_MAX_MUSIC_FAST_TIME, game_percent)

        min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME,
                                     END_MIN_MUSIC_SLOW_TIME, game_percent)
        max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME,
                                     END_MAX_MUSIC_SLOW_TIME, game_percent)
        if speed_up:
            added_time = random.uniform(min_music_fast, max_music_fast)
        else:
            added_time = random.uniform(min_music_slow, max_music_slow)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip(
            (time.time() - self.change_time) / INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED,
                                                 SLOW_MUSIC_SPEED,
                                                 change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED,
                                                 FAST_MUSIC_SPEED,
                                                 change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time(
        ) < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
        elif time.time(
        ) >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up = not self.speed_up
            self.change_time = self.get_change_time(speed_up=self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False

    def get_real_team(self, team):
        if team < 0:
            return -1
        else:
            return team

    def reveal(self):
        self.werewolf_reveal.value = 2

    def werewolf_audio_cue(self):
        if self.game_mode == common.Games.WereJoust:
            if self.werewolf_timer - (time.time() - self.start_timer
                                      ) <= 30 and self.audio_cue == 0:
                Audio('audio/Joust/vox/' + self.voice +
                      '/30 werewolf.wav').start_effect()
                self.audio_cue = 1
            if self.werewolf_timer - (time.time() - self.start_timer
                                      ) <= 10 and self.audio_cue == 1:
                Audio('audio/Joust/vox/' + self.voice +
                      '/10 werewolf.wav').start_effect()
                self.audio_cue = 2
            if self.werewolf_timer - (time.time() - self.start_timer
                                      ) <= 0 and self.audio_cue == 2:
                Audio('audio/Joust/vox/' + self.voice +
                      '/werewolf reveal 2.wav').start_effect()
                self.reveal()
                self.audio_cue = 3
                self.change_time = time.time() - 0.001
            elif self.audio_cue == 3:
                self.check_music_speed()

    def check_end_game(self):
        winning_team = -100
        team_win = True
        for move_serial, dead in self.dead_moves.items():
            #if we are alive
            if dead.value == 1:
                if winning_team == -100:
                    winning_team = self.get_real_team(self.teams[move_serial])
                elif self.get_real_team(
                        self.teams[move_serial]) != winning_team:
                    team_win = False
            if dead.value == 0:
                #This is to play the sound effect
                self.num_dead += 1
                dead.value = -1
                self.non_stop_deaths[move_serial] += 1
                if self.play_audio:
                    self.explosion.start_effect()
            if dead.value == 2:
                dead.value = 1
                if self.play_audio:
                    self.revive.start_effect()

        if self.game_mode == common.Games.NonStop:
            if self.audio_cue == 0 and time.time() > self.non_stop_time - 60:
                Audio('audio/Zombie/vox/' + self.voice +
                      '/1 minute.wav').start_effect()
                self.audio_cue += 1
            if self.audio_cue == 1 and time.time() > self.non_stop_time - 30:
                Audio('audio/Zombie/vox/' + self.voice +
                      '/30 seconds.wav').start_effect()
                self.audio_cue += 1
            if time.time() > self.non_stop_time:
                lowest_score = 100000
                for move, score in self.non_stop_deaths.items():
                    self.dead_moves[move].value = 0
                    if score == lowest_score:
                        self.winning_moves.append(move)
                    if score < lowest_score:
                        lowest_score = score
                        self.winning_moves = []
                        self.winning_moves.append(move)
                self.game_end = True

        elif team_win:
            self.update_status('ending', winning_team)
            if self.play_audio:
                self.end_game_sound(winning_team)
            for move_serial in self.teams.keys():
                if self.get_real_team(self.teams[move_serial]) == winning_team:
                    self.winning_moves.append(move_serial)
            self.game_end = True

    def stop_tracking_moves(self):
        self.restart.value = 1

    def end_game(self):
        if self.play_audio:
            self.audio.stop_audio()
        end_time = time.time() + END_GAME_PAUSE

        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                colors.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        if self.game_mode == common.Games.WereJoust:
            if winning_team == -1:
                team_win = Audio('audio/Joust/vox/' + self.voice +
                                 '/werewolf win.wav')
            else:
                team_win = Audio('audio/Joust/vox/' + self.voice +
                                 '/human win.wav')
            team_win.start_effect()
        elif self.game_mode != common.Games.JoustFFA and self.game_mode != common.Games.NonStop:
            win_team_name = self.team_colors[winning_team].name
            if winning_team == -1:
                team_win = Audio('audio/Joust/vox/' + self.voice +
                                 '/traitor win.wav')
            else:
                if win_team_name == 'Pink':
                    if self.voice == 'aaron':
                        os.popen(
                            'espeak -ven -p 70 -a 200 "And the winner is ...Pink Team'
                        )
                    else:
                        team_win = Audio('audio/Joust/vox/' + self.voice +
                                         '/pink team win.wav')
                if win_team_name == 'Magenta':
                    team_win = Audio('audio/Joust/vox/' + self.voice +
                                     '/magenta team win.wav')
                if win_team_name == 'Orange':
                    if self.voice == 'aaron':
                        os.popen(
                            'espeak -ven -p 70 -a 200 "And the winner is ...Orange Team'
                        )
                    else:
                        team_win = Audio('audio/Joust/vox/' + self.voice +
                                         '/orange team win.wav')
                if win_team_name == 'Yellow':
                    team_win = Audio('audio/Joust/vox/' + self.voice +
                                     '/yellow team win.wav')
                if win_team_name == 'Green':
                    team_win = Audio('audio/Joust/vox/' + self.voice +
                                     '/green team win.wav')
                if win_team_name == 'Turquoise':
                    team_win = Audio('audio/Joust/vox/' + self.voice +
                                     '/cyan team win.wav')
                if win_team_name == 'Blue':
                    team_win = Audio('audio/Joust/vox/' + self.voice +
                                     '/blue team win.wav')
                if win_team_name == 'Purple':
                    if self.voice == 'aaron':
                        os.popen(
                            'espeak -ven -p 70 -a 200 "And the winner is ...Purple Team'
                        )
                    else:
                        team_win = Audio('audio/Joust/vox/' + self.voice +
                                         '/purple team win.wav')
            try:
                team_win.start_effect()
            except:
                pass

    def werewolf_intro(self):
        #don't wait so colors change during prompts
        Audio('audio/Joust/vox/' + self.voice +
              '/werewolf intro.wav').start_effect()
        time.sleep(3)
        self.change_all_move_colors(0, 0, 80)
        time.sleep(2)
        self.change_all_move_colors(0, 0, 30)
        time.sleep(14)
        self.change_all_move_colors(20, 20, 20)
        time.sleep(6)
        self.start_timer = time.time()

    def game_loop(self):
        self.track_moves()
        self.restart.value = 0
        if self.game_mode == common.Games.WereJoust:
            self.werewolf_intro()
        self.werewolf_reveal.value = 1
        if self.game_mode == common.Games.JoustRandomTeams:
            self.show_team_colors.value = 1
            if self.play_audio:
                Audio('audio/Joust/sounds/teams_form.wav'
                      ).start_effect_and_wait()
        self.show_team_colors.value = 0
        self.count_down()
        self.change_time = time.time() + 6
        time.sleep(0.02)
        if self.play_audio:
            self.audio.start_audio_loop()
            self.audio.change_ratio(self.music_speed.value)
        else:
            #when no audio is playing set the music speed to middle speed
            self.music_speed.value = (FAST_MUSIC_SPEED + SLOW_MUSIC_SPEED) / 2

        time.sleep(0.8)
        if self.game_mode == common.Games.WereJoust:
            self.music_speed.value = SLOW_MUSIC_SPEED
            self.audio.change_ratio(self.music_speed.value)
            self.speed_up = False

        while self.running:
            #I think the loop is so fast that this causes
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            if self.game_mode != common.Games.WereJoust and self.play_audio:
                self.check_music_speed()
            self.check_end_game()
            if self.play_audio:
                self.werewolf_audio_cue()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': self.game_mode.pretty_name,
            'winning_team': winning_team
        }
        if self.game_mode == common.Games.JoustFFA or self.game_mode == common.Games.NonStop:
            data['total_players'] = len(self.move_serials)
            data['remaining_players'] = len(
                [x[0] for x in self.dead_moves.items() if x[1].value == 1])
        else:
            if self.game_mode in [
                    common.Games.WereJoust, common.Games.Traitor
            ]:
                num = self.num_teams + 1
                data['winning_team'] += 1
            else:
                num = self.num_teams
            team_alive = [0] * num
            team_total = [0] * num

            for move in self.move_serials:
                team = self.teams[move]
                if self.game_mode in [
                        common.Games.WereJoust, common.Games.Traitor
                ]:
                    team += 1  #shift so bad guy team is 0
                    if team < 0:
                        team = 0
                team_total[team] += 1
                if self.dead_moves[move].value == 1:
                    team_alive[team] += 1
            team_comp = list(zip(team_total, team_alive))
            data['team_comp'] = team_comp
            if self.game_mode == common.Games.WereJoust:
                data['team_names'] = ['Werewolves', 'Humans']
            elif self.game_mode == common.Games.Traitor:
                data['team_names'] = ['Traitors'] + [
                    color.name + ' Team' for color in self.team_colors
                ]
            elif self.game_mode != common.Games.JoustFFA or self.game_mode != common.Games.NonStop:
                data['team_names'] = [
                    color.name + ' Team' for color in self.team_colors
                ]
        if self.game_mode == common.Games.WereJoust:
            thyme = int(self.werewolf_timer - (time.time() - self.start_timer))
            if thyme < 0:
                data['time_to_reveal'] = 0
            else:
                data['time_to_reveal'] = thyme

        self.ns.status = data

    def kill_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright, 0, 0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10

        self.running = False
Пример #15
0
class Commander():
    def __init__(self, moves, command_queue, ns, music):

        self.command_queue = command_queue
        self.ns = ns

        self.sensitivity = self.ns.settings['sensitivity']
        self.random_teams = self.ns.settings['random_teams']

        global SLOW_MAX
        global SLOW_WARNING
        global FAST_MAX
        global FAST_WARNING

        SLOW_MAX = common.SLOW_MAX[self.sensitivity]
        SLOW_WARNING = common.SLOW_WARNING[self.sensitivity]
        FAST_MAX = common.FAST_MAX[self.sensitivity]
        FAST_WARNING = common.FAST_WARNING[self.sensitivity]

        self.update_time = 0

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.num_teams = 2
        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}
        self.current_commander = ["", ""]

        self.time_to_power = [20, 20]
        self.activated_time = [time.time(), time.time()]

        self.activated_overdrive = [time.time(), time.time()]

        self.powers = [Value('d', 0.0), Value('d', 0.0)]

        self.alpha_overdrive = Value('i', 0)
        self.bravo_overdrive = Value('i', 0)

        self.generate_random_teams(self.num_teams)
        self.commander_intro = Value('i', 1)

        self.powers_active = [False, False]

        ##        try:
        ##            music = 'audio/Commander/music/' + random.choice(os.listdir('audio/Commander/music'))
        ##        except:
        ##            print('no music in audio/Commander/music')
        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        end = False
        try:
            self.audio = music
        except:
            print('no audio loaded')
        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []

        self.game_loop()

    def generate_random_teams(self, num_teams):
        if self.random_teams == False:
            players_per_team = (len(self.move_serials) // num_teams) + 1
            team_num = [x for x in range(num_teams)] * players_per_team
            for num, move in zip(team_num, self.move_serials):
                self.teams[move] = num
        else:
            team_pick = list(range(num_teams))
            for serial in self.move_serials:
                random_choice = random.choice(team_pick)
                self.teams[serial] = random_choice
                team_pick.remove(random_choice)
                if not team_pick:
                    team_pick = list(range(num_teams))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            opts = Array('i', [0] * 5)
            power = self.powers[self.teams[move_serial]]

            if self.teams[move_serial] == Team.alpha.value:
                overdrive = self.alpha_overdrive
            else:
                overdrive = self.bravo_overdrive
            proc = Process(target=track_move,
                           args=(move_serial, move_num,
                                 self.teams[move_serial], self.num_teams,
                                 dead_move, force_color, self.music_speed,
                                 self.commander_intro, opts, power, overdrive))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        if speed_up:
            added_time = random.uniform(MIN_MUSIC_FAST_TIME,
                                        MAX_MUSIC_FAST_TIME)
        else:
            added_time = random.uniform(MIN_MUSIC_SLOW_TIME,
                                        MAX_MUSIC_SLOW_TIME)
        return time.time() + added_time

    def get_winning_team_members(self, winning_team):
        self.end_game_sound(winning_team)
        for move_serial in self.teams.keys():
            if self.teams[move_serial] == winning_team:
                self.winning_moves.append(move_serial)

    def check_end_game(self):
        winning_team = -100
        team_win = False
        for commander in self.current_commander:
            if self.dead_moves[commander].value <= 0:
                winning_team = (self.teams[commander] + 1) % 2
                self.get_winning_team_members(winning_team)
                self.game_end = True

                self.update_status('ending', winning_team)

        for move_serial, dead in self.dead_moves.items():
            if dead.value == 0:
                dead_team = self.teams[move_serial]
                winning_team = (self.teams[move_serial] + 1) % 2
                if self.time_to_power[winning_team] > 15:
                    self.time_to_power[winning_team] -= 1
                if self.time_to_power[dead_team] < 25:
                    self.time_to_power[dead_team] += 1

                #This is to play the sound effect
                dead.value = -1
                self.explosion.start_effect()

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                colors.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        #if self.game_mode == common.Games.JoustTeams:
        if winning_team == Team.alpha.value:
            team_win = Audio('audio/Commander/sounds/red winner.wav')
        if winning_team == Team.bravo.value:
            team_win = Audio('audio/Commander/sounds/blue winner.wav')
        team_win.start_effect()

    def check_commander_select(self):
        for move_serial in self.move_opts.keys():
            if self.move_opts[move_serial][
                    Opts.selection.
                    value] == Selections.triangle.value and self.move_opts[
                        move_serial][
                            Opts.holding.value] == Holding.holding.value:
                Audio('audio/Commander/sounds/commanderselect.wav'
                      ).start_effect()
                self.change_commander(move_serial)
                self.move_opts[move_serial][
                    Opts.selection.value] = Selections.nothing.value
            elif self.move_opts[move_serial][
                    Opts.selection.
                    value] == Selections.a_button.value and self.move_opts[
                        move_serial][
                            Opts.holding.value] == Holding.holding.value:
                Audio('audio/Commander/sounds/buttonselect.wav').start_effect()
                self.move_opts[move_serial][
                    Opts.selection.value] = Selections.nothing.value

    def change_commander(self, new_commander):
        #print 'changing commander to ' + str(new_commander)
        commander_team = self.teams[new_commander]
        if self.current_commander[commander_team] != '':
            self.move_opts[self.current_commander[commander_team]][
                Opts.is_commander.value] = Bool.no.value

        self.move_opts[new_commander][Opts.is_commander.value] = Bool.yes.value
        self.current_commander[commander_team] = new_commander

    def change_random_commander(self, team, exclude_commander=None):
        team_move_serials = [
            move_serial for move_serial in self.move_opts.keys()
            if (self.teams[move_serial] == team and move_serial !=
                exclude_commander and self.dead_moves[move_serial].value >= 1)
        ]
        print('team move serials is ' + str(team_move_serials))
        if len(team_move_serials) > 0:
            new_commander = random.choice(team_move_serials)
            self.change_commander(new_commander)
            return True
        return False

    def update_team_powers(self):
        self.powers[Team.alpha.value].value = max(
            min((time.time() - self.activated_time[Team.alpha.value]) /
                (self.time_to_power[Team.alpha.value] * 1.0), 1.0), 0.0)
        self.powers[Team.bravo.value].value = max(
            min((time.time() - self.activated_time[Team.bravo.value]) /
                (self.time_to_power[Team.bravo.value] * 1.0), 1.0), 0.0)

        if self.powers_active[Team.alpha.value] == False:
            if self.powers[Team.alpha.value].value >= 1.0:
                self.powers_active[Team.alpha.value] = True
                Audio('audio/Commander/sounds/power ready.wav').start_effect()
                Audio('audio/Commander/sounds/red power ready.wav'
                      ).start_effect()

        if self.powers_active[Team.bravo.value] == False:
            if self.powers[Team.bravo.value].value >= 1.0:
                self.powers_active[Team.bravo.value] = True
                Audio('audio/Commander/sounds/power ready.wav').start_effect()
                Audio('audio/Commander/sounds/blue power ready.wav'
                      ).start_effect()

    def overdrive(self, team):
        Audio('audio/Commander/sounds/overdrive.wav').start_effect()
        if team == Team.alpha.value:
            self.alpha_overdrive.value = 1
            self.activated_overdrive[Team.alpha.value] = time.time() + 10
            Audio('audio/Commander/sounds/red overdrive.wav').start_effect()
        else:
            self.bravo_overdrive.value = 1
            self.activated_overdrive[Team.bravo.value] = time.time() + 10
            Audio('audio/Commander/sounds/blue overdrive.wav').start_effect()

    def check_end_of_overdrive(self):
        if self.alpha_overdrive.value == 1:

            if time.time() >= self.activated_overdrive[Team.alpha.value]:
                #print 'its over'
                self.alpha_overdrive.value = 0
        if self.bravo_overdrive.value == 1:

            if time.time() >= self.activated_overdrive[Team.bravo.value]:
                #print 'itsa over'
                self.bravo_overdrive.value = 0

    def reset_power(self, team):
        self.powers[team].value == 0.0
        self.activated_time[team] = time.time()
        self.powers_active[team] = False

    def check_commander_power(self):
        for commander in self.current_commander:
            #print self.move_opts[commander][Opts.selection]
            if self.move_opts[commander][
                    Opts.selection.value] == Selections.trigger.value:
                self.overdrive(self.teams[commander])
                self.reset_power(self.teams[commander])
                self.move_opts[commander][
                    Opts.selection.value] = Selections.nothing.value

    def check_everyone_in(self):
        for move_serial in self.move_opts.keys():
            if self.move_opts[move_serial][
                    Opts.holding.value] == Holding.not_holding.value:
                return False
        return True

    def commander_intro_audio(self):
        intro_sound = Audio('audio/Commander/sounds/commander intro.wav')
        intro_sound.start_effect()
        #need while loop here
        play_last_one = True
        commander_select_time = time.time() + 50
        battle_ready_time = time.time() + 40
        while time.time() < commander_select_time:
            self.check_commander_select()
            if self.check_everyone_in():
                break

            if time.time() > battle_ready_time and play_last_one:
                play_last_one = False
                Audio('audio/Commander/sounds/10 seconds begins.wav'
                      ).start_effect()
        intro_sound.stop_effect()

        if self.current_commander[Team.alpha.value] == '':
            self.change_random_commander(Team.alpha.value)
        if self.current_commander[Team.bravo.value] == '':
            self.change_random_commander(Team.bravo.value)

        Audio('audio/Commander/sounds/commanders chosen.wav'
              ).start_effect_and_wait()
        self.reset_power(Team.alpha.value)
        self.reset_power(Team.bravo.value)
        self.commander_intro.value = 0

    def game_loop(self):
        self.track_moves()
        self.commander_intro_audio()

        self.count_down()
        time.sleep(0.02)
        try:
            self.audio.start_audio_loop()
        except:
            print('no audio loaded to start')
        time.sleep(0.8)

        while self.running:
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            self.update_team_powers()
            self.check_commander_power()
            self.check_end_of_overdrive()
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

# class Team(Enum):
#     alpha = 0
#     bravo = 1

#self.dead_moves[move_serial] = dead_move
#1=alive
#0=dead

    def update_status(self, game_status, winning_team=-1):
        if self.alpha_overdrive.value == 1:
            alpha_od_status = 'Active'
        elif self.powers_active[Team.alpha.value] == True:
            alpha_od_status = 'Ready'
        else:
            alpha_od_status = 'Charging'

        if self.bravo_overdrive.value == 1:
            bravo_od_status = 'Active'
        elif self.powers_active[Team.bravo.value] == True:
            bravo_od_status = 'Ready'
        else:
            bravo_od_status = 'Charging'

        alpha_team = [x for x in self.teams.keys() if self.teams[x] == 0]
        alpha_alive = [x for x in alpha_team if self.dead_moves[x].value == 1]

        bravo_team = [x for x in self.teams.keys() if self.teams[x] == 1]
        bravo_alive = [x for x in bravo_team if self.dead_moves[x].value == 1]

        data = {
            'game_status': game_status,
            'game_mode': 'Commander',
            'winning_team': winning_team,
            'alpha_players': len(alpha_team),
            'alpha_alive': len(alpha_alive),
            'alpha_od_status': alpha_od_status,
            'bravo_players': len(bravo_team),
            'bravo_alive': len(bravo_alive),
            'bravo_od_status': bravo_od_status,
            'team_names': ['Orange Team', 'Blue Team']
        }

        self.ns.status = data

    def kill_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright, 0, 0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10

        self.running = False
Пример #16
0
class Bomb():
    def __init__(self, moves, command_queue, status_ns, audio_toggle, music):

        self.audio_toggle = audio_toggle
        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.alive_moves = []
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.bomb_color = Array('i', [0] * 3)
        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_bombs = 2
        self.move_opts = {}
        self.false_colors = {}
        self.was_faked = {}
        self.rumble = {}
        self.bomb_length = 5.0

        self.game_start = Value('i', 0)
        self.current_rand_holder = ''
        self.next_rand_holder = ''
        self.prev_rand_holder = ''

        self.command_queue = command_queue
        self.status_ns = status_ns
        self.update_time = 0

        if self.audio_toggle:
            ##            try:
            ##                music = 'audio/Commander/music/' + random.choice(os.listdir('audio/Commander/music'))
            ##            except:
            ##                print('no music in audio/Commander/music')
            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            self.fakedout = Audio('audio/Joust/sounds/Fakedout.wav')
            self.explosion40 = Audio('audio/Joust/sounds/Explosion40.wav')
            self.countered = Audio('audio/Joust/sounds/countered.wav')
            self.Fakecountered = Audio(
                'audio/Joust/sounds/FakedoutCounter.wav')
            self.explosiondeath = Audio(
                'audio/Joust/sounds/explosiondeath.wav')

            end = False
            try:
                self.audio = music
            except:
                print('no audio loaded')

        self.game_end = False

        self.game_loop()

    def reset_bomb_length(self):
        self.bomb_length = 4.0

    def get_bomb_length(self):
        self.bomb_length -= 0.3
        if self.bomb_length < 1:
            self.bomb_length = 1
        return self.bomb_length

    def get_prev_random_holder(self):
        return self.bomb_serial

    def get_next_random_holder(self):
        #if self.current_rand_holder != self.bomb_serial:
        #self.current_rand_holder = self.move_serials[random.choice(range(len(self.move_serials)))]
        #while self.current_rand_holder == self.bomb_serial:
        #    self.current_rand_holder = self.move_serials[random.choice(range(len(self.move_serials)))]
        if self.next_rand_holder == self.bomb_serial:
            self.next_rand_holder = self.move_serials[random.choice(
                range(len(self.move_serials)))]
            while self.next_rand_holder == self.bomb_serial or self.dead_moves[
                    self.next_rand_holder].value <= 0:
                self.next_rand_holder = self.move_serials[random.choice(
                    range(len(self.move_serials)))]
                self.current_rand_holder = self.bomb_serial
        #print("returning " + self.next_rand_holder)
        return self.next_rand_holder

    def get_next_bomb_holder(self, serial=None):
        if serial:
            holder = self.get_serial_pos(serial)
        else:
            holder = random.choice(range(len(self.move_serials)))
        while True:
            yield self.get_next_random_holder()
            #new_serial = self.move_serials[holder]
            #if self.dead_moves[new_serial].value > 0:
            #    yield new_serial
            #holder = (holder +1) % len(self.move_serials)

    def reset_bomb_time(self):
        self.bomb_time = time.time() + self.get_bomb_length()
        self.bomb_start_time = time.time()

    def game_loop(self):
        self.track_moves()
        self.rotate_colors()
        self.bomb_serial = self.move_serials[random.choice(
            range(len(self.move_serials)))]
        self.next_rand_holder = self.bomb_serial
        self.bomb_generator = self.get_next_bomb_holder()

        self.bomb_serial = next(self.bomb_generator)
        self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.yes.value

        self.holding = True
        self.game_start.value = 1
        self.count_down()
        time.sleep(0.02)
        if self.audio_toggle:
            try:
                self.audio.start_audio_loop()
            except:
                print('no audio loaded to start')
        time.sleep(0.8)

        self.bomb_time = time.time() + self.get_bomb_length()
        self.bomb_start_time = time.time()

        while self.running:

            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            percentage = 1 - ((self.bomb_time - time.time()) /
                              (self.bomb_time - self.bomb_start_time))

            if (percentage > 0.8):
                self.bomb_color[0] = random.randrange(
                    int(100 + 55 * percentage), int(200 + 55 * percentage))
            else:
                self.bomb_color[0] = int(common.lerp(90, 255, percentage))
            self.bomb_color[1] = int(common.lerp(30, 0, percentage))
            self.bomb_color[2] = int(common.lerp(30, 0, percentage))

            if self.move_opts[self.bomb_serial][
                    Opts.selection.value] == Selections.nothing.value:
                self.holding = False
            if self.move_opts[self.bomb_serial][
                    Opts.selection.
                    value] == Selections.a_button.value and self.holding == False:
                self.reset_bomb_time()
                self.move_bomb()
                if self.audio_toggle:
                    self.start_beep.start_effect()
                self.holding = True
            if time.time() > self.bomb_time:
                if self.audio_toggle:
                    self.explosiondeath.start_effect()

                    self.explosion.start_effect()
                self.pause_for_player_death(self.bomb_serial)

                self.dead_moves[self.bomb_serial].value -= 1

                self.reset_bomb_length()
                self.reset_bomb_time()

                #if player is dead move bomb
                if not self.dead_moves[self.bomb_serial].value > 0:
                    self.move_bomb()

                print("TIME BOMB")

            self.check_dead_moves()
            self.check_faked_out()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def move_bomb(self):
        self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.no.value
        self.bomb_serial = next(self.bomb_generator)
        self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.yes.value

    def pause_for_player_death(self, dead_move, faker_move=None):
        end_time = time.time() + 1.5
        while (time.time() < end_time):
            time.sleep(0.01)

            dead_color_array = self.force_move_colors[dead_move]
            if faker_move:
                faker_color_array = self.force_move_colors[faker_move]

            for move_serial in self.move_serials:
                #if move_serial == faker_move:
                #    common.change_color(faker_color_array, random.randrange(100, 200), 10, 10)
                if move_serial == dead_move:
                    common.change_color(dead_color_array, 10,
                                        random.randrange(100, 200), 10)
                    self.rumble[move_serial].value = 150
                else:
                    common.change_color(self.force_move_colors[move_serial], 1,
                                        1, 1)
        self.rumble[dead_move].value = 0
        self.change_all_move_colors(0, 0, 0)

    def check_faked_out(self):
        #check for one controller left first
        for move_serial in self.move_serials:

            if self.dead_moves[move_serial].value > 0:

                #if we faked play sound
                if self.move_opts[move_serial][
                        Opts.selection.
                        value] == Selections.false_trigger.value and self.move_opts[
                            move_serial][Opts.holding.
                                         value] == Holding.not_holding.value:
                    faker = self.get_next_serial(move_serial)
                    self.reset_bomb_time()
                    self.reset_bomb_length()
                    self.false_colors[faker].value = 1
                    if self.audio_toggle:
                        self.start_beep.start_effect()
                    self.move_opts[move_serial][
                        Opts.holding.value] = Holding.holding.value

                #we are being faked out
                if self.false_colors[move_serial].value == 1:
                    prev_faker = self.get_prev_serial(move_serial)

                    #Pushed middle button, when faked
                    if self.was_faked[move_serial].value == 1:
                        faker = self.get_prev_serial(move_serial)
                        if self.audio_toggle:
                            self.explosion40.start_effect()
                            self.fakedout.start_effect()
                        self.pause_for_player_death(move_serial, faker)

                        self.dead_moves[move_serial].value -= 1
                        self.was_faked[move_serial].value = 2

                        self.reset_bomb_time()
                        self.reset_bomb_length()
                        self.move_bomb()

                    elif self.move_opts[move_serial][
                            Opts.selection.value] == Selections.counter.value:
                        if self.audio_toggle:
                            self.explosion40.start_effect()
                            self.countered.start_effect()
                        self.pause_for_player_death(prev_faker, move_serial)

                        self.dead_moves[prev_faker].value -= 1
                        self.false_colors[move_serial].value = 0
                        self.move_opts[move_serial][
                            Opts.holding.value] = Holding.holding.value

                        self.reset_bomb_length()
                        self.reset_bomb_time()

                        self.move_bomb()

                        print("JUST DIED TO BEING COUNTERED")

                    #only do this once per move
                    if self.move_opts[prev_faker][
                            Opts.holding.value] == Holding.not_holding.value:
                        self.false_colors[move_serial].value = 0

                #Probably should get rid of this, or only when we are being faked out
                #elif self.false_colors[move_serial].value == 0  and self.move_opts[move_serial][Opts.holding.value] == Holding.holding.value :
                #    if self.move_opts[move_serial][Opts.selection.value] == Selections.counter.value and self.move_opts[self.get_prev_serial(move_serial)][Opts.has_bomb.value] == Bool.yes.value:

                #        self.explosion40.start_effect()
                #        self.Fakecountered.start_effect()
                #        self.pause_for_player_death(move_serial)

                #        self.dead_moves[move_serial].value -= 1
                #self.move_opts[move_serial][Opts.holding.value] = Holding.holding.value

                #        self.reset_bomb_length()
                #        self.reset_bomb_time()

                #        self.move_bomb()
                #        print("JUST DIED TO PRESSING COUNTER")

                #check for faked

    def get_next_serial(self, serial):
        return self.get_next_random_holder()

        pos = (self.get_serial_pos(serial) + 1) % len(self.move_serials)
        #pos = random.choice(range(len(self.move_serials)))
        #while random_move == pos - 1:
        #   random_move = random.choice(range(len(self.move_serials)))

        new_serial = self.move_serials[pos]
        while self.dead_moves[new_serial].value == 0:
            pos = (pos + 1) % len(self.move_serials)
            new_serial = self.move_serials[pos]
        return self.move_serials[pos]

    def get_prev_serial(self, serial):
        self.get_next_random_holder()
        return self.get_prev_random_holder()

        pos = (self.get_serial_pos(serial) - 1) % len(self.move_serials)
        new_serial = self.move_serials[pos]
        while self.dead_moves[new_serial].value == 0:
            pos = (pos - 1) % len(self.move_serials)
            new_serial = self.move_serials[pos]
        return self.move_serials[pos]

    def get_serial_pos(self, serial):
        for i, move_serial in enumerate(self.move_serials):
            if serial == move_serial:
                return i

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            self.alive_moves.append(move_serial)
            time.sleep(0.02)
            dead_move = Value('i', 2)
            force_color = Array('i', [1] * 3)
            false_color = Value('i', 0)

            opts = Array('i', [0] * 5)
            faked = Value('i', 0)
            rumble = Value('i', 0)

            proc = Process(target=track_move,
                           args=(move_serial, move_num, dead_move, force_color,
                                 self.bomb_color, opts, self.game_start,
                                 false_color, faked, rumble))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts
            self.false_colors[move_serial] = false_color
            self.was_faked[move_serial] = faked
            self.rumble[move_serial] = rumble

    def rotate_colors(self):
        move_on = False
        time_change = 0.5

        in_cons = []
        for move_serial_beg in self.move_serials:
            self.move_opts[move_serial_beg][
                Opts.has_bomb.value] = Bool.yes.value
            self.move_opts[move_serial_beg][
                Opts.holding.value] = Holding.holding.value
        while len(in_cons) != len(self.move_serials):
            for move_serial in self.move_serials:
                for move_serial_beg in self.move_serials:
                    if self.move_opts[move_serial_beg][
                            Opts.selection.value] == Selections.a_button.value:
                        if move_serial_beg not in in_cons:
                            if self.audio_toggle:
                                self.start_beep.start_effect()
                            in_cons.append(move_serial_beg)
                    if move_serial_beg in in_cons:
                        common.change_color(
                            self.force_move_colors[move_serial_beg], 100, 100,
                            100)
                common.change_color(self.force_move_colors[move_serial], 100,
                                    0, 0)
                time.sleep(0.5)
                common.change_color(self.force_move_colors[move_serial], 0, 0,
                                    0)
        for move_serial_beg in self.move_serials:
            self.move_opts[move_serial_beg][
                Opts.has_bomb.value] = Bool.no.value

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.audio_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.audio_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.audio_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.audio_toggle:
            self.start_game.start_effect()

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #remove dead controllers, and change bomb holder
    def check_dead_moves(self):
        #check for one controller left first
        for alive_serial in self.alive_moves:
            if self.dead_moves[alive_serial].value == 0:
                if self.move_opts[alive_serial][
                        Opts.has_bomb.value] == Bool.yes.value:
                    self.move_opts[alive_serial][
                        Opts.has_bomb.value] = Bool.no.value
                    self.move_bomb()

                    #for i, bomb_serial in enumerate(self.bomb_serials):
                    #if self.bomb_serial == alive_serial:
                    #    self.bomb_serial = next(self.bomb_generators[i])
                    #self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.yes.value
                #remove alive move:

                self.alive_moves.remove(alive_serial)
                if self.audio_toggle:
                    self.explosion.start_effect()
                self.reset_bomb_time()

        if len(self.alive_moves) <= 1:
            self.end_game()

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        if self.audio_toggle:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE

        self.update_status('ending')

        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            if len(self.alive_moves) > 0:
                win_move = self.alive_moves[0]
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
                h_value = (h_value + 0.01)
                if h_value >= 1:
                    h_value = 0
        self.running = False

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': 'Ninja',
            'winning_team': winning_team,
            'total_players': len(self.move_serials),
            'remaining_players': len(self.alive_moves)
        }

        self.status_ns.status_dict = data

    def kill_game(self):
        if self.audio_toggle:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        h_value = 0
        while (time.time() < end_time):
            time.sleep(0.01)
            color = common.hsv2rgb(h_value, 1, 1)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                common.change_color(color_array, *color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False
Пример #17
0
class Bubble():
    def __init__(self, moves):

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.scores = {}
        self.music_speed = Value('d', 1.5)
        self.running = True
        self.force_move_colors = {}
        self.teams = {}
        self.win_amount = int(math.ceil((len(moves)/2.0)+2))
        self.team_num = 2

        self.generate_random_teams(self.team_num)

        music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))
        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        if len(moves) >= 5:
            fast_resample = True
        self.audio = Audio(music, fast_resample)
        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.losing_moves = []
        
        
        self.game_loop()


    def generate_random_teams(self, team_num):
        team_pick = range(team_num)
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = range(team_num)

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            dead_move = Value('i', 1)
            score = Value('i', 0)
            
            force_color = Array('i', [1] * 3)
            proc = Process(target=track_move, args=(move_serial,
                                                    move_num,
                                                    self.teams[move_serial],
                                                    self.team_num,
                                                    score,
                                                    self.win_amount,
                                                    dead_move,
                                                    force_color,
                                                    self.music_speed))
            proc.start()
            self.scores[move_serial] = score
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            
    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.itervalues():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(70, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()
        
    def get_change_time(self, speed_up):
        if speed_up:
            added_time = random.uniform(MIN_MUSIC_FAST_TIME, MAX_MUSIC_FAST_TIME)
        else:
            added_time = random.uniform(MIN_MUSIC_SLOW_TIME, MAX_MUSIC_SLOW_TIME)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
            self.audio.change_chunk_size(True)
        elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up =  not self.speed_up
            self.change_time = self.get_change_time(speed_up = self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False
            self.audio.change_chunk_size(False)

    def get_real_team(self, team):
        if team < 0:
            return -1
        else:
            return team

    def check_end_game(self):
        team_win = False
        for move_serial, score in self.scores.iteritems():
            #if we are alive
            if score.value >= self.win_amount:
                winning_team = self.teams[move_serial]
                team_win = True
                
        if team_win:
            for move_serial in self.teams.iterkeys():
                if self.teams[move_serial] == winning_team:
                    self.winning_moves.append(move_serial)
                else:
                    self.losing_moves.append(move_serial)
            self.game_end = True

    def check_for_points(self):
        for move_serial, score in self.scores.iteritems():
            if score.value == -1:
                score.value = 0
                self.explosion.start_effect()
                team_increase = self.teams[move_serial]
                for move_serial_increase, score_increase in self.scores.iteritems():
                    if self.teams[move_serial_increase] != team_increase:
                        score_increase.value += 1
            

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.itervalues():
            proc.terminate()
            proc.join()

    def end_game(self):
        self.audio.stop_audio()
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            for lose_move in self.losing_moves:
                lose_color_array = self.force_move_colors[lose_move]
                common.change_color(lose_color_array, 1,0,0)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def game_loop(self):
        self.track_moves()
        self.count_down()
        self.audio.start_audio_loop()
        
        while self.running:

            self.check_music_speed()
            self.check_for_points()
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #18
0
class Tournament():
    def __init__(self, moves, command_queue, ns, music):

        self.command_queue = command_queue
        self.ns = ns

        self.sensitivity = self.ns.settings['sensitivity']
        self.play_audio = self.ns.settings['play_audio']

        print("speed is {}".format(self.sensitivity))
        global SLOW_MAX
        global SLOW_WARNING
        global FAST_MAX
        global FAST_WARNING

        SLOW_MAX = common.SLOW_MAX[self.sensitivity]
        SLOW_WARNING = common.SLOW_WARNING[self.sensitivity]
        FAST_MAX = common.FAST_MAX[self.sensitivity]
        FAST_WARNING = common.FAST_WARNING[self.sensitivity]

        self.move_serials = moves

        self.tracked_moves = {}
        self.dead_moves = {}
        self.music_speed = Value('d', 1.5)
        self.running = True
        self.force_move_colors = {}
        self.invince_moves = {}

        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_dead = 0
        self.show_team_colors = Value('i', 0)
        self.teams = {}
        self.update_time = 0

        #self.num_teams = math.ceil(len(moves)/2)
        self.num_teams = len(moves)

        self.generate_random_teams(self.num_teams)

        self.tourney_list = self.generate_tourney_list(len(moves))
        fast_resample = False
        if self.play_audio:
            print("tourney list is " + str(self.tourney_list))

            ##            music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))
            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')

            end = False
            self.audio = music
        #self.change_time = self.get_change_time(speed_up = True)

        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.game_loop()

    def generate_tourney_list(self, player_num):
        def divide(arr, depth, m):
            if len(complements) <= depth:
                complements.append(2**(depth + 2) + 1)
            complement = complements[depth]
            for i in range(2):
                if complement - arr[i] <= m:
                    arr[i] = [arr[i], complement - arr[i]]
                    divide(arr[i], depth + 1, m)

        m = player_num

        arr = [1, 2]
        complements = []

        divide(arr, 0, m)
        dup_serials = self.move_serials[:]

        def insert_move(arr):
            for i in range(2):
                if type(arr[i]) is list:
                    insert_move(arr[i])
                else:
                    arr[i] = random.choice(dup_serials)
                    dup_serials.remove(arr[i])

        insert_move(arr)
        print(arr)
        return arr

    def generate_random_teams(self, num_teams):
        team_pick = list(range(num_teams))
        for serial in self.move_serials:
            random_choice = Value('i', random.choice(team_pick))
            self.teams[serial] = random_choice
            team_pick.remove(random_choice.value)
            if not team_pick:
                team_pick = list(range(num_teams))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):

            time.sleep(0.02)
            dead_move = Value('i', 1)

            force_color = Array('i', [1] * 3)
            invincibility = Value('i', 0)
            proc = Process(target=track_move,
                           args=(move_serial, move_num,
                                 self.teams[move_serial], self.num_teams,
                                 dead_move, force_color, self.music_speed,
                                 self.show_team_colors, invincibility))
            proc.start()
            self.invince_moves[move_serial] = invincibility
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.play_audio:
            self.start_game.start_effect()

    def get_change_time(self, speed_up):
        min_moves = len(self.move_serials) - 2
        if min_moves <= 0:
            min_moves = 1

        game_percent = (self.num_dead / min_moves)
        if game_percent > 1.0:
            game_percent = 1.0
        min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME,
                                     END_MIN_MUSIC_FAST_TIME, game_percent)
        max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME,
                                     END_MAX_MUSIC_FAST_TIME, game_percent)

        min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME,
                                     END_MIN_MUSIC_SLOW_TIME, game_percent)
        max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME,
                                     END_MAX_MUSIC_SLOW_TIME, game_percent)
        if speed_up:
            added_time = random.uniform(min_music_fast, max_music_fast)
        else:
            added_time = random.uniform(min_music_slow, max_music_slow)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip(
            (time.time() - self.change_time) / INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED,
                                                 SLOW_MUSIC_SPEED,
                                                 change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED,
                                                 FAST_MUSIC_SPEED,
                                                 change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time(
        ) < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
        elif time.time(
        ) >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up = not self.speed_up
            self.change_time = self.get_change_time(speed_up=self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False

    def check_matches(self):
        #do this only when a controller dies, or at the beginning
        def check_moves(arr):
            if (type(arr[0]) is not list and type(arr[1]) is not list):
                if self.teams[arr[1]].value != -1:
                    self.teams[arr[0]].value = self.teams[arr[1]].value
                else:
                    self.teams[arr[1]].value = self.teams[arr[0]].value
            elif (type(arr[0]) is not list and type(arr[1]) is list):
                self.teams[arr[0]].value = -1
                check_moves(arr[1])
            elif (type(arr[1]) is not list and type(arr[0]) is list):
                self.teams[arr[1]].value = -1
                check_moves(arr[0])
            elif (type(arr[0]) is list and type(arr[1]) is list):
                check_moves(arr[0])
                check_moves(arr[1])

        check_moves(self.tourney_list)

    def remove_dead_player(self, dead_serial):
        def remove_dead(arr):
            if type(arr) is list and dead_serial in arr:
                arr.remove(dead_serial)
            else:
                if type(arr[0]) is list:
                    remove_dead(arr[0])
                if type(arr[1]) is list:
                    remove_dead(arr[1])

        remove_dead(self.tourney_list)

        def move_up(arr):
            if type(arr) is list and len(arr) == 1:
                return arr[0]
            else:
                if type(arr[0]) is list and move_up(arr[0]):
                    arr[0] = move_up(arr[0])
                    if type(arr[1]) is not list:
                        self.teams[arr[1]].value = self.teams[arr[0]].value
                        self.invince_moves[arr[1]].value = 1
                        self.invince_moves[arr[0]].value = 1
                    else:
                        self.teams[arr[0]].value = -1
                elif type(arr[1]) is list and move_up(arr[1]):
                    arr[1] = move_up(arr[1])

                    if type(arr[0]) is not list:
                        self.teams[arr[0]].value = self.teams[arr[1]].value
                        self.invince_moves[arr[1]].value = 1
                        self.invince_moves[arr[0]].value = 1
                    else:
                        self.teams[arr[1]].value = -1

        move_up(self.tourney_list)

    def check_end_game(self):
        self.winning_moves = []
        for move_serial, dead in self.dead_moves.items():
            #if we are alive
            if dead.value == 1:
                self.winning_moves.append(move_serial)
            if dead.value == 0:
                self.remove_dead_player(move_serial)
                #This is to play the sound effect
                self.num_dead += 1
                dead.value = -1
                if self.play_audio:
                    self.explosion.start_effect()
        if len(self.winning_moves) <= 1:
            self.game_end = True

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        self.audio.stop_audio()
        self.update_status('ending')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                colors.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def game_loop(self):
        self.track_moves()
        self.show_team_colors.value = 0
        self.count_down()
        self.change_time = time.time() + 6
        time.sleep(0.02)
        if self.play_audio:
            self.audio.start_audio_loop()
        else:
            #when no audio is playing set the music speed to middle speed
            self.music_speed.value = (FAST_MUSIC_SPEED + SLOW_MUSIC_SPEED) / 2
        time.sleep(0.8)
        self.check_matches()

        while self.running:
            #I think the loop is so fast that this causes
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')
            if self.play_audio:
                self.check_music_speed()
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

    def kill_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright, 0, 0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10
        self.running = False

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': 'Tournament',
            'winning_team': winning_team
        }
        self.ns.status = data
Пример #19
0
class Zombie:
    def __init__(self, cont_alive, command_queue, ns, music):

        self.command_queue = command_queue
        self.ns = ns

        self.sensitivity = self.ns.settings['sensitivity']
        self.play_audio = self.ns.settings['play_audio']

        self.music = music

        global human_warning
        global human_max
        global zombie_warning
        global zombie_max

        human_warning = common.FAST_WARNING[self.sensitivity]
        human_max = common.FAST_MAX[self.sensitivity]
        zombie_warning = common.ZOMBIE_WARNING[self.sensitivity]
        zombie_max = common.ZOMBIE_MAX[self.sensitivity]

        self.update_time = 0
        self.humans = []
        self.alive_zombies = []
        self.dead_zombies = {}
        self.controller_opts = {}
        self.controllers_alive = cont_alive
        self.win_time = ((len(self.controllers_alive) * 3) / 16) * 60
        if self.win_time <= 0:
            self.win_time = 60
        self.start_time = time.time()
        if self.play_audio:
            self.pickup = Audio('audio/Zombie/sound_effects/pickup.wav')
        self.effect_cue = 0

        self.kill_game = False
        self.Start()

    def get_kill_time(self):
        percent_to_win = 1.0 * (time.time() -
                                self.start_time) / (self.win_time * 1.0)
        random_num = ((1.0 - percent_to_win) * 7) + 2
        return random.uniform(random_num, random_num + 2)

    def kill_zombies(self, num_zombies, random_bullet_chance):
        kill_zombie = False
        for i in range(num_zombies):
            if self.alive_zombies:
                kill_zombie = True
                shot_zombie_serial = random.choice(self.alive_zombies)
                self.controller_opts[shot_zombie_serial][3] = 0
                self.dead_zombies[shot_zombie_serial] = time.time(
                ) + self.get_kill_time()
                self.alive_zombies.remove(shot_zombie_serial)

        if kill_zombie:
            self.reward(random_bullet_chance)

    def reward(self, random_bullet_chance):
        if (len(self.dead_zombies) + len(self.alive_zombies)) > 3:
            random_bullet = random.choice(random_bullet_chance)
            sound = False
            for i in range(random_bullet):
                sound = True
                random_human = random.choice(self.humans)
                if self.controller_opts[random_human][4] < 5:
                    self.controller_opts[random_human][4] += 1
            if sound:
                self.pickup.start_effect()
            #one in 5 chance of getting a weapon
            #self.get_weapon(5)

    def audio_cue(self):
        if self.win_time - (time.time() -
                            self.start_time) <= 10 and self.effect_cue <= 4:
            Audio('audio/Zombie/sound_effects/10 seconds left.wav'
                  ).start_effect()
            self.effect_cue = 5
        elif self.win_time - (time.time() -
                              self.start_time) <= 30 and self.effect_cue <= 3:
            Audio('audio/Zombie/sound_effects/30 seconds.wav').start_effect()
            self.effect_cue = 4
        elif self.win_time - (time.time() - self.start_time
                              ) <= 1 * 60 and self.effect_cue <= 2:
            Audio('audio/Zombie/sound_effects/1 minute.wav').start_effect()
            self.effect_cue = 3
        elif self.win_time - (time.time() - self.start_time
                              ) <= 3 * 60 and self.effect_cue <= 1:
            Audio('audio/Zombie/sound_effects/3 minutes.wav').start_effect()
            self.effect_cue = 2
        #elif self.win_time - (time.time() - self.start_time) <= 5*60 and self.effect_cue <= 0:
        #   Audio('audio/Zombie/sound_effects/5 minutes.wav').start_effect()
        #   self.effect_cue = 1

    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)

        if self.play_audio:
            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:
                self.music.start_audio_loop()
##                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 random_human in random.sample(set(self.humans), 2):
            #random_human = random.choice(self.humans)
            self.controller_opts[random_human][3] = 0

        while running:
            if self.play_audio:
                self.audio_cue()
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            #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:
                    if self.play_audio:
                        pistol.start_effect()
                    self.kill_zombies(1, [0, 0, 0, 0, 1, 1, 1])
                    self.controller_opts[serial][1] = 0

                #molotov fired(5 bullets all alive zombies)
                elif self.controller_opts[serial][1] == 4:
                    if self.play_audio:
                        molotov.start_effect()
                    self.kill_zombies(50, [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 or self.kill_game:
                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 colors.hsv2rgb(*colour)]
                                for colour in HSV]
                win_controllers = []
                if len(self.humans) <= 0:
                    if self.play_audio:
                        zombie_victory.start_effect()
                    self.alive_zombies.extend(self.dead_zombies.keys())
                    self.update_status('ending', 1)
                    win_controllers = self.alive_zombies
                if (time.time() - self.start_time) > self.win_time:
                    if self.play_audio:
                        human_victory.start_effect()
                    win_controllers = self.humans
                    self.update_status('ending', 0)
                if self.kill_game:
                    self.alive_zombies.extend(self.dead_zombies.keys())
                    win_controllers = self.humans + self.alive_zombies
                    self.update_status('killed')
                #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
                if self.play_audio:
                    try:
                        self.music.stop_audio()
                    except:
                        print('no audio loaded')

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game = True

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': 'Zombies',
            'winning_team': winning_team,
            'humans': len(self.humans),
            'dead_zombies': len(self.dead_zombies),
            'alive_zombies': len(self.alive_zombies),
            'ticker': self.update_time,
            'time_left': int(self.win_time - (time.time() - self.start_time)),
            'team_names': ['Humans', 'Zombies']
        }

        self.ns.status = data
Пример #20
0
class kingoftheHill():
    def __init__(self, moves, speed):
        global SLOW_MAX
        global SLOW_WARNING
        global FAST_MAX
        global FAST_WARNING

        SLOW_MAX = common.SLOW_MAX[speed]
        SLOW_WARNING = common.SLOW_WARNING[speed]
        FAST_MAX = common.FAST_MAX[speed]
        FAST_WARNING = common.FAST_WARNING[speed]

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.team_num = 2

        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}

        self.generate_random_teams(self.team_num)

        music = 'audio/Joust/music/' + random.choice(
            os.listdir('audio/Joust/music'))

        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        end = False
        try:
            self.audio = Audio(music, end)
        except:
            print('no audio loaded')

        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.game_loop()

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        for serial in self.move_serials:
            random_choice = Value('i', random.choice(team_pick))
            self.teams[serial] = random_choice
            team_pick.remove(random_choice.value)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            opts = Array('i', [0] * 5)
            proc = Process(target=track_move,
                           args=(move_serial, move_num,
                                 self.teams[move_serial], self.team_num,
                                 dead_move, force_color, self.music_speed,
                                 opts))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def check_end_game(self):
        self.winning_team = -100
        team_win = True

        for move_serial, dead in self.dead_moves.items():
            if self.winning_team == -100:
                self.winning_team = self.teams[move_serial].value
            if self.teams[move_serial].value != self.winning_team:
                team_win = False
                #TODO: This wont work if the last move is the first of the dead_moves
                self.last_move = move_serial
            if dead.value == 0:
                #This is to play the sound effect
                dead.value = -1
                self.explosion.start_effect()
        self.game_end = team_win

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        self.end_game_sound(self.winning_team)
        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.move_serials:
                if win_move != self.last_move:
                    win_color_array = self.force_move_colors[win_move]
                    common.change_color(win_color_array, *win_color)
                else:
                    win_color_array = self.force_move_colors[win_move]
                    common.change_color(win_color_array, 1, 1, 1)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        #if self.game_mode == common.Games.JoustTeams:
        if winning_team == Team.red.value:
            team_win = Audio('audio/Commander/sounds/red winner.wav')
        if winning_team == Team.blue.value:
            team_win = Audio('audio/Commander/sounds/blue winner.wav')
        team_win.start_effect()

    def game_loop(self):
        self.track_moves()
        self.count_down()
        try:
            self.audio.start_audio_loop()
        except:
            print('no audio loaded to start')
        while self.running:

            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #21
0
class Bomb():
    def __init__(self, moves, command_queue, ns, music, bomb_color, game_start,
                 five_move_opts, dead_moves, force_move_colors, false_colors,
                 was_faked, rumble, music_speed, restart):

        self.command_queue = command_queue
        self.ns = ns

        self.voice = self.ns.settings['menu_voice']

        self.play_audio = self.ns.settings['play_audio']
        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = dead_moves
        self.alive_moves = []
        self.teams = {}
        self.music_speed = music_speed
        self.music_speed.value = 1
        self.running = True
        self.force_move_colors = force_move_colors
        self.bomb_color = bomb_color
        for i in range(3):
            self.bomb_color[i] = 0
        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_bombs = 2
        self.move_opts = five_move_opts
        self.false_colors = false_colors
        self.was_faked = was_faked
        self.rumble = rumble
        self.bomb_length = 5.0
        self.restart = restart

        self.game_start = game_start
        self.game_start.value = 0
        self.current_rand_holder = ''
        self.next_rand_holder = ''
        self.prev_rand_holder = ''

        self.update_time = 0

        if self.play_audio:
            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            self.fakedout = Audio('audio/Joust/vox/' + self.voice +
                                  '/Fakedout.wav')
            self.explosion40 = Audio('audio/Joust/sounds/Explosion40.wav')
            self.countered = Audio('audio/Joust/vox/' + self.voice +
                                   '/countered.wav')
            self.Fakecountered = Audio('audio/Joust/vox/' + self.voice +
                                       '/FakedoutCounter.wav')
            self.explosiondeath = Audio('audio/Joust/vox/' + self.voice +
                                        '/explosiondeath.wav')

            end = False
            try:
                self.audio = music
            except:
                print('no audio loaded')

        self.game_end = False

        self.game_loop()

    def reset_bomb_length(self):
        self.bomb_length = 4.0

    def get_bomb_length(self):
        self.bomb_length -= 0.3
        if self.bomb_length < 1:
            self.bomb_length = 1
        return self.bomb_length

    def get_prev_random_holder(self):
        return self.bomb_serial

    def get_next_random_holder(self):
        if self.next_rand_holder == self.bomb_serial:
            self.next_rand_holder = self.move_serials[random.choice(
                range(len(self.move_serials)))]
            while self.next_rand_holder == self.bomb_serial or self.dead_moves[
                    self.next_rand_holder].value <= 0:
                self.next_rand_holder = self.move_serials[random.choice(
                    range(len(self.move_serials)))]
                self.current_rand_holder = self.bomb_serial
        return self.next_rand_holder

    def get_next_bomb_holder(self, serial=None):
        if serial:
            holder = self.get_serial_pos(serial)
        else:
            holder = random.choice(range(len(self.move_serials)))
        while True:
            yield self.get_next_random_holder()

    def reset_bomb_time(self):
        self.bomb_time = time.time() + self.get_bomb_length()
        self.bomb_start_time = time.time()

    def game_loop(self):
        self.track_moves()
        self.restart.value = 0
        self.rotate_colors()
        self.bomb_serial = self.move_serials[random.choice(
            range(len(self.move_serials)))]
        self.next_rand_holder = self.bomb_serial
        self.bomb_generator = self.get_next_bomb_holder()

        self.bomb_serial = next(self.bomb_generator)
        self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.yes.value

        self.holding = True
        self.game_start.value = 1
        self.count_down()
        time.sleep(0.02)
        if self.play_audio:
            try:
                self.audio.start_audio_loop()
            except:
                print('no audio loaded to start')
        time.sleep(0.8)

        self.bomb_time = time.time() + self.get_bomb_length()
        self.bomb_start_time = time.time()

        while self.running:

            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            percentage = 1 - ((self.bomb_time - time.time()) /
                              (self.bomb_time - self.bomb_start_time))

            if (percentage > 0.8):
                self.bomb_color[0] = random.randrange(
                    int(100 + 55 * percentage), int(200 + 55 * percentage))
            else:
                self.bomb_color[0] = int(common.lerp(90, 255, percentage))
            self.bomb_color[1] = int(common.lerp(30, 0, percentage))
            self.bomb_color[2] = int(common.lerp(30, 0, percentage))

            if self.move_opts[self.bomb_serial][
                    Opts.selection.value] == Selections.nothing.value:
                self.holding = False
            if self.move_opts[self.bomb_serial][
                    Opts.selection.
                    value] == Selections.a_button.value and self.holding == False:
                self.reset_bomb_time()
                self.move_bomb()
                if self.play_audio:
                    self.start_beep.start_effect()
                self.holding = True
            if time.time() > self.bomb_time:
                if self.play_audio:
                    self.explosiondeath.start_effect()

                    self.explosion.start_effect()
                self.pause_for_player_death(self.bomb_serial)

                self.dead_moves[self.bomb_serial].value -= 1

                self.reset_bomb_length()
                self.reset_bomb_time()

                #if player is dead move bomb
                if not self.dead_moves[self.bomb_serial].value > 0:
                    self.move_bomb()

                print("TIME BOMB")

            self.check_dead_moves()
            self.check_faked_out()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def move_bomb(self):
        self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.no.value
        self.bomb_serial = next(self.bomb_generator)
        self.move_opts[self.bomb_serial][Opts.has_bomb.value] = Bool.yes.value

    def pause_for_player_death(self, dead_move, faker_move=None):
        end_time = time.time() + 1.5
        while (time.time() < end_time):
            time.sleep(0.01)

            dead_color_array = self.force_move_colors[dead_move]
            if faker_move:
                faker_color_array = self.force_move_colors[faker_move]

            for move_serial in self.move_serials:
                if move_serial == dead_move:
                    colors.change_color(dead_color_array, 10,
                                        random.randrange(100, 200), 10)
                    self.rumble[move_serial].value = 150
                else:
                    colors.change_color(self.force_move_colors[move_serial], 1,
                                        1, 1)
        self.rumble[dead_move].value = 0
        self.change_all_move_colors(0, 0, 0)

    def check_faked_out(self):
        #check for one controller left first
        for move_serial in self.move_serials:

            if self.dead_moves[move_serial].value > 0:

                #if we faked play sound
                if self.move_opts[move_serial][
                        Opts.selection.
                        value] == Selections.false_trigger.value and self.move_opts[
                            move_serial][Opts.holding.
                                         value] == Holding.not_holding.value:
                    print("faked out sound")
                    faker = self.get_next_serial(move_serial)
                    self.reset_bomb_time()
                    self.reset_bomb_length()
                    self.false_colors[faker].value = 1
                    if self.play_audio:
                        self.start_beep.start_effect()
                    self.move_opts[move_serial][
                        Opts.holding.value] = Holding.holding.value

                #we are being faked out
                if self.false_colors[move_serial].value == 1:
                    prev_faker = self.get_prev_serial(move_serial)

                    #Pushed middle button, when faked
                    if self.was_faked[move_serial].value == 1:
                        faker = self.get_prev_serial(move_serial)
                        if self.play_audio:
                            self.explosion40.start_effect()
                            self.fakedout.start_effect()
                        self.pause_for_player_death(move_serial, faker)

                        self.dead_moves[move_serial].value -= 1
                        self.was_faked[move_serial].value = 2

                        self.reset_bomb_time()
                        self.reset_bomb_length()
                        self.move_bomb()

                    elif self.move_opts[move_serial][
                            Opts.selection.value] == Selections.counter.value:
                        if self.play_audio:
                            self.explosion40.start_effect()
                            self.countered.start_effect()
                        self.pause_for_player_death(prev_faker, move_serial)

                        self.dead_moves[prev_faker].value -= 1
                        self.false_colors[move_serial].value = 0
                        self.move_opts[move_serial][
                            Opts.holding.value] = Holding.holding.value

                        self.reset_bomb_length()
                        self.reset_bomb_time()

                        self.move_bomb()

                        print("JUST DIED TO BEING COUNTERED")

                    #only do this once per move
                    if self.move_opts[prev_faker][
                            Opts.holding.value] == Holding.not_holding.value:
                        self.false_colors[move_serial].value = 0

    def get_next_serial(self, serial):
        return self.get_next_random_holder()

        pos = (self.get_serial_pos(serial) + 1) % len(self.move_serials)

        new_serial = self.move_serials[pos]
        while self.dead_moves[new_serial].value == 0:
            pos = (pos + 1) % len(self.move_serials)
            new_serial = self.move_serials[pos]
        return self.move_serials[pos]

    def get_prev_serial(self, serial):
        self.get_next_random_holder()
        return self.get_prev_random_holder()

        pos = (self.get_serial_pos(serial) - 1) % len(self.move_serials)
        new_serial = self.move_serials[pos]
        while self.dead_moves[new_serial].value == 0:
            pos = (pos - 1) % len(self.move_serials)
            new_serial = self.move_serials[pos]
        return self.move_serials[pos]

    def get_serial_pos(self, serial):
        for i, move_serial in enumerate(self.move_serials):
            if serial == move_serial:
                return i

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            self.alive_moves.append(move_serial)
            self.dead_moves[move_serial].value = 2
            for i in range(3):
                self.force_move_colors[move_serial][i] = 1
            for i in range(5):
                self.move_opts[move_serial][i] = 0
            self.false_colors[move_serial].value = 0
            self.was_faked[move_serial].value = 0
            self.rumble[move_serial].value = 0

    def rotate_colors(self):
        move_on = False
        time_change = 0.5

        in_cons = []
        for move_serial_beg in self.move_serials:
            self.move_opts[move_serial_beg][
                Opts.has_bomb.value] = Bool.yes.value
            self.move_opts[move_serial_beg][
                Opts.holding.value] = Holding.holding.value
        while len(in_cons) != len(self.move_serials):
            for move_serial in self.move_serials:
                for move_serial_beg in self.move_serials:
                    if self.move_opts[move_serial_beg][
                            Opts.selection.value] == Selections.a_button.value:
                        if move_serial_beg not in in_cons:
                            if self.play_audio:
                                self.start_beep.start_effect()
                            in_cons.append(move_serial_beg)
                    if move_serial_beg in in_cons:
                        colors.change_color(
                            self.force_move_colors[move_serial_beg], 100, 100,
                            100)
                colors.change_color(self.force_move_colors[move_serial], 100,
                                    0, 0)
                time.sleep(0.5)
                colors.change_color(self.force_move_colors[move_serial], 0, 0,
                                    0)
        for move_serial_beg in self.move_serials:
            self.move_opts[move_serial_beg][
                Opts.has_bomb.value] = Bool.no.value

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.play_audio:
            self.start_game.start_effect()

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #remove dead controllers, and change bomb holder
    def check_dead_moves(self):
        #check for one controller left first
        for alive_serial in self.alive_moves:
            if self.dead_moves[alive_serial].value == 0:
                if self.move_opts[alive_serial][
                        Opts.has_bomb.value] == Bool.yes.value:
                    self.move_opts[alive_serial][
                        Opts.has_bomb.value] = Bool.no.value
                    self.move_bomb()

                self.alive_moves.remove(alive_serial)
                if self.play_audio:
                    self.explosion.start_effect()
                self.reset_bomb_time()

        if len(self.alive_moves) <= 1:
            self.end_game()

    def stop_tracking_moves(self):
        self.restart.value = 1

    def end_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE

        self.update_status('ending')

        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            if len(self.alive_moves) > 0:
                win_move = self.alive_moves[0]
                win_color_array = self.force_move_colors[win_move]
                colors.change_color(win_color_array, *win_color)
                h_value = (h_value + 0.01)
                if h_value >= 1:
                    h_value = 0
        self.running = False

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': 'Ninja',
            'winning_team': winning_team,
            'total_players': len(self.move_serials),
            'remaining_players': len(self.alive_moves)
        }

        self.ns.status = data

    def kill_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright, 0, 0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10
        self.running = False
Пример #22
0
class Swapper():
    def __init__(self, moves, command_queue, ns, music):

        self.command_queue = command_queue
        self.ns = ns

        #save locally in case settings change from web
        self.play_audio = self.ns.settings['play_audio']
        self.sensitivity = self.ns.settings['sensitivity']
        self.color_lock = self.ns.settings['color_lock']
        self.color_lock_choices = self.ns.settings['color_lock_choices']
        self.random_teams = self.ns.settings['random_teams']

        global SLOW_MAX
        global SLOW_WARNING
        global FAST_MAX
        global FAST_WARNING
        
        SLOW_MAX = common.SLOW_MAX[self.sensitivity]
        SLOW_WARNING = common.SLOW_WARNING[self.sensitivity]
        FAST_MAX = common.FAST_MAX[self.sensitivity]
        FAST_WARNING = common.FAST_WARNING[self.sensitivity]
        
        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.num_teams = 2

        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}

        self.update_time = 0

        self.team_colors = colors.generate_team_colors(self.num_teams,self.color_lock,self.color_lock_choices)

        self.generate_random_teams(self.num_teams)

        if self.play_audio:
##            music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))

            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            fast_resample = False
            end = False
            try:
                self.audio = music
            except:
                print('no audio loaded')

        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.game_loop()

    def generate_random_teams(self, num_teams):
        if self.random_teams == False:
            players_per_team = (len(self.move_serials)//num_teams)+1
            team_num = [x for x in range(num_teams)]*players_per_team
            for num,move in zip(team_num,self.move_serials):
                self.teams[move] = Value('i',num)
        else:
            team_pick = list(range(num_teams))
            for serial in self.move_serials:
                random_choice = Value('i',  random.choice(team_pick) )
                self.teams[serial] = random_choice
                team_pick.remove(random_choice.value)
                if not team_pick:
                    team_pick = list(range(num_teams))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            opts = Array('i', [0] * 5)
            proc = Process(target=track_move, args=(move_serial,
                                                    move_num,
                                                    self.teams[move_serial],
                                                    self.num_teams,
                                                    self.team_colors,
                                                    dead_move,
                                                    force_color,
                                                    self.music_speed,

                                                    opts))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts
            
    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.play_audio:
            self.start_game.start_effect()

    def check_end_game(self):
        self.winning_team = -100
        team_win = True
        
        for move_serial, dead in self.dead_moves.items():
            if self.winning_team == -100:
                self.winning_team = self.teams[move_serial].value
            if self.teams[move_serial].value != self.winning_team:
                team_win = False
                #TODO: This wont work if the last move is the first of the dead_moves
                self.last_move = move_serial
            if dead.value == 0:
                #This is to play the sound effect
                dead.value = -1
                if self.play_audio:
                    self.explosion.start_effect()
        self.game_end = team_win


    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        self.update_status('ending',self.winning_team)
        if self.play_audio:
            self.end_game_sound(self.winning_team)
        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            for win_move in self.move_serials:
                if win_move != self.last_move:
                    win_color_array = self.force_move_colors[win_move]
                    colors.change_color(win_color_array, *win_color)
                else:
                    win_color_array = self.force_move_colors[win_move]
                    colors.change_color(win_color_array, 1,1,1)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        win_team_name = self.team_colors[winning_team].name
        if win_team_name == 'Pink':
            team_win = Audio('audio/Joust/sounds/human win.wav')
        if win_team_name == 'Magenta':
            team_win = Audio('audio/Joust/sounds/magenta team win.wav')
        if win_team_name == 'Orange':
            team_win = Audio('audio/Joust/sounds/human win.wav')
        if win_team_name == 'Yellow':
            team_win = Audio('audio/Joust/sounds/yellow team win.wav')
        if win_team_name == 'Green':
            team_win = Audio('audio/Joust/sounds/green team win.wav')
        if win_team_name == 'Turquoise':
            team_win = Audio('audio/Joust/sounds/cyan team win.wav')
        if win_team_name == 'Blue':
            team_win = Audio('audio/Joust/sounds/blue team win.wav')
        if win_team_name == 'Purple':
            team_win = Audio('audio/Joust/sounds/human win.wav')
        team_win.start_effect()

    def game_loop(self):
        self.track_moves()
        self.count_down()
        if self.play_audio:
            try:
                self.audio.start_audio_loop()
            except:
                print('no audio loaded to start')
        while self.running:
            #I think the loop is so fast that this causes 
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not(self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not(package == None):
            if command == 'killgame':
                self.kill_game()

    def kill_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')        
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE     
        
        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright,0,0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10
        self.running = False

    def update_status(self,game_status,winning_team=-1):
        data ={'game_status' : game_status,
               'game_mode' : 'Swapper',
               'winning_team' : winning_team}
        team_total = [0,0]
        team_alive = [0,0]
        for move in self.move_serials:
            team = self.teams[move].value
            team_total[team] += 1
            if self.dead_moves[move].value == 1:
                team_alive[team] += 1
        team_comp = list(zip(team_total,team_alive))
        data['team_comp'] = team_comp
        data['team_names'] = [color.name + ' Team' for color in self.team_colors]

        self.ns.status = data
                    
            
Пример #23
0
class Joust:
    def __init__(self, game_mode, moves, teams):

        self.move_serials = moves
        self.game_mode = game_mode
        self.tracked_moves = {}
        self.dead_moves = {}
        self.music_speed = Value("d", 1.5)
        self.running = True
        self.force_move_colors = {}
        self.teams = teams
        self.team_num = 6
        self.game_mode = game_mode
        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_dead = 0
        self.werewolf_reveal = Value("i", 2)
        if game_mode == common.Games.JoustFFA.value:
            self.team_num = len(moves)
        if game_mode == common.Games.JoustRandomTeams.value:
            # this should be 3 for smaller number of controllers
            self.team_num = 4
        if game_mode == common.Games.WereJoust.value:
            self.werewolf_reveal.value = 0
            self.team_num = 1
        if game_mode != common.Games.JoustTeams.value:
            self.generate_random_teams(self.team_num)

        if game_mode == common.Games.WereJoust.value:
            # were_num = int((len(moves)+2)/4)
            were_num = int((len(moves) * 7) / 16)
            if were_num <= 0:
                were_num = 1
            self.choose_werewolf(were_num)

        music = "audio/Joust/music/" + random.choice(os.listdir("audio/Joust/music"))
        self.start_beep = Audio("audio/Joust/sounds/start.wav")
        self.start_game = Audio("audio/Joust/sounds/start3.wav")
        self.explosion = Audio("audio/Joust/sounds/Explosion34.wav")
        fast_resample = False
        end = False
        self.audio = Audio(music, end)
        # self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []

        self.game_loop()

    def choose_werewolf(self, were_num):
        for were in range(were_num):
            werewolf = random.choice(self.move_serials)
            while self.teams[werewolf] < 0:
                werewolf = random.choice(self.move_serials)
            self.teams[werewolf] = (self.teams[werewolf] * -1) - 1

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        print(str(team_pick))
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):

            time.sleep(0.02)
            dead_move = Value("i", 1)
            force_color = Array("i", [1] * 3)
            proc = Process(
                target=track_move,
                args=(
                    move_serial,
                    move_num,
                    self.game_mode,
                    self.teams[move_serial],
                    self.team_num,
                    dead_move,
                    force_color,
                    self.music_speed,
                    self.werewolf_reveal,
                ),
            )
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    # need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        min_moves = len(self.move_serials) - 2
        if min_moves <= 0:
            min_moves = 1

        game_percent = self.num_dead / min_moves
        if game_percent > 1.0:
            game_percent = 1.0
        min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME, END_MIN_MUSIC_FAST_TIME, game_percent)
        max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME, END_MAX_MUSIC_FAST_TIME, game_percent)

        min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME, END_MIN_MUSIC_SLOW_TIME, game_percent)
        max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME, END_MAX_MUSIC_SLOW_TIME, game_percent)
        if speed_up:
            added_time = random.uniform(min_music_fast, max_music_fast)
        else:
            added_time = random.uniform(min_music_slow, max_music_slow)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip((time.time() - self.change_time) / INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
            self.audio.change_chunk_size(True)
        elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up = not self.speed_up
            self.change_time = self.get_change_time(speed_up=self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False
            self.audio.change_chunk_size(False)

    def get_real_team(self, team):
        if team < 0:
            return -1
        else:
            return team

    def reveal(self):
        self.werewolf_reveal.value = 2

    def werewolf_audio_cue(self):
        if self.game_mode == common.Games.WereJoust.value:
            # print self.werewolf_timer - (time.time() - self.start_timer)
            if self.werewolf_timer - (time.time() - self.start_timer) <= 30 and self.audio_cue == 0:
                Audio("audio/Joust/sounds/30 werewolf.wav").start_effect()
                self.audio_cue = 1
            if self.werewolf_timer - (time.time() - self.start_timer) <= 10 and self.audio_cue == 1:
                Audio("audio/Joust/sounds/10 werewolf.wav").start_effect()
                self.audio_cue = 2
            if self.werewolf_timer - (time.time() - self.start_timer) <= 0 and self.audio_cue == 2:
                Audio("audio/Joust/sounds/werewolf reveal 2.wav").start_effect()
                self.reveal()
                self.audio_cue = 3

    def check_end_game(self):
        winning_team = -100
        team_win = True
        for move_serial, dead in self.dead_moves.items():
            # if we are alive
            if dead.value == 1:
                if winning_team == -100:
                    winning_team = self.get_real_team(self.teams[move_serial])
                elif self.get_real_team(self.teams[move_serial]) != winning_team:
                    team_win = False
            if dead.value == 0:
                # This is to play the sound effect
                self.num_dead += 1
                dead.value = -1
                self.explosion.start_effect()

        if team_win:
            self.end_game_sound(winning_team)
            for move_serial in self.teams.keys():
                if self.get_real_team(self.teams[move_serial]) == winning_team:
                    self.winning_moves.append(move_serial)
            self.game_end = True

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        self.audio.stop_audio()
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while time.time() < end_time:
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            h_value = h_value + 0.01
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        if self.game_mode == common.Games.JoustTeams.value:
            if winning_team == 0:
                team_win = Audio("audio/Joust/sounds/yellow team win.wav")
            if winning_team == 1:
                team_win = Audio("audio/Joust/sounds/green team win.wav")
            if winning_team == 2:
                team_win = Audio("audio/Joust/sounds/cyan team win.wav")
            if winning_team == 3:
                team_win = Audio("audio/Joust/sounds/blue team win.wav")
            if winning_team == 4:
                team_win = Audio("audio/Joust/sounds/magenta team win.wav")
            if winning_team == 5:
                team_win = Audio("audio/Joust/sounds/red team win.wav")
            team_win.start_effect()
        if self.game_mode == common.Games.JoustRandomTeams.value:
            if winning_team == 0:
                team_win = Audio("audio/Joust/sounds/yellow team win.wav")
            if winning_team == 1:
                team_win = Audio("audio/Joust/sounds/cyan team win.wav")
            if winning_team == 2:
                team_win = Audio("audio/Joust/sounds/magenta team win.wav")
            if winning_team == 3:
                team_win = Audio("audio/Joust/sounds/red team win.wav")
            team_win.start_effect()
        if self.game_mode == common.Games.WereJoust.value:
            if winning_team == -1:
                team_win = Audio("audio/Joust/sounds/werewolf win.wav")
            else:
                team_win = Audio("audio/Joust/sounds/human win.wav")
            team_win.start_effect()
        # self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')

    def werewolf_intro(self):
        Audio("audio/Joust/sounds/werewolf intro.wav").start_effect()
        time.sleep(3)
        self.change_all_move_colors(80, 0, 0)
        time.sleep(2)
        self.change_all_move_colors(30, 0, 0)
        time.sleep(18)
        self.change_all_move_colors(20, 20, 20)
        time.sleep(2)
        self.start_timer = time.time()

    def game_loop(self):
        self.track_moves()
        if self.game_mode == common.Games.WereJoust.value:
            self.werewolf_intro()
        self.werewolf_reveal.value = 1
        self.count_down()
        time.sleep(0.02)
        self.audio.start_audio_loop()
        time.sleep(0.8)

        while self.running:
            self.check_music_speed()
            self.check_end_game()
            self.werewolf_audio_cue()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #24
0
class Joust():
    def __init__(self, game_mode, moves, teams):

        self.move_serials = moves
        self.game_mode = game_mode
        self.tracked_moves = {}
        self.dead_moves = {}
        self.music_speed = Value('d', 1.5)
        self.running = True
        self.force_move_colors = {}
        self.teams = teams
        self.team_num = 6
        self.game_mode = game_mode
        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_dead = 0
        self.werewolf_reveal = Value('i', 2)
        if game_mode == common.Games.JoustFFA.value:
            self.team_num = len(moves)
        if game_mode == common.Games.JoustRandomTeams.value:
            #this should be 3 for smaller number of controllers
            self.team_num = 4
        if game_mode == common.Games.WereJoust.value:
            self.werewolf_reveal.value = 0
            self.team_num = 1
        if game_mode != common.Games.JoustTeams.value:
            self.generate_random_teams(self.team_num)

        if game_mode == common.Games.WereJoust.value:
            #were_num = int((len(moves)+2)/4)
            were_num = int((len(moves)*7)/16)
            if were_num <= 0:
                were_num = 1
            self.choose_werewolf(were_num)

        music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))
        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        end = False
        self.audio = Audio(music, end)
        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        
        
        self.game_loop()

    def choose_werewolf(self, were_num):
        for were in range(were_num):
            werewolf = random.choice(self.move_serials)
            while self.teams[werewolf] < 0:
                werewolf = random.choice(self.move_serials)
            self.teams[werewolf] = (self.teams[werewolf] * -1) - 1

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        print (str(team_pick))
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            proc = Process(target=track_move, args=(move_serial,
                                                    move_num,
                                                    self.game_mode,
                                                    self.teams[move_serial],
                                                    self.team_num,
                                                    dead_move,
                                                    force_color,
                                                    self.music_speed,
                                                    self.werewolf_reveal))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            
    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        min_moves = len(self.move_serials) - 2
        if min_moves <= 0:
            min_moves = 1
        
        game_percent = (self.num_dead/min_moves)
        if game_percent > 1.0:
            game_percent = 1.0
        min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME, END_MIN_MUSIC_FAST_TIME, game_percent)
        max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME, END_MAX_MUSIC_FAST_TIME, game_percent)

        min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME, END_MIN_MUSIC_SLOW_TIME, game_percent)
        max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME, END_MAX_MUSIC_SLOW_TIME, game_percent)
        if speed_up:
            added_time = random.uniform(min_music_fast, max_music_fast)
        else:
            added_time = random.uniform(min_music_slow, max_music_slow)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
            self.audio.change_chunk_size(True)
        elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up =  not self.speed_up
            self.change_time = self.get_change_time(speed_up = self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False
            self.audio.change_chunk_size(False)

    def get_real_team(self, team):
        if team < 0:
            return -1
        else:
            return team

    def reveal(self):
        self.werewolf_reveal.value = 2

    def werewolf_audio_cue(self):
        if self.game_mode == common.Games.WereJoust.value:
            #print self.werewolf_timer - (time.time() - self.start_timer)
            if self.werewolf_timer - (time.time() - self.start_timer) <= 30 and self.audio_cue == 0:
                Audio('audio/Joust/sounds/30 werewolf.wav').start_effect()
                self.audio_cue = 1
            if self.werewolf_timer - (time.time() - self.start_timer) <= 10 and self.audio_cue == 1:
                Audio('audio/Joust/sounds/10 werewolf.wav').start_effect()
                self.audio_cue = 2
            if self.werewolf_timer - (time.time() - self.start_timer) <= 0 and self.audio_cue == 2:
                Audio('audio/Joust/sounds/werewolf reveal 2.wav').start_effect()
                self.reveal()
                self.audio_cue = 3
                

    def check_end_game(self):
        winning_team = -100
        team_win = True
        for move_serial, dead in self.dead_moves.items():
            #if we are alive
            if dead.value == 1:
                if winning_team == -100:
                    winning_team = self.get_real_team(self.teams[move_serial])
                elif self.get_real_team(self.teams[move_serial]) != winning_team:
                    team_win = False
            if dead.value == 0:
                #This is to play the sound effect
                self.num_dead += 1
                dead.value = -1
                self.explosion.start_effect()
                
        if team_win:
            self.end_game_sound(winning_team)
            for move_serial in self.teams.keys():
                if self.get_real_team(self.teams[move_serial]) == winning_team:
                    self.winning_moves.append(move_serial)
            self.game_end = True

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        self.audio.stop_audio()
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        if self.game_mode == common.Games.JoustTeams.value:
            if winning_team == 0:
                team_win = Audio('audio/Joust/sounds/yellow team win.wav')
            if winning_team == 1:
                team_win = Audio('audio/Joust/sounds/green team win.wav')
            if winning_team == 2:
                team_win = Audio('audio/Joust/sounds/cyan team win.wav')
            if winning_team == 3:
                team_win = Audio('audio/Joust/sounds/blue team win.wav')
            if winning_team == 4:
                team_win = Audio('audio/Joust/sounds/magenta team win.wav')
            if winning_team == 5:
                team_win = Audio('audio/Joust/sounds/red team win.wav')
            team_win.start_effect()
        if self.game_mode == common.Games.JoustRandomTeams.value:
            if winning_team == 0:
                team_win = Audio('audio/Joust/sounds/yellow team win.wav')
            if winning_team == 1:
                team_win = Audio('audio/Joust/sounds/cyan team win.wav')
            if winning_team == 2:
                team_win = Audio('audio/Joust/sounds/magenta team win.wav')
            if winning_team == 3:
                team_win = Audio('audio/Joust/sounds/red team win.wav')
            team_win.start_effect()
        if self.game_mode == common.Games.WereJoust.value:
            if winning_team == -1:
                team_win = Audio('audio/Joust/sounds/werewolf win.wav')
            else:
                team_win = Audio('audio/Joust/sounds/human win.wav')
            team_win.start_effect()
        #self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        
    def werewolf_intro(self):
        Audio('audio/Joust/sounds/werewolf intro.wav').start_effect()
        time.sleep(3)
        self.change_all_move_colors(80, 0, 0)
        time.sleep(2)
        self.change_all_move_colors(30, 0, 0)
        time.sleep(18)
        self.change_all_move_colors(20, 20, 20)
        time.sleep(2)
        self.start_timer = time.time()
        

    def game_loop(self):
        self.track_moves()
        if self.game_mode == common.Games.WereJoust.value:
            self.werewolf_intro()
        self.werewolf_reveal.value = 1
        self.count_down()
        time.sleep(0.02)
        self.audio.start_audio_loop()
        time.sleep(0.8)
        
        while self.running:
            self.check_music_speed()
            self.check_end_game()
            self.werewolf_audio_cue()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
                    
                
                
        
        

            
        

            
Пример #25
0
class Fight_club():
    def __init__(self, moves, command_queue, ns, music, show_team_colors,
                 music_speed, dead_moves, force_move_colors, invincible_moves,
                 fight_club_colors, restart):

        self.command_queue = command_queue
        self.ns = ns

        self.voice = self.ns.settings['menu_voice']

        self.sensitivity = self.ns.settings['sensitivity']
        self.play_audio = self.ns.settings['play_audio']

        self.move_serials = moves

        self.tracked_moves = {}
        self.dead_moves = dead_moves
        self.music_speed = music_speed
        self.music_speed.value = 1.5  #Value('d', 1.5)
        self.running = True
        self.force_move_colors = force_move_colors
        self.invince_moves = invincible_moves

        self.start_timer = time.time()
        self.audio_cue = 0
        self.num_dead = 0
        self.show_team_colors = show_team_colors
        self.show_team_colors.value = 0  #Value('i', 0)
        self.teams = {}
        self.update_time = 0

        self.fighter_list = []
        self.create_fighter_list()

        self.chosen_defender = self.fighter_list.pop()
        self.chosen_fighter = self.fighter_list.pop()

        self.round_num = len(self.move_serials) * 2

        #-1 because we reset at the beginning of the game
        self.round_counter = -1

        self.round_time = time.time()
        self.round_limit = 22
        self.score = {}
        self.add_initial_score()
        self.timer_beep = 4
        self.high_score = 1
        self.current_winner = ""

        self.revive_noise = True
        #just for the sound effects
        self.revive_time = time.time() + 4

        self.colors = fight_club_colors
        self.restart = restart

        fast_resample = False
        if self.play_audio:
            self.loud_beep = Audio('audio/Joust/sounds/beep_loud.wav')
            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            self.revive = Audio('audio/Commander/sounds/revive.wav')

            end = False
            self.audio = music

        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.game_loop()

    def create_fighter_list(self):
        self.fighter_list = self.move_serials[:]
        shuffle(self.fighter_list)

    def add_initial_score(self):
        for move in self.move_serials:
            self.score[move] = 0

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):

            time.sleep(0.1)
            for i in range(3):
                self.force_move_colors[move_serial][i] = 1

            self.colors[move_serial].value = 0
            self.invince_moves[move_serial].value = True
            self.dead_moves[move_serial].value = 0

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.play_audio:
            self.start_game.start_effect()

    def get_change_time(self, speed_up):
        min_moves = len(self.move_serials) - 2
        if min_moves <= 0:
            min_moves = 1

        game_percent = (self.num_dead / min_moves)
        if game_percent > 1.0:
            game_percent = 1.0
        min_music_fast = common.lerp(MIN_MUSIC_FAST_TIME,
                                     END_MIN_MUSIC_FAST_TIME, game_percent)
        max_music_fast = common.lerp(MAX_MUSIC_FAST_TIME,
                                     END_MAX_MUSIC_FAST_TIME, game_percent)

        min_music_slow = common.lerp(MIN_MUSIC_SLOW_TIME,
                                     END_MIN_MUSIC_SLOW_TIME, game_percent)
        max_music_slow = common.lerp(MAX_MUSIC_SLOW_TIME,
                                     END_MAX_MUSIC_SLOW_TIME, game_percent)
        if speed_up:
            added_time = random.uniform(min_music_fast, max_music_fast)
        else:
            added_time = random.uniform(min_music_slow, max_music_slow)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip(
            (time.time() - self.change_time) / INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED,
                                                 SLOW_MUSIC_SPEED,
                                                 change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED,
                                                 FAST_MUSIC_SPEED,
                                                 change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time(
        ) < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
        elif time.time(
        ) >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up = not self.speed_up
            self.change_time = self.get_change_time(speed_up=self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False

    def check_end_round(self):
        if self.play_audio:
            if time.time() > self.round_time - (3 * (self.timer_beep / 4)):
                self.loud_beep.start_effect()
                self.timer_beep -= 1
            if time.time() > self.revive_time and self.revive_noise:
                self.revive_noise = False
                self.revive.start_effect()

        if time.time() > self.round_time:
            self.dead_moves[self.chosen_fighter].value = 0
            self.dead_moves[self.chosen_defender].value = 0
            self.colors[self.chosen_defender].value = 0
            self.colors[self.chosen_fighter].value = 0
            self.fighter_list.insert(0, self.chosen_defender)
            self.fighter_list.insert(0, self.chosen_fighter)
            if self.play_audio:
                self.explosion.start_effect()
            self.chosen_defender = self.fighter_list.pop()
            self.chosen_fighter = self.fighter_list.pop()

            self.invince_moves[self.chosen_fighter].value = True
            self.invince_moves[self.chosen_defender].value = True
            self.revive_fighters()
            self.reset_round_timer()

    def alive_move_count(self):
        count = 0
        for move, lives in self.dead_moves.items():
            if lives.value == 1:
                count += 1
        return count

    #more than one tied winner, have them face off
    def face_off(self):

        Audio('audio/Fight_Club/vox/' + self.voice +
              '/tie_game.wav').start_effect()
        for move in self.move_serials:
            self.dead_moves[move].value = 0
        for move in self.winning_moves:
            self.dead_moves[move].value = 1
            self.colors[move].value = 4
        count_explode = self.alive_move_count()
        while count_explode > 1:
            if count_explode > self.alive_move_count():
                count_explode = self.alive_move_count()
                if self.play_audio:
                    self.explosion.start_effect()
        self.winning_moves = []
        for move, lives in self.dead_moves.items():
            if lives.value == 1:
                self.winning_moves.append(move)
        self.game_end = True

    #check to see if there is a winner,
    #if there is a tie, have them face off, no time limit
    #set winning moves
    def check_winner(self):
        self.winning_moves = []
        self.winning_score = 0
        print(self.score.items())
        for move, score in self.score.items():
            if score == self.winning_score:
                self.winning_moves.append(move)
            if score > self.winning_score:
                self.winning_moves = []
                self.winning_moves.append(move)
                self.winning_score = score
        if len(self.winning_moves) > 1:
            self.face_off()
        else:
            self.game_end = True

    def check_end_game(self):
        if self.round_counter >= self.round_num:
            self.check_winner()
        if self.round_counter == self.round_num - 5:
            Audio('audio/Fight_Club/vox/' + self.voice +
                  '/5_rounds.wav').start_effect()
        if self.round_counter == self.round_num - 1:
            Audio('audio/Fight_Club/vox/' + self.voice +
                  '/last_round.wav').start_effect()

    def stop_tracking_moves(self):
        self.restart.value = 1

    def end_game(self):
        self.audio.stop_audio()
        self.update_status('ending')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        for move in self.move_serials:
            self.dead_moves[move].value = 0
        Audio('audio/Fight_Club/vox/' + self.voice +
              '/game_over.wav').start_effect()
        #os.popen('espeak -ven -p 70 -a 200 "winner"')

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                colors.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def check_next_fighter(self):
        #dead_moves: 0 dead waiting to be a fighter
        #dead_moves: 1 alive fighting
        #dead_moves: -1 just died as a fighter, go to the back of the line

        if self.dead_moves[self.chosen_defender].value == -1:
            self.invince_moves[self.chosen_fighter].value = True
            if self.play_audio:
                self.explosion.start_effect()
            self.add_score(self.chosen_fighter)
            self.fighter_list.insert(0, self.chosen_defender)
            self.dead_moves[self.chosen_defender].value = 0
            self.colors[self.chosen_defender].value = 0
            self.chosen_defender = self.chosen_fighter
            self.chosen_fighter = self.fighter_list.pop()
            self.revive_fighters()
            self.reset_round_timer()

            #move to the back of the line
        elif self.dead_moves[self.chosen_fighter].value == -1:
            self.invince_moves[self.chosen_defender].value = True
            if self.play_audio:
                self.explosion.start_effect()
            self.add_score(self.chosen_defender)
            self.fighter_list.insert(0, self.chosen_fighter)
            self.colors[self.chosen_fighter].value = 0
            self.dead_moves[self.chosen_fighter].value = 0
            self.chosen_fighter = self.fighter_list.pop()
            self.revive_fighters()
            self.reset_round_timer()

            #move to the back of the line

    def revive_fighters(self):
        if self.dead_moves[self.chosen_defender].value == 0:
            self.dead_moves[self.chosen_defender].value = 1
        if self.dead_moves[self.chosen_fighter].value == 0:
            self.dead_moves[self.chosen_fighter].value = 1

    def add_score(self, serial):
        if serial not in self.score:
            self.score[serial] = 1
        else:
            self.score[serial] += 1

    def get_highest_score(self):
        max_score = 1
        for move, score in self.score.items():
            if score > max_score:
                max_score = score
        return max_score

    def set_highest_score_color(self):
        max_score = self.get_highest_score()
        for move, score in self.score.items():
            if score == max_score:
                if self.colors[move].value == 0:
                    self.colors[move].value = 3
            elif self.colors[move].value == 3:
                self.colors[move].value = 0

    def reset_round_timer(self):
        self.revive_time = time.time() + 4
        self.revive_noise = True

        self.round_counter += 1
        self.round_time = time.time() + self.round_limit
        self.timer_beep = 4
        self.colors[self.chosen_defender].value = 1
        self.colors[self.chosen_fighter].value = 2
        print(self.score.items())
        self.set_highest_score_color()
        print(self.get_highest_score())
        print(self.high_score)
        if self.get_highest_score() > self.high_score:
            self.high_score = self.get_highest_score()
            if self.current_winner != self.chosen_defender:
                self.current_winner = self.chosen_defender
                saying = random.randint(0, 2)
                if saying == 0:
                    Audio('audio/Fight_Club/vox/' + self.voice +
                          '/defender_lead.wav').start_effect()
                elif saying == 1:
                    Audio('audio/Fight_Club/vox/' + self.voice +
                          '/defender_winning.wav').start_effect()
                elif saying == 2:
                    Audio('audio/Fight_Club/vox/' + self.voice +
                          '/Defender_high_score.wav').start_effect()
        self.check_end_game()

    def game_loop(self):
        self.track_moves()
        self.restart.value = 0
        self.show_team_colors.value = 0
        self.count_down()
        self.change_time = time.time() + 6
        time.sleep(0.02)
        if self.play_audio:
            self.audio.start_audio_loop()
        else:
            #when no audio is playing set the music speed to middle speed
            self.music_speed.value = (FAST_MUSIC_SPEED + SLOW_MUSIC_SPEED) / 2
        time.sleep(0.8)

        self.revive_fighters()
        self.reset_round_timer()
        while self.running:
            #I think the loop is so fast that this causes
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')
            self.check_next_fighter()
            self.check_end_round()
            if self.game_end:
                print("end game")
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

    def kill_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright, 0, 0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10
        self.running = False

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': 'Fight Club',
            'winning_team': winning_team
        }
        self.ns.status = data
Пример #26
0
class Ninja():
    def __init__(self, moves):

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.team_num = 2
        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}

        self.generate_random_teams(self.team_num)

        try:
            music = 'audio/Commander/music/' + random.choice(
                os.listdir('audio/Commander/music'))
        except:
            print('no music in audio/Commander/music')
        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        end = False
        try:
            self.audio = Audio(music, end)
        except:
            print('no audio loaded')
        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []

        self.game_loop()

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            opts = Array('i', [0] * 5)

            proc = Process(target=track_move,
                           args=(move_serial, move_num,
                                 self.teams[move_serial], self.team_num,
                                 dead_move, force_color, self.music_speed,
                                 opts))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        if speed_up:
            added_time = random.uniform(MIN_MUSIC_FAST_TIME,
                                        MAX_MUSIC_FAST_TIME)
        else:
            added_time = random.uniform(MIN_MUSIC_SLOW_TIME,
                                        MAX_MUSIC_SLOW_TIME)
        return time.time() + added_time

    def get_winning_team_members(self, winning_team):
        self.end_game_sound(winning_team)
        for move_serial in self.teams.keys():
            if self.teams[move_serial] == winning_team:
                self.winning_moves.append(move_serial)

    def check_end_game(self):
        winning_team = -100
        team_win = False

        for move_serial, dead in self.dead_moves.items():
            if dead.value == 0:
                dead_team = self.teams[move_serial]
                winning_team = (self.teams[move_serial] + 1) % 2

                #This is to play the sound effect
                dead.value = -1
                self.explosion.start_effect()

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        #if self.game_mode == common.Games.JoustTeams:
        if winning_team == Team.red.value:
            team_win = Audio('audio/Commander/sounds/red winner.wav')
        if winning_team == Team.blue.value:
            team_win = Audio('audio/Commander/sounds/blue winner.wav')
        team_win.start_effect()

    def game_loop(self):
        self.track_moves()

        self.count_down()
        time.sleep(0.02)
        try:
            self.audio.start_audio_loop()
        except:
            print('no audio loaded to start')
        time.sleep(0.8)

        while self.running:
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #27
0
class Zombie:
    def __init__(self, cont_alive):
        self.humans = []
        self.alive_zombies = []
        self.dead_zombies = {}
        self.controller_opts = {}
        self.controllers_alive = cont_alive
        self.win_time =  ((len(self.controllers_alive) * 3)/16) * 60
        if self.win_time <= 0:
            self.win_time = 60
        self.start_time = time.time()
        self.pickup = Audio('audio/Zombie/sound_effects/pickup.wav')
        self.effect_cue = 0
        self.Start()

    #def get_weapon(self, random_chance):
    #    chance = random.choice(range(random_chance))
    #    if chance == 0:
    #        random_human = random.choice(self.humans)
     #       if self.controller_opts[random_human][6] == 4:
    #            for human in range(len(self.humans)):
    #                if self.controller_opts[random_human][6] < 4:
    #                    random_human = i
    #                    break
            
            #human has no weapon, give pistol
     #       if self.controller_opts[random_human][6] == 0:
    #            self.controller_opts[random_human][6] = 1
    #            self.controller_opts[random_human][5] = 1
            #has pistol give shotgun
    #        elif self.controller_opts[random_human][6] == 1:
    #            self.controller_opts[random_human][6] = 2
    #            self.controller_opts[random_human][5] = 2
    #            Audio('audio/Zombie/sound_effects/shotgun found.wav').start_effect()
            #has shotgun give molotov
     #       elif self.controller_opts[random_human][6] == 2:
     #           self.controller_opts[random_human][6] = 4
     #           self.controller_opts[random_human][5] = 3
     #           Audio('audio/Zombie/sound_effects/molotov found.wav').start_effect()
        
    def get_kill_time(self):
        percent_to_win = 1.0 * (time.time() - self.start_time)/(self.win_time * 1.0)
        random_num = ((1.0 - percent_to_win) * 7) + 2
        return random.uniform(random_num, random_num +2)

    def kill_zombies(self, num_zombies, random_bullet_chance):
        kill_zombie = False
        for i in range(num_zombies):
            if self.alive_zombies:
                kill_zombie = True
                shot_zombie_serial = random.choice(self.alive_zombies)
                self.controller_opts[shot_zombie_serial][3] = 0
                self.dead_zombies[shot_zombie_serial] = time.time() + self.get_kill_time()
                self.alive_zombies.remove(shot_zombie_serial)

        if kill_zombie:
            self.reward(random_bullet_chance)

    def reward(self, random_bullet_chance):
        random_bullet = random.choice(random_bullet_chance)
        sound = False
        for i in range(random_bullet):
            sound = True
            random_human = random.choice(self.humans)
            if self.controller_opts[random_human][4] < 5:
                self.controller_opts[random_human][4] += 1
        if sound:
            self.pickup.start_effect()
        #one in 5 chance of getting a weapon
        #self.get_weapon(5)

    def audio_cue(self):
        if self.win_time - (time.time() - self.start_time) <= 10 and self.effect_cue <= 4:
            Audio('audio/Zombie/sound_effects/10 seconds left.wav').start_effect()
            self.effect_cue = 5
        elif self.win_time - (time.time() - self.start_time) <= 30 and self.effect_cue <= 3:
            Audio('audio/Zombie/sound_effects/30 seconds.wav').start_effect()
            self.effect_cue = 4
        elif self.win_time - (time.time() - self.start_time) <= 1*60 and self.effect_cue <= 2:
            Audio('audio/Zombie/sound_effects/1 minute.wav').start_effect()
            self.effect_cue = 3
        elif self.win_time - (time.time() - self.start_time) <= 3*60 and self.effect_cue <= 1:
            Audio('audio/Zombie/sound_effects/3 minutes.wav').start_effect()
            self.effect_cue = 2
        #elif self.win_time - (time.time() - self.start_time) <= 5*60 and self.effect_cue <= 0:
        #   Audio('audio/Zombie/sound_effects/5 minutes.wav').start_effect()
        #   self.effect_cue = 1

        
    
    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')
Пример #28
0
 def end_game_sound(self, winning_team):
     if winning_team == Team.red.value:
         team_win = Audio('audio/Commander/sounds/red winner.wav')
     if winning_team == Team.blue.value:
         team_win = Audio('audio/Commander/sounds/blue winner.wav')
     team_win.start_effect()
Пример #29
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)

        if self.play_audio:
            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:
                self.music.start_audio_loop()
##                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 random_human in random.sample(set(self.humans), 2):
            #random_human = random.choice(self.humans)
            self.controller_opts[random_human][3] = 0

        while running:
            if self.play_audio:
                self.audio_cue()
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            #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:
                    if self.play_audio:
                        pistol.start_effect()
                    self.kill_zombies(1, [0, 0, 0, 0, 1, 1, 1])
                    self.controller_opts[serial][1] = 0

                #molotov fired(5 bullets all alive zombies)
                elif self.controller_opts[serial][1] == 4:
                    if self.play_audio:
                        molotov.start_effect()
                    self.kill_zombies(50, [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 or self.kill_game:
                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 colors.hsv2rgb(*colour)]
                                for colour in HSV]
                win_controllers = []
                if len(self.humans) <= 0:
                    if self.play_audio:
                        zombie_victory.start_effect()
                    self.alive_zombies.extend(self.dead_zombies.keys())
                    self.update_status('ending', 1)
                    win_controllers = self.alive_zombies
                if (time.time() - self.start_time) > self.win_time:
                    if self.play_audio:
                        human_victory.start_effect()
                    win_controllers = self.humans
                    self.update_status('ending', 0)
                if self.kill_game:
                    self.alive_zombies.extend(self.dead_zombies.keys())
                    win_controllers = self.humans + self.alive_zombies
                    self.update_status('killed')
                #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
                if self.play_audio:
                    try:
                        self.music.stop_audio()
                    except:
                        print('no audio loaded')
Пример #30
0
class Swapper():
    def __init__(self, moves, speed, command_queue, status_ns, audio_toggle, music):
        global SLOW_MAX
        global SLOW_WARNING
        global FAST_MAX
        global FAST_WARNING
        
        SLOW_MAX = common.SLOW_MAX[speed]
        SLOW_WARNING = common.SLOW_WARNING[speed]
        FAST_MAX = common.FAST_MAX[speed]
        FAST_WARNING = common.FAST_WARNING[speed]
        
        self.audio_toggle = audio_toggle
        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.team_num = 2

        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}

        self.command_queue = command_queue
        self.status_ns = status_ns
        self.update_time = 0


        self.generate_random_teams(self.team_num)
        if self.audio_toggle:
##            music = 'audio/Joust/music/' + random.choice(os.listdir('audio/Joust/music'))

            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            fast_resample = False
            end = False
            try:
                self.audio = music
            except:
                print('no audio loaded')

        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.game_loop()

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        for serial in self.move_serials:
            random_choice = Value('i',  random.choice(team_pick) )
            self.teams[serial] = random_choice
            team_pick.remove(random_choice.value)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            opts = Array('i', [0] * 5)
            proc = Process(target=track_move, args=(move_serial,
                                                    move_num,
                                                    self.teams[move_serial],
                                                    self.team_num,
                                                    dead_move,
                                                    force_color,
                                                    self.music_speed,

                                                    opts))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts
            
    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.audio_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.audio_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.audio_toggle:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.audio_toggle:
            self.start_game.start_effect()

    def check_end_game(self):
        self.winning_team = -100
        team_win = True
        
        for move_serial, dead in self.dead_moves.items():
            if self.winning_team == -100:
                self.winning_team = self.teams[move_serial].value
            if self.teams[move_serial].value != self.winning_team:
                team_win = False
                #TODO: This wont work if the last move is the first of the dead_moves
                self.last_move = move_serial
            if dead.value == 0:
                #This is to play the sound effect
                dead.value = -1
                if self.audio_toggle:
                    self.explosion.start_effect()
        self.game_end = team_win


    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        if self.audio_toggle:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        self.update_status('ending',self.winning_team)
        if self.audio_toggle:
            self.end_game_sound(self.winning_team)
        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.move_serials:
                if win_move != self.last_move:
                    win_color_array = self.force_move_colors[win_move]
                    common.change_color(win_color_array, *win_color)
                else:
                    win_color_array = self.force_move_colors[win_move]
                    common.change_color(win_color_array, 1,1,1)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        if winning_team == Team.red.value:
            team_win = Audio('audio/Commander/sounds/red winner.wav')
        if winning_team == Team.blue.value:
            team_win = Audio('audio/Commander/sounds/blue winner.wav')
        team_win.start_effect()

    def game_loop(self):
        self.track_moves()
        self.count_down()
        if self.audio_toggle:
            try:
                self.audio.start_audio_loop()
            except:
                print('no audio loaded to start')
        while self.running:
            #I think the loop is so fast that this causes 
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not(self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not(package == None):
            if command == 'killgame':
                self.kill_game()

    def kill_game(self):
        if self.audio_toggle:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')        
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE     
        
        h_value = 0
        while (time.time() < end_time):
            time.sleep(0.01)
            color = common.hsv2rgb(h_value, 1, 1)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                common.change_color(color_array, *color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def update_status(self,game_status,winning_team=-1):
        data ={'game_status' : game_status,
               'game_mode' : 'Swapper',
               'winning_team' : winning_team}
        team_total = [0,0]
        team_alive = [0,0]
        for move in self.move_serials:
            team = self.teams[move].value
            team_total[team] += 1
            if self.dead_moves[move].value == 1:
                team_alive[team] += 1
        team_comp = list(zip(team_total,team_alive))
        data['team_comp'] = team_comp
        self.status_ns.status_dict = data
                    
            
Пример #31
0
class Commander():
    def __init__(self, moves):

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value('d', 1)
        self.running = True
        self.force_move_colors = {}
        self.team_num = 2
        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}
        self.current_commander = ["", ""]

        self.time_to_power = [20, 20]
        self.activated_time = [time.time(), time.time()]

        self.activated_overdrive = [time.time(), time.time()]

        self.powers = [Value('d', 0.0), Value('d', 0.0)]
        #self.red_power = Value('d', 0.0)
        #self.blue_power = Value('d', 0.0)

        self.red_overdrive = Value('i', 0)
        self.blue_overdrive = Value('i', 0)

        self.generate_random_teams(self.team_num)
        self.commander_intro = Value('i', 1)

        self.powers_active = [False, False]

        try:
            music = 'audio/Commander/music/' + random.choice(
                os.listdir('audio/Commander/music'))
        except:
            print('no music in audio/Commander/music')
        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        end = False
        try:
            self.audio = Audio(music, end)
        except:
            print('no audio loaded')
        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []

        self.game_loop()

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value('i', 1)
            force_color = Array('i', [1] * 3)
            opts = Array('i', [0] * 5)
            power = self.powers[self.teams[move_serial]]
            #if self.teams[move_serial] == Team.red:
            #    power = self.red_power
            #else:
            #    power = self.blue_power

            if self.teams[move_serial] == Team.red.value:
                overdrive = self.red_overdrive
            else:
                overdrive = self.blue_overdrive
            proc = Process(target=track_move,
                           args=(move_serial, move_num,
                                 self.teams[move_serial], self.team_num,
                                 dead_move, force_color, self.music_speed,
                                 self.commander_intro, opts, power, overdrive))
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        if speed_up:
            added_time = random.uniform(MIN_MUSIC_FAST_TIME,
                                        MAX_MUSIC_FAST_TIME)
        else:
            added_time = random.uniform(MIN_MUSIC_SLOW_TIME,
                                        MAX_MUSIC_SLOW_TIME)
        return time.time() + added_time

    #def change_music_speed(self, fast):
    #    change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
    #    if fast:
    #        self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
    #    elif not fast:
    #        self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
    #    self.audio.change_ratio(self.music_speed.value)

    #def check_music_speed(self):
    #    if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
    #        self.change_music_speed(self.speed_up)
    #        self.currently_changing = True
    #        self.audio.change_chunk_size(True)
    #    elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
    #        self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
    #        self.speed_up =  not self.speed_up
    #        self.change_time = self.get_change_time(speed_up = self.speed_up)
    #        self.audio.change_ratio(self.music_speed.value)
    #       self.currently_changing = False
    #       self.audio.change_chunk_size(False)

    def get_winning_team_members(self, winning_team):
        self.end_game_sound(winning_team)
        for move_serial in self.teams.keys():
            if self.teams[move_serial] == winning_team:
                self.winning_moves.append(move_serial)

    def check_end_game(self):
        winning_team = -100
        team_win = False
        for commander in self.current_commander:
            if self.dead_moves[commander].value <= 0:
                winning_team = (self.teams[commander] + 1) % 2
                self.get_winning_team_members(winning_team)
                self.game_end = True

        for move_serial, dead in self.dead_moves.items():
            if dead.value == 0:
                dead_team = self.teams[move_serial]
                winning_team = (self.teams[move_serial] + 1) % 2
                if self.time_to_power[winning_team] > 15:
                    self.time_to_power[winning_team] -= 1
                if self.time_to_power[dead_team] < 25:
                    self.time_to_power[dead_team] += 1

                #This is to play the sound effect
                dead.value = -1
                self.explosion.start_effect()

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        try:
            self.audio.stop_audio()
        except:
            print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        #if self.game_mode == common.Games.JoustTeams:
        if winning_team == Team.red.value:
            team_win = Audio('audio/Commander/sounds/red winner.wav')
        if winning_team == Team.blue.value:
            team_win = Audio('audio/Commander/sounds/blue winner.wav')
        team_win.start_effect()

    def check_commander_select(self):
        for move_serial in self.move_opts.keys():
            if self.move_opts[move_serial][
                    Opts.selection.
                    value] == Selections.triangle.value and self.move_opts[
                        move_serial][
                            Opts.holding.value] == Holding.holding.value:
                Audio('audio/Commander/sounds/commanderselect.wav'
                      ).start_effect()
                self.change_commander(move_serial)
                self.move_opts[move_serial][
                    Opts.selection.value] = Selections.nothing.value
            elif self.move_opts[move_serial][
                    Opts.selection.
                    value] == Selections.a_button.value and self.move_opts[
                        move_serial][
                            Opts.holding.value] == Holding.holding.value:
                Audio('audio/Commander/sounds/buttonselect.wav').start_effect()
                self.move_opts[move_serial][
                    Opts.selection.value] = Selections.nothing.value

    def change_commander(self, new_commander):
        #print 'changing commander to ' + str(new_commander)
        commander_team = self.teams[new_commander]
        if self.current_commander[commander_team] != '':
            self.move_opts[self.current_commander[commander_team]][
                Opts.is_commander.value] = Bool.no.value

        self.move_opts[new_commander][Opts.is_commander.value] = Bool.yes.value
        self.current_commander[commander_team] = new_commander

    def change_random_commander(self, team, exclude_commander=None):
        team_move_serials = [
            move_serial for move_serial in self.move_opts.keys()
            if (self.teams[move_serial] == team and move_serial !=
                exclude_commander and self.dead_moves[move_serial].value >= 1)
        ]
        print('team move serials is ' + str(team_move_serials))
        if len(team_move_serials) > 0:
            new_commander = random.choice(team_move_serials)
            self.change_commander(new_commander)
            return True
        return False

    def update_team_powers(self):
        self.powers[Team.red.value].value = max(
            min((time.time() - self.activated_time[Team.red.value]) /
                (self.time_to_power[Team.red.value] * 1.0), 1.0), 0.0)
        self.powers[Team.blue.value].value = max(
            min((time.time() - self.activated_time[Team.blue.value]) /
                (self.time_to_power[Team.blue.value] * 1.0), 1.0), 0.0)

        if self.powers_active[Team.red.value] == False:
            if self.powers[Team.red.value].value >= 1.0:
                self.powers_active[Team.red.value] = True
                Audio('audio/Commander/sounds/power ready.wav').start_effect()
                Audio('audio/Commander/sounds/red power ready.wav'
                      ).start_effect()

        if self.powers_active[Team.blue.value] == False:
            if self.powers[Team.blue.value].value >= 1.0:
                self.powers_active[Team.blue.value] = True
                Audio('audio/Commander/sounds/power ready.wav').start_effect()
                Audio('audio/Commander/sounds/blue power ready.wav'
                      ).start_effect()

    def overdrive(self, team):
        Audio('audio/Commander/sounds/overdrive.wav').start_effect()
        if team == Team.red.value:
            self.red_overdrive.value = 1
            self.activated_overdrive[Team.red.value] = time.time() + 10
            Audio('audio/Commander/sounds/red overdrive.wav').start_effect()
        else:
            self.blue_overdrive.value = 1
            self.activated_overdrive[Team.blue.value] = time.time() + 10
            Audio('audio/Commander/sounds/blue overdrive.wav').start_effect()

    #def revive(self, team):
    #    print ('dadooda')
    #    dead_team_moves = [ move_serial for move_serial in self.move_opts.keys() if (self.teams[move_serial] == team and self.dead_moves[move_serial].value <= 0) ]
    #print 'dead_team_moves is ' + str(dead_team_moves)
    #    for move in dead_team_moves:
    #        self.dead_moves[move].value = 3
    #    Audio('audio/Commander/sounds/revive.wav').start_effect()
    #    if team == Team.red.value:
    #        Audio('audio/Commander/sounds/red revive.wav').start_effect()
    #    if team == Team.blue.value:
    #       Audio('audio/Commander/sounds/blue revive.wav').start_effect()

    #def shift(self, team, commander):
    #   print ('shifty')
    #    did_shift = self.change_random_commander(team, exclude_commander = commander)
#     if did_shift:
#         Audio('audio/Commander/sounds/shift.wav').start_effect()
#         if team == Team.red.value:
#            Audio('audio/Commander/sounds/red shift.wav').start_effect()
#        if team == Team.blue.value:
#            Audio('audio/Commander/sounds/blue shift.wav').start_effect()
#    return did_shift

    def check_end_of_overdrive(self):
        if self.red_overdrive.value == 1:

            if time.time() >= self.activated_overdrive[Team.red.value]:
                #print 'its over'
                self.red_overdrive.value = 0
        if self.blue_overdrive.value == 1:

            if time.time() >= self.activated_overdrive[Team.blue.value]:
                #print 'itsa over'
                self.blue_overdrive.value = 0

    def reset_power(self, team):
        self.powers[team].value == 0.0
        self.activated_time[team] = time.time()
        self.powers_active[team] = False

    def check_commander_power(self):
        #print str(self.powers[0].value)
        #print str(self.powers[1].value)
        for commander in self.current_commander:
            #print self.move_opts[commander][Opts.selection]
            if self.move_opts[commander][
                    Opts.selection.value] == Selections.trigger.value:
                self.overdrive(self.teams[commander])
                self.reset_power(self.teams[commander])
                self.move_opts[commander][
                    Opts.selection.value] = Selections.nothing.value

            #if self.move_opts[commander][Opts.selection.value] == Selections.a_button.value:
            #    self.revive(self.teams[commander])
            #    self.reset_power(self.teams[commander])
            #    self.move_opts[commander][Opts.selection.value] = Selections.nothing.value

            #if self.move_opts[commander][Opts.selection.value] == Selections.triangle.value:
            #    if self.shift(self.teams[commander], commander):
            #       self.reset_power(self.teams[commander])
            #    self.move_opts[commander][Opts.selection.value] = Selections.nothing.value

    def check_everyone_in(self):
        for move_serial in self.move_opts.keys():
            if self.move_opts[move_serial][
                    Opts.holding.value] == Holding.not_holding.value:
                return False
        return True

    def commander_intro_audio(self):
        #print 'BOOOP'
        intro_sound = Audio('audio/Commander/sounds/commander intro.wav')
        intro_sound.start_effect()
        #need while loop here
        play_last_one = True
        commander_select_time = time.time() + 50
        battle_ready_time = time.time() + 40
        while time.time() < commander_select_time:
            self.check_commander_select()
            if self.check_everyone_in():
                break

            if time.time() > battle_ready_time and play_last_one:
                play_last_one = False
                Audio('audio/Commander/sounds/10 seconds begins.wav'
                      ).start_effect()
        intro_sound.stop_effect()

        if self.current_commander[Team.red.value] == '':
            self.change_random_commander(Team.red.value)
        if self.current_commander[Team.blue.value] == '':
            self.change_random_commander(Team.blue.value)

        Audio('audio/Commander/sounds/commanders chosen.wav').start_effect()
        time.sleep(4)
        self.reset_power(Team.red.value)
        self.reset_power(Team.blue.value)
        self.commander_intro.value = 0

    def game_loop(self):
        self.track_moves()
        self.commander_intro_audio()

        self.count_down()
        time.sleep(0.02)
        try:
            self.audio.start_audio_loop()
        except:
            print('no audio loaded to start')
        time.sleep(0.8)

        while self.running:

            #self.check_music_speed()

            self.update_team_powers()
            self.check_commander_power()
            self.check_end_of_overdrive()
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #32
0
class Bubble():
    def __init__(self, moves):

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.scores = {}
        self.music_speed = Value('d', 1.5)
        self.running = True
        self.force_move_colors = {}
        self.teams = {}
        self.win_amount = int(math.ceil((len(moves) / 2.0) + 2))
        self.team_num = 2

        self.generate_random_teams(self.team_num)

        music = 'audio/Joust/music/' + random.choice(
            os.listdir('audio/Joust/music'))
        self.start_beep = Audio('audio/Joust/sounds/start.wav')
        self.start_game = Audio('audio/Joust/sounds/start3.wav')
        self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
        fast_resample = False
        if len(moves) >= 5:
            fast_resample = True
        self.audio = Audio(music, fast_resample)
        #self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.losing_moves = []

        self.game_loop()

    def generate_random_teams(self, team_num):
        team_pick = range(team_num)
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = range(team_num)

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            dead_move = Value('i', 1)
            score = Value('i', 0)

            force_color = Array('i', [1] * 3)
            proc = Process(target=track_move,
                           args=(move_serial, move_num,
                                 self.teams[move_serial], self.team_num, score,
                                 self.win_amount, dead_move, force_color,
                                 self.music_speed))
            proc.start()
            self.scores[move_serial] = score
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.itervalues():
            common.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(70, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        if speed_up:
            added_time = random.uniform(MIN_MUSIC_FAST_TIME,
                                        MAX_MUSIC_FAST_TIME)
        else:
            added_time = random.uniform(MIN_MUSIC_SLOW_TIME,
                                        MAX_MUSIC_SLOW_TIME)
        return time.time() + added_time

    def change_music_speed(self, fast):
        change_percent = numpy.clip(
            (time.time() - self.change_time) / INTERVAL_CHANGE, 0, 1)
        if fast:
            self.music_speed.value = common.lerp(FAST_MUSIC_SPEED,
                                                 SLOW_MUSIC_SPEED,
                                                 change_percent)
        elif not fast:
            self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED,
                                                 FAST_MUSIC_SPEED,
                                                 change_percent)
        self.audio.change_ratio(self.music_speed.value)

    def check_music_speed(self):
        if time.time() > self.change_time and time.time(
        ) < self.change_time + INTERVAL_CHANGE:
            self.change_music_speed(self.speed_up)
            self.currently_changing = True
            self.audio.change_chunk_size(True)
        elif time.time(
        ) >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
            self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
            self.speed_up = not self.speed_up
            self.change_time = self.get_change_time(speed_up=self.speed_up)
            self.audio.change_ratio(self.music_speed.value)
            self.currently_changing = False
            self.audio.change_chunk_size(False)

    def get_real_team(self, team):
        if team < 0:
            return -1
        else:
            return team

    def check_end_game(self):
        team_win = False
        for move_serial, score in self.scores.iteritems():
            #if we are alive
            if score.value >= self.win_amount:
                winning_team = self.teams[move_serial]
                team_win = True

        if team_win:
            for move_serial in self.teams.iterkeys():
                if self.teams[move_serial] == winning_team:
                    self.winning_moves.append(move_serial)
                else:
                    self.losing_moves.append(move_serial)
            self.game_end = True

    def check_for_points(self):
        for move_serial, score in self.scores.iteritems():
            if score.value == -1:
                score.value = 0
                self.explosion.start_effect()
                team_increase = self.teams[move_serial]
                for move_serial_increase, score_increase in self.scores.iteritems(
                ):
                    if self.teams[move_serial_increase] != team_increase:
                        score_increase.value += 1

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.itervalues():
            proc.terminate()
            proc.join()

    def end_game(self):
        self.audio.stop_audio()
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            for lose_move in self.losing_moves:
                lose_color_array = self.force_move_colors[lose_move]
                common.change_color(lose_color_array, 1, 0, 0)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def game_loop(self):
        self.track_moves()
        self.count_down()
        self.audio.start_audio_loop()

        while self.running:

            self.check_music_speed()
            self.check_for_points()
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #33
0
class Commander:
    def __init__(self, moves):

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = {}
        self.teams = {}
        self.music_speed = Value("d", 1)
        self.running = True
        self.force_move_colors = {}
        self.team_num = 2
        self.werewolf_timer = 35
        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = {}
        self.current_commander = ["", ""]

        self.time_to_power = [20, 20]
        self.activated_time = [time.time(), time.time()]

        self.activated_overdrive = [time.time(), time.time()]

        self.powers = [Value("d", 0.0), Value("d", 0.0)]
        # self.red_power = Value('d', 0.0)
        # self.blue_power = Value('d', 0.0)

        self.red_overdrive = Value("i", 0)
        self.blue_overdrive = Value("i", 0)

        self.generate_random_teams(self.team_num)
        self.commander_intro = Value("i", 1)

        self.powers_active = [False, False]

        try:
            music = "audio/Commander/music/" + random.choice(os.listdir("audio/Commander/music"))
        except:
            print("no music in audio/Commander/music")
        self.start_beep = Audio("audio/Joust/sounds/start.wav")
        self.start_game = Audio("audio/Joust/sounds/start3.wav")
        self.explosion = Audio("audio/Joust/sounds/Explosion34.wav")
        fast_resample = False
        end = False
        try:
            self.audio = Audio(music, end)
        except:
            print("no audio loaded")
        # self.change_time = self.get_change_time(speed_up = True)
        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []

        self.game_loop()

    def generate_random_teams(self, team_num):
        team_pick = list(range(team_num))
        for serial in self.move_serials:
            random_choice = random.choice(team_pick)
            self.teams[serial] = random_choice
            team_pick.remove(random_choice)
            if not team_pick:
                team_pick = list(range(team_num))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            time.sleep(0.02)
            dead_move = Value("i", 1)
            force_color = Array("i", [1] * 3)
            opts = Array("i", [0] * 5)
            power = self.powers[self.teams[move_serial]]
            # if self.teams[move_serial] == Team.red:
            #    power = self.red_power
            # else:
            #    power = self.blue_power

            if self.teams[move_serial] == Team.red.value:
                overdrive = self.red_overdrive
            else:
                overdrive = self.blue_overdrive
            proc = Process(
                target=track_move,
                args=(
                    move_serial,
                    move_num,
                    self.teams[move_serial],
                    self.team_num,
                    dead_move,
                    force_color,
                    self.music_speed,
                    self.commander_intro,
                    opts,
                    power,
                    overdrive,
                ),
            )
            proc.start()
            self.tracked_moves[move_serial] = proc
            self.dead_moves[move_serial] = dead_move
            self.force_move_colors[move_serial] = force_color
            self.move_opts[move_serial] = opts

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            common.change_color(color, r, g, b)

    # need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        self.start_game.start_effect()

    def get_change_time(self, speed_up):
        if speed_up:
            added_time = random.uniform(MIN_MUSIC_FAST_TIME, MAX_MUSIC_FAST_TIME)
        else:
            added_time = random.uniform(MIN_MUSIC_SLOW_TIME, MAX_MUSIC_SLOW_TIME)
        return time.time() + added_time

    # def change_music_speed(self, fast):
    #    change_percent = numpy.clip((time.time() - self.change_time)/INTERVAL_CHANGE, 0, 1)
    #    if fast:
    #        self.music_speed.value = common.lerp(FAST_MUSIC_SPEED, SLOW_MUSIC_SPEED, change_percent)
    #    elif not fast:
    #        self.music_speed.value = common.lerp(SLOW_MUSIC_SPEED, FAST_MUSIC_SPEED, change_percent)
    #    self.audio.change_ratio(self.music_speed.value)

    # def check_music_speed(self):
    #    if time.time() > self.change_time and time.time() < self.change_time + INTERVAL_CHANGE:
    #        self.change_music_speed(self.speed_up)
    #        self.currently_changing = True
    #        self.audio.change_chunk_size(True)
    #    elif time.time() >= self.change_time + INTERVAL_CHANGE and self.currently_changing:
    #        self.music_speed.value = SLOW_MUSIC_SPEED if self.speed_up else FAST_MUSIC_SPEED
    #        self.speed_up =  not self.speed_up
    #        self.change_time = self.get_change_time(speed_up = self.speed_up)
    #        self.audio.change_ratio(self.music_speed.value)
    #       self.currently_changing = False
    #       self.audio.change_chunk_size(False)

    def get_winning_team_members(self, winning_team):
        self.end_game_sound(winning_team)
        for move_serial in self.teams.keys():
            if self.teams[move_serial] == winning_team:
                self.winning_moves.append(move_serial)

    def check_end_game(self):
        winning_team = -100
        team_win = False
        for commander in self.current_commander:
            if self.dead_moves[commander].value <= 0:
                winning_team = (self.teams[commander] + 1) % 2
                self.get_winning_team_members(winning_team)
                self.game_end = True

        for move_serial, dead in self.dead_moves.items():
            if dead.value == 0:
                dead_team = self.teams[move_serial]
                winning_team = (self.teams[move_serial] + 1) % 2
                if self.time_to_power[winning_team] > 15:
                    self.time_to_power[winning_team] -= 1
                if self.time_to_power[dead_team] < 25:
                    self.time_to_power[dead_team] += 1

                # This is to play the sound effect
                dead.value = -1
                self.explosion.start_effect()

    def stop_tracking_moves(self):
        for proc in self.tracked_moves.values():
            proc.terminate()
            proc.join()
            time.sleep(0.02)

    def end_game(self):
        try:
            self.audio.stop_audio()
        except:
            print("no audio loaded to stop")
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0

        while time.time() < end_time:
            time.sleep(0.01)
            win_color = common.hsv2rgb(h_value, 1, 1)
            for win_move in self.winning_moves:
                win_color_array = self.force_move_colors[win_move]
                common.change_color(win_color_array, *win_color)
            h_value = h_value + 0.01
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        # if self.game_mode == common.Games.JoustTeams:
        if winning_team == Team.red.value:
            team_win = Audio("audio/Commander/sounds/red winner.wav")
        if winning_team == Team.blue.value:
            team_win = Audio("audio/Commander/sounds/blue winner.wav")
        team_win.start_effect()

    def check_commander_select(self):
        for move_serial in self.move_opts.keys():
            if (
                self.move_opts[move_serial][Opts.selection.value] == Selections.triangle.value
                and self.move_opts[move_serial][Opts.holding.value] == Holding.holding.value
            ):
                Audio("audio/Commander/sounds/commanderselect.wav").start_effect()
                self.change_commander(move_serial)
                self.move_opts[move_serial][Opts.selection.value] = Selections.nothing.value
            elif (
                self.move_opts[move_serial][Opts.selection.value] == Selections.a_button.value
                and self.move_opts[move_serial][Opts.holding.value] == Holding.holding.value
            ):
                Audio("audio/Commander/sounds/buttonselect.wav").start_effect()
                self.move_opts[move_serial][Opts.selection.value] = Selections.nothing.value

    def change_commander(self, new_commander):
        # print 'changing commander to ' + str(new_commander)
        commander_team = self.teams[new_commander]
        if self.current_commander[commander_team] != "":
            self.move_opts[self.current_commander[commander_team]][Opts.is_commander.value] = Bool.no.value

        self.move_opts[new_commander][Opts.is_commander.value] = Bool.yes.value
        self.current_commander[commander_team] = new_commander

    def change_random_commander(self, team, exclude_commander=None):
        team_move_serials = [
            move_serial
            for move_serial in self.move_opts.keys()
            if (
                self.teams[move_serial] == team
                and move_serial != exclude_commander
                and self.dead_moves[move_serial].value >= 1
            )
        ]
        print("team move serials is " + str(team_move_serials))
        if len(team_move_serials) > 0:
            new_commander = random.choice(team_move_serials)
            self.change_commander(new_commander)
            return True
        return False

    def update_team_powers(self):
        self.powers[Team.red.value].value = max(
            min((time.time() - self.activated_time[Team.red.value]) / (self.time_to_power[Team.red.value] * 1.0), 1.0),
            0.0,
        )
        self.powers[Team.blue.value].value = max(
            min(
                (time.time() - self.activated_time[Team.blue.value]) / (self.time_to_power[Team.blue.value] * 1.0), 1.0
            ),
            0.0,
        )

        if self.powers_active[Team.red.value] == False:
            if self.powers[Team.red.value].value >= 1.0:
                self.powers_active[Team.red.value] = True
                Audio("audio/Commander/sounds/power ready.wav").start_effect()
                Audio("audio/Commander/sounds/red power ready.wav").start_effect()

        if self.powers_active[Team.blue.value] == False:
            if self.powers[Team.blue.value].value >= 1.0:
                self.powers_active[Team.blue.value] = True
                Audio("audio/Commander/sounds/power ready.wav").start_effect()
                Audio("audio/Commander/sounds/blue power ready.wav").start_effect()

    def overdrive(self, team):
        Audio("audio/Commander/sounds/overdrive.wav").start_effect()
        if team == Team.red.value:
            self.red_overdrive.value = 1
            self.activated_overdrive[Team.red.value] = time.time() + 10
            Audio("audio/Commander/sounds/red overdrive.wav").start_effect()
        else:
            self.blue_overdrive.value = 1
            self.activated_overdrive[Team.blue.value] = time.time() + 10
            Audio("audio/Commander/sounds/blue overdrive.wav").start_effect()

    # def revive(self, team):
    #    print ('dadooda')
    #    dead_team_moves = [ move_serial for move_serial in self.move_opts.keys() if (self.teams[move_serial] == team and self.dead_moves[move_serial].value <= 0) ]
    # print 'dead_team_moves is ' + str(dead_team_moves)
    #    for move in dead_team_moves:
    #        self.dead_moves[move].value = 3
    #    Audio('audio/Commander/sounds/revive.wav').start_effect()
    #    if team == Team.red.value:
    #        Audio('audio/Commander/sounds/red revive.wav').start_effect()
    #    if team == Team.blue.value:
    #       Audio('audio/Commander/sounds/blue revive.wav').start_effect()

    # def shift(self, team, commander):
    #   print ('shifty')
    #    did_shift = self.change_random_commander(team, exclude_commander = commander)
    #     if did_shift:
    #         Audio('audio/Commander/sounds/shift.wav').start_effect()
    #         if team == Team.red.value:
    #            Audio('audio/Commander/sounds/red shift.wav').start_effect()
    #        if team == Team.blue.value:
    #            Audio('audio/Commander/sounds/blue shift.wav').start_effect()
    #    return did_shift

    def check_end_of_overdrive(self):
        if self.red_overdrive.value == 1:

            if time.time() >= self.activated_overdrive[Team.red.value]:
                # print 'its over'
                self.red_overdrive.value = 0
        if self.blue_overdrive.value == 1:

            if time.time() >= self.activated_overdrive[Team.blue.value]:
                # print 'itsa over'
                self.blue_overdrive.value = 0

    def reset_power(self, team):
        self.powers[team].value == 0.0
        self.activated_time[team] = time.time()
        self.powers_active[team] = False

    def check_commander_power(self):
        # print str(self.powers[0].value)
        # print str(self.powers[1].value)
        for commander in self.current_commander:
            # print self.move_opts[commander][Opts.selection]
            if self.move_opts[commander][Opts.selection.value] == Selections.trigger.value:
                self.overdrive(self.teams[commander])
                self.reset_power(self.teams[commander])
                self.move_opts[commander][Opts.selection.value] = Selections.nothing.value

            # if self.move_opts[commander][Opts.selection.value] == Selections.a_button.value:
            #    self.revive(self.teams[commander])
            #    self.reset_power(self.teams[commander])
            #    self.move_opts[commander][Opts.selection.value] = Selections.nothing.value

            # if self.move_opts[commander][Opts.selection.value] == Selections.triangle.value:
            #    if self.shift(self.teams[commander], commander):
            #       self.reset_power(self.teams[commander])
            #    self.move_opts[commander][Opts.selection.value] = Selections.nothing.value

    def check_everyone_in(self):
        for move_serial in self.move_opts.keys():
            if self.move_opts[move_serial][Opts.holding.value] == Holding.not_holding.value:
                return False
        return True

    def commander_intro_audio(self):
        # print 'BOOOP'
        intro_sound = Audio("audio/Commander/sounds/commander intro.wav")
        intro_sound.start_effect()
        # need while loop here
        play_last_one = True
        commander_select_time = time.time() + 50
        battle_ready_time = time.time() + 40
        while time.time() < commander_select_time:
            self.check_commander_select()
            if self.check_everyone_in():
                break

            if time.time() > battle_ready_time and play_last_one:
                play_last_one = False
                Audio("audio/Commander/sounds/10 seconds begins.wav").start_effect()
        intro_sound.stop_effect()

        if self.current_commander[Team.red.value] == "":
            self.change_random_commander(Team.red.value)
        if self.current_commander[Team.blue.value] == "":
            self.change_random_commander(Team.blue.value)

        Audio("audio/Commander/sounds/commanders chosen.wav").start_effect()
        time.sleep(4)
        self.reset_power(Team.red.value)
        self.reset_power(Team.blue.value)
        self.commander_intro.value = 0

    def game_loop(self):
        self.track_moves()
        self.commander_intro_audio()

        self.count_down()
        time.sleep(0.02)
        try:
            self.audio.start_audio_loop()
        except:
            print("no audio loaded to start")
        time.sleep(0.8)

        while self.running:

            # self.check_music_speed()

            self.update_team_powers()
            self.check_commander_power()
            self.check_end_of_overdrive()
            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()
Пример #34
0
class Swapper():
    def __init__(self, moves, command_queue, ns, music, controller_colors,
                 dead_moves, music_speed, force_move_colors, controller_opts,
                 controller_teams, restart):

        self.command_queue = command_queue
        self.ns = ns
        self.voice = self.ns.settings['menu_voice']
        #save locally in case settings change from web
        self.play_audio = self.ns.settings['play_audio']
        self.sensitivity = self.ns.settings['sensitivity']
        self.color_lock = self.ns.settings['color_lock']
        self.color_lock_choices = self.ns.settings['color_lock_choices']
        self.random_teams = self.ns.settings['random_teams']

        self.move_serials = moves
        self.tracked_moves = {}
        self.dead_moves = dead_moves
        self.teams = controller_teams
        self.music_speed = music_speed
        self.music_speed.value = 1
        self.running = True
        self.force_move_colors = force_move_colors
        self.num_teams = 2
        self.restart = restart

        self.start_timer = time.time()
        self.audio_cue = 0

        self.move_opts = controller_opts

        self.update_time = 0

        self.team_colors = colors.generate_team_colors(self.num_teams,
                                                       self.color_lock,
                                                       self.color_lock_choices)
        self.controller_colors = controller_colors
        self.controller_colors[0] = self.team_colors[0].value[0]
        self.controller_colors[1] = self.team_colors[0].value[1]
        self.controller_colors[2] = self.team_colors[0].value[2]

        self.controller_colors[3] = self.team_colors[1].value[0]
        self.controller_colors[4] = self.team_colors[1].value[1]
        self.controller_colors[5] = self.team_colors[1].value[2]

        self.generate_random_teams(self.num_teams)

        if self.play_audio:

            self.start_beep = Audio('audio/Joust/sounds/start.wav')
            self.start_game = Audio('audio/Joust/sounds/start3.wav')
            self.explosion = Audio('audio/Joust/sounds/Explosion34.wav')
            fast_resample = False
            end = False
            try:
                self.audio = music
            except:
                print('no audio loaded')

        self.change_time = time.time() + 8
        self.speed_up = True
        self.currently_changing = False
        self.game_end = False
        self.winning_moves = []
        self.game_loop()

    def generate_random_teams(self, num_teams):
        if self.random_teams == False:
            players_per_team = (len(self.move_serials) // num_teams) + 1
            team_num = [x for x in range(num_teams)] * players_per_team
            for num, move in zip(team_num, self.move_serials):
                self.teams[move].value = num  # = Value('i',num)
        else:
            team_pick = list(range(num_teams))
            for serial in self.move_serials:
                self.teams[serial].value = random.choice(
                    team_pick)  # = random_choice
                team_pick.remove(self.teams[serial].value)
                if not team_pick:
                    team_pick = list(range(num_teams))

    def track_moves(self):
        for move_num, move_serial in enumerate(self.move_serials):
            self.dead_moves[move_serial].value = 1
            for i in range(3):
                self.force_move_colors[move_serial][i] = 1
            for i in range(5):
                self.move_opts[move_serial][i] = 0

    def change_all_move_colors(self, r, g, b):
        for color in self.force_move_colors.values():
            colors.change_color(color, r, g, b)

    #need to do the count_down here
    def count_down(self):
        self.change_all_move_colors(80, 0, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(70, 100, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 70, 0)
        if self.play_audio:
            self.start_beep.start_effect()
        time.sleep(0.75)
        self.change_all_move_colors(0, 0, 0)
        if self.play_audio:
            self.start_game.start_effect()

    def check_end_game(self):
        self.winning_team = -100
        team_win = True

        for move_serial, dead in self.dead_moves.items():
            if self.winning_team == -100:
                self.winning_team = self.teams[move_serial].value
            if self.teams[move_serial].value != self.winning_team:
                team_win = False
                #TODO: This wont work if the last move is the first of the dead_moves
                self.last_move = move_serial
            if dead.value == 0:
                #This is to play the sound effect
                dead.value = -1
                if self.play_audio:
                    self.explosion.start_effect()
        self.game_end = team_win

    def stop_tracking_moves(self):
        self.restart.value = 1

    def end_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        end_time = time.time() + END_GAME_PAUSE
        h_value = 0
        self.update_status('ending', self.winning_team)
        if self.play_audio:
            self.end_game_sound(self.winning_team)
        while (time.time() < end_time):
            time.sleep(0.01)
            win_color = colors.hsv2rgb(h_value, 1, 1)
            for win_move in self.move_serials:
                if win_move != self.last_move:
                    win_color_array = self.force_move_colors[win_move]
                    colors.change_color(win_color_array, *win_color)
                else:
                    win_color_array = self.force_move_colors[win_move]
                    colors.change_color(win_color_array, 1, 1, 1)
            h_value = (h_value + 0.01)
            if h_value >= 1:
                h_value = 0
        self.running = False

    def end_game_sound(self, winning_team):
        win_team_name = self.team_colors[winning_team].name
        if win_team_name == 'Pink':
            if self.voice == 'aaron':
                os.popen(
                    'espeak -ven -p 70 -a 200 "And the winner is ...Pink Team')
            else:
                team_win = Audio('audio/Joust/vox/' + self.voice +
                                 '/pink team win.wav')
        if win_team_name == 'Magenta':
            team_win = Audio('audio/Joust/vox/' + self.voice +
                             '/magenta team win.wav')
        if win_team_name == 'Orange':
            if self.voice == 'aaron':
                os.popen(
                    'espeak -ven -p 70 -a 200 "And the winner is ...Orange Team'
                )
            else:
                team_win = Audio('audio/Joust/vox/' + self.voice +
                                 '/orange team win.wav')
        if win_team_name == 'Yellow':
            team_win = Audio('audio/Joust/vox/' + self.voice +
                             '/yellow team win.wav')
        if win_team_name == 'Green':
            team_win = Audio('audio/Joust/vox/' + self.voice +
                             '/green team win.wav')
        if win_team_name == 'Turquoise':
            team_win = Audio('audio/Joust/vox/' + self.voice +
                             '/cyan team win.wav')
        if win_team_name == 'Blue':
            team_win = Audio('audio/Joust/vox/' + self.voice +
                             '/blue team win.wav')
        if win_team_name == 'Purple':
            if self.voice == 'aaron':
                os.popen(
                    'espeak -ven -p 70 -a 200 "And the winner is ...Purple Team'
                )
            else:
                team_win = Audio('audio/Joust/vox/' + self.voice +
                                 '/purple team win.wav')
        team_win.start_effect()

    def game_loop(self):
        self.track_moves()
        self.restart.value = 0
        self.count_down()
        if self.play_audio:
            try:
                self.audio.start_audio_loop()
            except:
                print('no audio loaded to start')
        while self.running:
            #I think the loop is so fast that this causes
            #a crash if done every loop
            if time.time() - 0.1 > self.update_time:
                self.update_time = time.time()
                self.check_command_queue()
                self.update_status('in_game')

            self.check_end_game()
            if self.game_end:
                self.end_game()

        self.stop_tracking_moves()

    def check_command_queue(self):
        package = None
        while not (self.command_queue.empty()):
            package = self.command_queue.get()
            command = package['command']
        if not (package == None):
            if command == 'killgame':
                self.kill_game()

    def kill_game(self):
        if self.play_audio:
            try:
                self.audio.stop_audio()
            except:
                print('no audio loaded to stop')
        self.update_status('killed')
        all_moves = [x for x in self.dead_moves.keys()]
        end_time = time.time() + KILL_GAME_PAUSE

        bright = 255
        while (time.time() < end_time):
            time.sleep(0.01)
            color = (bright, 0, 0)
            for move in all_moves:
                color_array = self.force_move_colors[move]
                colors.change_color(color_array, *color)
            bright = bright - 1
            if bright < 10:
                bright = 10
        self.running = False

    def update_status(self, game_status, winning_team=-1):
        data = {
            'game_status': game_status,
            'game_mode': 'Swapper',
            'winning_team': winning_team
        }
        team_total = [0, 0]
        team_alive = [0, 0]
        for move in self.move_serials:
            team = self.teams[move].value
            team_total[team] += 1
            if self.dead_moves[move].value == 1:
                team_alive[team] += 1
        team_comp = list(zip(team_total, team_alive))
        data['team_comp'] = team_comp
        data['team_names'] = [
            color.name + ' Team' for color in self.team_colors
        ]

        self.ns.status = data