Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f2ce4be
submitting project proposal
arianaolson419 Feb 29, 2016
238ff43
the open cv portion
arianaolson419 Mar 3, 2016
fa02d62
pygame part
Mar 3, 2016
6e5a5f5
Merge pull request #1 from ericasaywhat/master
arianaolson419 Mar 3, 2016
23b02de
the screen works, but open cv not quite there yet...
arianaolson419 Mar 4, 2016
b1377a3
skeleton code for dodgygame
Mar 4, 2016
6f0253e
AHHH
arianaolson419 Mar 4, 2016
330729d
AHHH
arianaolson419 Mar 4, 2016
08256e7
Merge branch 'ericasaywhat-master'
arianaolson419 Mar 4, 2016
84af899
skeleton code for dodgygame
Mar 4, 2016
cd8f8db
Merge pull request #3 from ericasaywhat/master
arianaolson419 Mar 4, 2016
edfe8a7
progress made on the opencv portion
arianaolson419 Mar 5, 2016
00c8a15
press q a lot and this kinda works
arianaolson419 Mar 6, 2016
1b0c790
ahh it actually works!
arianaolson419 Mar 6, 2016
d3194c3
now with color changing balls
arianaolson419 Mar 6, 2016
dfdd601
scared eyes for user
arianaolson419 Mar 7, 2016
8ed5223
game over screen
arianaolson419 Mar 7, 2016
4390a2b
falling bird
arianaolson419 Mar 7, 2016
4c04e50
now with a game over screen!
arianaolson419 Mar 7, 2016
42f4945
the ostrich grows now!
arianaolson419 Mar 8, 2016
7c36926
final commit
arianaolson419 Mar 9, 2016
0702192
here's the reflection
arianaolson419 Mar 9, 2016
0a01683
Delete DodgyGame.py
arianaolson419 Mar 9, 2016
d76a592
Changed screen size and made it harder to cheat
arianaolson419 Mar 10, 2016
c6da873
Merge branch 'master' of https://github.com/arianaolson419/Interactiv…
arianaolson419 Mar 10, 2016
5e30709
Final revisions
arianaolson419 Apr 18, 2016
53371ba
added a space
arianaolson419 May 13, 2016
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
283 changes: 283 additions & 0 deletions DodgyGame2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
'''
DODGY GAME 2

You are minding your own business when all of a sudden, a plane flies overhead. A hatch must
have malfunctioned because you see some small objects up above. As they get closer, you realize there
are ostriches falling right towards you! See how long you can last dodging the ostriches.

In this updated version of Dodgy Game, you have 3 lives and the game does not automatically start over.

To start the game, run the program in the terminal. Click the start button to begin.
You control the player at the bottom of the screen using your movement. The game utilizes opencv
and facial recognition to control the player, so make sure you have enough light and you have a webcam available.
Best result when the user is not wearing glasses.

Enjoy!

Authors: Ariana Olson and Erica Lee
Version 2 author: Ariana Olson
'''

import pygame
from pygame.locals import QUIT
import time
from random import *
import cv2
import numpy as np

class View(object):
""" Provides a view of the Dodgy Game model in a pygame
window """

def __init__(self, model, size):
""" Initialize with the specified model """
self.model = model
self.size = size
self.screen = pygame.display.set_mode(size)
self.end = pygame.transform.scale(pygame.image.load('gameover.png'), (1000,1000))
self.eyes = pygame.transform.scale(
pygame.image.load('eyes.png'), (self.model.user.radius, self.model.user.radius))
self.lives = 3

def draw_button(self):
"""draws the starting screen with an interactive button"""
self.screen.fill(pygame.Color(135, 206, 250))
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
coords = pygame.Rect(self.model.button.x, self.model.button.y, self.model.button.w, self.model.button.h)
ac = pygame.Color('white')
ic = pygame.Color('yellow')

if self.model.button.x+self.model.button.w > mouse[0] > self.model.button.x and self.model.button.y + self.model.button.h > mouse[1] > self.model.button.y:
pygame.draw.rect(self.screen, ac, coords)

else:
pygame.draw.rect(self.screen, ic, coords)

font = pygame.font.SysFont("monospace", 140)
label = font.render(self.model.button.msg, 1, (135, 206, 255))
text_pos = label.get_rect()
text_pos.centerx = coords.centerx
text_pos.centery = coords.centery

self.screen.blit(label, (text_pos[0], text_pos[1]))
pygame.display.update()

def draw(self):
""" Draw the active game to the pygame window """
self.screen.fill(pygame.Color(135, 206, 250)) # sky

