您的位置:首页 > 其它

[游戏模版15] Win32 飞机射击

2014-05-17 14:57 375 查看
>_<:Only give you the code,try to understand it!

>_<:picture resource

#include <windows.h>
// C 运行时头文件
#include <stdlib.h>
#include <cstdio>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <string>

//定义结构,飞机子弹
struct BULLET{
int x,y;
bool exist;
};

// 全局变量:
HINSTANCE hInst;                                // 当前实例

HBITMAP bg,ship,bullet;//背景图,飞机图,子弹图
HDC hdc,mdc,bufdc;
HWND hWnd;
int x,y,nowX,nowY;//鼠标坐标,飞机坐标(贴图坐标)
int w=0,bcount;//滚动背景所要剪切的宽度,子弹数目
BULLET b[30];//存储飞机发出的子弹

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
void                MyPaint(HDC hdc);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR    lpCmdLine,
int       nCmdShow){

MSG msg;
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow)){
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
ATOM MyRegisterClass(HINSTANCE hInstance){
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style            = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra        = 0;
wcex.cbWndExtra        = 0;
wcex.hInstance        = hInstance;
wcex.hIcon            = NULL;
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName    = "Beautifulzzzz";
wcex.lpszClassName    = "Beautifulzzzz";
wcex.hIconSm        = NULL;

return RegisterClassEx(&wcex);
}

//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//        1.设定飞机的初始位置
//        2.设定鼠标位置及隐藏
//        3.设定鼠标光标移动区域
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){

HBITMAP bmp;
POINT pt,lt,rb;
RECT rect;

hInst = hInstance; // 将实例句柄存储在全局变量中

hWnd = CreateWindow("Beautifulzzzz","Beautifulzzzz", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,10,10,640,480,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

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

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

bg=(HBITMAP)LoadImageA(NULL,"bg.bmp",IMAGE_BITMAP,648,480,LR_LOADFROMFILE);
ship=(HBITMAP)LoadImageA(NULL,"ship.bmp",IMAGE_BITMAP,100,148,LR_LOADFROMFILE);
bullet=(HBITMAP)LoadImageA(NULL,"bullet.bmp",IMAGE_BITMAP,10,20,LR_LOADFROMFILE);

x=300;
y=300;
nowX=300;
nowY=300;

//设定鼠标光标位置
pt.x=300;
pt.y=300;
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);

SetTimer(hWnd,1,50,NULL);
MyPaint(hdc);

return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND    - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY    - 发送退出消息并返回
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
int i;
int wmId, wmEvent;
PAINTSTRUCT ps;

switch (message)
{
case WM_KEYDOWN:          //按下消息
if(wParam==VK_ESCAPE) //按下[esc]
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:      //单击鼠标左键消息
for(i=0;i<30;i++)
{
if(!b[i].exist)
{
b[i].x=nowX;
b[i].y=nowY+30;
b[i].exist=true;
bcount++;
break;
}
}
case WM_MOUSEMOVE:
x=LOWORD(lParam);    //取得鼠标x坐标
if(x>530)
x=530;
else if(x<0)
x=0;

y=HIWORD(lParam);
if(y>380)
y=380;
else if(y<0)
y=0;

break;
case WM_TIMER:
A:MyPaint(hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
ClipCursor(NULL);//回复鼠标移动区域

DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
DeleteObject(bullet);
DeleteObject(ship);
ReleaseDC(hWnd,hdc);

PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

//1.设定飞机坐标并进行贴图
//2.设定所有子弹坐标并进行贴图
//3.显示真正的鼠标坐标所在的坐标
void MyPaint(HDC hdc){
char str[20]="";
int i;

//贴上背景图
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,w,480,bufdc,640-w,0,SRCCOPY);
BitBlt(mdc,w,0,640-w,480,bufdc,0,0,SRCCOPY);

//飞机坐标向鼠标坐标位置靠近
if(nowX<x){
nowX+=10;
if(nowX>x)
nowX=x;
}else{
nowX-=10;
if(nowX<x)
nowX=x;
}

if(nowY<y){
nowY-=10;
if(nowY<y)
nowY=y;
}else{
nowY+=10;
if(nowY>y)
nowY=y;
}

//贴上飞机图
SelectObject(bufdc,ship);
BitBlt(mdc,nowX,nowY,100,74,bufdc,0,74,SRCAND);
BitBlt(mdc,nowX,nowY,100,74,bufdc,0,0,SRCPAINT);

SelectObject(bufdc,bullet);
if(bcount!=0)
for(i=0;i<30;i++)
if(b[i].exist){
//贴上子弹图
BitBlt(mdc,b[i].x,b[i].y,10,10,bufdc,0,10,SRCAND);
BitBlt(mdc,b[i].x,b[i].y,10,10,bufdc,0,0,SRCPAINT);

b[i].x-=10;
if(b[i].x<0){
bcount--;
b[i].exist=false;
}
}

//显示鼠标坐标
sprintf(str,"x坐标:  %d   ",x);
TextOutA(mdc,0,0,str,strlen(str));
sprintf(str,"y坐标:  %d   ",y);
TextOutA(mdc,0,20,str,strlen(str));

BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

w+=10;
if(w==640)
w=0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: