http://blog.csdn.net/column/details/vc-game-programming.html?page=2
我也是跟着个人学的SDK图形图像,想学游戏开发的同学可以先看一下这个教程,下面是代码
最重要的函数就是BitBlt贴图函数
还有这个程序需要的图片和声音资源 下载地址(资源显示有延迟稍后加上)
#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <windowsx.h> #include <mmsystem.h> #pragma comment(lib, "WINMM.LIB") #include <time.h> //全局变量声明 HINSTANCE hInst; HBITMAP bmp,bmp1,people,bg,peoplebg,mboom,mboombg,mbooming,mboomingbg,mobj1,mobj1bg, mobj2,mobj2bg,mground,mgroundbg,bg2,goods,goodsbg,mrobot,mrobotbg; HDC hdc,mdc,bufdc,bufdc1,bufdc2,bufdc3,mdc1; HWND hWnd; DWORD tPre,tNow; RECT rect; int num,dir,x,y,keyd,moveloop,boomnum,mx=0,my=15; struct Peo { int x,y; int moveloop; int num; int power; int boomlimit; int boomn; int speed; int dir; int dead; }player; struct Rob { int x,y; int moveloop; int num; int power; int boomlimit; int boomn; int speed; int dir; int premove; int move; int planstep; int stepnow; int plan[900]; int plan1[900]; int dead; }robot; struct Bo { int x,y,time,num,power; }boom[1000]; struct Map { int ground; int obj; int goods; int moveBox; int distory; int boom; int booming; int boomingdir; }map[35][35]; //全局函数声明 ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc); //****WinMain函数,程序入口点函数*********************** int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; MyRegisterClass(hInstance); //初始化 if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } GetMessage(&msg,NULL,NULL,NULL); //初始化msg //消息循环 while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { tNow = GetTickCount(); if(tNow-tPre >= 40) MyPaint(hdc); } } return msg.wParam; } //****设计一个窗口类,类似填空题,使用窗口结构体******************* ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "canvas"; wcex.hIconSm = NULL; return RegisterClassEx(&wcex); } //****初始化函数************************************* // 加载位图并设定各种初始值 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; hWnd = CreateWindow("canvas", "泡泡堂" , WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } //MoveWindow(hWnd,10,10,1024,768,true); ShowWindow(hWnd, SW_SHOWMAXIMIZED); UpdateWindow(hWnd); hdc = GetDC(hWnd); mdc = CreateCompatibleDC(hdc); mdc1 = CreateCompatibleDC(hdc); bufdc = CreateCompatibleDC(hdc); bufdc1 =CreateCompatibleDC(hdc); //建立空的位图并置入mdc中 GetClientRect(hWnd,&rect); bmp = CreateCompatibleBitmap(hdc,rect.right,rect.bottom);//?为什么是hdc SelectObject(mdc,bmp); bmp1 = CreateCompatibleBitmap(hdc,rect.right,rect.bottom); SelectObject(mdc1,bmp1); //设定人物贴图初始位置和移动方向 x = 120; y = 80; robot.x=840; robot.y=420; robot.premove=-1; robot.move=1; robot.dir=3; dir = 1; num = 0; moveloop=0; keyd=0; boomnum=0; robot.boomn=player.boomn=0; robot.boomlimit=player.boomlimit=1; robot.power=player.power=1; robot.speed=player.speed=0; robot.dead=player.dead=0; memset(boom,0,sizeof(boom)); memset(map,0,sizeof(map)); freopen("map.txt","r",stdin); int i,j,k; for(j=0;j<15;j++) { for(i=0;i<25;i++) { scanf("%d %d",&map[i][j].ground,&map[i][j].obj); if(map[i][j].obj/10==1) { k=rand()%7; if(k==0) map[i][j].goods=1; else if(k==1) map[i][j].goods=7; else if(k==2) map[i][j].goods=9; else map[i][j].goods=0; } /*k=rand()%2; while(k==0) k=rand()%3; if(k==1) map[i][j].obj=10+rand()%3; else map[i][j].obj=20+rand()%4;*/ } } /*map[5][5].obj=10; map[5][6].obj=23; map[0][0].obj=0;*/ //载入各连续移动位图及背景图 people = (HBITMAP)LoadImage(NULL,"people.bmp",IMAGE_BITMAP,192,228,LR_LOADFROMFILE); peoplebg = (HBITMAP)LoadImage(NULL,"peoplebg.bmp",IMAGE_BITMAP,192,228,LR_LOADFROMFILE); mrobot = (HBITMAP)LoadImage(NULL,"robot.bmp",IMAGE_BITMAP,192,224,LR_LOADFROMFILE); mrobotbg = (HBITMAP)LoadImage(NULL,"robotbg.bmp",IMAGE_BITMAP,192,224,LR_LOADFROMFILE); bg = (HBITMAP)LoadImage(NULL,"bg1.bmp",IMAGE_BITMAP,rect.right,rect.bottom,LR_LOADFROMFILE); mboom = (HBITMAP)LoadImage(NULL,"boom.bmp",IMAGE_BITMAP,180,45,LR_LOADFROMFILE); mboombg = (HBITMAP)LoadImage(NULL,"boombg.bmp",IMAGE_BITMAP,180,45,LR_LOADFROMFILE); mbooming = (HBITMAP)LoadImage(NULL,"booming.bmp",IMAGE_BITMAP,160,80,LR_LOADFROMFILE); mboomingbg = (HBITMAP)LoadImage(NULL,"boomingbg.bmp",IMAGE_BITMAP,160,80,LR_LOADFROMFILE); mobj1 = (HBITMAP)LoadImage(NULL,"obj1.bmp",IMAGE_BITMAP,120,44,LR_LOADFROMFILE); mobj1bg = (HBITMAP)LoadImage(NULL,"obj1bg.bmp",IMAGE_BITMAP,40,44,LR_LOADFROMFILE); mobj2 = (HBITMAP)LoadImage(NULL,"obj2.bmp",IMAGE_BITMAP,200,56,LR_LOADFROMFILE); mobj2bg = (HBITMAP)LoadImage(NULL,"obj2bg.bmp",IMAGE_BITMAP,200,56,LR_LOADFROMFILE); mground = (HBITMAP)LoadImage(NULL,"ground.bmp",IMAGE_BITMAP,200,40,LR_LOADFROMFILE); mgroundbg = (HBITMAP)LoadImage(NULL,"groundbg.bmp",IMAGE_BITMAP,40,40,LR_LOADFROMFILE); bg2 = (HBITMAP)LoadImage(NULL,"obj1bg.bmp",IMAGE_BITMAP,rect.right,rect.bottom,LR_LOADFROMFILE); goods = (HBITMAP)LoadImage(NULL,"goods.bmp",IMAGE_BITMAP,400,45,LR_LOADFROMFILE); goodsbg = (HBITMAP)LoadImage(NULL,"goodsbg.bmp",IMAGE_BITMAP,400,45,LR_LOADFROMFILE); MyPaint(hdc); return TRUE; } //****自定义绘图函数********************************* // 人物贴图坐标修正及窗口贴图 void Booming(HDC hdc,Bo &b) { int dir1=1,dir2=1,dir3=1,dir4=1; int i,k; SelectObject(bufdc,mbooming); SelectObject(bufdc1,mboomingbg); BitBlt(mdc,b.x+20,b.y+20,40,40,bufdc1,0,0,SRCAND); BitBlt(mdc,b.x+20,b.y+20,40,40,bufdc,0,0,SRCPAINT); k=(11-b.time)*2; if(k>b.power) k=b.power; for(i=1;i<=k;i++) { if(b.y-i*40>=0&&dir1) { if(map[b.x/40][(b.y-i*40)/40].obj||map[b.x/40][(b.y-i*40)/40].boom) { dir1=0; if(b.time==0) map[b.x/40][(b.y-i*40)/40].obj=0; if(map[b.x/40][(b.y-i*40)/40].boom) { if(boom[map[b.x/40][(b.y-i*40)/40].boom-1].time>10) boom[map[b.x/40][(b.y-i*40)/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20,b.y+20-i*40,40,40,bufdc1,40,0,SRCAND); BitBlt(mdc,b.x+20,b.y+20-i*40,40,40,bufdc,40,0,SRCPAINT); } } if(b.y+i*40<=600-40&&dir2) { if(map[b.x/40][(b.y+i*40)/40].obj||map[b.x/40][(b.y+i*40)/40].boom) { dir2=0; if(b.time==0) map[b.x/40][(b.y+i*40)/40].obj=0; if(map[b.x/40][(b.y+i*40)/40].boom) { if(boom[map[b.x/40][(b.y+i*40)/40].boom-1].time>10) boom[map[b.x/40][(b.y+i*40)/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20,b.y+20+i*40,40,40,bufdc1,40,0,SRCAND); BitBlt(mdc,b.x+20,b.y+20+i*40,40,40,bufdc,40,0,SRCPAINT); } } if(b.x-i*40>=0&&dir3) { if(map[(b.x-i*40)/40][b.y/40].obj||map[(b.x-i*40)/40][b.y/40].boom) { dir3=0; if(b.time==0) map[(b.x-i*40)/40][b.y/40].obj=0; if(map[(b.x-i*40)/40][b.y/40].boom) { if(boom[map[(b.x-i*40)/40][b.y/40].boom-1].time>10) boom[map[(b.x-i*40)/40][b.y/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20-i*40,b.y+20,40,40,bufdc1,80,0,SRCAND); BitBlt(mdc,b.x+20-i*40,b.y+20,40,40,bufdc,80,0,SRCPAINT); } } if(b.x+i*40<=800-40&&dir4) { if(map[(b.x+i*40)/40][b.y/40].obj||map[(b.x+i*40)/40][b.y/40].boom) { dir4=0; if(b.time==0) map[(b.x+i*40)/40][b.y/40].obj=0; if(map[(b.x+i*40)/40][b.y/40].boom) { if(boom[map[(b.x+i*40)/40][b.y/40].boom-1].time>10) boom[map[(b.x+i*40)/40][b.y/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20+i*40,b.y+20,40,40,bufdc1,80,0,SRCAND); BitBlt(mdc,b.x+20+i*40,b.y+20,40,40,bufdc,80,0,SRCPAINT); } } } if(b.y-i*40>=0&&dir1) { if(map[b.x/40][(b.y-i*40)/40].obj||map[b.x/40][(b.y-i*40)/40].boom) { dir1=0; if(b.time==0) map[b.x/40][(b.y-i*40)/40].obj=0; if(map[b.x/40][(b.y-i*40)/40].boom) { if(boom[map[b.x/40][(b.y-i*40)/40].boom-1].time>10) boom[map[b.x/40][(b.y-i*40)/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20,b.y+20-i*40,40,40,bufdc1,40,0,SRCAND); BitBlt(mdc,b.x+20,b.y+20-i*40,40,40,bufdc,40,0,SRCPAINT); } } if(b.y+i*40<=600-40&&dir2) { if(map[b.x/40][(b.y+i*40)/40].obj||map[b.x/40][(b.y+i*40)/40].boom) { dir2=0; if(b.time==0) map[b.x/40][(b.y+i*40)/40].obj=0; if(map[b.x/40][(b.y+i*40)/40].boom) { if(boom[map[b.x/40][(b.y+i*40)/40].boom-1].time>10) boom[map[b.x/40][(b.y+i*40)/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20,b.y+20+i*40,40,40,bufdc1,40,0,SRCAND); BitBlt(mdc,b.x+20,b.y+20+i*40,40,40,bufdc,40,0,SRCPAINT); } } if(b.x-i*40>=0&&dir3) { if(map[(b.x-i*40)/40][b.y/40].obj||map[(b.x-i*40)/40][b.y/40].boom) { dir3=0; if(b.time==0) map[(b.x-i*40)/40][b.y/40].obj=0; if(map[(b.x-i*40)/40][b.y/40].boom) { if(boom[map[(b.x-i*40)/40][b.y/40].boom-1].time>10) boom[map[(b.x-i*40)/40][b.y/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20-i*40,b.y+20,40,40,bufdc1,80,0,SRCAND); BitBlt(mdc,b.x+20-i*40,b.y+20,40,40,bufdc,80,0,SRCPAINT); } } if(b.x+i*40<=800-40&&dir4) { if(map[(b.x+i*40)/40][b.y/40].obj||map[(b.x+i*40)/40][b.y/40].boom) { dir4=0; if(b.time==0) map[(b.x+i*40)/40][b.y/40].obj=0; if(map[(b.x+i*40)/40][b.y/40].boom) { if(boom[map[(b.x+i*40)/40][b.y/40].boom-1].time>10) boom[map[(b.x+i*40)/40][b.y/40].boom-1].time=10; } } else { BitBlt(mdc,b.x+20+i*40,b.y+20,40,40,bufdc1,80,0,SRCAND); BitBlt(mdc,b.x+20+i*40,b.y+20,40,40,bufdc,80,0,SRCPAINT); } } } void PreBooming(Bo& b) { int dir1=1,dir2=1,dir3=1,dir4=1; int i; map[b.x/40][(b.y)/40].booming=3; map[b.x/40][(b.y)/40].boomingdir=0; for(i=1;i<=b.power;i++) { if(b.y-i*40>=0&&dir1) { if(map[b.x/40][(b.y-i*40)/40].obj||map[b.x/40][(b.y-i*40)/40].boom) { dir1=0; if(b.time==0) map[b.x/40][(b.y-i*40)/40].distory=1; if(map[b.x/40][(b.y-i*40)/40].boom) { int res=map[b.x/40][(b.y-i*40)/40].boom; while(res>1000) res-=1000; if(boom[res-1].time>10) { boom[res-1].time=10; PreBooming(boom[res-1]); } } } else if(i<b.power) { map[b.x/40][(b.y-i*40)/40].goods=0; map[b.x/40][(b.y-i*40)/40].boomingdir=1; if(b.time>=2) map[b.x/40][(b.y-i*40)/40].booming=1; else map[b.x/40][(b.y-i*40)/40].booming=2; } else if(i==b.power) { map[b.x/40][(b.y-i*40)/40].goods=0; map[b.x/40][(b.y-i*40)/40].boomingdir=1; map[b.x/40][(b.y-i*40)/40].booming=2; } } if(b.y+i*40<=600-40&&dir2) { if(map[b.x/40][(b.y+i*40)/40].obj||map[b.x/40][(b.y+i*40)/40].boom) { dir2=0; if(b.time==0) map[b.x/40][(b.y+i*40)/40].distory=1; if(map[b.x/40][(b.y+i*40)/40].boom) { int res=map[b.x/40][(b.y+i*40)/40].boom; while(res>1000) res-=1000; if(boom[res-1].time>10) { boom[res-1].time=10; PreBooming(boom[res-1]); } } } else if(i<b.power) { map[b.x/40][(b.y+i*40)/40].goods=0; map[b.x/40][(b.y+i*40)/40].boomingdir=2; if(b.time>=2) map[b.x/40][(b.y+i*40)/40].booming=1; else map[b.x/40][(b.y+i*40)/40].booming=2; } else if(i==b.power) { map[b.x/40][(b.y+i*40)/40].goods=0; map[b.x/40][(b.y+i*40)/40].boomingdir=2; map[b.x/40][(b.y+i*40)/40].booming=2; } } if(b.x-i*40>=0&&dir3) { if(map[(b.x-i*40)/40][b.y/40].obj||map[(b.x-i*40)/40][b.y/40].boom) { dir3=0; if(b.time==0) map[(b.x-i*40)/40][b.y/40].distory=1; if(map[(b.x-i*40)/40][b.y/40].boom) { int res=map[(b.x-i*40)/40][b.y/40].boom; while(res>1000) res-=1000; if(boom[res-1].time>10) { boom[res-1].time=10; PreBooming(boom[res-1]); } } } else if(i<b.power) { map[(b.x-i*40)/40][b.y/40].goods=0; map[(b.x-i*40)/40][b.y/40].boomingdir=3; if(b.time>=2) map[(b.x-i*40)/40][b.y/40].booming=1; else map[(b.x-i*40)/40][b.y/40].booming=2; } else if(i==b.power) { map[(b.x-i*40)/40][b.y/40].goods=0; map[(b.x-i*40)/40][b.y/40].boomingdir=3; map[(b.x-i*40)/40][b.y/40].booming=2; } } if(b.x+i*40<=1000-40&&dir4) { if(map[(b.x+i*40)/40][b.y/40].obj||map[(b.x+i*40)/40][b.y/40].boom) { dir4=0; if(b.time==0) map[(b.x+i*40)/40][b.y/40].distory=1; if(map[(b.x+i*40)/40][b.y/40].boom) { int res=map[(b.x+i*40)/40][b.y/40].boom; while(res>1000) res-=1000; if(boom[res-1].time>10) { boom[res-1].time=10; PreBooming(boom[res-1]); } } } else if(i<b.power) { map[(b.x+i*40)/40][b.y/40].goods=0; map[(b.x+i*40)/40][b.y/40].boomingdir=4; if(b.time>=2) map[(b.x+i*40)/40][b.y/40].booming=1; else map[(b.x+i*40)/40][b.y/40].booming=2; } else if(i==b.power) { map[(b.x+i*40)/40][b.y/40].goods=0; map[(b.x+i*40)/40][b.y/40].boomingdir=4; map[(b.x+i*40)/40][b.y/40].booming=2; } } } } void Boom(HDC hdc) { int i,j; for(i=0;i<boomnum;i++) { boom[i].time--; if(boom[i].time%8==0) { boom[i].num++; if(boom[i].num==3) boom[i].num=0; } } for(i=0;i<boomnum;i++) { if(boom[i].time<=10) { PreBooming(boom[i]); if(boom[i].time<=0) { if(map[boom[i].x/40][boom[i].y/40].boom<2000) player.boomn--; else robot.boomn--; map[boom[i].x/40][boom[i].y/40].boom=0; for(j=i;j<boomnum-1;j++) { boom[j].x=boom[j+1].x; boom[j].y=boom[j+1].y; boom[j].time=boom[j+1].time; boom[j].num=boom[j+1].num; boom[j].power=boom[j+1].power; int res=map[boom[j].x/40][boom[j].y/40].boom; while(res>1000) res-=1000; if(res-1>i) map[boom[j].x/40][boom[j].y/40].boom--; } boomnum--; i--; } } } } void Move(HDC hdc) { int h; //先在mdc中贴上背景图SRCPAINT SRCAND //按照目前的移动方向取出对应人物的连续走动图,并确定截取人物图的宽度与高度 SelectObject(bufdc,people); SelectObject(bufdc1,peoplebg); switch(dir) { case 0: h = 171; break; case 1: h = 0; break; case 2: h = 114; break; case 3: h = 57; break; } //按照目前的X,Y的值在mdc上进行透明贴图,然后显示在窗口画面上 int i=(x+20)/40; int j=(y+20)/40; RECT p; p.left=(x+1)/40; p.right=(x+39)/40; p.top=(y+1)/40; p.bottom=(y+39)/40; if(map[i][j].goods) { if(map[i][j].goods==1||map[i][j].goods==2) { if(player.boomlimit<10) player.boomlimit++; } else if(map[i][j].goods==7||map[i][j].goods==8) { if(player.power<7) player.power++; } else if(map[i][j].goods==9||map[i][j].goods==10) { if(player.speed<3) player.speed++; } map[i][j].goods=0; } if(keyd) { moveloop++; switch(dir) { case 0: if(!(y-(8+4*player.speed)<j*40&&j>0&&(map[i][j-1].obj||map[i][j-1].boom))) { if(y-(8+4*player.speed)<j*40&&j>0&&(map[p.left][j-1].obj||map[p.left][j-1].boom)) { x+=5; if(x>i*40) x=i*40; } else if(y-(8+4*player.speed)<j*40&&j>0&&(map[p.right][j-1].obj||map[p.right][j-1].boom)) { x-=5; if(x<(i+1)*40) x=i*40; } else { y-=8+4*player.speed; if(y<0) y=0; } } else { y=j*40; } if(y-(8+4*player.speed)<j*40&&j>1&&map[i][j-1].obj==10&&map[i][j-2].obj==0&&map[i][j-2].boom==0&&!map[i][j-1].moveBox) { map[i][j-1].moveBox=10; } break; case 1: if(!(y+40+8+4*player.speed>(j+1)*40&&j<14&&(map[i][j+1].obj||map[i][j+1].boom))) { if((y+40+8+4*player.speed>(j+1)*40&&j<14&&(map[p.left][j+1].obj||map[p.left][j+1].boom))) { x+=5; if(x>i*40) x=i*40; } else if((y+40+8+4*player.speed>(j+1)*40&&j<14&&(map[p.right][j+1].obj||map[p.right][j+1].boom))) { x-=5; if(x<(i+1)*40) x=i*40; } else { y+=8+4*player.speed; if(y>600-40) y=600-40; } } else { y=j*40; } if(y+40+8+4*player.speed>(j+1)*40&&j<13&&map[i][j+1].obj==10&&map[i][j+2].obj==0&&map[i][j+2].boom==0&&!map[i][j+1].moveBox) { map[i][j+1].moveBox=20; } break; case 2: if(!(x-(8+4*player.speed)<i*40&&i>0&&(map[i-1][j].obj||map[i-1][j].boom))) { if(x-(8+4*player.speed)<i*40&&i>0&&(map[i-1][p.top].obj||map[i-1][p.top].boom)) { y+=5; if(y>j*40) y=j*40; } else if(x-(8+4*player.speed)<i*40&&i>0&&(map[i-1][p.bottom].obj||map[i-1][p.bottom].boom)) { y-=5; if(y<(j+1)*40) y=j*40; } else { x-=8+4*player.speed; if(x<0) x=0; } } else { x=i*40; } if(x-(8+4*player.speed)<i*40&&i>1&&map[i-1][j].obj==10&&map[i-2][j].obj==0&&map[i-2][j].boom==0&&!map[i-1][j].moveBox) { map[i-1][j].moveBox=30; } break; case 3: if(!(x+40+8+4*player.speed>(i+1)*40&&i<24&&(map[i+1][j].obj||map[i+1][j].boom))) { if(x+40+8+4*player.speed>(i+1)*40&&i<24&&(map[i+1][p.top].obj||map[i+1][p.top].boom)) { y+=5; if(y>j*40) y=j*40; } else if(x+40+8+4*player.speed>(i+1)*40&&i<24&&(map[i+1][p.bottom].obj||map[i+1][p.bottom].boom)) { y-=5; if(y<(j+1)*40) y=j*40; } else { x+=8+4*player.speed; if(x>1000-40) x=1000-40; } } else { x=i*40; } if((x+40+8+4*player.speed)>(i+1)*40&&i<23&&map[i+1][j].obj==10&&map[i+2][j].obj==0&&map[i+2][j].boom==0&&!map[i+1][j].moveBox) { map[i+1][j].moveBox=40; } break; } if(moveloop==2) { num++; if(num>=4) num=0; BitBlt(mdc,x+mx-4,y+my-17,48,57,bufdc1,num*48,h,SRCAND); BitBlt(mdc,x+mx-4,y+my-17,48,57,bufdc,num*48,h,SRCPAINT); moveloop=0; } else { BitBlt(mdc,x+mx-4,y+my-17,48,57,bufdc1,num*48,h,SRCAND); BitBlt(mdc,x+mx-4,y+my-17,48,57,bufdc,num*48,h,SRCPAINT); } } else { BitBlt(mdc,x+mx-4,y+my-17,48,57,bufdc1,0,h,SRCAND); BitBlt(mdc,x+mx-4,y+my-17,48,57,bufdc,0,h,SRCPAINT); } i=(x+20)/40; j=(y+20)/40+1; if(j<15) { if(map[i][j].obj) { if(map[i][j].obj/10==1&&!map[i][j].moveBox) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } i--; if(i>=0&&map[i][j].obj) { if(map[i][j].obj/10==1&&!map[i][j].moveBox) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } i+=2; if(i<20&&map[i][j].obj&&!map[i][j].moveBox) { if(map[i][j].obj/10==1) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } } } int isDead() { char s[100]; sprintf(s,"%d %d %d",(x+20)/40,(y+20)/40,map[(x+20)/40][(x+20)/40].booming); //MessageBox(NULL,s,NULL,MB_OK); if(map[(x+20)/40][(y+20)/40].booming) { return 1; } if(map[(robot.x+20)/40][(robot.y+20)/40].booming) { return 2; } return 0; } void Obj(HDC hdc) { int i,j; static int goodsloop=0; for(j=0;j<15;j++) { for(i=0;i<25;i++) { SelectObject(bufdc,mground); SelectObject(bufdc1,mgroundbg); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,40*map[i][j].ground,0,SRCPAINT); } } goodsloop++; for(j=0;j<15;j++) { for(i=0;i<25;i++) { if(map[i][j].distory&&map[i][j].obj/10==1) { map[i][j].obj=0; map[i][j].distory=0; } else if(map[i][j].obj) { if(map[i][j].obj/10==1) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); if(map[i][j].obj==10&&map[i][j].moveBox>=10) { map[i][j].moveBox++; int bx,by; switch(map[i][j].moveBox/10) { case 1: bx=0; by=-5*(map[i][j].moveBox%10); break; case 2: bx=0; by=5*(map[i][j].moveBox%10); break; case 3: bx=-5*(map[i][j].moveBox%10); by=0; break; case 4: bx=5*(map[i][j].moveBox%10); by=0; break; } BitBlt(mdc,i*40+mx+bx,j*40+my-4+by,40,44,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx+bx,j*40+my-4+by,40,44,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); if(map[i][j].moveBox%10==8) { map[i][j].obj=0; switch(map[i][j].moveBox/10) { case 1: map[i][j-1].obj=10; map[i][j-1].goods=map[i][j].goods; break; case 2: map[i][j+1].obj=10; map[i][j+1].goods=map[i][j].goods; break; case 3: map[i-1][j].obj=10; map[i-1][j].goods=map[i][j].goods; break; case 4: map[i+1][j].obj=10; map[i+1][j].goods=map[i][j].goods; break; } map[i][j].moveBox=0; map[i][j].goods=0; } } else { BitBlt(mdc,i*40+mx,j*40+my-4,40,44,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,44,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,56,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,56,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } else if(map[i][j].boom&&!map[i][j].booming) { SelectObject(bufdc,mboom); SelectObject(bufdc1,mboombg); int res=map[i][j].boom; while(res>1000) res-=1000; BitBlt(mdc,i*40+mx-5,j*40+my-5,45,45,bufdc1,boom[res-1].num*45,0,SRCAND); BitBlt(mdc,i*40+mx-5,j*40+my-5,45,45,bufdc,boom[res-1].num*45,0,SRCPAINT); } else if(map[i][j].booming) { SelectObject(bufdc,mbooming); SelectObject(bufdc1,mboomingbg); if(map[i][j].booming==3) { BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,0,0,SRCPAINT); } else if(map[i][j].booming==2) { switch(map[i][j].boomingdir) { case 1: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,40,40,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,40,40,SRCPAINT); break; case 2: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,0,40,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,0,40,SRCPAINT); break; case 3: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,120,40,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,120,40,SRCPAINT); break; case 4: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,80,40,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,80,40,SRCPAINT); break; } } else if(map[i][j].booming==1) { switch(map[i][j].boomingdir) { case 1: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,40,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,40,0,SRCPAINT); break; case 2: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,40,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,40,0,SRCPAINT); break; case 3: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,80,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,80,0,SRCPAINT); break; case 4: BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc1,80,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my,40,40,bufdc,80,0,SRCPAINT); break; } } map[i][j].booming=0; map[i][j].boomingdir=0; } else if(map[i][j].goods) { static int goodsmove=0; if(goodsloop==5) { goodsloop=0; if(map[i][j].goods%2==1) goodsmove=3; else goodsmove=0; switch(map[i][j].goods) { case 1: map[i][j].goods=2; break; case 2: map[i][j].goods=1; break; case 7: map[i][j].goods=8; break; case 8: map[i][j].goods=7; break; case 9: map[i][j].goods=10; break; case 10: map[i][j].goods=9; break; } } SelectObject(bufdc,goods); SelectObject(bufdc1,goodsbg); BitBlt(mdc,i*40+mx,j*40+my-5+goodsmove,40,45,bufdc1,(map[i][j].goods-1)*40,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-5+goodsmove,40,45,bufdc,(map[i][j].goods-1)*40,0,SRCPAINT); } } } } void Music(HDC hdc) { //PlaySound(TEXT("ppt.wav"),NULL,SND_FILENAME | SND_ASYNC); static int alltime,isMusic=1,currenttime,i=0; static TCHAR time[345],timeon[345]; static TCHAR MusicPath[100],Play[100]; if(!i) { i++; if(i==20) i=0; return; } if(isMusic) { strcpy(MusicPath,"ppt.mp3"); sprintf(Play,"play %s",MusicPath); mciSendString(Play,"",0,NULL); // isMusic=0; } /*wsprintf(time,"status %s length",MusicPath); mciSendString(time,timeon,345,NULL); alltime=atoi(timeon)/1000; MessageBox(NULL,timeon,NULL,MB_OK); wsprintf(time,"status %s position",MusicPath); mciSendString(time,timeon,345,NULL); currenttime=atoi(timeon)/1000; if(currenttime==0) isMusic=1;*/ } int searchPath(int& sx,int& sy,int& ex,int& ey,int epath[900],int &pathnum,int robotMap[30][30],int d) { robotMap[ex][ey]=0; int dir[4][2]={0,-1,0,1,-1,0,1,0}; int save[900][2],path[30][30]; int i,j,k; int front=1,rear=1,newx,newy; int end=0; memset(path,0,sizeof(path)); save[rear][0]=sx; save[rear++][1]=sy; path[sx][sy]=5; while(front<rear) { if(end) break; for(i=0;i<4;i++) { newx=save[front][0]+dir[i][0]; newy=save[front][1]+dir[i][1]; if(newx>=0&&newx<25&&newy>=0&&newy<15&&!path[newx][newy]&&robotMap[newx][newy]!=1&&robotMap[newx][newy]!=d) { save[rear][0]=newx; save[rear++][1]=newy; path[newx][newy]=i+1; if(newx==ex&&newy==ey) end=1; } } front++; } if(!end) return 0; else { int epath1[900]; int num=0; newx=ex; newy=ey; while(newx!=sx||newy!=sy) { epath1[num++]=path[newx][newy]-1; int newx1=newx; int newy1=newy; newx-=dir[path[newx1][newy1]-1][0]; newy-=dir[path[newx1][newy1]-1][1]; } j=num; for(i=0;i<num;i++) epath[i]=epath1[--j]; pathnum=num; return 1; } } int isDangous(int rx,int ry,int robotMap[30][30]) { int i,j; for(i=0;i<25;i++) { for(j=0;j<15;j++) { robotMap[i][j]=0; } } for(i=0;i<25;i++) { for(j=0;j<15;j++) { if(map[i][j].obj) { robotMap[i][j]=1; } if(map[i][j].booming) robotMap[i][j]=1; if(map[i][j].boom) { robotMap[i][j]=1; int k,res=map[i][j].boom; while(res>1000) res-=1000; int dir1=1,dir2=1,dir3=1,dir4=1; for(k=1;k<=boom[res-1].power;k++) { if(j-k>=0&&!map[i][j-k].obj&&dir1) { if(boom[res-1].time<=20) robotMap[i][j-k]=1; else robotMap[i][j-k]=2; } else dir1=0; if(j+k<15&&!map[i][j+k].obj&&dir2) { if(boom[res-1].time<=20) robotMap[i][j+k]=1; else robotMap[i][j+k]=2; } else dir2=0; if(i-k>=0&&!map[i-k][j].obj&&dir3) { if(boom[res-1].time<=20) robotMap[i-k][j]=1; else robotMap[i-k][j]=2; } else dir3=0; if(i+k<25&&!map[i+k][j].obj&&dir4) { if(boom[res-1].time<=20) robotMap[i+k][j]=1; else robotMap[i+k][j]=2; } else dir4=0; } } } } if(robotMap[rx][ry]) return 1; else return 0; } int findSafeplace(int& sx,int& sy,int map[30][30]) { sx=sy=-1; int bx1=(robot.x+20)/40-6; if(bx1<0) bx1=0; int by1=(robot.y+20)/40-6; if(by1<0) by1=0; int i,j; int path[900],pathnum,rx=(robot.x+20)/40,ry=(robot.y+20)/40; for(i=bx1;i<bx1+12&&i<25;i++) { for(j=by1;j<by1+12&&j<15;j++) { if(!map[i][j]&&(sx==-1||(i-(robot.x+20)/40)*(i-(robot.x+20)/40)+(j-(robot.y+20)/40)*(j-(robot.y+20)/40) <(sx-(robot.x+20)/40)*(sx-(robot.x+20)/40)+(sy-(robot.y+20)/40)*(sy-(robot.y+20)/40))) { if(searchPath(rx,ry,i,j,path,pathnum,map,1)) { sx=i; sy=j; } } } } if(sx==-1) return 0; else return 1; } int findGoods(int& sx,int& sy) { sx=sy=-1; int bx1=(robot.x+20)/40-6; if(bx1<0) bx1=0; int by1=(robot.y+20)/40-6; if(by1<0) by1=0; int i,j; for(i=bx1;i<bx1+12&&i<25;i++) { for(j=by1;j<by1+12&&j<15;j++) { if(map[i][j].goods&&!map[i][j].obj&&(sx==-1||(i-(robot.x+20)/40)*(i-(robot.x+20)/40)+(j-(robot.y+20)/40)*(j-(robot.y+20)/40) <(sx-(robot.x+20)/40)*(sx-(robot.x+20)/40)+(sy-(robot.y+20)/40)*(sy-(robot.y+20)/40))) { sx=i; sy=j; } } } if(sx==-1) return 0; else return 1; } int findObj1(int &sx,int& sy,int robotMap[30][30]) { sx=sy=-1; int bx1=(robot.x+20)/40-6; if(bx1<0) bx1=0; int by1=(robot.y+20)/40-6; if(by1<0) by1=0; int i,j; int path[900],pathnum; int rx=(robot.x+20)/40,ry=(robot.y+20)/40; for(i=bx1;i<bx1+12&&i<25;i++) { for(j=by1;j<by1+12&&j<15;j++) { if(map[i][j].obj/10==1&&(sx==-1||(i-(robot.x+20)/40)*(i-(robot.x+20)/40)+(j-(robot.y+20)/40)*(j-(robot.y+20)/40) <(sx-(robot.x+20)/40)*(sx-(robot.x+20)/40)+(sy-(robot.y+20)/40)*(sy-(robot.y+20)/40))) { if(searchPath(rx,ry,i,j,path,pathnum,robotMap,2)) { sx=i; sy=j; } } } } if(sx==-1) return 0; else return 1; } void Robot(HDC hdc) { int px=(x+20)/40,py=(y+20)/40; int x=robot.x; int y=robot.y; int j=(y+20)/40; int i=(x+20)/40; int k; int robotMap[30][30]; int safex,safey,path[900],pathnum; /*if(robot.x+robot.premove/10*8>=0&&robot.x+robot.premove/10*8<=1000-40&&robot.y+robot.premove%10*8>=0&&robot.y+robot.premove%10*8<=600-40 &&!map[(robot.x+robot.premove/10*8)/40][(robot.y+robot.premove%10*8)/40].boom&&!map[(robot.x+robot.premove/10*8)/40][(robot.y+robot.premove%10*8)/40].obj) { } else { for(i=0;i<4;i++) { if(robot.x+dir[i][0]*8>=0&&robot.x+dir[i][0]*8<=1000-40&&robot.y+dir[i][1]*8>=0&&robot.y+dir[i][1]*8<600-40 &&!map[(robot.x+dir[i][0]*8)/40][(robot.y+dir[i][1]*8)/40].boom&&!map[(robot.x+dir[i][0]*8)/40][(robot.y+dir[i][1]*8)/40].obj) { if(rand()%2==1) robot.dir=i; break; } } }*/ int t1,t2,t3,t4,t5,t6; t1=isDangous(i,j,robotMap); if(t1) { t2=findSafeplace(safex,safey,robotMap); if(t2) { t3=searchPath(i,j,safex,safey,path,pathnum,robotMap,1); if(t3) { robot.planstep=pathnum; for(k=1;k<=robot.planstep;k++) { robot.plan[k]=path[k-1]; robot.plan1[k]=0; } robot.stepnow=1; robot.move=1; } } } /*char s[100]; sprintf(s,"%d %d %d %d",t1,robotMap[i][j],t2,t3); MessageBox(NULL,s,NULL,MB_OK);*/ else { int gx,gy; t5=findGoods(gx,gy); if(t5) { if(searchPath(i,j,gx,gy,path,pathnum,robotMap,2)) { robot.planstep=pathnum; for(k=1;k<=robot.planstep;k++) { robot.plan[k]=path[k-1]; robot.plan1[k]=0; } robot.stepnow=1; robot.move=1; } } else { //if((i-1>=0&&map[i-1][j].obj/10==1||i+1<25&&map[i+1][j].obj/10==1|| // j-1>=0&&map[i][j-1].obj/10==1||j+1<15&&map[i][j+1].obj/10==1)&&!robot.boomn&&!map[i][j].boom) int obj1x,obj1y; t6=findObj1(obj1x,obj1y,robotMap); if(t6) { if(searchPath(i,j,obj1x,obj1y,path,pathnum,robotMap,2)) { robot.planstep=pathnum; for(k=1;k<=robot.planstep;k++) { robot.plan[k]=path[k-1]; robot.plan1[k]=0; } if(!robot.boomn) robot.plan1[robot.planstep]=2; robot.stepnow=1; robot.move=1; } } else { if(searchPath(i,j,px,py,path,pathnum,robotMap,2)) { robot.planstep=pathnum; for(k=1;k<=robot.planstep;k++) { robot.plan[k]=path[k-1]; robot.plan1[k]=0; } for(k=robot.planstep;k>=robot.planstep-4&&k>=0;k--) robot.plan1[k]=1; robot.stepnow=1; robot.move=1; } } //char s[100]; //sprintf(s,"%d %d %d %d %d %d",t1,t2,t3,obj1x,obj1y,t6); //MessageBox(NULL,s,NULL,MB_OK); } } /*char s[100]; sprintf(s,"%d %d %d %d %d %d",t1,t2,t3,safex,safey,t6);*/ //if(i==21&&j==9) //MessageBox(NULL,s,NULL,MB_OK); /*if(!map[(x+20)/40][(y+20)/40].boom&&robot.boomlimit>robot.boomn) { boom[boomnum].x=(x+20)/40*40; boom[boomnum].y=(y+20)/40*40; boom[boomnum].time=70; boom[boomnum].num=0; boom[boomnum].power=robot.power; map[boom[boomnum].x/40][boom[boomnum].y/40].boom=2000+boomnum+1; boomnum++; robot.boomn++; } switch(robot.dir) { case 0: if((y-(8+4*robot.speed)<j*40&&j>0&&(map[i][j-1].obj||map[i][j-1].boom))||y-(8+4*robot.speed)<0) { srand((unsigned)time(NULL)); for(k=0;k<4;k++) if(rand()%2==1) robot.dir=k; } break; case 1: if((y+40+8+4*robot.speed>(j+1)*40&&j<14&&(map[i][j+1].obj||map[i][j+1].boom))||y+40+8+4*robot.speed>600) { srand((unsigned)time(NULL)); for(k=0;k<4;k++) if(rand()%2==1) robot.dir=k; } break; case 2: if((x-(8+4*robot.speed)<i*40&&i>0&&(map[i-1][j].obj||map[i-1][j].boom))||x-(8+4*robot.speed)<0) { srand((unsigned)time(NULL)); for(k=0;k<4;k++) if(rand()%2==1) robot.dir=k; } break; case 3: if((x+40+8+4*robot.speed>(i+1)*40&&i<24&&(map[i+1][j].obj||map[i+1][j].boom))||x+40+8+4*robot.speed>1000) { srand((unsigned)time(NULL)); for(k=0;k<4;k++) if(rand()%2==1) robot.dir=k; } break; }*/ if(robot.stepnow>robot.planstep) robot.move=0; robot.dir=robot.plan[robot.stepnow]; SelectObject(bufdc,mrobot); SelectObject(bufdc1,mrobotbg); int h; //先在mdc中贴上背景图SRCPAINT SRCAND //按照目前的移动方向取出对应人物的连续走动图,并确定截取人物图的宽度与高度 switch(robot.dir) { case 0: h = 168; break; case 1: h = 0; break; case 2: h = 112; break; case 3: h = 56; break; } //按照目前的X,Y的值在mdc上进行透明贴图,然后显示在窗口画面上 RECT p; p.left=(x+1)/40; p.right=(x+39)/40; p.top=(y+1)/40; p.bottom=(y+39)/40; if(map[i][j].goods) { if(map[i][j].goods==1||map[i][j].goods==2) { if(robot.boomlimit<10) robot.boomlimit++; } else if(map[i][j].goods==7||map[i][j].goods==8) { if(robot.power<7) robot.power++; } else if(map[i][j].goods==9||map[i][j].goods==10) { if(robot.speed<3) robot.speed++; } map[i][j].goods=0; } if(robot.plan1[robot.stepnow]&&robot.boomn<robot.boomlimit&&!map[i][j].boom) { boom[boomnum].x=(x+20)/40*40; boom[boomnum].y=(y+20)/40*40; boom[boomnum].time=70; boom[boomnum].num=0; boom[boomnum].power=robot.power; map[boom[boomnum].x/40][boom[boomnum].y/40].boom=2000+boomnum+1; boomnum++; robot.boomn++; } if(robot.move) { robot.moveloop++; switch(robot.dir) { case 0: if(!(y-(8+4*robot.speed)<j*40&&j>0&&(map[i][j-1].obj||map[i][j-1].boom))) { if(y-(8+4*robot.speed)<j*40&&j>0&&(map[p.left][j-1].obj||map[p.left][j-1].boom)) { x+=5; if(x>i*40) x=i*40; } else if(y-(8+4*robot.speed)<j*40&&j>0&&(map[p.right][j-1].obj||map[p.right][j-1].boom)) { x-=5; if(x<(i+1)*40) x=i*40; } else { y-=8+4*robot.speed; if(y<0) y=0; } } else { y=j*40; } if(y-(8+4*robot.speed)<j*40&&j>1&&map[i][j-1].obj==10&&map[i][j-2].obj==0&&map[i][j-2].boom==0&&!map[i][j-1].moveBox) { map[i][j-1].moveBox=10; } break; case 1: if(!(y+40+8+4*robot.speed>(j+1)*40&&j<14&&(map[i][j+1].obj||map[i][j+1].boom))) { if((y+40+8+4*robot.speed>(j+1)*40&&j<14&&(map[p.left][j+1].obj||map[p.left][j+1].boom))) { x+=5; if(x>i*40) x=i*40; } else if((y+40+8+4*robot.speed>(j+1)*40&&j<14&&(map[p.right][j+1].obj||map[p.right][j+1].boom))) { x-=5; if(x<(i+1)*40) x=i*40; } else { y+=8+4*robot.speed; if(y>600-40) y=600-40; } } else { y=j*40; } if(y+40+8+4*robot.speed>(j+1)*40&&j<13&&map[i][j+1].obj==10&&map[i][j+2].obj==0&&map[i][j+2].boom==0&&!map[i][j+1].moveBox) { map[i][j+1].moveBox=20; } break; case 2: if(!(x-(8+4*robot.speed)<i*40&&i>0&&(map[i-1][j].obj||map[i-1][j].boom))) { if(x-(8+4*robot.speed)<i*40&&i>0&&(map[i-1][p.top].obj||map[i-1][p.top].boom)) { y+=5; if(y>j*40) y=j*40; } else if(x-(8+4*robot.speed)<i*40&&i>0&&(map[i-1][p.bottom].obj||map[i-1][p.bottom].boom)) { y-=5; if(y<(j+1)*40) y=j*40; } else { x-=8+4*robot.speed; if(x<0) x=0; } } else { x=i*40; } if(x-(8+4*robot.speed)<i*40&&i>1&&map[i-1][j].obj==10&&map[i-2][j].obj==0&&map[i-2][j].boom==0&&!map[i-1][j].moveBox) { map[i-1][j].moveBox=30; } break; case 3: if(!(x+40+8+4*robot.speed>(i+1)*40&&i<24&&(map[i+1][j].obj||map[i+1][j].boom))) { if(x+40+8+4*robot.speed>(i+1)*40&&i<24&&(map[i+1][p.top].obj||map[i+1][p.top].boom)) { y+=5; if(y>j*40) y=j*40; } else if(x+40+8+4*robot.speed>(i+1)*40&&i<24&&(map[i+1][p.bottom].obj||map[i+1][p.bottom].boom)) { y-=5; if(y<(j+1)*40) y=j*40; } else { x+=8+4*robot.speed; if(x>1000-40) x=1000-40; } } else { x=i*40; } if((x+40+8+4*robot.speed)>(i+1)*40&&i<23&&map[i+1][j].obj==10&&map[i+2][j].obj==0&&map[i+2][j].boom==0&&!map[i+1][j].moveBox) { map[i+1][j].moveBox=40; } break; } if(robot.moveloop==2) { robot.num++; if(robot.num>=4) robot.num=0; BitBlt(mdc,x+mx-4,y+my-14,48,54,bufdc1,robot.num*48,h,SRCAND); BitBlt(mdc,x+mx-4,y+my-14,48,54,bufdc,robot.num*48,h,SRCPAINT); robot.moveloop=0; } else { BitBlt(mdc,x+mx-4,y+my-14,48,54,bufdc1,robot.num*48,h,SRCAND); BitBlt(mdc,x+mx-4,y+my-14,48,54,bufdc,robot.num*48,h,SRCPAINT); } } else { BitBlt(mdc,x+mx-4,y+my-14,48,54,bufdc1,0,h,SRCAND); BitBlt(mdc,x+mx-4,y+my-14,48,54,bufdc,0,h,SRCPAINT); } if(((robot.x+20)/40!=i||(robot.y+20/40)!=j)&&robot.stepnow<=robot.planstep) robot.stepnow++; robot.x=x; robot.y=y; i=(x+20)/40; j=(y+20)/40+1; if(j<15) { if(map[i][j].obj) { if(map[i][j].obj/10==1&&!map[i][j].moveBox) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } i--; if(i>=0&&map[i][j].obj) { if(map[i][j].obj/10==1&&!map[i][j].moveBox) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } i+=2; if(i<20&&map[i][j].obj&&!map[i][j].moveBox) { if(map[i][j].obj/10==1) { SelectObject(bufdc,mobj1); SelectObject(bufdc1,mobj1bg); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc1,0,0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-4,40,20,bufdc,40*(map[i][j].obj-10),0,SRCPAINT); } else if(map[i][j].obj/10==2) { SelectObject(bufdc,mobj2); SelectObject(bufdc1,mobj2bg); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc1,40*(map[i][j].obj-20),0,SRCAND); BitBlt(mdc,i*40+mx,j*40+my-16,40,40,bufdc,40*(map[i][j].obj-20),0,SRCPAINT); } } } } void MyPaint(HDC hdc) { Music(hdc); SelectObject(mdc1,bg); // BitBlt(mdc1,0,0,rect.right,rect.bottom,bufdc,0,0,SRCCOPY); Boom(hdc); int t=isDead(); Obj(hdc); Move(hdc); Robot(hdc); BitBlt(mdc1,32,44,1024,615,mdc,0,0,SRCCOPY); BitBlt(hdc,0,0,rect.right,rect.bottom,mdc1,0,0,SRCCOPY); tPre = GetTickCount(); //记录此次绘图时间 SelectObject(bufdc,bg2); BitBlt(mdc,0,0,rect.right,rect.bottom,bufdc,0,0,SRCCOPY); if(t==1) { MessageBox(NULL,TEXT("失败!"),NULL,MB_OK); x = 120; y = 80; robot.x=840; robot.y=420; robot.premove=-1; robot.move=1; robot.dir=3; dir = 1; num = 0; moveloop=0; keyd=0; boomnum=0; robot.boomn=player.boomn=0; robot.boomlimit=player.boomlimit=1; robot.power=player.power=1; robot.speed=player.speed=0; robot.dead=player.dead=0; memset(boom,0,sizeof(boom)); memset(map,0,sizeof(map)); freopen("map.txt","r",stdin); int i,j,k; for(j=0;j<15;j++) { for(i=0;i<25;i++) { scanf("%d %d",&map[i][j].ground,&map[i][j].obj); if(map[i][j].obj/10==1) { k=rand()%7; if(k==0) map[i][j].goods=1; else if(k==1) map[i][j].goods=7; else if(k==2) map[i][j].goods=9; else map[i][j].goods=0; } /*k=rand()%2; while(k==0) k=rand()%3; if(k==1) map[i][j].obj=10+rand()%3; else map[i][j].obj=20+rand()%4;*/ } } } else if(t==2) { MessageBox(NULL,TEXT("成功!"),NULL,MB_OK); x = 120; y = 80; robot.x=840; robot.y=420; robot.premove=-1; robot.move=1; robot.dir=3; dir = 1; num = 0; moveloop=0; keyd=0; boomnum=0; robot.boomn=player.boomn=0; robot.boomlimit=player.boomlimit=1; robot.power=player.power=1; robot.speed=player.speed=0; robot.dead=player.dead=0; memset(boom,0,sizeof(boom)); memset(map,0,sizeof(map)); freopen("map.txt","r",stdin); int i,j,k; for(j=0;j<15;j++) { for(i=0;i<25;i++) { scanf("%d %d",&map[i][j].ground,&map[i][j].obj); if(map[i][j].obj/10==1) { k=rand()%7; if(k==0) map[i][j].goods=1; else if(k==1) map[i][j].goods=7; else if(k==2) map[i][j].goods=9; else map[i][j].goods=0; } /*k=rand()%2; while(k==0) k=rand()%3; if(k==1) map[i][j].obj=10+rand()%3; else map[i][j].obj=20+rand()%4;*/ } } } } //****消息处理函数*********************************** // 1.按下【Esc】键结束程序 // 2.按下方向键重设贴图坐标 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int i,j; switch (message) { case WM_KEYDOWN: //按下键盘消息 //判断按键的虚拟键码 i=(x+20)/40; j=(y+20)/40; switch (wParam) { case VK_ESCAPE: //按下【Esc】键 PostQuitMessage( 0 ); //结束程序 break; case VK_UP: //按下【↑】键 keyd=1; if(dir!=0) { dir=0; num=0; if(!(y-1<j*40&&j>0&&(map[i][j-1].obj||map[i][j-1].boom))) { y-=5+3*player.speed; if(y<0) y=0; } } break; case VK_DOWN: //按下【↓】键 keyd=2; if(dir!=1) { dir=1; num=0; if(!(y+41>(j+1)*40&&j<15&&(map[i][j+1].obj||map[i][j+1].boom))) { y+=5+3*player.speed; if(y>600-40) y=600-40; } } break; case VK_LEFT: //按下【←】键 keyd=3; if(dir!=2) { dir=2; num=0; if(!(x-1<i*40&&i>0&&(map[i-1][j].obj||map[i-1][j].boom))) { x-=5+3*player.speed; if(x<0) x=0; } } break; case VK_RIGHT: //按下【→】键 keyd=4; if(dir!=3) { dir=3; num=0; if(!(x+41>(i+1)*40&&i<24&&(map[i+1][j].obj||map[i+1][j].boom))) { x+=5+3*player.speed; if(x>1000-40) x=1000-40; } } break; case VK_SPACE: if(!map[(x+20)/40][(y+20)/40].boom&&player.boomlimit>player.boomn) { boom[boomnum].x=(x+20)/40*40; boom[boomnum].y=(y+20)/40*40; boom[boomnum].time=70; boom[boomnum].num=0; boom[boomnum].power=player.power; /*char s[100]; sprintf(s,"%d %d %d %d",boom[boomnum].x/40,boom[boomnum].y/40,map[boom[boomnum].x/40][boom[boomnum].y/40].boomnum,boomnum); MessageBox(NULL,s,NULL,MB_OK);*/ map[boom[boomnum].x/40][boom[boomnum].y/40].boom=boomnum+1; boomnum++; player.boomn++; } break; } break; case WM_KEYUP: switch (wParam) { case VK_UP: //按下【↑】键 if(keyd==1) keyd=0; case VK_DOWN: //按下【↓】键 if(keyd==2) keyd=0; break; case VK_LEFT: //按下【←】键 if(keyd==3) keyd=0; break; case VK_RIGHT: //按下【→】键 if(keyd==4) keyd=0; break; } break; case WM_DESTROY: //窗口结束消息 DeleteDC(mdc); DeleteDC(bufdc); DeleteDC(bufdc1); DeleteObject(people); DeleteObject(peoplebg); DeleteObject(mboom); DeleteObject(mboombg); DeleteObject(mbooming); DeleteObject(mboomingbg); DeleteObject(bg); ReleaseDC(hWnd,hdc); PostQuitMessage(0); break; default: //其他消息 return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }