5
from emotion import EmotionSystem, EmotionState
8
def __init__(self, screen_width, screen_height):
9
self.screen_width = screen_width
10
self.screen_height = screen_height
11
self.emotion_system = EmotionSystem()
17
self.animation_timer = 0
18
self.current_frame = 0
19
self.facing_right = True
23
self.current_action = "idle"
25
self.load_animations()
27
def reset_position(self):
29
self.x = random.randint(100, self.screen_width - 100)
30
self.y = random.randint(100, self.screen_height - 100)
31
self.target_x = self.x
32
self.target_y = self.y
36
def load_animations(self):
37
"""加载动画资源 - 这里需要替换为实际图片加载"""
40
emotions = [EmotionState.HAPPY, EmotionState.SAD, EmotionState.NORMAL, EmotionState.ANGRY]
41
colors = [(255, 255, 0), (0, 0, 255), (128, 128, 128), (255, 0, 0)]
43
for emotion, color in zip(emotions, colors):
46
surf = pygame.Surface((50, 50), pygame.SRCALPHA)
47
pygame.draw.circle(surf, color, (25, 25), 20)
50
pygame.draw.circle(surf, (255, 255, 255), (15, 15), 5)
51
pygame.draw.circle(surf, (255, 255, 255), (35, 15), 5)
53
pygame.draw.circle(surf, (255, 255, 255), (15, 20), 5)
54
pygame.draw.circle(surf, (255, 255, 255), (35, 20), 5)
56
self.animations[emotion] = frames
58
def update_behavior(self):
60
emotion = self.emotion_system.get_current_emotion()
61
energy = self.emotion_system.energy
64
self.update_movement_parameters(emotion, energy)
67
self.decide_action(emotion, energy)
73
self.update_animation(emotion)
75
def update_movement_parameters(self, emotion, energy):
78
EmotionState.HAPPY: 4,
79
EmotionState.NORMAL: 2,
84
self.speed = base_speeds.get(emotion, 2) * (energy / 100)
85
self.speed = max(0.5, min(5, self.speed))
87
def decide_action(self, emotion, energy):
92
if energy < 20 and random.random() < 0.8:
93
self.current_action = "rest"
97
action_probabilities = {
98
EmotionState.HAPPY: {"move": 0.7, "idle": 0.2, "special": 0.1},
99
EmotionState.NORMAL: {"move": 0.4, "idle": 0.5, "special": 0.1},
100
EmotionState.SAD: {"move": 0.2, "idle": 0.7, "special": 0.1},
101
EmotionState.ANGRY: {"move": 0.6, "idle": 0.3, "special": 0.1}
104
probs = action_probabilities.get(emotion, {"move": 0.4, "idle": 0.5, "special": 0.1})
106
if self.move_timer > 180:
107
rand = random.random()
108
if rand < probs["move"]:
109
self.set_new_target()
110
self.current_action = "move"
111
elif rand < probs["move"] + probs["idle"]:
112
self.current_action = "idle"
114
self.current_action = "special"
117
def set_new_target(self):
119
self.target_x = random.randint(50, self.screen_width - 50)
120
self.target_y = random.randint(50, self.screen_height - 50)
123
if self.target_x > self.x:
124
self.facing_right = True
126
self.facing_right = False
128
def execute_action(self):
130
if self.current_action == "move":
131
self.move_to_target()
132
elif self.current_action == "rest":
134
self.emotion_system.energy = min(100, self.emotion_system.energy + 0.1)
135
elif self.current_action == "special":
136
self.do_special_action()
138
def move_to_target(self):
140
dx = self.target_x - self.x
141
dy = self.target_y - self.y
142
distance = math.sqrt(dx*dx + dy*dy)
149
self.x += dx * self.speed
150
self.y += dy * self.speed
153
self.current_action = "idle"
155
def do_special_action(self):
160
def update_animation(self, emotion):
162
self.animation_timer += 1
163
if self.animation_timer >= 10:
164
self.current_frame = (self.current_frame + 1) % len(self.animations[emotion])
165
self.animation_timer = 0
167
def draw(self, screen):
169
emotion = self.emotion_system.get_current_emotion()
170
current_sprite = self.animations[emotion][self.current_frame]
173
if not self.facing_right:
174
current_sprite = pygame.transform.flip(current_sprite, True, False)
176
screen.blit(current_sprite, (self.x - 25, self.y - 25))
179
self.draw_mood_bubble(screen)
181
def draw_mood_bubble(self, screen):
183
mood_value = self.emotion_system.mood_value
187
color = (144, 238, 144)
189
elif mood_value > 30:
190
color = (255, 255, 224)
193
color = (255, 182, 193)
196
color = (255, 160, 122)
200
bubble_rect = pygame.Rect(self.x - 20, self.y - 60, 40, 20)
201
pygame.draw.rect(screen, color, bubble_rect, border_radius=10)
202
pygame.draw.rect(screen, (0, 0, 0), bubble_rect, 2, border_radius=10)
205
font = pygame.font.Font(None, 20)
206
text_surface = font.render(text, True, (0, 0, 0))
207
text_rect = text_surface.get_rect(center=bubble_rect.center)
208
screen.blit(text_surface, text_rect)