]> Skullheadx's Git Forge - fruit-ninja.git/commitdiff
fruits + player hit fruit
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Thu, 1 Jun 2023 17:56:05 +0000 (13:56 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Thu, 1 Jun 2023 17:56:05 +0000 (13:56 -0400)
fruit.py [new file with mode: 0644]
game.py
main.py
player.py
setup.py

diff --git a/fruit.py b/fruit.py
new file mode 100644 (file)
index 0000000..26b598f
--- /dev/null
+++ b/fruit.py
@@ -0,0 +1,31 @@
+import random
+
+from setup import *
+
+
+class Fruit:
+    SPAWN_RANGE = [WIDTH / 5, WIDTH * 4 / 5]
+    VERTICAL_VELOCITY_RANGE = [-500, -350]
+    HORIZONTAL_VELOCITY_RANGE = [-100, 100]
+    GRAVITY = 275
+
+    RADIUS = 50
+
+    def __init__(self):
+        self.position = pygame.Vector2(lerp(self.SPAWN_RANGE[0], self.SPAWN_RANGE[1], random.random()),
+                                       HEIGHT - self.RADIUS)
+        self.velocity = pygame.Vector2(
+            lerp(self.HORIZONTAL_VELOCITY_RANGE[0], self.HORIZONTAL_VELOCITY_RANGE[1], random.random()),
+            lerp(self.VERTICAL_VELOCITY_RANGE[0], self.VERTICAL_VELOCITY_RANGE[1], random.random()))
+        self.acceleration = pygame.Vector2(0, self.GRAVITY)
+
+    def update(self, delta):
+        self.velocity += self.acceleration * delta / 1000
+        self.position += self.velocity * delta / 1000
+
+    def get_rect(self):
+        return pygame.Rect(self.position - pygame.Vector2(self.RADIUS / 2, self.RADIUS / 2),
+                           pygame.Vector2(self.RADIUS, self.RADIUS))
+
+    def draw(self, surf):
+        pygame.draw.circle(surf, GREEN, self.position, self.RADIUS)
diff --git a/game.py b/game.py
index a12de98e5256ad7f687c9dd443d6a2c46b4cd515..52fe0bd0045edca214c3db34d6fcf3c47848a1cf 100644 (file)
--- a/game.py
+++ b/game.py
@@ -1,18 +1,30 @@
 from setup import *
 from player import Player
+from fruit import Fruit
 
 
 class Game:
 
     def __init__(self):
         self.player = Player()
+        self.fruits = [Fruit()]
 
     def update(self, delta):
         for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 return COMMAND_EXIT
         self.player.update(delta)
+        hits = []
+        for fruit in self.fruits:
+            fruit.update(delta)
+            if self.player.hits(fruit):
+                hits.append(fruit)
+
+        for hit in hits:
+            self.fruits.remove(hit)
 
     def draw(self, surf):
         screen.fill(WHITE)
+        for fruit in self.fruits:
+            fruit.draw(surf)
         self.player.draw(surf)
diff --git a/main.py b/main.py
index 0b5ea1151f03595a4e73e402a35efca9d10cf4e0..6972ec4ae06613430d5ba3fc134165ee2ee59348 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,7 +1,6 @@
 from setup import *
 from game import Game
 
-
 FPS = 120
 clock = pygame.time.Clock()
 
index 07e0171111fa6f8444e527d93dd341adc9d20333..d134bbd97a6803385626d59ebce4f78412169724 100644 (file)
--- a/player.py
+++ b/player.py
@@ -26,9 +26,15 @@ class Player:
                                                  (self.sliced_points[i][0] - self.sliced_points[i + 1][0])).inflate(
                     self.INFLATE_SCALE, self.INFLATE_SCALE))
 
+    def hits(self, fruit):
+        for hitbox in self.hitboxes:
+            if hitbox.colliderect(fruit.get_rect()):
+                return True
+        return False
+
     def draw(self, surf):
         for hitbox in self.hitboxes:
-            pygame.draw.rect(surf, GREEN, hitbox)
+            pygame.draw.rect(surf, RED, hitbox)
         # for pos, time in self.sliced_points:
         #     pygame.draw.circle(surf, RED, pos, 10)
         # if len(self.sliced_points) > 1:
index 8f9b5e9957092b9bfc446642be74b439fb1217cf..b797d1a8020b4e84ce99cc5ad9f559c7900f6714 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -1,4 +1,5 @@
 import pygame
+import random
 
 pygame.init()
 WIDTH, HEIGHT = 800, 500
@@ -17,3 +18,7 @@ BLUE = (0, 0, 255)
 # commands
 COMMAND_EXIT = 0
 COMMAND_START = 1
+
+
+def lerp(start, end, weight):
+    return weight * (end - start) + start