forked from arnav-t/Shooting-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (101 loc) · 2.62 KB
/
main.cpp
File metadata and controls
115 lines (101 loc) · 2.62 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
#include <iostream>
#include "ai.hpp"
Player *p;
clock_t prevFire;
clock_t start;
void upImg(int event, int x, int y, int flags, void* a)
{
if(event == EVENT_MOUSEMOVE)
{
Point pLoc = p->getLocation();
float angle = atan2(y - pLoc.y, x - pLoc.x);
p->setAim(angle);
}
else if(event == EVENT_RBUTTONDOWN)
p->setKeyState(true);
else if(event == EVENT_RBUTTONUP)
p->setKeyState(false);
else if(event == EVENT_LBUTTONDOWN && timeSinceFire >= fireRate)
{
prevFire = clock();
p->shoot();
}
}
double checkDelay(clock_t *start_ref)
{
clock_t now = clock();
double delay = double(now- (*start_ref))/CLOCKS_PER_SEC;
delay *= 1000;
(*start_ref) = now;
if(delay >= 16)
return 1;
else
return (ceil((double)16 - delay));
}
int main()
{
p = new Player;
int dead=0;
activeChars.push_back(p);
start = clock();
for(int i=0; i<aiPlayers; ++i)
activeChars.push_back(new AI);
namedWindow("Game",CV_WINDOW_AUTOSIZE);
imshow("Game",img);
setMouseCallback("Game", upImg, NULL);
prevFire = clock();
double delay = 1;
Mat trans = img;
trans.setTo(Scalar(0, 0, 0));
while(p->keyInput(waitKey((int) delay)) && !escpressed)
{ if(pause==1)
{putText(img, "pause", Point(150, 300), FONT_HERSHEY_SIMPLEX, 3, Scalar(255, 0, 0), 10, 2);
imshow("Game", img);
continue;
}
img = imread(IMAGE,1);
printscore(p);
timeSinceFire = (dead==1)?fireRate:(double)(clock() - prevFire)/CLOCKS_PER_SEC;
double alpha = 0.5;
double beta = (double)1 - alpha;
if(dead == 1)
{
addWeighted(img, alpha, trans, beta, 0.0, img);
putText(img, "Game Over", Point(40, 250), FONT_HERSHEY_SIMPLEX, 3, Scalar(0, 0, 255), 10, 2);
putText(img, "You Lose", Point(90, 350), FONT_HERSHEY_SIMPLEX, 3, Scalar(0, 0, 255), 10, 2);
}
for(int i=activeChars.size()-1;i>=0;--i)
{
if(activeChars[i]->checkLife() == 0)
{
Character *tempChar = activeChars[i];
activeChars[i] = activeChars.back();
activeChars.pop_back();
delete tempChar;
}
else if(activeChars[i]->checkLife() == 2)
{
activeChars[i] = activeChars.back();
activeChars.pop_back();
dead=1;
}
else
{
activeChars[i]->think();
activeChars[i]->draw();
activeChars[i]->updateProjectiles(activeChars,i);
}
}
drawRechargeRect();
drawHealthRect(p);
if(dead == 0 && activeChars.size() == 1)
{
addWeighted(img, alpha, trans, beta, 0.0, img);
putText(img, "Game Over", Point(40, 250), FONT_HERSHEY_SIMPLEX, 3, Scalar(0, 255, 0), 10, 2);
putText(img, "You Win", Point(100, 350), FONT_HERSHEY_SIMPLEX, 3, Scalar(0, 255, 0), 10, 2);
}
imshow("Game", img);
delay = checkDelay(&start);
}
return 0;
}