#pragma comment(lib,"winmm.lib")
#include <Windows.h>
#include <wchar.h>
#include <chrono>
#include "Map.h"
#include "Pacman.h"
#include "Beans.h"
#include "HitTest.h"
#include "Ghost.h"
//定义宏
#define WINDOW_WIDTH 820
#define WINDOW_HEIGHT 680
#define WINDOW_TITLE L"Hello World!"
//函数声明
//处理消息函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
BOOL Game_Cleanup(HWND hwnd);
BOOL Game_Init(HWND hwnd);
VOID Game_Paint(HWND hwnd);
//声明全局变量
HDC ghdc = nullptr, gmdc = nullptr, buffDC = nullptr;
HBITMAP beanBitmap = nullptr , gameBitmap = nullptr, emptyBitmap = nullptr;
std::unique_ptr<Map> map = nullptr;
std::unique_ptr<Pacman> pacman = nullptr;
std::unique_ptr<Beans> beans = nullptr;
std::unique_ptr<Ghost> ghost = nullptr;
HFONT hFont = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, L"叶根友圆趣卡通体");
std::chrono::time_point<std::chrono::system_clock> tick;
//lpCmdLine 命令行参数
//nCmdShow 指定程序窗口如何显示
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;//设置消息处理函数
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = (HICON)LoadImage(NULL, L"icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszClassName = NULL;
wndClass.lpszClassName = L"HWWND";
if (!RegisterClassEx(&wndClass)) return -1;
HWND hwnd = CreateWindow(L"HWWND", WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
//MoveWindow(hwnd, 100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, true);
ShowWindow(hwnd, nCmdShow);
//UpdateWindow(hwnd);
Game_Init(hwnd);
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);//这个会把消息发送到winproc处理函数
}
//这里处理游戏tick
auto diff = std::chrono::system_clock::now() - tick;
if (std::chrono::duration<double, std::milli>(diff).count() >= 16)
{
ghost->update();
pacman->update();
HitTest::HitObject(*pacman, *ghost, *map, *beans);
Game_Paint(hwnd);
tick = std::chrono::system_clock::now();
}
}
UnregisterClass(L"HWWND", hInstance);
return 0;
}
//消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//PAINTSTRUCT paintStruct;
switch (message)
{
// 去的设备环境句柄的两种方法
//1. HDC BeginPaint(__in HWND hwnd, __out LPPINTSTRUCT lpPaint) 和 BOOL EndPaint(__in HWND hwnd, __in const PAINTSTRUCT *lpPaint)
//2. HDC GetDC(__in HWND hWnd) 和 int ReleaseDC(__in HWND hWnd, __in HDC hDC)
/*case WM_PAINT:
ghdc = BeginPaint(hwnd, &paintStruct);
Game_Paint(hwnd);
EndPaint(hwnd, &paintStruct);
ValidateRect(hwnd, NULL);
break;*/
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
{
DestroyWindow(hwnd);//销毁窗体并发送一个WM_DESTROY消息
}
if (wParam == VK_LEFT)
{
pacman->setDir(2);
}
if (wParam == VK_RIGHT)
{
pacman->setDir(0);
}
if (wParam == VK_UP)
{
pacman->setDir(3);
}
if (wParam == VK_DOWN)
{
pacman->setDir(1);
}
break;
case WM_DESTROY:
Game_Cleanup(hwnd);
PostQuitMessage(0);
break;
default://这里一定不要忘记写这个!
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL Game_Init(HWND hwnd)
{
ghdc = GetDC(hwnd);
gmdc = CreateCompatibleDC(ghdc);
buffDC = CreateCompatibleDC(ghdc);
emptyBitmap = CreateCompatibleBitmap(ghdc, WINDOW_WIDTH, WINDOW_HEIGHT);
SelectObject(gmdc, emptyBitmap);
gameBitmap = (HBITMAP)LoadImage(nullptr, L"1.bmp", IMAGE_BITMAP, 448, 128, LR_LOADFROMFILE);
beanBitmap = (HBITMAP)LoadImage(nullptr, L"2.bmp", IMAGE_BITMAP, 96, 32, LR_LOADFROMFILE);
map = std::make_unique<Map>(25, 20,
std::vector<int>{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0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_WIDTH, WINDOW_HEIGHT, gmdc, 0, 0, SRCCOPY);*/
//先清空gmdc的画布
RECT clientArea;
GetClientRect(hwnd, &clientArea);
HBRUSH brush = (HBRUSH)GetStockObject(WHITE_BRUSH);
FillRect(gmdc, &clientArea, brush);
map->draw(gmdc, buffDC, gameBitmap);
beans->draw(gmdc, buffDC, beanBitmap);
pacman->draw(gmdc, buffDC, gameBitmap);
ghost->draw(gmdc, buffDC, gameBitmap);
SelectObject(gmdc, hFont);
SetBkMode(gmdc, TRANSPARENT);
SetTextColor(gmdc, RGB(62, 255, 80));
wchar_t t[20];
wsprintf(t, L"Score %d", HitTest::score);
TextOut(gmdc, 30, 5, t, wcslen(t));
BitBlt(ghdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, gmdc, 0, 0, SRCCOPY);
}
BOOL Game_Cleanup(HWND hwnd)
{
DeleteObject(hFont);
DeleteObject(gameBitmap);
DeleteDC(buffDC);
DeleteDC(gmdc);
ReleaseDC(hwnd, ghdc);
return TRUE;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Pacman GDI C++ GAME
共64个文件
tlog:12个
h:11个
obj:10个
需积分: 32 59 下载量 54 浏览量
2016-08-06
17:26:35
上传
评论
收藏 7.61MB RAR 举报
温馨提示
学习 GDI 的时候写的吃豆人游戏,由于业余时间有限,几乎是想到哪写到哪, 有很多代码结构不规范的地方。 完成的功能有: 1. 地图的布局 2. 吃豆人 的键盘控制 3. 吃豆人 与墙壁和豆子的碰撞检测 4. 吃豆人 吃豆子时的声音和记分 5. 幽灵 与墙壁的碰撞检测 6. 幽灵 的 AI 其中幽灵的 AI 为了简便,主要是用了绕路算法。 开发IDE: Visual Studio 2015 用到了 C++11 和 C++14的一些特性
资源推荐
资源详情
资源评论
收起资源包目录
GDI Pacman GAME.rar (64个子文件)
Win32Project1
Win32Project1.sln 1KB
Debug
Win32Project1.pdb 1.41MB
2.bmp 9KB
1.wav 32KB
Win32Project1.exe 159KB
1.bmp 168KB
Win32Project1.ilk 658KB
1.png 29KB
2.png 2KB
Win32Project1.VC.db 30.56MB
Win32Project1
x64
Debug
Win32Project1.log 343B
vc140.idb 1.12MB
Map.obj 373KB
Pacman.obj 81KB
vc140.pdb 644KB
main.obj 551KB
Ghost.obj 82KB
Beans.obj 305KB
Win32Project1.tlog
link.read.1.tlog 5KB
link.command.1.tlog 2KB
CL.command.1.tlog 3KB
CL.write.1.tlog 3KB
CL.read.1.tlog 163KB
link.write.1.tlog 1KB
Win32Project1.lastbuildstate 205B
Beans.h 304B
main.cpp 7KB
Map.cpp 663B
GameObject.h 635B
Win32Project1.vcxproj 8KB
IMoveable.h 242B
Constants.h 284B
2.bmp 9KB
1.wav 32KB
Pacman.h 533B
Naruto.bmp 3MB
Ghost.cpp 1KB
Debug
Win32Project1.log 184B
vc140.idb 1.12MB
Map.obj 262KB
Pacman.obj 70KB
vc140.pdb 636KB
main.obj 391KB
Ghost.obj 71KB
Beans.obj 221KB
Win32Project1.tlog
link.read.1.tlog 4KB
link.command.1.tlog 2KB
CL.command.1.tlog 3KB
CL.write.1.tlog 3KB
CL.read.1.tlog 164KB
link.write.1.tlog 1KB
Win32Project1.lastbuildstate 207B
Bean.h 758B
Beans.cpp 344B
Pacman.cpp 958B
IDrawable.h 190B
Win32Project1.vcxproj.filters 2KB
1.bmp 168KB
Ghost.h 755B
Grid.h 743B
HitTest.h 6KB
Map.h 393B
1.png 29KB
2.png 2KB
共 64 条
- 1
资源评论
DreamSoar
- 粉丝: 37
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功