diff options
-rw-r--r-- | __pycache__/cockroach_game.cpython-310.pyc | bin | 1568 -> 1581 bytes | |||
-rw-r--r-- | cockroach_game.py | 12 | ||||
-rw-r--r-- | game1.py | 70 |
3 files changed, 48 insertions, 34 deletions
diff --git a/__pycache__/cockroach_game.cpython-310.pyc b/__pycache__/cockroach_game.cpython-310.pyc Binary files differindex 173fdd9..8006cbf 100644 --- a/__pycache__/cockroach_game.cpython-310.pyc +++ b/__pycache__/cockroach_game.cpython-310.pyc diff --git a/cockroach_game.py b/cockroach_game.py index 23e8265..2959654 100644 --- a/cockroach_game.py +++ b/cockroach_game.py @@ -11,14 +11,16 @@ SCREEN_HEIGHT = 600 # Load cockroach image cockroach_image = pygame.image.load('res/cockroach.png') +cockroach_image = pygame.image.load('res/cockroach.png') + class Cockroach: def __init__(self): - self.x = SCREEN_WIDTH - self.y = random.randint(0, SCREEN_HEIGHT - 40) - self.speed_x = random.randint(1, 5) + self.x = random.randint(80, 800) # Random x position within the screen width + self.y = 60 # Starting y position at the top + self.speed_y = random.randint(1, 5) # Random downward speed - def move(self): - self.x -= self.speed_x + def move_down(self): + self.y += self.speed_y def draw(self, screen): screen.blit(cockroach_image, (self.x, self.y)) @@ -9,8 +9,8 @@ from cockroach_game import Cockroach pygame.init() # Define screen dimensions -SCREEN_WIDTH = 800 -SCREEN_HEIGHT = 600 +SCREEN_WIDTH = 802 +SCREEN_HEIGHT = 601 # Load cockroach image cockroach_image = pygame.image.load('res/cockroach.png') @@ -24,41 +24,32 @@ class MyGame: self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Camera Feed and Cockroach Panel') + # Track whether the Pygame window should be closed + self.running = True + + # Create an event to signal the camera feed thread to stop + self.stop_event = threading.Event() + def run_cockroach_game(self): # Create a list to hold cockroaches cockroaches = [] - # Game loop - running = True - while running: + while not self.stop_event.is_set(): for event in pygame.event.get(): if event.type == pygame.QUIT: - running = False + # Set the stop event when the 'x' button is clicked + self.stop_event.set() # Spawn a new cockroach randomly if len(cockroaches) < 5 and random.random() < 0.02: cockroaches.append(Cockroach()) - # Move cockroaches and remove off-screen ones + # Move cockroaches downwards and remove off-screen ones for cockroach in cockroaches: - cockroach.move() - if cockroach.x <= -40: + cockroach.move_down() + if cockroach.y >= SCREEN_HEIGHT: cockroaches.remove(cockroach) - # Draw everything on the screen - self.screen.fill((255, 255, 255)) # Fill the screen with white - for cockroach in cockroaches: - cockroach.draw(self.screen) - - # Update the display - pygame.display.flip() - - def loop(self) -> None: - # Start the webcam feed and cockroach panel in separate threads - cockroach_thread = threading.Thread(target=self.run_cockroach_game) - cockroach_thread.start() - - while True: # Read frame from the webcam ret, frame = self.cap.read() @@ -66,29 +57,50 @@ class MyGame: if not ret: break + # Rotate the frame counter-clockwise by 90 degrees + frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE) + # Convert frame from BGR to RGB frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert frame to Pygame surface frame_pygame = pygame.surfarray.make_surface(frame_rgb) - # Blit the camera feed onto the Pygame screen + # Draw the camera feed as the background self.screen.blit(frame_pygame, (80, 60)) + # Draw the cockroach panel on top of the camera feed + for cockroach in cockroaches: + cockroach.draw(self.screen) + # Update the display pygame.display.flip() - # Quit if 'q' is pressed - if cv2.waitKey(1) & 0xFF == ord('q'): - self.stop() - - def stop(self) -> None: # Release the captured webcam self.cap.release() # Destroy all CV2 Windows cv2.destroyAllWindows() + def loop(self) -> None: + # Start the webcam feed and cockroach panel in separate threads + cockroach_thread = threading.Thread(target=self.run_cockroach_game) + cockroach_thread.start() + + while self.running: + # Handle events for the Pygame window + for event in pygame.event.get(): + if event.type == pygame.QUIT: + # Close the Pygame window + self.running = False + + # Quit if 'q' is pressed + if cv2.waitKey(1) & 0xFF == ord('q'): + self.running = False + + # Signal the camera feed thread to stop + self.stop_event.set() + if __name__ == '__main__': game = MyGame() |