コード例 #1
0
ファイル: commander.py プロジェクト: aangert/PiParty
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()
コード例 #2
0
ファイル: commander.py プロジェクト: shihyu/JoustMania
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()
コード例 #3
0
ファイル: joust.py プロジェクト: shihyu/JoustMania
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()
                    
                
                
        
        

            
        

            
コード例 #4
0
ファイル: joust.py プロジェクト: aangert/PiParty
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()
コード例 #5
0
ファイル: bubble.py プロジェクト: aangert/PiParty
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()
コード例 #6
0
ファイル: bubble.py プロジェクト: shihyu/JoustMania
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()
コード例 #7
0
ファイル: speed_bomb.py プロジェクト: MrAbodi/JoustMania
class Bomb():
    def __init__(self, moves):

        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 = ''

        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 = Audio(music, end)
        except:
            print('no audio loaded')

        self.game_end = False

        self.game_loop()

    def reset_bomb_length(self):
        self.bomb_length = 5.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.print_bombs()
        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)
        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:
            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()

                self.start_beep.start_effect()
                self.holding = True
            if time.time() > self.bomb_time:
                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
                    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)
                        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:
                        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()
        #print("returing prev faker as " + self.get_prev_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:
                            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
            #self.move_opts[move_serial_beg][Opts.holding.value] = Holding.not_holding.value

        #print("rotate colors print bombs")
        self.print_bombs()

    def print_bombs(self):
        for i, move_serial in enumerate(self.move_serials):
            pass
            #print("controller {}, serial {}, is dead {}, is holding {}, has bomb {}".format(i, move_serial, self.dead_moves[move_serial].value, self.move_opts[move_serial][Opts.holding.value], self.move_opts[move_serial][Opts.has_bomb.value]))
        #print("\n")

    #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 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)
                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):
        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)
            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 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()
コード例 #8
0
ファイル: kingofthehill.py プロジェクト: MrAbodi/JoustMania
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()
コード例 #9
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()