-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathplayer.hpp
186 lines (181 loc) · 5.69 KB
/
player.hpp
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
#include "character.hpp"
int escpressed=0;
int pause=0;
int stop=0;//pause but dosent print pause
const int step = 4;
const double fireRate = 0.5;
const int bPadding = 2;
Point rBarCorner(20,575);
Point hBarCorner(480,575);
Size rBarSize(100,15);
class Player : public Character
{
private:
bool rKeyDown;
public:
Player()
{ score=0;
srand(time(NULL));
type = 0;
health = 100;
aim = rand()%360;
rKeyDown = false;
location = Point(rand()%img.cols,rand()%img.rows);
while(imgg.at<uchar>(location.y,location.x) >= 128)
location = Point(rand()%img.cols,rand()%img.rows);
draw();
}
bool setKeyState(bool state)
{
rKeyDown = state;
}
int keyInput(int keyCode)
{
if(keyCode == 27)
escpressed=1;
if(keyCode==112)
{ pause+=1;
pause%=2;
}
if(keyCode == 100&&!(pause||stop))
{
if(isValid(location.y + step*cos(aim*CV_PI/180),location.x - step*sin(aim*CV_PI/180)))
if(imgg.at<uchar>(location.y + step*cos(aim*CV_PI/180),location.x - step*sin(aim*CV_PI/180)) < 128)
{
location.x -= step*sin(aim*CV_PI/180);
location.y += step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y ,location.x - step*cos(aim*CV_PI/180)) < 128)
{
location.x -= step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y + step*sin(aim*CV_PI/180),location.x) < 128)
{
location.y += step*sin(aim*CV_PI/180);
draw();
}
}
else if((keyCode == 119 || rKeyDown)&&!(pause||stop))//w
{
if(isValid(location.y + step*sin(aim*CV_PI/180),location.x + step*cos(aim*CV_PI/180)))
if(imgg.at<uchar>(location.y + step*sin(aim*CV_PI/180),location.x + step*cos(aim*CV_PI/180)) < 128)
{
location.y += step*sin(aim*CV_PI/180);
location.x += step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y ,location.x + step*cos(aim*CV_PI/180)) < 128)
{
location.x += step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y + step*sin(aim*CV_PI/180),location.x) < 128)
{
location.y += step*sin(aim*CV_PI/180);
draw();
}
}
else if(keyCode == 97&&!(pause||stop))
{
if(isValid(location.y - step*cos(aim*CV_PI/180),location.x + step*sin(aim*CV_PI/180)))
if(imgg.at<uchar>(location.y - step*cos(aim*CV_PI/180),location.x + step*sin(aim*CV_PI/180)) < 128)
{
location.x += step*sin(aim*CV_PI/180);
location.y -= step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y ,location.x + step*cos(aim*CV_PI/180)) < 128)
{
location.x += step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y - step*sin(aim*CV_PI/180),location.x) < 128)
{
location.y -= step*sin(aim*CV_PI/180);
draw();
}
}
else if(keyCode == 115&&!(pause||stop))
{
if(isValid(location.y - step*sin(aim*CV_PI/180),location.x - step*cos(aim*CV_PI/180)))
if(imgg.at<uchar>(location.y - step*sin(aim*CV_PI/180),location.x - step*cos(aim*CV_PI/180)) < 128)
{
location.y -= step*sin(aim*CV_PI/180);
location.x -= step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y ,location.x - step*cos(aim*CV_PI/180)) < 128)
{
location.x -= step*cos(aim*CV_PI/180);
draw();
}
else if(imgg.at<uchar>(location.y - step*sin(aim*CV_PI/180),location.x) < 128)
{
location.y -= step*sin(aim*CV_PI/180);
draw();
}
}
return 1;
}
void think()
{
return;
}
int checkLife()
{
return health > 0 ? 1:2;
}
void givepoints()
{
score+=15;
}
void healthboost()
{
health=100;
}
int gethealth()
{
return health;
}
int getscore()
{
return score;
}
friend void drawHealthRect(Player* p);
};
double timeSinceFire = 0.0;
void drawRechargeRect(double timeSinceFire)
{
// Border
if(timeSinceFire < 0.5)
rectangle(img, Point(rBarCorner.x - bPadding, rBarCorner.y - bPadding), Point(rBarCorner.x + rBarSize.width + bPadding, rBarCorner.y + rBarSize.height + bPadding), Scalar(0, 0,255), 1);
else
rectangle(img, Point(rBarCorner.x - bPadding, rBarCorner.y - bPadding), Point(rBarCorner.x + rBarSize.width + bPadding, rBarCorner.y + rBarSize.height + bPadding), Scalar(255,255,255), 1);
cvtColor(img, img,CV_BGR2HSV);
// Change hue from 360 deg to 240 deg
rectangle(img, rBarCorner, Point(rBarCorner.x + rBarSize.width*min(1.0,(double)timeSinceFire/fireRate), rBarCorner.y + rBarSize.height), Scalar(180 - min(1.0,(double)timeSinceFire/fireRate)*60,255,255), CV_FILLED);
// cvtColor(img, img,CV_HSV2BGR); // Would be called in drawHealthRect()
// imshow("Game",img); // Would be called in drawHealthRect()
}
void drawHealthRect(Player *p, double timeSinceDamage)
{
// Border
if(timeSinceDamage <= 0.5)
rectangle(img, Point(hBarCorner.x - bPadding, hBarCorner.y - bPadding), Point(hBarCorner.x + rBarSize.width + bPadding, hBarCorner.y + rBarSize.height + bPadding), Scalar(0, 255, 255), 1);
else
rectangle(img, Point(hBarCorner.x - bPadding, hBarCorner.y - bPadding), Point(hBarCorner.x + rBarSize.width + bPadding, hBarCorner.y + rBarSize.height + bPadding), Scalar(0, 0, 255), 1);
// Change hue from 0 deg to 120 deg
rectangle(img, hBarCorner, Point(hBarCorner.x + rBarSize.width*min(1.0,(double)(p->gethealth())/100), rBarCorner.y + rBarSize.height), Scalar(min(1.0,(double)(p->gethealth())/100)*60,255,255), CV_FILLED);
cvtColor(img, img,CV_HSV2BGR);
imshow("Game",img);
}
void printscore(Player *p)
{
ostringstream a;
a << p->getscore();
string s = a.str();
s = "SCORE: " + s;
putText(img,s,Point(5,20),FONT_HERSHEY_COMPLEX_SMALL,0.8,Scalar(0,0,255),1,CV_AA);
}