]> Skullheadx's Git Forge - fruit-ninja.git/commitdiff
combo counter + clean up
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 2 Jun 2023 22:34:44 +0000 (18:34 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 2 Jun 2023 22:34:44 +0000 (18:34 -0400)
combo_counter.py [new file with mode: 0644]
fruit.py
game.py
main.py
setup.py

diff --git a/combo_counter.py b/combo_counter.py
new file mode 100644 (file)
index 0000000..31e5d21
--- /dev/null
@@ -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)
index e29cd26d3e0935c12ed7051b3c3e39a8fdb26ab9..56e04cca64c2166aace58c4ffe5f7b8f77ff9153 100644 (file)
--- 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 d03790ee08e4fb7e554b8dc2a787de4875053747..c408ec7cbb22f0ea1f4b927b44dc954943a47db7 100644 (file)
--- 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 f0fe7f1da28d9a6b890cd0ed71db2d39c4890a8f..3c632a3067ef6dd3fe714a3293326a5aa89b496e 100644 (file)
--- a/main.py
+++ b/main.py
@@ -19,5 +19,4 @@ while is_running:
     elif status == COMMAND_START:
         scene = Game()
 
-
 pygame.quit()
index 95f9c358ea065ca96ab4d6fa0132695ec860be1e..801367d1b45e46f4bd535dc05a5966733225d194 100644 (file)
--- 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)