#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#pragma comment(lib,"Winmm.lib")
#define waterspeed 40.0
#define buildingnum 30
#define rainnum 300
#define carspeed 10.0
#define TIME 30
struct water
{
double waterx;
double watery;
};
struct build
{
int howx;
int howy;
int RBGR;
int maxmin;
double wherex;
};
int a,b,c,d,e,t;
water rain[rainnum];
build building[buildingnum];
bool ExtractResource(LPCTSTR strDstFile, LPCTSTR strResType, LPCTSTR strResName)
{
// 创建文件
HANDLE hFile = ::CreateFile(strDstFile, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
// 查找资源文件中、加载资源到内存、得到资源大小
HRSRChRes= ::FindResource(NULL, strResName, strResType);
HGLOBALhMem= ::LoadResource(NULL, hRes);
DWORDdwSize= ::SizeofResource(NULL, hRes);
// 写入文件
DWORD dwWrite = 0; // 返回写入字节
::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL);
::CloseHandle(hFile);
return true;
}
void start()
{
initgraph(640, 480);
setbkcolor(RGB(50,50,50));
srand(time(NULL));
for(a = 0;a < rainnum;a++)
{
rain[a].waterx = rand() % int(640 + (carspeed / waterspeed)*480) - (carspeed / waterspeed)*480;
rain[a].watery = rand() % 481;
}
for(a = 0;a < buildingnum;a++)
{
building[a].howx = rand () % 61 + 40;
building[a].howy = rand () % 201 + 50;
building[a].RBGR = rand () % 81 + 70;
building[a].wherex = rand () % (641 + building[a].howx) - building[a].howx;
building[a].maxmin = rand () % 6+8;
}
}
void draw()
{
BeginBatchDraw();
cleardevice();
for(a = 0;a < buildingnum;a++)
{
setfillcolor(RGB(building[a].RBGR,building[a].RBGR,building[a].RBGR));
solidrectangle(int(building[a].wherex),480 - building[a].howy,int(building[a].wherex + building[a].howx),480);
if (int((building[a].howx-10)/20.0) != 0)
{
d = int(building[a].wherex) + 10;
for(b = 0;b < int((building[a].howx-building[a].maxmin)/(2.0*building[a].maxmin)) ;b++)
{
e = 490 - building[a].howy ;
for(c = 0;c < int((building[a].howy-building[a].maxmin)/(2.0*building[a].maxmin)) ;c++)
{
t = rand ()%11+180;
setfillcolor(RGB(t,t,t));
solidrectangle(d,e,d+building[a].maxmin,e+building[a].maxmin);
e =e+2*building[a].maxmin;
}
d =d+2*building[a].maxmin;
}
}
}
for(a = 0;a < rainnum;a++)
putpixel(int(rain[a].waterx), int(rain[a].watery), RGB(255,255,255));
EndBatchDraw();
}
void calution()
{
for(a = 0;a < buildingnum;a++)
{
building[a].wherex = building[a].wherex + carspeed / TIME;
if (building[a].wherex > 641)
{
building[a].howx = rand () % 61 + 40;
building[a].howy = rand () % 201 + 50;
building[a].RBGR = rand () % 51 + 100;
building[a].wherex = - building[a].howx;
}
}
for(a = 0;a < rainnum;a++)
{
rain[a].waterx = rain[a].waterx + carspeed / TIME;
rain[a].watery = rain[a].watery + waterspeed / TIME;
if (rain[a].waterx > 640 || rain[a].watery > 480)
{
rain[a].waterx = rand() % int(640 + (carspeed / waterspeed)*480) - (carspeed / waterspeed)*480;
rain[a].watery = 0 ;
}
}
}
int main()
{
start();
char s[300];
char tmpmp3[_MAX_PATH];
::GetTempPath(_MAX_PATH, tmpmp3);
strcat(tmpmp3, "playmp3_background.mp3");
ExtractResource(tmpmp3, _T("MP3"), _T("2.mp3"));
sprintf(s, "open \"%s\" alias mymusic", tmpmp3);
mciSendString(s, NULL, 0, NULL);
mciSendString(_T("play mymusic repeat"), NULL, 0, NULL);
while(1)
{
draw();
calution();
}
closegraph();
return 0;
}