self.ostrich = pygame.transform.scale(pygame.image.load(
'ostrich.png'), (self.model.bird.radius * 2, self.model.bird.radius * 2))
self.ostrich2 = pygame.transform.scale(pygame.image.load(
'ostrich.png'), (self.model.bird2.radius * 2, self.model.bird2.radius * 2))

self.screen.blit(self.ostrich, (self.model.bird.center_x -
self.model.bird.radius, self.model.bird.center_y - self.model.bird.radius))
self.screen.blit(self.ostrich2, (self.model.bird2.center_x -
self.model.bird2.radius, self.model.bird2.center_y - self.model.bird2.radius))

pygame.draw.circle(self.screen, # player
pygame.Color('white'),
(self.model.user.center_x,
self.model.user.center_y),
self.model.user.radius)
self.screen.blit(
self.eyes, (self.model.user.center_x - 70, self.model.user.center_y - 140))



if (self.model.bird.center_x + self.model.bird.radius >= self.model.user.center_x - self.model.user.radius) and \
(self.model.bird.center_y + self.model.bird.radius >= self.model.user.center_y - (self.model.user.radius - 20)) and \
(self.model.bird.center_x - self.model.bird.radius <= self.model.user.center_x + self.model.user.radius - 20 ) and \
(self.model.bird.center_y + self.model.bird.radius >= self.model.user.center_y - (self.model.user.radius - 20)):

self.model.bird.center_y = self.size[1]+1
self.lives -= 1


if (self.model.bird2.center_x + self.model.bird2.radius >= self.model.user.center_x - self.model.user.radius) and \
(self.model.bird2.center_y + self.model.bird2.radius >= self.model.user.center_y - (self.model.user.radius - 20)) and \
(self.model.bird2.center_x - self.model.bird2.radius <= self.model.user.center_x + self.model.user.radius - 20 ) and \
(self.model.bird2.center_y + self.model.bird2.radius >= self.model.user.center_y - (self.model.user.radius - 20)):

self.model.bird2.center_y = self.size[1]+1
self.lives -= 1


for heart in self.model.hearts[0:self.lives]:
h = pygame.transform.scale(
pygame.image.load('heart.png'), (heart.width, heart.height))
self.screen.blit(h, (heart.left, heart.top))

pygame.display.update()
def gameover(self):
'''draws the gameover screen'''
self.screen.blit(self.end, (0, 0))
pygame.display.update()

class SkyModel(object):
'''Represents the game state for Dodgy Game'''

def __init__(self, width, height):
self.width = width
self.height = height

self.BIRD_Y = 0
self.USER_X = 250
self.USER_RADIUS = 140
self.BIRD_RADIUS = 10
self.HEART_WIDTH = 100
self.HEART_HEIGHT = 100
self.MARGIN = 20
self.bird = Bird(randint(0, self.width), self.BIRD_Y, self.BIRD_RADIUS)
#bird 2 has to be offset from bird so it is initialized above the top of the screen and its radius starts smaller
self.bird2 = Bird(randint(self.width/2, self.width), self.BIRD_Y - 500, self.BIRD_RADIUS-9)
self.user = User(self.USER_X, width, self.USER_RADIUS)
self.button = Button('START', self.width, self.height)

self.hearts = []
for left in range(self.MARGIN, 3*(self.MARGIN+self.HEART_WIDTH),self.MARGIN+self.HEART_WIDTH):
self.hearts.append(Heart(left, self.MARGIN, self.HEART_WIDTH, self.HEART_HEIGHT))


def update(self):
'''Update the model state'''
self.bird.update()
self.bird2.update()

class Button(object):
'''represents the start button'''
def __init__(self, msg, screenw, screenh, w = 500, h = 300): #left is (screen width - button width)/2 similar for top
self.msg = msg
self.x = (screenw-w)/2
self.y = (screenh-h)/2
self.w = w
self.h = h

class Bird(object):
""" Represents a bird in dodging game """

def __init__(self, center_x, center_y, radius):
""" Create a ball object with the specified geometry """
self.center_x = center_x
self.center_y = center_y
self.radius = radius
self.growth = 2 # rate that the bird gets bigger as it gets 'closer'

def update(self):
""" Update the position of the bird due to time passing """
self.radius += self.growth

if self.center_y < 1000:
# if the bird has not reached the bottom of the screen
self.center_y += 35

else:
# restart position at top of screen
self.center_y = 0
self.radius = 20
self.center_x = randint(0, 500)


class User(object):
""" Represents the user in the game """

