File size: 2,280 Bytes
cf5e7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Modifying the code to ensure the mouth is open when the character starts talking

import random
import time


class CharacterFace:
    def __init__(self):
        self.mouth_open = False
        self.last_change_time = 0
        self.next_change_in = 0

    def update(self, is_talking, start_talking=False):
        current_time = time.time()

        # Open the mouth when the character starts talking
        if start_talking:
            self.mouth_open = True
            self.next_change_in = current_time + random.uniform(0.1, 0.5)
            return self.mouth_open

        # Initialize the next change time if it's zero.
        if self.next_change_in == 0:
            self.next_change_in = current_time + random.uniform(0.1, 0.5)

        # Update the mouth state only if the character is talking.
        if is_talking:
            # Check if it's time to change the mouth state.
            if current_time >= self.next_change_in:
                self.mouth_open = not self.mouth_open
                self.next_change_in = current_time + random.uniform(0.1, 0.5)
        else:
            # Close the mouth if the character is not talking.
            self.mouth_open = False

        return self.mouth_open


def _debug_test():
    # Example usage
    face = CharacterFace()
    output = []

    # Initialize variables to control talk and pause durations
    next_talk_time = 0
    next_pause_time = 0
    is_talking = False

    # Simulate the character talking and not talking with variable durations
    for _ in range(500):  # Increase the number of iterations for a longer simulation
        current_time = time.time()
        start_talking = False
        
        if is_talking and current_time >= next_talk_time:
            is_talking = False
            next_pause_time = current_time + random.uniform(0.5, 3.0)
            
        if not is_talking and current_time >= next_pause_time:
            is_talking = True
            start_talking = True  # Set flag to open mouth at the start of talking
            next_talk_time = current_time + random.uniform(1.0, 5.0)
        
        mouth_open = face.update(is_talking, start_talking)
        print(f"Is Talking: {is_talking}, Mouth Open: {mouth_open}")
        time.sleep(random.uniform(0.1, 0.5))