#include"game.h"
IMAGE background;
IMAGE OVER_BACK[2];
IMAGE bird[2];
IMAGE POLL[3];
IMAGE down[2];
IMAGE UP[2];
struct poll
{
int x;
int y;
int height;
int speed;
int w, h;
};
//对柱子的结构体进行初始化
//试图让整个地图动起来
//初步的想法是
//1.想让地图的横坐标想左移动通过反复的粘贴函数(到0的时候要回去)
//2.
struct bake_ground
{
int x;
int y;
int speed;
};
struct endimage
{
int x;
int y;
int speed;
};
struct endimage flyend = { 320,700,50 };
struct bird
{
int x, y;
int speed;
};
struct bird flybird = { 250,250,50 };
//创建柱子变量
struct poll allocpoll[5];
//初始话柱子的参数
int initpoll(struct poll allocpoll[],int i)
{
allocpoll[i].height = rand() % 150 + 200;
allocpoll[i].x = 1024;
allocpoll[i].y = 0;
allocpoll[i].w = 100;
allocpoll[i].h = 550;
return 0;
}
//用户按键函数
int keyboard()
{
char userkey = _getch();
switch (userkey)
{
case ' ':
flybird.y -= flybird.speed;
CreateThread(NULL, NULL, playMusic,NULL, NULL, NULL);//多线程搞清楚
break;
default:
break;
}
return 0;
}
//多线程音乐音乐导入
DWORD WINAPI playMusic(LPVOID lpvoid)
{
mciSendString("open 11.mp3",0,0,0);
mciSendString("play 11.mp3 wait", 0, 0, 0);
mciSendString("close 11.mp3 ", 0, 0, 0);
return 0;
}
//资源导入
int Loadimage()
{
loadimage(&background, "111.png", 1024, 768);
loadimage(&bird[0], "bird.png", 60, 60);
loadimage(&bird[1], "birdy.png", 60, 60);
loadimage(&OVER_BACK[0], "end.png", 400, 400);
loadimage(&OVER_BACK[1], "endy.png", 400, 400);
loadimage(&OVER_BACK[0], "end.png", 400, 400);
loadimage(&OVER_BACK[1], "endy.png", 400, 400);
loadimage(&down[0], "poll.png");
loadimage(&down[1], "polly.png");
loadimage(&UP[0], "poll.png");
loadimage(&UP[1], "polly.png");
return 0;
}
//绘制结束图像
int Drawend()
{
putimage(flyend.x, flyend.y, &OVER_BACK[1], SRCAND);//end的贴图
putimage(flyend.x, flyend.y, &OVER_BACK[0], SRCPAINT);
return 0;
}
//绘制鸟函数
int Drawbird()
{
putimage(flybird.x, flybird.y, &bird[1],SRCAND);//鸟的贴图
putimage(flybird.x, flybird.y, &bird[0],SRCPAINT);
return 0;
}
//绘制柱子函数
int Drawpoll(struct poll allocpoll[],int i)
{
putimage(allocpoll[i].x, 0, allocpoll[i].w, allocpoll[i].h, &UP[1], 0, allocpoll[i].h - allocpoll[i].height, SRCAND);
putimage(allocpoll[i].x, 0, allocpoll[i].w, allocpoll[i].h, &UP[0], 0, allocpoll[i].h -allocpoll[i].height, SRCPAINT);
putimage(allocpoll[i].x, 768 - (allocpoll[i].h - allocpoll[i].height), allocpoll[i].w, allocpoll[i].h, &down[1], 0, 0, SRCAND);
putimage(allocpoll[i].x, 768 - (allocpoll[i].h - allocpoll[i].height), allocpoll[i].w, allocpoll[i].h, &down[0], 0, 0, SRCPAINT);
return 0;
}
//游戏结束函数
int gameover()
{
Loadimage();
int cnt = 1;
//settextstyle(60, 0, "黑体");//settextstyle(字体大小,0-默认,字体)
//outtextxy(380, 100, "GAME OVER");
while (cnt)
{
putimage(0, 0, &background);
Drawend();
flyend.y -= 20;
if (flyend.y <= 100)
cnt = 0;
Sleep(30);
}
_getch();//防止闪屏
return 0;
}
//碰撞柱子的情况
int BANG(int bird_x, int bird_y, int poll_x)
{
int i,number=0;
for (i = 0; i < allocpoll[1].height; i++)
{
if (poll_x <= bird_x && i == bird_y)
{
gameover();
number = 1;
break;
}
}
for (i = poll_x; i <= poll_x + allocpoll[1].w; i++)
{
if (bird_y <= allocpoll[1].height && i == bird_x)
{
gameover();
number = 1;
break;
}
}
for (i = 768-allocpoll[1].height; i < 768; i++)
{
if (poll_x <=bird_x && i == bird_y)
{
gameover();
number = 1;
break;
}
}
for (i = poll_x; i < poll_x+ allocpoll[1].w; i++)
{
if (bird_y >= 768-allocpoll[1].height && i == bird_x)
{
gameover();
number = 1;
break;
}
}
return number;
}
int main()
{
int i;
srand((unsigned int)time(NULL));//随机种子
initgraph(1024, 768);
Loadimage();
for (i = 0; i < 4; i++)//给每个柱子初始化
{
initpoll(allocpoll, i);
allocpoll[i].x += 308 * i;
}
while (1)
{
putimage(0, 0, &background);//刷新的功能---不然的话小鸟会连续贴在画布上
Drawbird();
for (i = 0; i < 4; i++)
{
Drawpoll(allocpoll, i);
if (allocpoll[i].x < (-100 - 156))
{
initpoll(allocpoll, i);
}
}
for (i = 0; i < 4; i++)
{
allocpoll[i].x -= 10;
if (BANG(flybird.x, flybird.y, allocpoll[i].x))
gameover();
}
if (flybird.y >= 500 || flybird.y <= 0)
{
gameover();
break;
}
flybird.y += 10;
if (_kbhit()) //有按键的情况才会返回一个非0的值
{
keyboard();
}
Sleep(50);
}
gameover();
system("pause");
return 0;
}
//putimage(0, 0, 150, 600, &UP[1], 0, 300, SRCAND);
//putimage(0, 0, 150, 600, &UP[0], 0, 300, SRCPAINT);
//putimage(0, 768 - (600 - 300), 150, 600, &down[1], 0, 0, SRCAND);
//putimage(0, 768 - (600 - 300), 150, 600, &down[0], 0, 0, SRCPAINT);