def __init__(self, center_x, center_y, radius):
""" Create a ball object with the specified geometry """
self.center_x = center_x
self.center_y = center_y
self.radius = radius

class Heart(object):
'''represents the life counters in the game'''

def __init__(self, left, top , width, height):
self.left = left
self.top = top
self.width = width
self.height = height

class Movement(object):
'''allows theplayer to move backand forthin the game'''
def __init__(self, model):
self.model = model
self.MOVE = pygame.USEREVENT + 1
move_event = pygame.event.Event(self.MOVE)
# this event occurs every millisecond
pygame.time.set_timer(self.MOVE, 1)
self.cap = cv2.VideoCapture(0)
self.face_cascade = cv2.CascadeClassifier(
'haarcascade_frontalface_alt.xml')

def handle_event(self, event):
'''uses the position of player's face to control the user'''
for (x, y, w, h) in faces:
if x > 0 and x < 1000-160:
self.model.user.center_x = 1000 - (2 * x)





if __name__ == '__main__':
pygame.init()
size = (1000, 1000)

model = SkyModel(size[0], size[1])
view = View(model, size)
movement = Movement(model)

screen_on = True
while screen_on:
starting = True
while starting:
#start screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
starting = False
running = False
screen_on = False
if event.type == pygame.MOUSEBUTTONDOWN:
#moves to next loop when the button is clicked
starting = False
running = True
view.draw_button()
time.sleep(.01)

while running:
#the main game
ret, frame = movement.cap.read()
faces = movement.face_cascade.detectMultiScale(
frame, scaleFactor=1.2, minSize=(20, 20))
for event in pygame.event.get():
if event.type == QUIT:
ending = False
running = False
screen_on = False
else:
movement.handle_event(event)
if view.lives == 0:
#moves to next loop when lives run out
ending = True
running = False

model.update()

view.draw()
time.sleep(.01)

while ending:
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen_on = False
ending = False
if event.type == pygame.MOUSEBUTTONDOWN:
#moves to starting loop when mouse is clicked
starting = True
ending = False

view.gameover()
time.sleep(.01)
movement.cap.release()
cv2.destroyAllWindows()
Binary file added DodgyGameWriteupandReflection.pdf
Binary file not shown.
Binary file added Miniproject4Proposal.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2016 at Olin College.
# InteractiveProgramming2
Improving and adding new features to Mini Project 4 for Mini Project 5
Binary file added eyes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions face_detect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
""" Experiment with face detection and image filtering using OpenCV """

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('/home/arianaolson/haarcascade_frontalface_alt.xml')
kernel = np.ones((21,21), 'uint8')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
faces = face_cascade.detectMultiScale(frame, scaleFactor=1.2, minSize=(20,20))
a = int(cap.get(3))
for (x,y,w,h) in faces:
frame[y:y+h, x:x+w, :] = cv2.dilate(frame[y:y+h, x:x+w, :], kernel)
# cv2.rectangle(frame, (0,0), int((cap.get(3)/2), int(cap.get(4))), (0,255,255))
if x <= a/2 - x:
cv2.rectangle(frame, (x,y),(x+w, y+h), (0,0,255))
print 'right',
break
elif x >= a/2 - x:
cv2.rectangle(frame, (x,y),(x+w, y+h), (0,0,255))
print 'left',
break
# cv2.circle(frame, (x+w/2, y+h/2), w/4, (0, 200, 250), 150) #make the face circle
# cv2.circle(frame, (x+w/3, y+h/3), 15, (255, 0, 0), 20) #the eye circles
# cv2.circle(frame, (x+w/3, y+h/3), 5, (0, 0, 0), 20)
# cv2.circle(frame, (x+w/3, y+h/3), 20, (255,255,255), 30)
# cv2.circle(frame, (x+w/3, y+h/3), 15, (255, 0, 0), 20)
# cv2.circle(frame, (x+w/3, y+h/3), 5, (0, 0, 0), 20)
# cv2.circle(frame, (x+2*w/3, y+h/3), 20, (255,255,255), 30)
# cv2.circle(frame, (x+2*w/3, y+h/3), 15, (255, 0, 0), 20)
# cv2.circle(frame, (x+2*w/3, y+h/3), 5, (0, 0, 0), 20)
# cv2.ellipse(frame, (x+w/2, y+3*h/5),(x/5, y/5), 0, 180, 0, (0,0,255), 30) #make the mouth
# cv2.ellipse(frame, (x+w/2, y+3*h/5),(x/5, y/5), 0, 180, 0, (255,255,255), 10)

# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Binary file added gameover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading