/
faceinvaders.cpp
165 lines (131 loc) · 5.22 KB
/
faceinvaders.cpp
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
#include <opencv/cv.hpp>
#include <opencv/highgui.h>
#include <deque>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <ctime>
#include "playerposition.h"
#include "graphic.h"
#include "gameimage.h"
#include "player.h"
#include "invader.h"
#include "shot.h"
static const int DEFAULT_DEVICE = 0;
static const int FLIPPING_AROUND_Y_AXIS = 1; ///< flipping around y-axis
static const int SHOT_LINE_PIX = 300;
static const unsigned MAX_INVADERS = 8;
static const int WAIT_DELAY_MS = 10;
static const int SHOT_SPEED = 6;
static const char PLAYER_PNG[] = "./data/player.png";
static const char INVADER_PNG [] = "./data/invader.png";
static const char HAARCASCADE_XML[] = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml";
class GraphicPaint {
cv::Mat &i;
public:
inline GraphicPaint(cv::Mat &i):i(i){}
inline void operator()(Graphic &g) { g.paint(i); }
};
static bool checkColision(const cv::Rect &a, const cv::Rect &b){
if (b.contains(a.tl())) return true;
if (b.contains(a.br())) return true;
if (b.contains(cv::Point(a.x+a.width,a.y))) return true;
if (b.contains(cv::Point(a.x,a.y+a.height))) return true;
return false;
}
class GraphicColision {
cv::Rect a;
public:
inline GraphicColision(const cv::Rect &a):a(a){}
inline bool operator()(const Graphic &g) const { return checkColision(a,g.rect()); }
};
inline static void GraphicUpdate(Graphic &g) { g.update(); }
inline static bool isInvalidShot(const Shot &s) { return !s.isValid(); }
int main() {
typedef std::deque<Invader> InvaderList;
typedef std::deque<Shot> ShotList;
cv::VideoCapture cap( DEFAULT_DEVICE );
if(!cap.isOpened()) {
std::cerr << "Error opening VideoCapture!" << std::endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1024);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 768);
cv::namedWindow("SpaceInvaders", CV_WINDOW_AUTOSIZE);
cv::Mat cameraImage;
cap >> cameraImage;
const cv::Size CameraImageSize( cameraImage.size() );
for (int key = -1; ' ' != key; key = cv::waitKey(WAIT_DELAY_MS)) {
cap >> cameraImage;
cv::flip(cameraImage, cameraImage, FLIPPING_AROUND_Y_AXIS);
cv::putText(cameraImage, "Press space to play!", cv::Point(30,80), cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar::all(255), 2, 8);
cv::resize(cameraImage, cameraImage, CameraImageSize * 2);
cv::imshow("SpaceInvaders", cameraImage);
}
const GameImage playerImage( PLAYER_PNG );
const GameImage invaderImage( INVADER_PNG );
const int YPlayerShot = CameraImageSize.height - playerImage.size().height;
const int YGameOver = YPlayerShot - invaderImage.size().height;
Player player(playerImage, CameraImageSize, SHOT_LINE_PIX);
PlayerPosition playerPosition(player, HAARCASCADE_XML, CameraImageSize);
Shot playerShot;
bool gameOver = false;
InvaderList invaders;
std::generate_n(std::back_inserter(invaders), MAX_INVADERS, Invader::Factory(invaderImage, CameraImageSize, 5));
ShotList shots;
cv::RNG rng(uint64(std::time(0)));
for (int key = -1; 'q' != key; key = cv::waitKey(WAIT_DELAY_MS)) {
cap >> cameraImage;
cv::flip(cameraImage, cameraImage, FLIPPING_AROUND_Y_AXIS);
if (!gameOver) {
playerPosition.update(cameraImage);
playerShot.update();
std::for_each(invaders.begin(), invaders.end(), GraphicUpdate);
std::for_each(shots.begin(), shots.end(), GraphicUpdate);
if (playerShot.isValid()) {
const InvaderList::iterator iInvaderEnd = std::remove_if(invaders.begin(),invaders.end(), GraphicColision(playerShot.rect()));
if (iInvaderEnd != invaders.end()) {
invaders.erase(iInvaderEnd, invaders.end());
playerShot = Shot();
}
}
if (!shots.empty()) {
const ShotList::iterator iShotsEnd = std::remove_if(shots.begin(), shots.end(), isInvalidShot);
if (iShotsEnd != shots.end()) {
shots.erase(iShotsEnd, shots.end());
}
}
for (InvaderList::const_iterator iInvader = invaders.begin(); iInvader != invaders.end() && !gameOver; ++iInvader) {
const cv::Rect irect( iInvader->rect() );
if ((rng.uniform(0.0,1.0) < 0.05) && (shots.size() < MAX_INVADERS)) {
cv::Point shotPos(irect.x + (irect.width / 2), irect.y + irect.height);
shots.push_back( Shot(shotPos, SHOT_SPEED, cv::Scalar(100,50,100), CameraImageSize) );
}
if (irect.y >= YGameOver) {
gameOver = true;
}
}
if (!playerShot.isValid() && player.isShooting()) {
cv::Point shotPoint( player.facePosition().x, YPlayerShot );
playerShot = Shot(shotPoint, -SHOT_SPEED, cv::Scalar(100,170,10), CameraImageSize);
}
for (ShotList::iterator iShot(shots.begin()); iShot != shots.end() && !gameOver; ++iShot) {
if (iShot->isValid() && checkColision(iShot->rect(),player.rect())) {
gameOver = true;
}
}
}
std::for_each(invaders.begin(), invaders.end(), GraphicPaint(cameraImage));
std::for_each(shots.begin(), shots.end(), GraphicPaint(cameraImage));
player.paint(cameraImage);
playerShot.paint(cameraImage);
if (invaders.empty()) {
cv::putText(cameraImage, "Winner!", cv::Point(30,80), cv::FONT_HERSHEY_SIMPLEX, 3, cv::Scalar::all(255), 2, 8);
} else if (gameOver) {
cv::putText(cameraImage, "Game Over!", cv::Point(30,80), cv::FONT_HERSHEY_SIMPLEX, 3, cv::Scalar::all(255), 2, 8);
}
cv::resize(cameraImage, cameraImage, CameraImageSize * 2);
cv::imshow("SpaceInvaders", cameraImage);
}
return 0;
}