aboutsummaryrefslogtreecommitdiff
path: root/game1.py
blob: b22d903d8e07741b61b1338159894f3b802830fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import cv2
import numpy as np
import pygame
import random
import threading

# Initialize Pygame
pygame.init()

# Define screen dimensions
SCREEN_WIDTH = 800
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')

    # Create a list to hold cockroaches
    cockroaches = []

    # Game loop
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # Spawn a new cockroach randomly
        if len(cockroaches) < 5 and random.random() < 0.02:
            cockroaches.append(Cockroach())

        # Move cockroaches and remove off-screen ones
        for cockroach in cockroaches:
            cockroach.move()
            if cockroach.x <= -40:
                cockroaches.remove(cockroach)

        # Draw everything on the screen
        screen.fill((255, 255, 255))  # Fill the screen with white
        for cockroach in cockroaches:
            cockroach.draw(screen)

        # Update the display
        pygame.display.flip()

    pygame.quit()

class Game:
    def __init__(self) -> None:
        # Open the webcam (0 is usually the default)
        self.cap = cv2.VideoCapture(0)

    def loop(self) -> None:
        while True:
            # Read frame
            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)
            
            # Quit if 'q' is pressed
            if cv2.waitKey(1) & 0xFF == ord('q'):
                self.stop()

    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()

    # Start the webcam feed and foot tracking loop
    game.loop()