From 0cacad549b59bfd463d16a7435dba9fd31911028 Mon Sep 17 00:00:00 2001 From: "Hannes F. Kuchelmeister" Date: Fri, 5 Nov 2021 20:05:25 +0100 Subject: [PATCH] add vacuum cleaner to suck up balls Co-authored-by: Laura Januleviciute --- Vacuum.py | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 33 ++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 Vacuum.py diff --git a/Vacuum.py b/Vacuum.py new file mode 100644 index 0000000..a34b6cd --- /dev/null +++ b/Vacuum.py @@ -0,0 +1,38 @@ +import pygame +import random +from Bubble import Bubble + +class Vacuum: + def __init__(self, bubble_objects): + self.posX = -50 + self.posY = -50 + self.width = 50 + self.height = 50 + self.bubble_objects = bubble_objects + + def draw(self, screen): + pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(self.posX, self.posY, self.width, self.height)) + + def update(self, screen): + x, y = pygame.mouse.get_pos() + self.posX = x -(self.width / 2) + self.posY = y -(self.height / 2) + + to_remove = [] + + for i in range(len(self.bubble_objects)): + x = self.bubble_objects[i].posX + y = self.bubble_objects[i].posY + if isPointInsideRect(x, y, pygame.Rect(self.posX, self.posY, self.width, self.height)): + to_remove.append(i) + + for i in reversed(to_remove): + del self.bubble_objects[i] + + + +def isPointInsideRect(x, y, rect): + if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): + return True + else: + return False diff --git a/main.py b/main.py index 89aeb9d..db8778c 100644 --- a/main.py +++ b/main.py @@ -3,32 +3,43 @@ import random import sys from pygame.locals import * -from Bubble import Bubble from Car import Car +from Vacuum import Vacuum pygame.init() +BUBBLE_LIMIT = 1000 + SCREEN = pygame.display.set_mode((800,600)) BACKGROUND = (255,255,255) SCREEN.fill(BACKGROUND) CLOCK = pygame.time.Clock() -car_objects = [] +game_objects = [] bubble_objects = [] for i in range(10): - car_objects.append(Car(random.uniform(0, 200), i * 50, 30, 50, car_objects, directionX=random.uniform(1, 5))) + game_objects.append(Car(random.uniform(0, 200), i * 50, 30, 50, bubble_objects, directionX=random.uniform(1, 5))) + +game_objects.append(Vacuum(bubble_objects)) while True: SCREEN.fill(BACKGROUND) - for game_object in car_objects: - game_object.update(SCREEN) - for game_object in car_objects: - game_object.draw(SCREEN) - for game_object in bubble_objects: - game_object.update(SCREEN) - for game_object in bubble_objects: - game_object.draw(SCREEN) + if len(bubble_objects) < BUBBLE_LIMIT: + for game_object in bubble_objects: + game_object.update(SCREEN) + for game_object in bubble_objects: + game_object.draw(SCREEN) + + for game_object in game_objects: + game_object.update(SCREEN) + for game_object in game_objects: + game_object.draw(SCREEN) + else: + # TODO game over screen with message to reduce driving cars + pass + + pygame.display.flip() for event in pygame.event.get():