-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDodgyGame2.py
More file actions
283 lines (225 loc) · 10.6 KB
/
DodgyGame2.py
File metadata and controls
283 lines (225 loc) · 10.6 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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()