我的windows里游戏入门程序
程序代码:
#include <windows.h> #include <windowsx.h> #include <stdio.h> #include <stdlib.h> #include <mmsystem.h> #pragma comment(lib,"WINMM.LIB") bool win_init(); void load_tu(); HWND win_create(); void win_show(HWND); void win_run(); void show_tu(HWND ); WNDCLASS wnd; HBITMAP bg; HBITMAP testbk; HBITMAP bk; HBITMAP zidan; HBITMAP bmp; HWND hwnd; HDC bufdc; HDC hdc; HDC mdc; DWORD now,pre; int x=0,y=0,xclient=0,yclient=0; int num=0; struct Bullet { int x; int y; int flag; int dir; }bullet[255]; LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) {MSG msg; if(win_init()) { hwnd=win_create(); if(hwnd) { win_show(hwnd); while(msg.message!=WM_QUIT) {if(PeekMessage(&msg,0,0,0,PM_REMOVE)) {TranslateMessage(&msg); DispatchMessage(&msg); } else {now=GetTickCount(); if(now-pre>=20) { load_tu();} } } } } return false; } bool win_init() { memset(&wnd,0,sizeof(wnd)); wnd.lpfnWndProc=WndProc; wnd.hbrBackground=GetStockBrush(WHITE_BRUSH); wnd.lpszClassName="xiaoyuer"; wnd.hInstance=GetModuleHandle(0); if(RegisterClass(&wnd)) return true; else return FALSE; } HWND win_create() { return CreateWindow("xiaoyuer","HELLO 测试",WS_OVERLAPPEDWINDOW,0,0,640,480,0,0,wnd.hInstance,0); } void win_show(HWND hwnd) { ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); bg=(HBITMAP)LoadImage(0,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); bk=(HBITMAP)LoadImage(0,"1.bmp",IMAGE_BITMAP,xclient,yclient,LR_LOADFROMFILE); testbk=(HBITMAP)LoadImage(0,"testbk.bmp",IMAGE_BITMAP,xclient,yclient,LR_LOADFROMFILE); zidan=(HBITMAP)LoadImage(0,"12.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); hdc=GetDC(hwnd); bmp=CreateCompatibleBitmap(hdc,640,480); bufdc=CreateCompatibleDC(hdc); mdc=CreateCompatibleDC(hdc); SelectObject(mdc,bmp); load_tu(); } void win_run() { MSG msg; while(GetMessage(&msg,0,0,0)) {TranslateMessage(&msg); DispatchMessage(&msg); } } LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) { switch(msg) { case WM_CREATE:PlaySound("1.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP); break; case WM_DESTROY:DeleteDC(bufdc); DeleteObject(bg); DeleteObject(bk); DeleteObject(testbk); PostQuitMessage(0); break; case WM_CHAR: switch(wparam) { case 'w': if(y-10>0) {yclient=6*64; y=y-10; } break; case 's':if(y+10<=380) {y+=10; yclient=7*64; } break; case 'a':if(x-10>0) {yclient=4*64; x-=10; }break; case 'd':if(x+10<480) { yclient=5*64; x+=10; } break; case 'k': if(num+1<255) num++; for(int n=0;n<num;n++) { if(!(bullet[n].flag)) {bullet[n].x=x; bullet[n].y=y+30; bullet[n].flag=true; switch(yclient/64) {case 6: bullet[n].dir=1; break; case 7:bullet[n].dir=2; break; case 4:bullet[n].dir=3; break; case 5:bullet[n].dir=4; break; } } } break; } break; case WM_KEYDOWN:if(xclient==768) xclient=0; else xclient+=64; break; default:return DefWindowProc(hwnd,msg,wparam,lparam); } return TRUE; } void load_tu() { SelectObject(bufdc,bg); BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY); SelectObject(bufdc,testbk); BitBlt(mdc,x,y,64,64,bufdc,xclient,yclient,SRCAND); SelectObject(bufdc,bk); BitBlt(mdc,x,y,64,64,bufdc,xclient,yclient,SRCPAINT); SelectObject(bufdc,zidan); if(num) { for(int i=0;i<255;i++) {if(bullet[i].flag) {BitBlt(mdc,bullet[i].x,bullet[i].y,10,10,bufdc,0,10,SRCAND); BitBlt(mdc,bullet[i].x,bullet[i].y,10,10,bufdc,0,0,SRCPAINT); switch(bullet[i].dir) {case 1:bullet[i].y-=10;break; case 2: bullet[i].y+=10;break; case 3: bullet[i].x-=10;break; case 4: bullet[i].x+=10;break; } if(bullet[i].x<=0||bullet[i].x>=640||bullet[i].y<=0||bullet[i].y>=480) {bullet[i].flag=false; num--; } }}} BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); pre=GetTickCount(); }w s d k四个方向,k发子弹 可以四个方向。画面有点不流畅,看有没有改进的地方。子弹好像有BUG。能给我几本学习好的好书,小弟在这里就感激不尽啊。
小弟在这里感谢 μāη洋给了一本书给我入门。但强烈鄙视给他给我发一个弹出鬼图的程序,害我半夜吓的要死。。。
游戏下载地址
http://u. 有个小小的问题CreateCompatibleBitmap()这个函数有什么用,我感觉有时候SelectObjec()也就可以。
也能画图。引进位图我没有实验。
在这个程序里面如果除掉 就会什么都没有。
[ 本帖最后由 小鱼儿c 于 2011-6-13 10:56 编辑 ]