From 28d7cee3ed843e4cb183ebe53f7a508d4df6d3de Mon Sep 17 00:00:00 2001 From: Raghuram Subramani Date: Sun, 30 Jul 2023 14:30:26 +0530 Subject: add asteroids --- game.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'game.py') diff --git a/game.py b/game.py index 82f1c66..1094c2f 100644 --- a/game.py +++ b/game.py @@ -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() -- cgit v1.2.3