import cv2
import numpy as np
import random
import time
from collections import deque
from imutils.video import VideoStream
import imutils
cap = cv2.VideoCapture(0)
# Game အတွက် Setting များ
levels = 3 # ကစားရမယ့် Level အရေအတွက်
points_per_level = 10 # Level တစ်ခုစီမှာထိရမယ့် point အရေအတွက်
touch_radius = 40 # ဘောလုံးနဲ့ပွိုင့်ကို ထိတဲ့ အဝေးအကွာ (radius)
game_duration = 180 # ဂိမ်းကစားချိန် စုစုပေါင်း (seconds)
font = cv2.FONT_HERSHEY_SIMPLEX # အက္ခရာဖောင့်
padding = 100 # ပွိုင့် random မထွက်ရအောင် edge ကနေ ဝေးတာ
# ဘောလုံးရောင်စပ် (HSV Color Space)
orangeLower = (5, 150, 150)
orangeUpper = (15, 255, 255)
# Game State များ
score = 0 # အမှတ်
current_level = 1 # စတင်မှာ Level 1
points = [] # ပွိုင့်စာရင်း
start_time = time.time() # စတင်ချိန်
point_index = 0 # ပွိုင့် Index
pts = deque(maxlen=64) # ဘောလုံးလမ်းကြောင်း Trail History
# Random Point တစ်ခု ဖန်တီးခြင်း
def generate_random_point(frame):
h, w, _ = frame.shape
return (
random.randint(padding, w - padding),
random.randint(padding, h - padding)
)
# Random Point မှာ အနည်းငယ် Animation ပြသခြင်း
def show_animation(frame, target):
x, y = target
for i in range(10):
radius = 30 + i * 5 # Radius တိုးတက်လာ
color = (0, 255, 255) # အရောင် Yellow
thickness = 10 # Border ထူထူ
temp = frame.copy()
cv2.circle(temp, (x, y), radius, color, thickness)
cv2.imshow("Game", temp)
cv2.waitKey(1)
# Ball tracking နဲ့ Game Loop
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1) # မျက်နှာပြောင်း
frame = cv2.resize(frame, (1920, 1080)) # Frame size မြှင့်
frame = imutils.resize(frame, width=1500) # နောက်ထပ် resize
# Frame ကို နူးညံ့စေပြီး HSV Color Space ကိုပြောင်း
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
# ဘောလုံးရောင် mask ပြုလုပ်
mask = cv2.inRange(hsv, orangeLower, orangeUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# ဘောလုံးကိုရှာ
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
ball_center = None
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea) # အကြီးဆုံး Contour
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
if M["m00"] > 0:
ball_center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if radius > 10:
# ဘောလုံးကို Draw လုပ်
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
cv2.circle(frame, ball_center, 5, (0, 0, 255), 10)
# ဘောလုံးလမ်းကြောင်း Update လုပ်
pts.appendleft(ball_center)
# Red Trail လမ်းကြောင်းကို တစ်ချောင်းချင်းဆွဲ
for i in range(1, len(pts)):
if pts[i - 1] is None or pts[i] is None:
continue
thickness = int(np.sqrt(64 / float(i + 1)) * 2.5)
cv2.line(frame, pts[i - 1], pts[i], (250, 205, 126), thickness)
# Random Point များ ဖန်တီး
if len(points) < points_per_level:
points.append(generate_random_point(frame))
# လက်ရှိ Target Point ကို ဆွဲ
if point_index < len(points):
target = points[point_index]
cv2.circle(frame, target, 60, (255, 255, 255), 6) # Outer White
cv2.circle(frame, target, 50, (15, 255, 255), -1) # Inner Blue
# ဘောလုံးနဲ့ ပွိုင့် ထိစစ်
if ball_center and point_index < len(points):
px, py = points[point_index]
bx, by = ball_center
dist = np.sqrt((bx - px) ** 2 + (by - py) ** 2)
if dist < touch_radius + 20:
score += 10 # အမှတ်ပေါင်း
show_animation(frame, target) # Animation ပြ
point_index += 1 # နောက်ပွိုင့်ဆက်
if point_index == points_per_level:
current_level += 1 # Level တိုး
point_index = 0
points = []
if current_level > levels:
break
# Game အခြေအနေတွေ ပြသ
elapsed_time = int(time.time() - start_time)
remaining_time = max(0, game_duration - elapsed_time)
cv2.putText(frame, f"Level: {min(current_level, levels)}", (1350, 40), font, 1, (0, 0, 0), 2)
cv2.putText(frame, f"Score: {score}", (10, 40), font, 1, (0, 0, 0), 2)
cv2.putText(frame, f"Time: {remaining_time}s", (650, 40), font, 1, (0, 0, 0), 2)
cv2.imshow("Game", frame)
# Game ပြီးဆုံးပဲဖြစ်ဖြစ် Quit နှိပ်ရင် break
if remaining_time <= 0 or current_level > levels:
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# ဂိမ်းအဆုံးအတွင် ပြသရန်
cap.release()
cv2.destroyAllWindows()
# Final Screen ပြသ
final_frame = 255 * np.ones((480, 640, 3), dtype=np.uint8)
cv2.putText(final_frame, "Game Over!", (150, 200), font, 2, (0, 0, 255), 3)
cv2.putText(final_frame, f"Final Score: {score}", (150, 270), font, 1.5, (0, 100, 255), 3)
cv2.putText(final_frame, "Press any key to restart...", (80, 350), font, 1, (128, 128, 128), 2)
cv2.imshow("Game Over", final_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()