From f318ce54a58b5be97d061053c5c7e62c478f8a6b Mon Sep 17 00:00:00 2001 From: Hennzau <dev@enzo-le-van.fr> Date: Sun, 19 Nov 2023 15:09:38 +0100 Subject: [PATCH 1/2] Correction of player bounces --- level/level.py | 44 +++++++++++++++++++++++++++++++++----------- level/player.py | 16 ++++++++++++---- main.py | 4 ++-- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/level/level.py b/level/level.py index fefb958..9eb6c1c 100644 --- a/level/level.py +++ b/level/level.py @@ -43,6 +43,7 @@ class Level: self.light_system = LightSystem() self.particle_system = ParticleSystem() + # We initialize special obstacles in the grid like starting points and ending points for k in range( len(initial_positions)): # sometimes there are two players and we can imagine a level with even more @@ -55,6 +56,8 @@ class Level: self.initial_colors[k], False, False, True) + # those obstacles have also a soft lighting point + self.light_system.add_light("start" + str(k), colors[self.initial_colors[k]], (self.initial_positions[k][0] * pixel_size + pixel_size / 2, self.initial_positions[k][1] * pixel_size + pixel_size / 2), @@ -65,12 +68,14 @@ class Level: self.final_positions[k][1] * pixel_size + pixel_size / 2), 400, 0.1) + # each player has a point of light that follows him + self.light_system.add_light("player" + str(k), colors[self.initial_colors[k]], (self.initial_positions[k][0] * pixel_size + pixel_size / 2, self.initial_positions[k][1] * pixel_size + pixel_size / 2), 200, 0.2) - self.reload_level() + self.reload_level() # load / reload this level def reload_level(self): self.players = [] @@ -79,8 +84,14 @@ class Level: for k in range( len(self.initial_positions)): + + # play again with players at their starting points + self.players.append(Player(colors[self.initial_colors[k]], self.initial_positions[k][0] * pixel_size, self.initial_positions[k][1] * pixel_size)) + + # at the beginning of the level, particles explode from the each player position + particles = [] for i in range(100): particles.append(PointParticle(colors[self.initial_colors[k]], @@ -97,40 +108,51 @@ class Level: def update(self, delta_time): # this function is called 60 times per second in average, so delta_time = 1/60 finished = True - for i in range(len(self.players)): # check if all players are at their final positions at the same time + for i in range(len(self.players)): # update data for all players player = self.players[i] player.update(delta_time, self.grid) + # when a player bounces on an obstacle we generate particles + if player.bounces and player.bounce_time == 0: particles = [] for k in range(50): - collision_position = player.position + np.array([pixel_size/2, pixel_size/2]) + player.bounce_direction * pixel_size / 2 + collision_position = player.position + np.array( + [pixel_size / 2, pixel_size / 2]) + player.bounce_direction * pixel_size / 2 - collision_dir = np.array([1, 1]) - np.abs (player.bounce_direction) - if np.random.randint (100) > 50: + collision_dir = np.array([1, 1]) - np.abs(player.bounce_direction) + if np.random.randint(100) > 50: collision_dir = -collision_dir - particles.append(PointParticle (player.color, - (collision_position[0], collision_position[1]), - (collision_dir[0] * np.random.randint (300) + np.random.randint (100) * np.sin((2 * 3.14 * k) / 10), - collision_dir[1] * np.random.randint(300) + np.random.randint (100) * np.cos((2 * 3.14 * k) / 10)), - 3, np.random.randint(20) / 100)) + particles.append(PointParticle(player.color, + (collision_position[0], collision_position[1]), + (collision_dir[0] * np.random.randint(300) + np.random.randint( + 100) * np.sin((2 * 3.14 * k) / 10), + collision_dir[1] * np.random.randint(300) + np.random.randint( + 100) * np.cos((2 * 3.14 * k) / 10)), + 3, np.random.randint(20) / 100)) self.particle_system.add(particles) + # check if all players are at their final positions at the same time + if int(player.position[0] / pixel_size) != self.final_positions[i][0] or int( player.position[1] / pixel_size) != self.final_positions[i][1]: finished = False + # update the position and the color of the player + self.light_system.lights["player" + str(i)].move( (player.render_position[0] + pixel_size / 2, player.render_position[1] + pixel_size / 2)) - self.light_system.lights["player" + str(i)].change_color(player.color) + self.light_system.lights["player" + str(i)].change_color( + player.color) # this will regenerate the lighting mask when the previous color is different if finished: # if yes, trigger an event to tell the game to stop the current level if self.victory_timer > self.victory_delay: pygame.event.post(victory_event) self.finished = True + elif self.victory_timer == 0: sound_victory() diff --git a/level/player.py b/level/player.py index dd759b5..91f5183 100644 --- a/level/player.py +++ b/level/player.py @@ -9,6 +9,7 @@ from sound import sound_collision from effects.particle_system import ParticleSystem from effects.point_particle import PointParticle + # ---------------- # class Player: @@ -135,28 +136,32 @@ class Player: # those are the functions that can be called from the game to move the player def move_up(self, grid): - self.bounce_direction = np.array([0, -1]) # changes the future bounce direction + if (self.bounce_direction == np.array([0, 0])).all(): + self.bounce_direction = np.array([0, -1]) # changes the future bounce direction if not self.is_moving: # the player can not move if it's alreadu moving self.destination = self.calculate_destination(grid, "up") * pixel_size self.is_moving = True def move_down(self, grid): - self.bounce_direction = np.array([0, 1]) + if (self.bounce_direction == np.array([0, 0])).all(): + self.bounce_direction = np.array([0, 1]) if not self.is_moving: self.destination = self.calculate_destination(grid, "down") * pixel_size self.is_moving = True def move_left(self, grid): - self.bounce_direction = np.array([-1, 0]) + if (self.bounce_direction == np.array([0, 0])).all(): + self.bounce_direction = np.array([-1, 0]) if not self.is_moving: self.destination = self.calculate_destination(grid, "left") * pixel_size self.is_moving = True def move_right(self, grid): - self.bounce_direction = np.array([1, 0]) + if (self.bounce_direction == np.array([0, 0])).all(): + self.bounce_direction = np.array([1, 0]) if not self.is_moving: self.destination = self.calculate_destination(grid, "right") * pixel_size @@ -187,6 +192,7 @@ class Player: self.bounce_time = 0 self.bounce_amplitude = 0 self.bounces = False + self.bounce_direction = np.array([0, 0]) self.render_position = self.position + self.bounce_direction * self.bounce_amplitude * np.sin( -20 * self.bounce_time) @@ -204,6 +210,8 @@ class Player: direction = (self.destination - self.position) / distance delta = self.speed * delta_time + self.bounce_direction = direction + # check that the next frame the player will not cross the destination (if the speed is too high) if (direction == np.array([1, 0])).all(): diff --git a/main.py b/main.py index ab13283..763c5d4 100644 --- a/main.py +++ b/main.py @@ -32,8 +32,8 @@ def main(): if game.stage == "Launched": game.render() - #if timer == 0: - # print("Game launched, currently rendering", int(clock.get_fps()), "FPS") + if timer == 0: + print("Game launched, currently rendering", int(clock.get_fps()), "FPS") if game.stage == "Main Menu": draw_main_menu(surface, menu, game) -- GitLab From 7235ad83da43096b6ca34cc6cbbc0b7f1003cf4a Mon Sep 17 00:00:00 2001 From: Hennzau <enzo.le-van@student-cs.fr> Date: Sun, 19 Nov 2023 14:16:45 +0100 Subject: [PATCH 2/2] FPS calculation --- main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 763c5d4..5449993 100644 --- a/main.py +++ b/main.py @@ -23,12 +23,15 @@ def main(): timer = 0 - frame_cap = 60 + frame_cap = 120 while game.is_open: surface.clear((0, 0, 0)) - game.update(float(1 / 60)) + if clock.get_fps() > 0: + game.update(float(1 / clock.get_fps())) + else: + game.update(float(1 / 60)) if game.stage == "Launched": game.render() -- GitLab