-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
224 lines (188 loc) · 8.75 KB
/
Player.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from utils.constants import BOTTOM_VIEWPORT_MARGIN, LEFT_VIEWPORT_MARGIN, RIGHT_VIEWPORT_MARGIN, TOP_VIEWPORT_MARGIN
from utils.ItemContainer import *
class Player(ac.Sprite):
def __init__(self, window: ac.Window, level, user_id):
super(Player, self).__init__(filename="assets/player_0.png", scale=0.05)
self.window = window
self.level = level
self.user_id = user_id
self.physics_engine = None
self.ui_manager = None
self.score = 0
self.name = self.user_id # to change later
self.health_bar = HealthBar(self, max_health=100, health=0)
self.items = ac.SpriteList()
self.speed = 4
self.jump_force = 18
self.can_move = True
self.can_attack = True
self.items = ac.SpriteList()
self.items_containers = []
self.platforms = []
self.last_attacker = None
self.last_attackers = {}
def reset(self):
self.health_bar = HealthBar(self, max_health=100, health=100)
self.speed = 4
self.jump_force = 18
self.can_move = True
self.can_attack = True
self.last_attacker = None
self.last_attackers = {}
self.items = ac.SpriteList()
def setup(self):
# load textures
self.textures.append(ac.load_texture("assets/player_0.png", flipped_horizontally=True))
self.set_position(400, 300)
if self.user_id == self.window.user_id:
self.items_containers.append(ShurikenContainer(self.window))
self.items_containers.append(FireWorkContainer(self.window))
self.items_containers.append(PlatformContainer(self.window))
def update(self):
self.items.update()
if self.can_move:
self.physics_engine.update()
if self is self.level.player:
for container in self.items_containers:
container.update()
if len(self.last_attackers) > 0:
self.last_attacker = max(self.last_attackers.keys(), key=lambda x: self.last_attackers[x])
self.last_attackers = {}
# viewport scroll
changed = False
left_boundary = self.window.x_offset + LEFT_VIEWPORT_MARGIN
if self.left < left_boundary:
self.window.x_offset -= left_boundary - self.left
changed = True
right_boundary = self.window.x_offset + self.window.width - RIGHT_VIEWPORT_MARGIN
if self.right > right_boundary:
self.window.x_offset += self.right - right_boundary
changed = True
top_boundary = self.window.y_offset + self.window.height - TOP_VIEWPORT_MARGIN
if self.top > top_boundary:
self.window.y_offset += self.top - top_boundary
changed = True
bottom_boundary = self.window.y_offset + BOTTOM_VIEWPORT_MARGIN
if self.bottom < bottom_boundary:
self.window.y_offset -= bottom_boundary - self.bottom
changed = True
if changed:
self.window.y_offset = int(self.window.y_offset)
self.window.x_offset = int(self.window.x_offset)
self.window.set_viewport(self.window.x_offset,
self.window.width + self.window.x_offset,
self.window.y_offset,
self.window.height + self.window.y_offset)
def draw_all(self):
self.items.draw()
for index, item in enumerate(self.items_containers):
item.draw(index)
self.health_bar.draw()
self.draw()
def key_press(self, key):
if key == ac.key.Z and self.physics_engine.can_jump():
self.physics_engine.jump(self.jump_force)
elif key == ac.key.Q:
self.change_x -= max(self.speed, self.change_x - self.speed)
elif key == ac.key.D:
self.change_x = min(self.speed, self.change_x + self.speed)
self.update_texture()
def key_release(self, key):
if key == ac.key.Q:
self.change_x += self.speed
elif key == ac.key.D:
self.change_x -= self.speed
self.update_texture()
def mouse_press(self, x, y, button):
if button == ac.MOUSE_BUTTON_LEFT and self.can_attack:
item = self.items_containers[1].launch(self, (x, y))
if item is not None:
self.items.append(item)
elif button == ac.MOUSE_BUTTON_RIGHT and self.can_attack:
item = self.items_containers[0].launch(self, (x, y))
if item is not None:
self.items.append(item)
elif button == ac.MOUSE_BUTTON_MIDDLE and self.can_attack:
item = self.items_containers[2].launch(self, (x, y))
if item is not None:
if not item.collides_with_list(self.level.grounds_list):
self.level.grounds_list.append(item)
self.items.append(item)
else:
self.items_containers[2].reset_countdown()
def update_texture(self):
if self.change_x < 0:
self.set_texture(1)
self.cur_texture_index = 1
elif self.change_x > 0:
self.set_texture(0)
self.cur_texture_index = 0
def from_dict(self, data: dict):
"""
Update the player attributes with new ones, network function
:param data:dict
"""
keys = data.keys()
if "health" in keys:
self.health_bar.health = data["health"]
if "position" in keys and False:
self.position = data["position"]
if "texture" in keys and self.cur_texture_index != data["texture"]:
self.cur_texture_index = data["texture"]
self.set_texture(self.cur_texture_index)
if "score" in keys:
self.score = data["score"]
if "items" in keys:
for p_type, p_pos in data["items"]:
if p_type == "shuriken":
item = ShurikenContainer.force_launch(self, p_pos)
elif p_type == "firework":
item = FireWorkContainer.force_launch(self, p_pos)
elif p_type == "platform":
item = PlatformContainer.force_launch(self, p_pos)
self.level.grounds_list.append(item)
self.items.append(item)
if "change" in keys:
pass
#self.change_x, self.change_y = data["change"]
def objectify(self) -> dict:
"""
Concat the player data inside a dict, network function
:return:dict
"""
return {"msg": "game_data",
"user_id": self.window.user_id,
"position": self.position,
"health": self.health_bar.health,
"texture": self.cur_texture_index,
"last_attacker": self.last_attacker,
"change": (self.change_x, self.change_y),
"items": [(item.id, item.type, item.goal_pos) for item in self.items]}
class HealthBar:
def __init__(self, player: Player, max_health: int, health: int):
self.player = player
self.max_health = max_health
self.health = health
self.health = 100
self.last_health = None
self.health_bar_complete = ac.load_texture("assets/health_bar_complete.png")
self.health_bar_black = ac.load_texture("assets/health_bar_black.png")
self.health_bar_red = None
def draw(self):
if self.player is self.player.level.player:
x, y = self.player.window.x_offset, self.player.window.y_offset
self.health_bar_complete.draw_scaled(center_x=x + self.health_bar_complete.width / 2,
center_y=y + self.health_bar_complete.height / 2, scale=1)
if self.last_health != self.health:
new_width = self.health * 291 / self.max_health
self.health_bar_red = ac.load_texture("assets/health_bar_red.png", x=0, y=0, height=27, width=new_width)
self.last_health = self.health
self.health_bar_red.draw_transformed(left=x + 75, bottom=y + 59,
width=self.health_bar_red.width, height=self.health_bar_red.height)
else:
width, height = self.player.width, self.player.height // 5
x, y = self.player.center_x, self.player.top + height * 2
ac.draw_rectangle_filled(center_x=x, center_y=y, width=width, height=height, color=(22, 0, 0))
n_width = self.health * width / 100
ac.draw_rectangle_filled(center_x=x - width / 2 + n_width / 2, center_y=y,
width=n_width, height=height, color=(203, 0, 2))