diff options
Diffstat (limited to 'game.py')
-rw-r--r-- | game.py | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -1,9 +1,12 @@ import cv2 import numpy as np + import pygame import random import threading + from cockroach import Cockroach +from asteroid import Asteroid # Initialize Pygame pygame.init() @@ -33,6 +36,7 @@ class MyGame: def run_cockroach_game(self): # Create a list to hold cockroaches cockroaches = [] + asteroids = [] while not self.stop_event.is_set(): for event in pygame.event.get(): @@ -50,6 +54,16 @@ class MyGame: if cockroach.y >= SCREEN_HEIGHT: cockroaches.remove(cockroach) + # Spawn a new asteroid randomly + if len(asteroids) < 5 and random.random() < 0.02: + asteroids.append(Asteroid()) + + # Move asteroid downwards and remove off-screen ones + for asteroid in asteroids: + asteroid.move_down() + if asteroid.y >= SCREEN_HEIGHT: + asteroids.remove(asteroid) + # Read frame from the webcam ret, frame = self.cap.read() @@ -93,6 +107,10 @@ class MyGame: for cockroach in cockroaches: cockroach.draw(self.screen) + # Draw the cockroach panel on top of the camera feed + for asteroid in asteroids: + asteroid.draw(self.screen) + # Update the display pygame.display.flip() |