//自定义一个结构
struct dragon
{
int x, y;//坐标
int dir;//方向
};
//常量
const int draNum = 10;
//全局变量
HBITMAP draPic[4], bg;
HDC hdc, mdc, bufdc;
HWND hWnd;
DWORD tPre, tNow;
int picNum;
dragon dra[draNum];

 

 

 

//这个是在主函数中添加的

//游戏循环
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
tNow = GetTickCount();
if (tNow - tPre >= 100)//相差0.1秒时再进行重绘操作
MyPaint(hdc);
}
}

 


//--------------------------------------------InitInstance() 函数----------------------------------------------
//加载资源
//-------------------------------------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
char filename[20] = "";
HBITMAP bmp;

//创建窗口第三步:正式创建窗口
//创建窗口函数
hWnd = CreateWindow("GameClass", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
//创建窗口第四步:窗口的移动,显示和更新
MoveWindow(hWnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);//调整窗口显示时的位置及窗口的大小
ShowWindow(hWnd, nShowCmd);//设定显示窗口时的状态
UpdateWindow(hWnd);//将窗口绘制于显示设备上

hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);

bmp = CreateCompatibleBitmap(hdc, 640, 480);
SelectObject(mdc, bmp);

draPic[0] = (HBITMAP)LoadImage(NULL, "dra0.bmp", IMAGE_BITMAP, 528, 188, LR_LOADFROMFILE);
draPic[1] = (HBITMAP)LoadImage(NULL, "dra1.bmp", IMAGE_BITMAP, 544, 164, LR_LOADFROMFILE);
draPic[2] = (HBITMAP)LoadImage(NULL, "dra2.bmp", IMAGE_BITMAP, 760, 198, LR_LOADFROMFILE);
draPic[3] = (HBITMAP)LoadImage(NULL, "dra3.bmp", IMAGE_BITMAP, 760, 198, LR_LOADFROMFILE);
bg = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);

for (int i = 0; i < draNum; i++)
{
dra[i].dir = 3; //起始方向
dra[i].x = 200; //贴图起始x坐标
dra[i].y = 200; //贴图起始y坐标
}

MyPaint(hdc);
return TRUE;
}
//气泡排序法
void BubSort(int n)
{
int i, j;
bool f;
dragon tmp;

for (i = 0; i < n - 1; i++)
{
f = false;
for (j = 0; j < n - i - 1; j++)
{
if (dra[j + 1].y < dra[j].y)
{
//进行数组元素交换
tmp = dra[j + 1];
dra[j + 1] = dra[j];
dra[j] = tmp;
f = true;
}
}
if (!f)
break;
}
}
//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//1.对窗口中跑动的恐龙进行排序贴图
//2.恐龙贴图坐标修正
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
int w, h, i;

if (picNum == 8)
picNum = 0;

//在mdc中先贴上背景图
SelectObject(bufdc, bg);
BitBlt(mdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);

//恐龙图片排序
BubSort(draNum);

for (i = 0; i < draNum; i++)
{
SelectObject(bufdc, draPic[dra[i].dir]);
switch (dra[i].dir)
{
case 0:
w = 66;
h = 94;
break;
case 1:
w = 68;
h = 82;
break;
case 2:
w = 95;
h = 99;
break;
case 3:
w = 95;
h = 99;
break;
}
//进行透明处理
BitBlt(mdc, dra[i].x, dra[i].y, w, h, bufdc, picNum*w, h, SRCAND);
BitBlt(mdc, dra[i].x, dra[i].y, w, h, bufdc, picNum*w, 0, SRCPAINT);
}

//最后将画面显示在窗口中
BitBlt(hdc, 0, 0, 640, 480, mdc, 0, 0, SRCCOPY);

tPre = GetTickCount(); //记录此次绘图时间
picNum++;

for (i = 0; i < draNum; i++)
{
switch (rand() % 4) //随机决定下次移动方向
{
case 0: //上
switch (dra[i].dir)
{
case 0:
dra[i].y -= 20;
break;
case 1:
dra[i].x += 2;
dra[i].y -= 31;
break;
case 2:
dra[i].x += 14;
dra[i].y -= 20;
break;
case 3:
dra[i].x += 14;
dra[i].y -= 20;
break;
}
if (dra[i].y < 0)
dra[i].y = 0;
dra[i].dir = 0;
break;
case 1: //下
switch (dra[i].dir)
{
case 0:
dra[i].x -= 2;
dra[i].y += 31;
break;
case 1:
dra[i].y += 20;
break;
case 2:
dra[i].x += 15;
dra[i].y += 29;
break;
case 3:
dra[i].x += 15;
dra[i].y += 29;
break;
}

if (dra[i].y > 370)
dra[i].y = 370;
dra[i].dir = 1;
break;
case 2: //左
switch (dra[i].dir)
{
case 0:
dra[i].x -= 34;
break;
case 1:
dra[i].x -= 34;
dra[i].y -= 9;
break;
case 2:
dra[i].x -= 20;
break;
case 3:
dra[i].x -= 20;
break;
}
if (dra[i].x < 0)
dra[i].x = 0;
dra[i].dir = 2;
break;
case 3: //右
switch (dra[i].dir)
{
case 0:
dra[i].x += 6;
break;
case 1:
dra[i].x += 6;
dra[i].y -= 10;
break;
case 2:
dra[i].x += 20;
break;
case 3:
dra[i].x += 20;
break;
}
if (dra[i].x > 535)
dra[i].x = 535;
dra[i].dir = 3;
break;
}
}
}


//--------------------------------------------Game_CleanUp() 函数----------------------------------------------
//资源清理函数,在此函数中进行退出前的资源的清理工作
//-------------------------------------------------------------------------------------------------------------
BOOL Game_CleanUp()
{
DeleteDC(mdc);
DeleteDC(bufdc);
for (int i = 0; i < 4; i++)
{
DeleteObject(draPic[i]);
}
DeleteObject(bg);
ReleaseDC(hWnd, hdc);
return TRUE;
}

 

如需整体运行框架,请点击: 游戏框架

游戏开发的梦想

梦开始的地方

 

posted on 2015-11-22 23:40  zishen  阅读(289)  评论(0编辑  收藏  举报