aboutsummaryrefslogtreecommitdiff
path: root/game.py
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2023-07-30 14:30:26 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2023-07-30 14:30:26 +0530
commit28d7cee3ed843e4cb183ebe53f7a508d4df6d3de (patch)
tree589bef284d4016724d0c3946f8596bddeca7f051 /game.py
parentbf8050124434b5b3c5bb1ba1eb64496e7df59355 (diff)
add asteroids
Diffstat (limited to 'game.py')
-rw-r--r--game.py18
1 files changed, 18 insertions, 0 deletions
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()