-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand.py
152 lines (138 loc) · 5.69 KB
/
hand.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pygame as pg
from math import hypot, sqrt
from vars import *
class Hand(pg.sprite.Sprite):
def __init__(self, _position, _zone, _velocity, _size, _head, _is_left):
pg.sprite.Sprite.__init__(self)
self.zone = _zone
self.velocity = _velocity
self.rect = pg.Rect(*_position, _size, _size)
self.radius = HAND_RADIUS
self.is_left = _is_left
if self.is_left:
self.path_open = "./imgs/left_hand_open.png"
self.path_closed = "./imgs/left_hand_closed.png"
self.side_rect = pg.Rect(10, 10, SCREEN_WIDTH // 2 - 10, SCREEN_HEIGHT - 10)
else:
self.path_open = "./imgs/right_hand_open.png"
self.path_closed = "./imgs/right_hand_closed.png"
self.side_rect = pg.Rect(SCREEN_WIDTH // 2 + 10, 10, SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10)
self.image_open = pg.transform.scale(pg.image.load(self.path_open), (_size, _size))
self.image_closed = pg.transform.scale(pg.image.load(self.path_closed), (_size, _size))
self.image = self.image_open
self.head = _head
self.mode = HUNTING
self.target = None
self.def_pos = self.default_position()
self.rect.center = self.def_pos
def default_position(self):
if self.is_left:
def_x = self.head.head_rect.centerx - SCREEN_WIDTH//8
else:
def_x = self.head.head_rect.centerx + SCREEN_WIDTH//8
def_y = self.head.rect.centery + 10
return pg.Vector2(def_x, def_y)
def min_couple(self, couple_list):
min_dist = 0
min_food = None
for couple in couple_list:
if min_dist == 0 or couple[0] < min_dist:
min_dist = couple[0]
min_food = couple[1]
return (min_dist, min_food)
def choose_target(self):
"""
Finds closest healthy food and sets it as target
"""
healthy_couple_list = [ (pg.Vector2(food.rect.center).distance_squared_to(self.head.head_rect.center), food) for food in FOOD_LIST if food.is_healthy]
sided_couple_list = [(dist, food) for dist, food in healthy_couple_list if self.side_rect.colliderect(food.rect)]
if sided_couple_list:
self.target = self.min_couple(sided_couple_list)[1]
self.mode = FOLLOWING
else:
self.target = None
# def move_by(self, dx, dy):
# new_x = self.rect.centerx + dx
# new_y = self.rect.centery + dy
# new_dx = dx
# new_dy = dy
# # Correct if wants to go too far
# if new_x < self.left_boundary:
# new_dx = self.left_boundary - self.rect.centerx
# if new_x > self.right_boundary:
# new_dx = self.right_boundary - self.rect.centerx
# if new_y > self.bottom_boundary:
# new_dy = self.bottom_boundary - self.rect.centery
# if new_y < self.top_boundary:
# new_dy = self.top_boundary - self.rect.centery
# # Apply movement
# self.rect.move_ip(new_dx, new_dy)
# def move_towards(self, sprite):
# """
# Move towards sprite using self.velocity
# """
# dx = sprite.rect.centerx - self.rect.centerx
# dy = sprite.rect.centery - self.rect.centery
# dist = hypot(dx, dy)
# if dist == 0:
# coeff = 0
# else:
# coeff = self.velocity / dist
# dx *= coeff
# dy *= coeff
# self.move_by(dx, dy)
def update(self):
if self.mode == HUNTING:
"""
This mode is when Hand has no target
But is looking for one
If none is in its hunting zone, it will move to its default position
"""
self.image = self.image_open
self.choose_target()
if self.target == None:
direction = self.def_pos - pg.Vector2(self.rect.center)
if self.velocity < direction.length(): # to avoid flickering
self.rect.move_ip(direction.normalize() * self.velocity)
else:
self.mode = FOLLOWING
elif self.mode == FOLLOWING:
"""
This mode is when Hand has a Sprite of type Aliment
as target.
It keeps following it until:
* Hand catches target
* Target gets too far
"""
self.image = self.image_open
if self.target.alive() \
and self.side_rect.colliderect(self.target.rect):
if self.rect.colliderect(self.target.rect):
self.target.caught = True
self.target.master = self
self.mode = EATING
else:
direction = pg.Vector2(self.target.rect.center) - pg.Vector2(self.rect.center)
if (direction.length() > 0):
self.rect.move_ip(direction.normalize() * self.velocity)
else:
self.mode = HUNTING
self.target = None
elif self.mode == EATING:
"""
This if for when the target is caught
The target is set to follow Hand
and Hand moves to Head
When Hand and Head collide, the food can be eaten
and Hand goes back to hunting
"""
self.image = self.image_closed
direction = self.head.head_pos - pg.Vector2(self.rect.center)
if direction.length() > 0:
self.rect.move_ip(direction.normalize() * self.velocity)
else:
"""
This should never happen
"""
print("Error: what the heck is this mode ?")
self.mode = HUNTING