-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
207 lines (186 loc) · 5.35 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <opencv/highgui.h>
#include <conio.h>
#include <time.h>
IplImage * imgCanvas=NULL;
IplImage * imgstart=NULL;
IplImage * imgKid=NULL;
IplImage * imgOver=NULL;
IplImage * imglife=NULL;
IplImage * imgupteeth=NULL;
IplImage * imgdownteeth=NULL;
IplImage * imgboard=NULL;
IplImage * imgleftboard=NULL;
IplImage * imgrightboard=NULL;
int kidX=100,kidY=50;
int boardX[14]={0}, boardY[14]={390}, boardv=3;
bool onboard=false;
int Gameover(IplImage * Over)
{
cvShowImage("Game", Over);
cvWaitKey(0);
Sleep(1000);
return 0;
}
int Move(IplImage * imgCanvas, IplImage * imgKid)
{
int key=cvWaitKey(30);
if(key=='a' && kidX-10>10)
kidX-=15;
else if(key=='a' && kidX-10<=10)
{
kidX=11;
}
if(key=='d' && kidX+10+imgKid->width < (imgCanvas->width-10))
kidX+=15;
else if(key=='d' && kidX+10+imgKid->width >= imgCanvas->width)
{
kidX=270;
}
return 1;
}
void myPutImage(IplImage * canvas, IplImage *s,int x,int y)
{
cvSetImageROI(canvas, cvRect(x,y,s->width, s->height));
cvCopy(s,canvas);
cvResetImageROI(canvas);
}
int main()
{
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1.0f,1.0f,0,1,CV_AA);
srand(time(NULL));
imgCanvas = cvCreateImage(cvSize(300,400),IPL_DEPTH_8U,3);
imgstart = cvLoadImage("start.png");
imgKid = cvLoadImage("kid.png");
imgOver = cvLoadImage("gameover.png");
imglife = cvLoadImage("life.png");
imgupteeth = cvLoadImage("teeth2.png");
imgdownteeth = cvLoadImage("teeth.png");
imgleftboard = cvLoadImage("leftboard.png");
imgrightboard = cvLoadImage("rightboard.png");
imgboard = cvLoadImage("board.png");
int life=10,times=1,boardnum=1,hurt=0,time=0,run=0;
char word[5];
for(int i=0; i<10; i++){
boardX[i]=rand()%263;
}
cvShowImage("Game", imgstart);
cvWaitKey(0);
Sleep(500);
while(1)
{
cvSetZero(imgCanvas);
///清理畫布
time=run/8;
cvPutText(imgCanvas,"Time: ",cvPoint(210,27),&font,CV_RGB(0,255,255));
cvPutText(imgCanvas,itoa(time,word,10),cvPoint(260,27),&font,CV_RGB(0,255,255));
Sleep(6);
run+=1;
myPutImage(imgCanvas, imgKid, kidX, kidY);
for(int i=0 ; i<life ; i++)
{
myPutImage(imgCanvas, imglife,10+i*(imglife->width),15);
}
for(int i=0 ; i<10 ; i++)
{
myPutImage(imgCanvas, imgupteeth, i*(imgupteeth->width), 0);
}
if(times%50==0)
{
if(boardnum<13)
boardnum+=1;
}
for(int i=0; i<boardnum; i++)
{
if(i%2==0)
myPutImage(imgCanvas, imgboard, boardX[i], boardY[i]);
else
myPutImage(imgCanvas, imgdownteeth, boardX[i], boardY[i]);
}
for(int i=0;i<8;i++)
{
myPutImage(imgCanvas, imgleftboard, 0, i*(imgleftboard->height ));
}
for(int i=0;i<8;i++)
{
myPutImage(imgCanvas, imgrightboard, 290, i*(imgrightboard->height ));
}
cvShowImage("Game", imgCanvas);
///以上載入圖片
if(Move(imgCanvas, imgKid)==0)
return 0;
///移動
for(int i=0; i<boardnum; i++)
{
if((kidY + imgKid->height == boardY[i]) || ((boardY[i] <= (kidY + imgKid->height+4)) && ( (kidY + imgKid->height + 4) <= boardY[i]+boardv ) ) )
{
if(kidX >= boardX[i] + imgboard->width || kidX + imgKid->width <= boardX[i])
{
onboard=false;
hurt=0;
}
else if(i%2==0)
{
onboard=true;
}
else
{
if(hurt==0){
life-=1;
hurt=1;
}
if(life==0)
{
if(Gameover(imgOver)==0)
return 0;
}
onboard=true;
}
}
if(boardY[i]-boardv <= 20 )
{
boardY[i]=390;
boardX[i]=rand()%263;
}
else{
boardY[i]-=boardv;
}
}
///看人物有沒有站在任何一塊板子上
if(onboard==false){
kidY+=3;
if(kidY+imgKid->height >= imgCanvas->height)
{
if(Gameover(imgOver)==0)
return 0;
}
}
else if(onboard==true){
kidY-=boardv;
if(life>0 && kidY<=20)
{
life-=1;
kidY=100;
onboard=false;
hurt=0;
kidY+=1;
if(life==0)
{
if(Gameover(imgOver)==0)
return 0;
}
}
}
times+=1;
if(times%500==0 && boardv<=12){
boardv+=1;
}
if(times%750==0 && boardnum>3){
boardnum-=1;
}
}
return 0;
}