From c92fad89517790a672ef25d3126f12865720c731 Mon Sep 17 00:00:00 2001 From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Fri, 2 Jun 2023 18:34:44 -0400 Subject: [PATCH] combo counter + clean up --- combo_counter.py | 21 +++++++++++++++++++++ fruit.py | 5 ++--- game.py | 25 ++++++++++++++++++++++--- main.py | 1 - setup.py | 4 ++-- 5 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 combo_counter.py diff --git a/combo_counter.py b/combo_counter.py new file mode 100644 index 0000000..31e5d21 --- /dev/null +++ b/combo_counter.py @@ -0,0 +1,21 @@ +from setup import * + + +class ComboCounter: + LIFE_TIME = 1000 + + def __init__(self, position, combo): + self.position = pygame.Vector2(position) + self.velocity = pygame.Vector2(0, -1) + self.time = self.LIFE_TIME + self.combo = combo + self.text_surface = font.render(self.combo, True, BLACK) + + def update(self, delta): + self.position += self.velocity * delta / 1000 + self.time -= delta + if self.time <= 0: + return True + + def draw(self, surf): + surf.blit(self.text_surface, self.position) diff --git a/fruit.py b/fruit.py index e29cd26..56e04cc 100644 --- a/fruit.py +++ b/fruit.py @@ -2,13 +2,13 @@ from setup import * class Fruit: - RADIUS_RANGE = [35 ,65]#[25, 50] + RADIUS_RANGE = [35, 65] # [25, 50] HORIZONTAL_SPAWN_RANGE = [max(RADIUS_RANGE), WIDTH - max(RADIUS_RANGE)] VERTICAL_SPAWN_RANGE = [HEIGHT + max(RADIUS_RANGE), HEIGHT * 2 + max(RADIUS_RANGE)] VERTICAL_TARGET_RANGE = [max(RADIUS_RANGE), HEIGHT * 4 / 5] - HORIZONTAL_TARGET_RANGE = [WIDTH / 6, WIDTH * 5/6] + HORIZONTAL_TARGET_RANGE = [WIDTH / 6, WIDTH * 5 / 6] GRAVITY = 275 @@ -50,5 +50,4 @@ class Fruit: pygame.draw.circle(surf, self.color, self.position, self.radius) pygame.draw.circle(surf, DARKEN[self.color], self.position, self.radius, self.OUTLINE_WIDTH) - # pygame.draw.circle(surf, BLACK, self.target, 10) diff --git a/game.py b/game.py index d03790e..c408ec7 100644 --- a/game.py +++ b/game.py @@ -3,6 +3,7 @@ from player import Player from fruit import Fruit from bomb import Bomb from effect import Effect +from combo_counter import ComboCounter class Game: @@ -15,9 +16,11 @@ class Game: self.fruits = [Fruit()] self.bombs = [] self.effects = [] + self.combo_counters = [] self.wave = 1 self.score = 0 self.time_since_last_hit = 0 + self.current_combo = 0 def update(self, delta): for event in pygame.event.get(): @@ -36,21 +39,33 @@ class Game: if ((not -fruit.radius < fr.x < WIDTH + fruit.radius) or fr.y > HEIGHT) and fruit.velocity.y > 0: self.fruits.remove(fruit) self.time_since_last_hit += delta + + if self.time_since_last_hit < self.COMBO_TIME: + self.score += self.current_combo + else: + self.current_combo = 0 for hit in hits: for i in range(self.EFFECT_COUNT_PER_FRUIT): self.effects.append(Effect(hit.position, hit.radius, hit.color)) if hit in self.fruits: self.fruits.remove(hit) self.score += 1 + if self.time_since_last_hit < self.COMBO_TIME: + self.current_combo += 1 + self.combo_counters.append(ComboCounter(hit.position, str(self.current_combo))) + self.time_since_last_hit = 0 - if self.time_since_last_hit < self.COMBO_TIME: - self.score += len(hits) for effect in self.effects: effect_status = effect.update(delta) if effect_status: self.effects.remove(effect) + for combo in self.combo_counters: + combo_status = combo.update(delta) + if combo_status: + self.combo_counters.remove(combo) + for bomb in self.bombs: bomb.update(delta) if self.player.hits(bomb): @@ -75,6 +90,10 @@ class Game: fruit.draw(surf) for bomb in self.bombs: bomb.draw(surf) + for combo in self.combo_counters: + combo.draw(surf) self.player.draw(surf) text_surf = font.render(str(self.score), True, BLACK) - surf.blit(text_surf, (WIDTH - text_surf.get_width(), 0)) \ No newline at end of file + surf.blit(text_surf, (WIDTH - text_surf.get_width(), 0)) + text_surf2 = font.render(f"TIME SINCE LAST HIT {round(self.time_since_last_hit / 1000, 1)}", True, BLACK) + surf.blit(text_surf2, (WIDTH - text_surf2.get_width(), text_surf.get_height())) diff --git a/main.py b/main.py index f0fe7f1..3c632a3 100644 --- a/main.py +++ b/main.py @@ -19,5 +19,4 @@ while is_running: elif status == COMMAND_START: scene = Game() - pygame.quit() diff --git a/setup.py b/setup.py index 95f9c35..801367d 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ pygame.display.set_caption("Fruit Ninja") WHITE = (255, 255, 255) LIGHT_GRAY = (211, 211, 211) GRAY = (128, 128, 128) -DARK_GRAY = (25,25,25) +DARK_GRAY = (25, 25, 25) BLACK = (0, 0, 0) RED = (255, 0, 0) @@ -43,4 +43,4 @@ def lerp(start, end, weight): return weight * (end - start) + start -font = pygame.font.SysFont("Arial", 50) \ No newline at end of file +font = pygame.font.SysFont("Arial", 50) -- 2.54.0