From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:56:05 +0000 (-0400) Subject: fruits + player hit fruit X-Git-Tag: game~31 X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.css?a=commitdiff_plain;h=e3051db2286e712e1837a35754d4c48f3631ed28;p=fruit-ninja.git fruits + player hit fruit --- diff --git a/fruit.py b/fruit.py new file mode 100644 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 a12de98..52fe0bd 100644 --- 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 0b5ea11..6972ec4 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ from setup import * from game import Game - FPS = 120 clock = pygame.time.Clock() diff --git a/player.py b/player.py index 07e0171..d134bbd 100644 --- 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: diff --git a/setup.py b/setup.py index 8f9b5e9..b797d1a 100644 --- 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