#include <windows.h>
#include <ddraw.h>
#include "ddutil.h"
#include "stdio.h"
#include "resource.h"
//Download by http://www.codefans.net
//函数声明
INT UpdateFrame();
LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);
BOOL InitDDraw( void );
void GetRect(RECT* rect, long left, long top, long width, long height);
void Flip(void);
void DrawAll();
void InitData();
void CheckHit();
void StepAll();
void StartGame();
void DrawNumber(LPDIRECTDRAWSURFACE dd,LPDIRECTDRAWSURFACE ss,int num,int x,int y,int width,int height);
//全局变量
char szClassName[] = "ForEpork";
HWND hwnd;
HINSTANCE hInst;
BOOL bActive;
DWORD delay; //延时
DWORD dwTickCount,dwLastTickCount;
//关于游戏的全局变量
BOOL hit,get;
POINT pos; //木锤位置
int forewidth, foreheight;//前台页面的宽度和高度
RECT backrect; //后台页面映射矩形
int backwidth, backheight;//后台页面的宽度和高度
int mouse[9]; //老鼠洞出现东东的纪录
POINT holepos[9]; //老鼠洞的位置
POINT mousepos[9]; //老鼠头的位置
int status[9]; //老鼠状态,0-4
int behit[9]; //老鼠各个被打的纪录
int mousewidth;
int mouseheight;
POINT pt;
int time; //剩余时间
int score; //分数
int GameStatus; //游戏状态
//DirectDraw变量
LPDIRECTDRAW lpDD;
LPDIRECTDRAWCLIPPER lpClipper;
LPDIRECTDRAWSURFACE lpDDSFront; //以下均为DirectDrawSurface对象
LPDIRECTDRAWSURFACE lpDDSBack;
LPDIRECTDRAWSURFACE lpDDSMouse[3];
LPDIRECTDRAWSURFACE lpDDSHammer;
LPDIRECTDRAWSURFACE lpDDSBk;
LPDIRECTDRAWSURFACE lpDDSLogo;
LPDIRECTDRAWSURFACE lpDDSBow;
LPDIRECTDRAWSURFACE lpDDSFig;
LPDIRECTDRAWSURFACE lpDDSHamfig;
LPDIRECTDRAWSURFACE lpDDSTime;
LPDIRECTDRAWSURFACE lpDDSScore[3];
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nMode)
{
MSG msg;
WNDCLASSEX wcl;
wcl.cbClsExtra = 0;
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wcl.hCursor = LoadCursor(NULL,IDC_ARROW);
wcl.hIcon = LoadIcon(hInstance,"IDI_ICON1");
wcl.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
wcl.hInstance = hInstance;
wcl.lpfnWndProc = WindowFunc;
wcl.lpszClassName = szClassName;
wcl.lpszMenuName = NULL;
wcl.style = 0;
if (!RegisterClassEx(&wcl)) return 0;
hwnd = CreateWindow(
szClassName,
"打老鼠",
WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX|WS_POPUP,
180,
80,
433,
348,
HWND_DESKTOP,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nMode);
UpdateWindow(hwnd);
if (!InitDDraw())
{
MessageBox(GetActiveWindow(), "初始化DirectDraw过程中出错!请检查你是否正确的安装了DirectX。", "Error", MB_OK );
return 0;
}
hInst = hInstance;
InitData();
SetTimer(hwnd,1,200,NULL);
SetTimer(hwnd,2,1000,NULL);
while(1)
{
if( PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
if (!GetMessage(&msg,NULL,0,0))
{
return msg.wParam;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if( bActive)
{
UpdateFrame();
}
else
{
WaitMessage();
}
}
KillTimer(hwnd,1);
return msg.wParam;
}
LRESULT CALLBACK WindowFunc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_ACTIVATEAPP://
bActive = wParam;
break;
case WM_CREATE:
//写入注册表
HKEY hRegKey;
unsigned long result;
char version[10];
sprintf(version,"1.00");
RegCreateKeyEx(HKEY_CURRENT_USER,"Software\\Raynor Game\\For Epork",0,"Epork",0,KEY_ALL_ACCESS,
NULL,&hRegKey,&result);
if (result == REG_CREATED_NEW_KEY)
{
RegSetValueEx(hRegKey,"Version",0,REG_SZ,(LPBYTE)version,4);
}
else
{
RegSetValueEx(hRegKey,"Version",0,REG_SZ,(LPBYTE)version,4);
}
RegCloseKey(hRegKey);
break;
case WM_SETCURSOR:
SetCursor(NULL);
break;
case WM_KEYDOWN:
switch (char(wParam))
{
case VK_SUBTRACT:
delay += 2;
break;
case VK_ADD:
if (delay>2) delay -= 2;
break;
case VK_ESCAPE:
MessageBox(hwnd,"送给你的...:)\n From:\n 耗子\nhttp://rpg.3322.net\n Raynor@ah163.com","关于这个游戏",MB_OK);
PostQuitMessage(0);
break;
}
break;
case WM_LBUTTONDOWN:
if (GameStatus == 1)
{
hit = true;
get = false;
}
else
{
StartGame();
}
break;
case WM_LBUTTONUP:
hit = false;
break;
case WM_DESTROY:
MessageBox(hwnd,"送给你的...:)\n From:\n 耗子\nhttp://rpg.3322.net\n Raynor@ah163.com","关于这个游戏",MB_OK);
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
pos.x = LOWORD(lParam);
pos.y = HIWORD(lParam);
break;
case WM_TIMER:
if (bActive)
{
switch (wParam)
{
case 1:
StepAll();
break;
case 2:
time --;
break;
}
}
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
break;
}
return 0;
}
INT UpdateFrame()
{
if (bActive)
{
dwTickCount = GetTickCount();
if ((dwTickCount - dwLastTickCount) <= delay)
{
return 0;
}
dwLastTickCount = dwTickCount;
//Here to add my code...
DrawAll();
CheckHit();
Flip();
}
return 1;
}
BOOL InitDDraw()
{
DWORD dwFlags;
DDSURFACEDESC ddsd;
//创建DirectDraw对象
DirectDrawCreate(NULL, &lpDD, NULL);
//设置协作级别为窗口模式
dwFlags = DDSCL_NORMAL;
lpDD->SetCooperativeLevel(hwnd, dwFlags);
//创建主页面
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
lpDD->CreateSurface(&ddsd, &lpDDSFront, NULL);
//创建离屏页面
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
RECT rect;
GetClientRect(hwnd, &rect);
forewidth=rect.right-rect.left;
foreheight=rect.bottom-rect.top;
GetRect(&backrect, 0, 0, 433,348);
ddsd.dwWidth = backwidth=forewidth;
ddsd.dwHeight = backheight=foreheight;
lpDD->CreateSurface( &ddsd, &lpDDSBack, NULL );
//创建老鼠页面
ddsd.dwWidth = 124;
ddsd.dwHeight = 425;
lpDD->CreateSurface( &ddsd, &lpDDSMouse[0], NULL );
lpDD->CreateSurface( &ddsd, &lpDDSMouse[1], NULL );
lpDD->CreateSurface( &ddsd, &lpDDSMouse[2], NULL );
DDReLoadBitmap(lpDDSMouse[0], "MOUSE00.BMP");
DDReLoadBitmap(lpDDSMouse[1], "MOUSE01.BMP");
DDReLoadBitmap(lpDDSMouse[2], "MOUSE02.BMP");
DDSetColorKey(lpDDSMouse[0],RGB(0,0,255));
DDSetColorKey(lpDDSMouse[1],RGB(0,0,255));
DDSetColorKey(lpDDSMouse[2],RGB(0,0,255));
//创建木锤
ddsd.dwWidth = 68;
ddsd.dwHeight = 136;
lpDD->CreateSurface( &ddsd, &lpDDSHammer, NULL );
DDReLoadBitmap(lpDDSHammer, "HAM.BMP");
DDSetColorKey(lpDDSHammer,RGB(0,0,255));
//创建火花
ddsd.dwWidth = 68;
ddsd.dwHeight = 24;
lpDD->CreateSurface( &ddsd, &lpDDSBow, NULL );
DDReLoadBitmap(lpDDSBow, "Bow.BMP");
DDSetColorKey(lpDDSBow,RGB(255,0,127));
//创建标题
ddsd.dwWidth = 319;
ddsd.dwHeight = 56;
lpDD->CreateSurface( &ddsd, &lpDDSLogo, NULL );
DDReLoadBitmap(lpDDSLogo, "logo.BMP");
DDSetColorKey(lpDDSLogo,RGB(0,0,255));
//创建背景
ddsd.dwWidth = 433;
ddsd.dwHeight = 348;
lpDD->CreateSurface( &ddsd, &lpDDSBk, NULL );
DDReLoadBitmap(lpDDSBk, "BK.BMP");
//创建时间
ddsd.dwWidth = 45;
ddsd.dwHeight = 13;
lpDD->CreateSurface( &ddsd, &lpDDSTime, NULL );
DDReLoadBitmap(lpDDSTime, "Time.BMP");
DDSetColorKey(lpDDSTime,RGB(255,0,127));
//创建数字
ddsd.dwWidth = 260;
ddsd.dwHeight = 38;
lpDD->CreateSurface( &ddsd, &lpDDSHamfig, NULL );
DDReLoadBitmap(lpDDSHamfig, "Hamfig.BMP");
DDSetColorKey(lpDDSHamfig,RGB(255,0,127));
//创建数字
ddsd.dwWidth = 160;
ddsd.dwHeight = 23;
lpDD->CreateSurface( &ddsd, &lpDDSFig, NULL );
DDReLoadBitmap(lpDDSFig, "fig.BMP");
DDSetColorKey(lpDDSFig,RGB(255,0,127));
//创建分数页面
ddsd.dwWidth = 43;
ddsd.dwHeight = 25;
lpDD->CreateSurface( &ddsd, &lpDDSScore[0], NULL );
lpDD->CreateSurface( &ddsd, &lpDDSScor
寒泊
- 粉丝: 86
- 资源: 1万+
最新资源
- 钢结构焊接连接节点通用图 HUALU 1X02-2010.pdf
- Lecture 11-September 7.pptx
- 国际焊接工程师(IWE)、技术员(IWT)培训教程2006-2007.pdf
- Lecture 12-September 8 (Tutorial).pptx
- Lecture 13-September 10.pptx
- Web网页设计期末大作业(完美解决大作业).zip
- Lecture 14-September 13.pptx
- 本科毕业设计-基于Hyperledger Fabric的分布式身份可信认证区块链端,包含链码、网络启动脚本以及网络交互sdk全部资料+详细文档+高分项目.zip
- 基于fisco bcos区块链实现的nft数字藏品网站,用IPFS进行存储,每次交易均进行上链,实现交易不可篡改,可追溯溯源等功能全部资料+详细文档+高分项目.zip
- 基于go实现的区块链工程全部资料+详细文档+高分项目.zip
- Lecture 15-September 14 (Tutorial)-2 marks.pptx
- 基于go语言的区块链实现全部资料+详细文档+高分项目.zip
- 基于powergate的区块链存储项目全部资料+详细文档+高分项目.zip
- ASME B16.34-2009 中文版 阀门.带法兰、有螺纹和焊接端部.pdf
- 基于Nebulas(nas)区块链开发的Dapp全部资料+详细文档+高分项目.zip
- 基于web3.js数字钱包,区块链助力全球权益投资全部资料+详细文档+高分项目.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