-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
357 lines (302 loc) · 12.8 KB
/
player.py
File metadata and controls
357 lines (302 loc) · 12.8 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# player.py
import random
import pygame
import math
from shapes import Circle
EXPERIMENTAL_SLIDING = True
class Player(Circle):
"""
Represents the player and its actions.
Inherits from `Circle` class.
"""
def __init__(self, x, y, z, radius=18):
"""
Initialize player with position and attributes.
Args:
x (float): Player's x-coordinate.
y (float): Player's y-coordinate.
z (float): Player's z-coordinate.
radius (int, optional): Player's radius. Defaults to 18.
"""
super().__init__(x, y, z, radius)
# Movement attributes
self.z_speed = 1 # If you intend to keep vertical movement without gravity
self.velocity = pygame.math.Vector3(0, 0, 0)
self.acceleration = 0.5
self.friction = 0.1
self.max_speed = 5
# Dash attributes
self.is_dashing = False
self.dash_speed = 15
self.dash_duration = 0.2 # in seconds
self.dash_cooldown = 1.0 # in seconds
self.last_dash_time = -self.dash_cooldown
# Teleport attributes
self.is_teleporting = False
self.teleport_cooldown = 5.0 # in seconds
self.last_teleport_time = -self.teleport_cooldown
self.teleport_end_time = 0 # Initialize teleport end time
# Temporary effect timers
self.speed_boost_active = False
self.speed_boost_end_time = 0
# Load and scale the player images
try:
# Default sprite
self.original_surf = pygame.image.load("graphics/player/linty.png").convert_alpha()
self.original_surf = pygame.transform.scale(self.original_surf, (self.radius * 2, self.radius * 2))
# Dash sprite
self.dash_surf = pygame.image.load("graphics/player/lintydash.png").convert_alpha()
self.dash_surf = pygame.transform.scale(self.dash_surf, (self.radius * 2, self.radius * 2))
# Teleport sprite
self.teleport_surf = pygame.image.load("graphics/player/lintyteleport.png").convert_alpha()
self.teleport_surf = pygame.transform.scale(self.teleport_surf, (self.radius * 2, self.radius * 2))
# Set the current sprite to the default
self.current_surf = self.original_surf
except pygame.error as e:
from main import DEBUG_MODE
if DEBUG_MODE:
print(f"Failed to load player images: {e}")
self.current_surf = None # Fallback if image loading fails
def handle_movement(self, maze):
"""
Manage movement and actions.
Args:
maze (Maze): Maze object for collision checks.
"""
keys = pygame.key.get_pressed()
current_time = pygame.time.get_ticks() / 1000 # Current time in seconds
# Reset acceleration
accel = pygame.math.Vector3(0, 0, 0)
# Movement input
if keys[pygame.K_UP]:
accel.y -= self.acceleration
if keys[pygame.K_DOWN]:
accel.y += self.acceleration
if keys[pygame.K_LEFT]:
accel.x -= self.acceleration
if keys[pygame.K_RIGHT]:
accel.x += self.acceleration
# Apply acceleration
self.velocity += accel
# Apply friction
if not accel.x:
self.velocity.x *= (1 - self.friction)
if not accel.y:
self.velocity.y *= (1 - self.friction)
if not accel.z:
self.velocity.z *= (1 - self.friction)
# Clamp velocity to max speed
self.velocity.x = max(-self.max_speed, min(self.velocity.x, self.max_speed))
self.velocity.y = max(-self.max_speed, min(self.velocity.y, self.max_speed))
# Dash input
if keys[pygame.K_SPACE]:
if not self.is_dashing and (current_time - self.last_dash_time) >= self.dash_cooldown:
self.is_dashing = True
self.dash_start_time = current_time
self.last_dash_time = current_time
# Increase velocity for dash
dash_vector = pygame.math.Vector3(self.velocity.x, self.velocity.y, self.velocity.z)
if dash_vector.length() != 0:
dash_vector = dash_vector.normalize() * self.dash_speed
self.velocity += dash_vector
from main import DEBUG_MODE
if DEBUG_MODE:
print("Dash activated!")
# Handle dash duration
if self.is_dashing:
if (current_time - self.dash_start_time) >= self.dash_duration:
self.is_dashing = False
# Reset velocity after dash
if self.velocity.length() > 0:
self.velocity = self.velocity.normalize() * self.max_speed
from main import DEBUG_MODE
if DEBUG_MODE:
print("Dash ended.")
# Removed gravity application
# self.velocity.z += self.gravity # Removed
old_location = self.get_location()
if not EXPERIMENTAL_SLIDING:
# Simple collision handling without sliding
# Attempt to move along the X-axis
self.x += self.velocity.x
if not maze.is_move_allowed(self):
self.x = old_location[0]
# Attempt to move along the Y-axis
self.y += self.velocity.y
if not maze.is_move_allowed(self):
self.y = old_location[1]
else:
# Experimental sliding collision handling
self.x += self.velocity.x
self.y += self.velocity.y
if not maze.is_move_allowed(self):
# If movement is blocked, revert to old location
self.x, self.y = old_location[:2]
max_angle = 60 # Maximum angle to attempt sliding
# Attempt to slide by rotating the velocity vector incrementally
for angle in range(1, max_angle + 1):
rad = angle / 180 * math.pi # Convert angle to radians
# Rotate velocity vector clockwise by 'angle' degrees
self.x += math.cos(rad) * self.velocity.x - math.sin(rad) * self.velocity.y
self.y += math.sin(rad) * self.velocity.x + math.cos(rad) * self.velocity.y
if not maze.is_move_allowed(self):
# If still blocked, revert to old position
self.x, self.y = old_location[:2]
else:
# Successful slide exit loop
break
# Rotate velocity vector counter-clockwise by 'angle' degrees
rad = -rad
self.x += math.cos(rad) * self.velocity.x - math.sin(rad) * self.velocity.y
self.y += math.sin(rad) * self.velocity.x + math.cos(rad) * self.velocity.y
if not maze.is_move_allowed(self):
# If still blocked, revert to old position
self.x, self.y = old_location[:2]
else:
# Successful slide exit loop
break
# Attempt to move along the Z-axis (if vertical movement is desired)
if keys[pygame.K_w]:
self.z += self.z_speed
if keys[pygame.K_s]:
self.z -= self.z_speed
if not maze.is_move_allowed(self):
self.z = old_location[2]
self.velocity.z = 0
# Handle temporary effects
self.handle_timers()
# print("Player Position:", self.x, self.y, self.z)
def display_player(self):
"""
Render the player sprite on screen.
"""
from main import screen # Importing here to avoid circular imports
if self.current_surf is None:
from main import DEBUG_MODE
if DEBUG_MODE:
print("Player image not loaded. Cannot display player.")
return
# Blit the current sprite onto the screen at the player's position
# Adjust position to center the image
screen.blit(self.current_surf, (int(self.x - self.radius), int(self.y - self.radius)))
def set_position(self, x, y, z):
"""
Update player's position.
Args:
x (float): New x-coordinate.
y (float): New y-coordinate.
z (float): New z-coordinate.
"""
self.x = x
self.y = y
self.z = z
def get_location(self):
"""
Get current player location.
Returns:
tuple: (x, y, z) coordinates.
"""
return self.x, self.y, self.z
def get_parameters(self):
"""
Get collision detection parameters.
Returns:
tuple: (x, y, z, radius).
"""
return self.x, self.y, self.z, self.radius
def teleport(self, maze):
"""
Teleport player to a random position.
Args:
maze (Maze): Maze object for valid position checks.
"""
from main import WIDTH, HEIGHT
has_found = False
attempts = 0
max_attempts = 100 # Prevent infinite loop
while not has_found and attempts < max_attempts:
temp_x = random.randint(self.radius, WIDTH - self.radius)
temp_y = random.randint(self.radius, HEIGHT - self.radius)
temp_z = self.z # Teleport to the same z_level
if maze.is_move_allowed(Player(temp_x, temp_y, temp_z)):
has_found = True
self.set_position(temp_x, temp_y, temp_z)
from main import DEBUG_MODE
if DEBUG_MODE:
print(f"Player teleported to ({temp_x}, {temp_y}, {temp_z})")
attempts += 1
if has_found:
# Switch to Teleport sprite
self.current_surf = self.teleport_surf
# Set a timer to revert to default sprite after a short duration
self.teleport_end_time = pygame.time.get_ticks() / 1000 + 0.5 # 0.5 seconds duration
self.is_teleporting = True
else:
from main import DEBUG_MODE
if DEBUG_MODE:
print("Teleport failed: No free position found.")
def handle_timers(self):
"""
Manage timers for effects.
"""
current_time = pygame.time.get_ticks() / 1000
# Handle speed boost timer
if self.speed_boost_active and current_time >= self.speed_boost_end_time:
self.max_speed -= 2 # Revert max_speed
self.speed_boost_active = False
# Only revert sprite if not teleporting
if not self.is_teleporting:
self.current_surf = self.original_surf
from main import DEBUG_MODE
if DEBUG_MODE:
print("Speed boost ended.")
# Handle teleport timer
if self.is_teleporting and current_time >= self.teleport_end_time:
self.is_teleporting = False
# Only revert sprite if speed boost is not active
if not self.speed_boost_active:
self.current_surf = self.original_surf
from main import DEBUG_MODE
if DEBUG_MODE:
print("Teleport effect ended.")
# Ensure sprite reflects current state priority
if self.is_teleporting:
self.current_surf = self.teleport_surf
elif self.speed_boost_active:
self.current_surf = self.dash_surf
else:
self.current_surf = self.original_surf
def apply_speed_boost(self, duration=5.0):
"""
Apply a speed boost temporarily.
Args:
duration (float, optional): Boost duration in seconds. Defaults to 5.0.
"""
if not self.speed_boost_active:
self.max_speed += 2 # Increase max_speed
self.speed_boost_active = True
self.speed_boost_end_time = pygame.time.get_ticks() / 1000 + duration
self.current_surf = self.dash_surf # Switch to Dash sprite
from main import DEBUG_MODE
if DEBUG_MODE:
print("Speed boost activated!")
else:
from main import DEBUG_MODE
if DEBUG_MODE:
print("Speed boost is already active. Cannot stack boosts.")
def reduce_dash_cooldown(self):
"""
Decrease dash cooldown period.
"""
self.dash_cooldown = max(0.5, self.dash_cooldown - 0.1)
from main import DEBUG_MODE
if DEBUG_MODE:
print(f"Dash cooldown reduced to {self.dash_cooldown} seconds.")
def reduce_teleport_cooldown(self):
"""
Decrease teleport cooldown period.
"""
self.teleport_cooldown = max(2.0, self.teleport_cooldown - 0.5)
from main import DEBUG_MODE
if DEBUG_MODE:
print(f"Teleport cooldown reduced to {self.teleport_cooldown} seconds.")