Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open CV Flappy Bird Game #1193

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Open CV Game/bird_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Open CV Game/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
8 changes: 8 additions & 0 deletions Open CV Game/idea/FlappyBirdCV.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
6 changes: 6 additions & 0 deletions Open CV Game/idea/IncpectionProfiles/profiles_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
4 changes: 4 additions & 0 deletions Open CV Game/idea/misc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>
8 changes: 8 additions & 0 deletions Open CV Game/idea/modules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/FlappyBirdCV.iml" filepath="$PROJECT_DIR$/.idea/FlappyBirdCV.iml" />
</modules>
</component>
</project>
7 changes: 7 additions & 0 deletions Open CV Game/idea/vcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
149 changes: 149 additions & 0 deletions Open CV Game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import sys, time, random, pygame
from collections import deque
import cv2 as cv, mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
pygame.init()

# Initialize required elements/environment
VID_CAP = cv.VideoCapture(3)
window_size = (VID_CAP.get(cv.CAP_PROP_FRAME_WIDTH), VID_CAP.get(cv.CAP_PROP_FRAME_HEIGHT)) # width by height
screen = pygame.display.set_mode(window_size)

# Bird and pipe init
bird_img = pygame.image.load("bird_sprite.png")
bird_img = pygame.transform.scale(bird_img, (bird_img.get_width() / 6, bird_img.get_height() / 6))
bird_frame = bird_img.get_rect()
bird_frame.center = (window_size[0] // 6, window_size[1] // 2)
pipe_frames = deque()
pipe_img = pygame.image.load("pipe_sprite_single.png")

pipe_starting_template = pipe_img.get_rect()
space_between_pipes = 250

# Game loop
game_clock = time.time()
stage = 1
pipeSpawnTimer = 0
time_between_pipe_spawn = 40
dist_between_pipes = 500
pipe_velocity = lambda: dist_between_pipes / time_between_pipe_spawn
level = 0
score = 0
didUpdateScore = False
game_is_running = True

with mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
while True:
# Check if game is running
if not game_is_running:
text = pygame.font.SysFont("Helvetica Bold.ttf", 64).render('Game over!', True, (99, 245, 255))
tr = text.get_rect()
tr.center = (window_size[0]/2, window_size[1]/2)
screen.blit(text, tr)
pygame.display.update()
pygame.time.wait(2000)
VID_CAP.release()
cv.destroyAllWindows()
pygame.quit()
sys.exit()

# Check if user quit window
for event in pygame.event.get():
if event.type == pygame.QUIT:
VID_CAP.release()
cv.destroyAllWindows()
pygame.quit()
sys.exit()

# Get frame
ret, frame = VID_CAP.read()
if not ret:
print("Empty frame, continuing...")
continue

# Clear screen
screen.fill((125, 220, 232))

# Face mesh
frame.flags.writeable = False
frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
results = face_mesh.process(frame)
frame.flags.writeable = True

# Draw mesh
if results.multi_face_landmarks and len(results.multi_face_landmarks) > 0:
# 94 = Tip of nose
marker = results.multi_face_landmarks[0].landmark[94].y
bird_frame.centery = (marker - 0.5) * 1.5 * window_size[1] + window_size[1]/2
if bird_frame.top < 0: bird_frame.y = 0
if bird_frame.bottom > window_size[1]: bird_frame.y = window_size[1] - bird_frame.height

# Mirror frame, swap axes because opencv != pygame
frame = cv.flip(frame, 1).swapaxes(0, 1)

# Update pipe positions
for pf in pipe_frames:
pf[0].x -= pipe_velocity()
pf[1].x -= pipe_velocity()

if len(pipe_frames) > 0 and pipe_frames[0][0].right < 0:
pipe_frames.popleft()

# Update screen
pygame.surfarray.blit_array(screen, frame)
screen.blit(bird_img, bird_frame)
checker = True
for pf in pipe_frames:
# Check if bird went through to update score
if pf[0].left <= bird_frame.x <= pf[0].right:
checker = False
if not didUpdateScore:
score += 1
didUpdateScore = True
# Update screen
screen.blit(pipe_img, pf[1])
screen.blit(pygame.transform.flip(pipe_img, 0, 1), pf[0])
if checker: didUpdateScore = False

# Stage, score text
text = pygame.font.SysFont("Helvetica Bold.ttf", 50).render(f'Stage {stage}', True, (99, 245, 255))
tr = text.get_rect()
tr.center = (100, 50)
screen.blit(text, tr)
text = pygame.font.SysFont("Helvetica Bold.ttf", 50).render(f'Score: {score}', True, (99, 245, 255))
tr = text.get_rect()
tr.center = (100, 100)
screen.blit(text, tr)

# Update screen
pygame.display.flip()

# Check if bird is touching a pipe
if any([bird_frame.colliderect(pf[0]) or bird_frame.colliderect(pf[1]) for pf in pipe_frames]):
game_is_running = False

# Time to add new pipes
if pipeSpawnTimer == 0:
top = pipe_starting_template.copy()
top.x, top.y = window_size[0], random.randint(120 - 1000, window_size[1] - 120 - space_between_pipes - 1000)
bottom = pipe_starting_template.copy()
bottom.x, bottom.y = window_size[0], top.y + 1000 + space_between_pipes
pipe_frames.append([top, bottom])

# Update pipe spawn timer - make it cyclical
pipeSpawnTimer += 1
if pipeSpawnTimer >= time_between_pipe_spawn: pipeSpawnTimer = 0

# Update stage
if time.time() - game_clock >= 10:
time_between_pipe_spawn *= 5 / 6
stage += 1
game_clock = time.time()

Binary file added Open CV Game/pipe_sprite_single.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading