aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__pycache__/cockroach_game.cpython-310.pycbin0 -> 1565 bytes
-rw-r--r--game1.py129
2 files changed, 52 insertions, 77 deletions
diff --git a/__pycache__/cockroach_game.cpython-310.pyc b/__pycache__/cockroach_game.cpython-310.pyc
new file mode 100644
index 0000000..09a3fe6
--- /dev/null
+++ b/__pycache__/cockroach_game.cpython-310.pyc
Binary files differ
diff --git a/game1.py b/game1.py
index b22d903..6937d2c 100644
--- a/game1.py
+++ b/game1.py
@@ -3,6 +3,7 @@ import numpy as np
import pygame
import random
import threading
+from cockroach_game import Cockroach
# Initialize Pygame
pygame.init()
@@ -14,91 +15,69 @@ SCREEN_HEIGHT = 600
# Load cockroach image
cockroach_image = pygame.image.load('res/cockroach.png')
-class Cockroach:
- def __init__(self):
- self.x = SCREEN_WIDTH
- self.y = random.randint(0, SCREEN_HEIGHT - 40)
- self.speed_x = random.randint(1, 5)
-
- def move(self):
- self.x -= self.speed_x
-
- def draw(self, screen):
- screen.blit(cockroach_image, (self.x, self.y))
-
-def run_cockroach_game():
- # Create the game screen
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption('Cockroach Spawning and Movement')
+class MyGame:
+ def __init__(self) -> None:
+ # Open the webcam (0 is usually the default)
+ self.cap = cv2.VideoCapture(0)
- # Create a list to hold cockroaches
- cockroaches = []
+ # Create the game screen
+ self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
+ pygame.display.set_caption('Camera Feed and Cockroach Panel')
- # Game loop
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
+ def run_cockroach_game(self):
+ # Create a list to hold cockroaches
+ cockroaches = []
- # Spawn a new cockroach randomly
- if len(cockroaches) < 5 and random.random() < 0.02:
- cockroaches.append(Cockroach())
+ # Game loop
+ running = True
+ while running:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ running = False
- # Move cockroaches and remove off-screen ones
- for cockroach in cockroaches:
- cockroach.move()
- if cockroach.x <= -40:
- cockroaches.remove(cockroach)
+ # Spawn a new cockroach randomly
+ if len(cockroaches) < 5 and random.random() < 0.02:
+ cockroaches.append(Cockroach())
- # Draw everything on the screen
- screen.fill((255, 255, 255)) # Fill the screen with white
- for cockroach in cockroaches:
- cockroach.draw(screen)
+ # Move cockroaches and remove off-screen ones
+ for cockroach in cockroaches:
+ cockroach.move()
+ if cockroach.x <= -40:
+ cockroaches.remove(cockroach)
- # Update the display
- pygame.display.flip()
+ # Draw everything on the screen
+ self.screen.fill((255, 255, 255)) # Fill the screen with white
+ for cockroach in cockroaches:
+ cockroach.draw(self.screen)
- pygame.quit()
-
-class Game:
- def __init__(self) -> None:
- # Open the webcam (0 is usually the default)
- self.cap = cv2.VideoCapture(0)
+ # Update the display
+ pygame.display.flip()
def loop(self) -> None:
+ # Start the webcam feed and cockroach panel in separate threads
+ cockroach_thread = threading.Thread(target=self.run_cockroach_game)
+ cockroach_thread.start()
+
while True:
- # Read frame
+ # Read frame from the webcam
ret, frame = self.cap.read()
# If frame is not returned, break
if not ret:
break
- # Convert frame from RGB to HSV and define frame used for detection
- det_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
-
- # Define lower green and upper green
- lower_green = np.array([60, 100, 100])
- upper_green = np.array([100, 255, 255])
-
- # Define green mask
- green_mask = cv2.inRange(det_frame, lower_green, upper_green)
-
- # Detect contours
- contours, _ = cv2.findContours(green_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-
- # Find the largest contour
- largest_contour = max(contours, key=cv2.contourArea) if contours else None
-
- # If the largest contour is present, draw a green rectangle
- if largest_contour is not None:
- x, y, w, h = cv2.boundingRect(largest_contour)
- cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
-
- # Show the frame
- cv2.imshow('Foot Tracking', frame)
-
+ # Convert frame from BGR to RGB
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
+
+ # Convert frame to Pygame surface
+ frame_pygame = pygame.surfarray.make_surface(frame_rgb)
+
+ # Blit the camera feed onto the Pygame screen
+ self.screen.blit(frame_pygame, (80, 60))
+
+ # Update the display
+ pygame.display.flip()
+
# Quit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
self.stop()
@@ -106,16 +85,12 @@ class Game:
def stop(self) -> None:
# Release the captured webcam
self.cap.release()
-
+
# Destroy all CV2 Windows
cv2.destroyAllWindows()
if __name__ == '__main__':
- game = Game()
-
- # Start the cockroach game in a separate thread
- cockroach_thread = threading.Thread(target=run_cockroach_game)
- cockroach_thread.start()
+ game = MyGame()
- # Start the webcam feed and foot tracking loop
- game.loop() \ No newline at end of file
+ # Start the webcam feed and cockroach panel together
+ game.loop()