游戏小demo
#include <windows.h>#include <tchar.h>
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"Msimg32.lib")
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"[游戏]程序核心框架"
struct SwordBullets
{
int x,y;
bool exist;
};
HDC g_hdc=NULL,g_mdc=NULL,g_bufdc=NULL;
HBITMAP g_hSwordMan=NULL,g_hSwordBlade=NULL,g_hBackGround=NULL;
DWORD g_tPre=0,g_tNow=0;
int g_iX=0,g_iY=0,g_iXnow=0,g_iYnow=0;
int g_iBGOffset=0,g_iBulletNum=0;
SwordBullets Bullet[30];
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
BOOL Game_Init(HWND hwnd);
VOID Game_Paint( HWND hwnd);
BOOL Game_CleanUp(HWND hwnd );
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX WNDCLASS={0};
WNDCLASS.cbClsExtra=0;
WNDCLASS.cbSize=sizeof(WNDCLASSEX);
WNDCLASS.cbWndExtra=0;
WNDCLASS.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
WNDCLASS.hCursor=LoadCursor(NULL,IDC_ARROW);
WNDCLASS.hIcon=(HICON)::LoadImage(NULL,L"icon.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
WNDCLASS.hInstance=hInstance;
WNDCLASS.lpfnWndProc=WndProc;
WNDCLASS.lpszClassName=L"ForTheDreamOfGameDevelop";
WNDCLASS.lpszMenuName=NULL;
WNDCLASS.style=CS_HREDRAW|CS_VREDRAW;
if( !RegisterClassEx(&WNDCLASS) )
return -1;
HWND hwnd=CreateWindow(L"ForTheDreamOfGameDevelop",WINDOW_TITLE,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);
MoveWindow(hwnd,250,80,WINDOW_WIDTH,WINDOW_HEIGHT,true);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
if(!Game_Init(hwnd))
{
MessageBox(hwnd,L"资源初始化失败",L"消息窗口",0);
return FALSE;
}
PlaySound(L"仙剑三·原版战斗3.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
MSG msg={0};
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
g_tNow=GetTickCount();
if(g_tNow-g_tPre>=5)
Game_Paint(hwnd);
}
}
UnregisterClass(L"ForTheDreamOfGameDevelop",WNDCLASS.hInstance);
return 0;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch(message)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
}
break;
case WM_LBUTTONDOWN:
for(int i=0;i<30;i++)
{
if(!Bullet[i].exist)
{
Bullet[i].x=g_iXnow;
Bullet[i].y=g_iYnow+30;
Bullet[i].exist=true;
g_iBulletNum++;
break;
}
}
case WM_MOUSEMOVE:
g_iX=LOWORD(lParam);
if(g_iX>WINDOW_WIDTH-317)
g_iX=WINDOW_WIDTH-317;
else if(g_iX<0)
g_iX=0;
g_iY=HIWORD(lParam);
if(g_iY>WINDOW_HEIGHT-283)
g_iY=WINDOW_HEIGHT-283;
else if(g_iY<-200)
g_iY=-200;
break;
case WM_DESTROY:
Game_CleanUp(hwnd);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
BOOL Game_Init(HWND hwnd)
{
HBITMAP bmp;
g_hdc=GetDC(hwnd);
g_mdc=CreateCompatibleDC(g_hdc);
g_bufdc=CreateCompatibleDC(g_hdc);
bmp=CreateCompatibleBitmap(g_hdc,WINDOW_WIDTH,WINDOW_HEIGHT);
g_iX=300;
g_iY=100;
g_iXnow=300;
g_iYnow=100;
SelectObject(g_mdc,bmp);
g_hSwordMan=(HBITMAP)LoadImage(NULL,L"swordman.bmp",IMAGE_BITMAP,317,283,LR_LOADFROMFILE);
g_hSwordBlade=(HBITMAP)LoadImage(NULL,L"swordblade.bmp",IMAGE_BITMAP,100,26,LR_LOADFROMFILE);
g_hBackGround=(HBITMAP)LoadImage(NULL,L"bg.bmp",IMAGE_BITMAP,WINDOW_WIDTH,WINDOW_HEIGHT,LR_LOADFROMFILE);
POINT pt,lt,rb;
RECT rect;
pt.x=300;
pt.y=100;
ClientToScreen(hwnd,&pt);
SetCursorPos(pt.x,pt.y);
ShowCursor(false);
GetClientRect(hwnd,&rect);
lt.x=rect.left;
lt.y=rect.top;
rb.x=rect.right;
rb.y=rect.bottom;
ClientToScreen(hwnd,<);
ClientToScreen(hwnd,&rb);
rect.left=lt.x;
rect.top=lt.y;
rect.right=rb.x;
rect.bottom=rb.y;
ClipCursor(&rect);
Game_Paint(hwnd);
return TRUE;
}
VOID Game_Paint(HWND hwnd)
{
SelectObject(g_bufdc,g_hBackGround);
BitBlt(g_mdc,0,0,g_iBGOffset,WINDOW_HEIGHT,g_bufdc,WINDOW_WIDTH-g_iBGOffset,0,SRCCOPY);
BitBlt(g_mdc,g_iBGOffset,0,WINDOW_WIDTH-g_iBGOffset,WINDOW_HEIGHT,g_bufdc,0,0,SRCCOPY);
wchar_t str[20]={};
if(g_iXnow<g_iX)
{
g_iXnow+=10;
if(g_iXnow>g_iX)
g_iXnow=g_iX;
}
else
{
g_iXnow-=10;
if(g_iXnow<g_iX)
g_iXnow=g_iX;
}
if(g_iYnow<g_iY)
{
g_iYnow+=10;
if(g_iYnow>g_iY)
g_iYnow=g_iY;
}
else
{
g_iYnow-=10;
if(g_iYnow<g_iY)
g_iYnow=g_iY;
}
SelectObject(g_bufdc,g_hSwordMan);
TransparentBlt(g_mdc,g_iXnow,g_iYnow,317,283,g_bufdc,0,0,317,283,RGB(0,0,0));
SelectObject(g_bufdc,g_hSwordBlade);
if(g_iBulletNum!=0)
for(int i=0;i<30;i++)
if(Bullet[i].exist)
{
TransparentBlt(g_mdc,Bullet[i].x-70,Bullet[i].y+100,100,33,g_bufdc,0,0,100,26,RGB(0,0,0));
Bullet[i].x-=10;
if(Bullet[i].x<0)
{
g_iBulletNum--;
Bullet[i].exist=false;
}
}
HFONT hFont;
hFont=CreateFont(20,0,0,0,0,0,0,0,GB2312_CHARSET,0,0,0,0,TEXT("微软雅黑"));
SelectObject(g_mdc,hFont);
SetBkMode(g_mdc,TRANSPARENT);
SetTextColor(g_mdc,RGB(255,255,0));
swprintf_s(str,L"鼠标X坐标为%d ",g_iX);
TextOut(g_mdc,0,0,str,wcslen(str));
swprintf_s(str,L"鼠标Y坐标为%d ",g_iY);
TextOut(g_mdc,0,20,str,wcslen(str));
BitBlt(g_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,g_mdc,0,0,SRCCOPY);
g_tPre=GetTickCount();
g_iBGOffset+=5;
if(g_iBGOffset==WINDOW_WIDTH)
g_iBGOffset=0;
}
BOOL Game_CleanUp(HWND hwnd)
{
DeleteObject(g_hBackGround);
DeleteDC(g_bufdc);
DeleteDC(g_mdc);
ReleaseDC(hwnd,g_hdc);
return TRUE;
}
照着书打的,看看怎么样
射击游戏demo.rar
(2.62 MB)