From ee751b27e9f9afeaef530f2886f44de48eec1b05 Mon Sep 17 00:00:00 2001 From: Raghuram Subramani Date: Sun, 30 Jul 2023 15:18:59 +0530 Subject: update --- game.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'game.py') diff --git a/game.py b/game.py index 0ec5ce0..93df5b3 100644 --- a/game.py +++ b/game.py @@ -24,6 +24,11 @@ class MyGame: self.cockroaches = [] self.asteroids = [] + self.detect_x = 0 + self.detect_y = 0 + self.detect_w = 0 + self.detect_h = 0 + # Create the game screen self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Camera Feed and Cockroach Panel') @@ -56,17 +61,15 @@ class MyGame: if asteroid.y >= SCREEN_HEIGHT: self.asteroids.remove(asteroid) - def detect_collision(self, x, y): - cockroaches_copy = self.cockroaches.copy() - - # Iterate through the list of cockroaches - for cockroach in cockroaches_copy: - # Check if the detection rectangle intersects with the cockroach - if x < cockroach.x < x + self.w and y < cockroach.y < y + self.h: - # Remove the cockroach from the list - self.cockroaches.remove(cockroach) - return True - return False + def detect_collision(self): + cockroaches_to_remove = [] + for cockroach in self.cockroaches: + if (self.detect_x < cockroach.x < self.detect_x + self.detect_w and + self.detect_y < cockroach.y < self.detect_y + self.detect_h): + cockroaches_to_remove.append(cockroach) + + for cockroach in cockroaches_to_remove: + self.cockroaches.remove(cockroach) def start_game(self): @@ -118,10 +121,7 @@ class MyGame: # Draw the camera feed as the background self.screen.blit(frame_pygame, (0, 0)) - try: - self.detect_collision(x,y) - except: - print('passing') + self.detect_collision() # Draw the cockroach panel x,yon top of the camera feed for cockroach in self.cockroaches: cockroach.draw(self.screen) -- cgit v1.2.3