#include
#include
#include
#include
#include
#include
int bird_x,bird_y;
int barrier_x,barrier_y,barrier_blank;
int Width,High;
int score=0;
IMAGE img_bk,img_bd1,img_bd2,img_bdown1,img_bdown2,img_bup1,img_bup2;
void Gameover();
void startup();
void updateWithInput();
void updateWithoutInput();
void startup()
{
High = 487;
Width = 720;
bird_y = High / 2;
bird_x = Width / 4;
barrier_x = 700;
barrier_blank = 100;
barrier_y = 300;
loadimage(&img_bd1,"D:\\resources1\\bird1.jpg");
loadimage(&img_bd2,"D:\\resources1\\bird2.jpg");
loadimage(&img_bk,"D:\\resources1\\background.jpg");
loadimage(&img_bdown1,"D:\\resources1\\bar_down1.gif");
loadimage(&img_bdown2,"D:\\resources1\\bar_down2.gif");
loadimage(&img_bup1,"D:\\resources1\\bar_up1.gif");
loadimage(&img_bup2,"D:\\resources1\\bar_up2.gif");
initgraph(Width,High);
BeginBatchDraw();
}
void show()
{
char score_char[50]="你的得分:";
char score_c[5];
itoa(score,score_c,10);
strcat(score_char,score_c);
putimage(0,0,&img_bk);
putimage(bird_x,bird_y,&img_bd1,NOTSRCERASE);
putimage(bird_x,bird_y,&img_bd2,SRCINVERT);
putimage(barrier_x,barrier_y,&img_bdown1,NOTSRCERASE);
putimage(barrier_x,barrier_y,&img_bdown2,SRCINVERT);
putimage(barrier_x,barrier_y-barrier_blank-600,&img_bup1,NOTSRCERASE);
putimage(barrier_x,barrier_y-barrier_blank-600,&img_bup2,SRCINVERT);
outtextxy(Width/2,0,score_char);
FlushBatchDraw();
Sleep(5);
}
void updateWithoutInput()
{
bird_y ++;
barrier_x--;
srand((unsigned int)time(NULL));
if (barrier_x<=0)
{
barrier_x=720;
barrier_y=rand()%250+150;
}
if ((bird_x>=barrier_x) &&(bird_x<=barrier_x+140)&&(bird_y>=barrier_y))
Gameover();
if ((bird_x>=barrier_x) &&(bird_x<=barrier_x+140)&&(bird_y<=barrier_y-barrier_blank))
Gameover();
if(bird_y>=High)
Gameover();
if (bird_x==barrier_x)
score++;
}
void updateWithInput()
{
MOUSEMSG m;
while(MouseHit())
{
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONUP)
{
bird_y-=40;
}
FlushMouseMsgBuffer();
}
}
void Gameover()
{
closegraph();
initgraph(Width,High);
BeginBatchDraw();
char s[] = "Game over!!!Made by 叶田";
settextstyle(30,0,_T("宋体"));
outtextxy(Width/2-150,High/2,s);
FlushBatchDraw();
EndBatchDraw();
getch();
closegraph();
}
int main()
{
startup();
while (1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}