aboutsummaryrefslogtreecommitdiff
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
parentbf8050124434b5b3c5bb1ba1eb64496e7df59355 (diff)
add asteroids
Diffstat (limited to '')
-rw-r--r--__pycache__/asteroid.cpython-310.pycbin0 -> 913 bytes
-rw-r--r--__pycache__/cockroach.cpython-310.pycbin0 -> 920 bytes
-rw-r--r--asteroid.py (renamed from astroid.py)8
-rw-r--r--game.py18
-rw-r--r--res/asteroid.png (renamed from res/astroid.png)bin9284 -> 9284 bytes
5 files changed, 22 insertions, 4 deletions
diff --git a/__pycache__/asteroid.cpython-310.pyc b/__pycache__/asteroid.cpython-310.pyc
new file mode 100644
index 0000000..71caa9f
--- /dev/null
+++ b/__pycache__/asteroid.cpython-310.pyc
Binary files differ
diff --git a/__pycache__/cockroach.cpython-310.pyc b/__pycache__/cockroach.cpython-310.pyc
new file mode 100644
index 0000000..7c04eaf
--- /dev/null
+++ b/__pycache__/cockroach.cpython-310.pyc
Binary files differ
diff --git a/astroid.py b/asteroid.py
index 6a7187d..1735c8b 100644
--- a/astroid.py
+++ b/asteroid.py
@@ -1,17 +1,17 @@
import pygame
import random
-# Load asteroid image
-asteroid_image = pygame.image.load('res/asteroid.png')
-
class Asteroid:
def __init__(self):
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, 3) # Random downward speed
+ # Load asteroid image
+ self.asteroid_image = pygame.image.load('res/asteroid.png')
+
def move_down(self):
self.y += self.speed_y
def draw(self, screen):
- screen.blit(asteroid_image, (self.x, self.y))
+ screen.blit(self.asteroid_image, (self.x, self.y))
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()
diff --git a/res/astroid.png b/res/asteroid.png
index 7f96811..7f96811 100644
--- a/res/astroid.png
+++ b/res/asteroid.png
Binary files differ