14
def __init__(self, screen_width, screen_height):
15
self.screen_width = screen_width
16
self.screen_height = screen_height
17
self.state = PetState.IDLE
20
self.x = screen_width // 2
21
self.y = screen_height // 2
22
self.target_x = self.x
23
self.target_y = self.y
28
self.animation_frames = {}
29
self.current_frame = 0
30
self.animation_speed = 0.2
31
self.frame_counter = 0
35
self.state_duration = random.randint(300, 600)
40
def load_resources(self):
44
self.animation_frames[PetState.IDLE] = [
45
pygame.image.load("assets/images/remi_idle_1.png").convert_alpha(),
46
pygame.image.load("assets/images/remi_idle_2.png").convert_alpha()
50
self.animation_frames[PetState.WALKING] = [
51
pygame.image.load("assets/images/remi_walk_1.png").convert_alpha(),
52
pygame.image.load("assets/images/remi_walk_2.png").convert_alpha()
56
for state in self.animation_frames:
57
for i in range(len(self.animation_frames[state])):
58
original_img = self.animation_frames[state][i]
59
scaled_img = pygame.transform.scale(original_img, (120, 120))
60
self.animation_frames[state][i] = scaled_img
62
except pygame.error as e:
65
self.create_placeholder_images()
67
def create_placeholder_images(self):
69
self.animation_frames[PetState.IDLE] = []
70
self.animation_frames[PetState.WALKING] = []
74
surf_idle = pygame.Surface((120, 120), pygame.SRCALPHA)
75
pygame.draw.circle(surf_idle, (255, 200, 200), (60, 60), 50)
76
self.animation_frames[PetState.IDLE].append(surf_idle)
78
surf_walk = pygame.Surface((120, 120), pygame.SRCALPHA)
79
pygame.draw.rect(surf_walk, (200, 255, 200), (10, 10, 100, 100))
80
self.animation_frames[PetState.WALKING].append(surf_walk)
84
self.frame_counter += 1
87
if self.frame_counter >= 1 / self.animation_speed:
88
self.current_frame = (self.current_frame + 1) % len(self.animation_frames[self.state])
89
self.frame_counter = 0
93
if self.state_timer >= self.state_duration:
97
self.update_movement()
99
def change_state(self):
101
if self.state == PetState.IDLE:
102
if random.random() < 0.3:
103
self.state = PetState.WALKING
104
self.set_random_target()
105
elif self.state == PetState.WALKING:
106
self.state = PetState.IDLE
109
self.state_duration = random.randint(300, 600)
110
self.current_frame = 0
112
def set_random_target(self):
115
self.target_x = random.randint(margin, self.screen_width - margin)
116
self.target_y = random.randint(margin, self.screen_height - margin)
119
if self.target_x > self.x:
124
def update_movement(self):
126
if self.state == PetState.WALKING:
128
dx = self.target_x - self.x
129
dy = self.target_y - self.y
130
distance = (dx**2 + dy**2)**0.5
133
self.state = PetState.IDLE
138
self.x += (dx / distance) * self.speed
139
self.y += (dy / distance) * self.speed
142
self.x = max(0, min(self.screen_width - 120, self.x))
143
self.y = max(0, min(self.screen_height - 120, self.y))
145
def handle_event(self, event):
147
if event.type == pygame.MOUSEBUTTONDOWN:
148
mouse_x, mouse_y = pygame.mouse.get_pos()
149
pet_rect = pygame.Rect(self.x, self.y, 120, 120)
151
if pet_rect.collidepoint(mouse_x, mouse_y):
152
self.state = PetState.INTERACTING
154
self.state_duration = 120
156
def draw(self, screen):
158
if self.state in self.animation_frames:
159
current_image = self.animation_frames[self.state][self.current_frame]
162
if self.direction == -1:
163
current_image = pygame.transform.flip(current_image, True, False)
165
screen.blit(current_image, (self.x, self.y))