diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2023-07-30 14:30:26 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2023-07-30 14:30:26 +0530 |
commit | 28d7cee3ed843e4cb183ebe53f7a508d4df6d3de (patch) | |
tree | 589bef284d4016724d0c3946f8596bddeca7f051 | |
parent | bf8050124434b5b3c5bb1ba1eb64496e7df59355 (diff) |
add asteroids
-rw-r--r-- | __pycache__/asteroid.cpython-310.pyc | bin | 0 -> 913 bytes | |||
-rw-r--r-- | __pycache__/cockroach.cpython-310.pyc | bin | 0 -> 920 bytes | |||
-rw-r--r-- | asteroid.py (renamed from astroid.py) | 8 | ||||
-rw-r--r-- | game.py | 18 | ||||
-rw-r--r-- | res/asteroid.png (renamed from res/astroid.png) | bin | 9284 -> 9284 bytes |
5 files changed, 22 insertions, 4 deletions
diff --git a/__pycache__/asteroid.cpython-310.pyc b/__pycache__/asteroid.cpython-310.pyc Binary files differnew file mode 100644 index 0000000..71caa9f --- /dev/null +++ b/__pycache__/asteroid.cpython-310.pyc diff --git a/__pycache__/cockroach.cpython-310.pyc b/__pycache__/cockroach.cpython-310.pyc Binary files differnew file mode 100644 index 0000000..7c04eaf --- /dev/null +++ b/__pycache__/cockroach.cpython-310.pyc @@ -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)) @@ -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 Binary files differindex 7f96811..7f96811 100644 --- a/res/astroid.png +++ b/res/asteroid.png |