今年的c++面向对象大作业 老师让我们自己做东西 然后我想了想 不如做一个2048吧 不过这游戏我从来没有赢过
好 我们开始看代码吧 let‘s go;
#include<windows.h>
#include<mmsystem.h>//导入声音#include<stdio.h>//导入声音
#pragma comment(lib, "WINMM.LIB")//导入声音
int make1(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow);//声明子对象的窗口
这边是我写的头文件“head.h”
主要是为了界面的转换和添加游戏背景音乐
接下来是我的主界面程序 说实话UI界面很丑很丑很丑 虽然想做的好看一点 但是能力有限
code.cpp
#include"head.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#define IDBUTTON1 201//按钮
#define IDBUTTON2 202
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口函数说明
//全局窗口句柄
HBITMAP hbmp;
HWND hWnd;
//#define IDBUTTON3 203
HINSTANCE hInstance2;
HINSTANCE hPrevInst2;
LPSTR lpszCmdLine2;
int nCmdShow2;
HINSTANCE hInstance;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
hInstance2=hInstance;
hPrevInst2=hPrevInstance;
lpszCmdLine2=szCmdLine;
nCmdShow2=iCmdShow;
TCHAR *Name = TEXT("2048");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;//结构体
wndclass.cbClsExtra = 0;//窗口类无扩展
wndclass.cbWndExtra = 0;//窗口实例无扩展
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//窗口背景为白色
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);//窗口采用箭头光标
//wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//窗口的最小化图标
wndclass.hIcon = (HICON)LoadImage(NULL, "255", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
wndclass.hInstance = hInstance;//创建此窗口的应用程序的当前句柄
wndclass.lpfnWndProc = WndProc;//默认窗口实现程序是WndProc这个函数实现的
wndclass.lpszClassName = szAppName;//窗口类名15行
wndclass.lpszMenuName = NULL;//窗口中无菜单
wndclass.style = CS_HREDRAW | CS_VREDRAW;//缺省类型
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("无法注册窗口类"), TEXT("注册错误"), MB_ICONERROR);
return -1;
}
hwnd = CreateWindow(Name,//窗口类名
TEXT("2048"),//窗口实例的标题名
WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX|WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT, 430,450,//窗口大小
NULL, //此窗口无父窗口
NULL, //此窗口无主菜单
hInstance, //创建此窗口的应用程序的当前句柄
NULL); //不使用该值
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hButton1;
static HWND hButton2;
/*static HWND hButton2;*/
switch (msg)
{
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
HDC hdcMem;
HBITMAP hbmp; //一张位图的句柄
BITMAP bmp;
hdc = BeginPaint(hwnd,&ps); //为指定句柄hwnd窗口进行绘画准备工作
hdcMem = CreateCompatibleDC(hdc); //创建一个与指定设备兼容的内存设备上下文环境
hbmp = (HBITMAP)LoadImage(NULL, "2048.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
GetObject(hbmp, sizeof(BITMAP), &bmp); //得到一个位图对象
SelectObject(hdcMem, hbmp);
BitBlt(hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY); //显示位图
DeleteDC(hdcMem);
DeleteObject(hbmp);
EndPaint(hwnd,&ps);
}
break;
case WM_CREATE:
{
hButton1 = CreateWindow("Button", "开始游戏 ", WS_VISIBLE | WS_CHILD
| BS_PUSHBUTTON,60, 310,135, 50, hwnd, (HMENU)IDBUTTON1, hInstance, NULL);
hButton2 = CreateWindow("Button", "游戏玩法 ", WS_VISIBLE | WS_CHILD
| BS_PUSHBUTTON,250, 310,135, 50, hwnd, (HMENU)IDBUTTON2, hInstance, NULL);
//newNum();
hbmp=(HBITMAP)LoadImage(NULL,"2048.bmp",IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
//break;
PlaySound("123.wav",NULL,SND_ASYNC|SND_LOOP);
}
break;
case WM_COMMAND://事件监听
{
switch(wParam){
case IDBUTTON1:
make1(hInstance2,hPrevInst2, lpszCmdLine2, nCmdShow2);
break;
case IDBUTTON2:
MessageBox(hwnd, TEXT("\n通过上下左右来控制\n遇到一样的会相加\n知道出现2048或方格都被铺满"), TEXT("游戏玩法"), MB_OK);
break;
}
}
break;
case WM_DESTROY://关闭应用程序
PostQuitMessage(0);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
大部分我都做了注释相信应该没有什么问题
接下来是我的游戏窗口界面
由于前面的窗口创建部分雷同我就不放上去了
void Num()
{
int num = 0;//一个在0至9之间的数
int xPos = 0, yPos = 0;
srand(time(0));
do{
xPos = rand() % 4;
yPos = rand() % 4;
} while (arr[xPos][yPos] != 0);
num = rand() % 10;
if (num < 7)
arr[xPos][yPos] = 2;
else
arr[xPos][yPos] = 4;
}
//画数组
void DrawCanvas(HWND hwnd)
{
HBRUSH hBrush;//bBrush为画刷句柄
HDC hdc;//定义指向设备的句柄
RECT rt;
TCHAR szBuffer[10];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (arr[i][j] != 0)
{
LOGFONT logfont;
hdc = GetDC(hwnd);
//设置数字的字体大小
ZeroMemory(&logfont, sizeof(LOGFONT));
logfont.lfCharSet = GB2312_CHARSET;
logfont.lfHeight = -50; //设置字体的大小
HFONT hFont = CreateFontIndirect(&logfont);
SetTextColor(hdc, RGB(255, 255, 255));
SetBkColor(hdc, RGB(200, 200, 0));
SetBkMode(hdc, TRANSPARENT);
SelectObject(hdc, hFont);
//创建画刷
hBrush = CreateSolidBrush(RGB((arr[i][j] * 10) % 256, (arr[i][j]*40)%256, 0));
SetRect(&rt, j * 100, i * 100, j * 100 + 100, i * 100 + 100);
FillRect(hdc, &rt, hBrush);
TextOut(hdc, j * 100 , i * 100 + 25, szBuffer, wsprintf(szBuffer, TEXT("%d"), arr[i][j]));
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
else//背景
{
SetRect(&rt, j * 100, i * 100, j * 100 + 100, i * 100 + 100);
hdc = GetDC(hwnd);
hBrush = CreateSolidBrush(RGB(200, 250, 0));//创建具有指定颜色的单色画刷
FillRect(hdc, &rt, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);//删除画刷
}
}
}
}
//判断数组是否满了
bool isFull()
{
bool full = true;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (arr[i][j] == 0)
{
full = false;
}
}
}
return full;
}
bool end()
{
bool end=true;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(arr[i][j]==2048)
end=true;
}
return end;
}
//处理数组函数
void changeArr(int direct)
{
bool xiaoyici = false;
bool chendi = false;
switch (direct)
{
//上键 done
case 0:
{
//开始数组操作
for (int i = 0; i < 4; i++)
{
for (int a = 0; a < 4; a++){
//1先沉底
for (int b = 0; b < 4; b++)
{
for (int m = 0; m < 4; ++m)
{
for (int k = m; k < 3; ++k)
{
if (arr[k][i] == 0 && arr[k+1][i]!=0)
{
chendi = true;
arr[k][i] = arr[k + 1][i];
arr[k + 1][i] = 0;
}
}
}
}
//2两两比较
for (int j = a; j < 4; ++j)
{
if (arr[j][i] != 0 && arr[j][i] == arr[j + 1][i])
{
xiaoyici = true;
arr[j][i] <<= 1;
arr[j + 1][i] = 0;
j++;
a++;
}
else
{
continue;
}
}
}
}//结束数组操作
//判断是否死亡
if (!xiaoyici && isFull())
{
MessageBox(g_hwnd, TEXT("you lose"), TEXT("wrong"), MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}
/*if(end())
{
MessageBox(g_hwnd,TEXT("you win"),TEXT("great"),MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}*/
else{
//随机生成一个新的2或4放入数组
if (xiaoyici || chendi){ newNum(); }
}
}
break;
//下键
case 1:
{
//开始数组操作
for (int i = 0; i < 4; i++)
{
for (int a = 0; a < 4; a++)
{
//1先沉底
for (int b = 3; b >= 0; b--)
{
for (int m = 3; m >= 0; --m)
{
for (int k = m; k > 0; --k)
{
if (arr[k][i] == 0&&arr[k-1][i]!=0)
{
chendi = true;
arr[k][i] = arr[k - 1][i];
arr[k - 1][i] = 0;
}
}
}
}
//2两两比较
for (int j = 3 - a; j >= 0; --j)
{
if (arr[j][i] != 0 && arr[j][i] == arr[j - 1][i])
{
xiaoyici = true;
arr[j][i] += arr[j][i];
arr[j - 1][i] = 0;
a++;
--j;
}
else
{
continue;
}
}
}
}
//结束数组操作
//判断是否死亡
if (!xiaoyici && isFull())
{
MessageBox(g_hwnd, TEXT("you lose"), TEXT("wrong"), MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}
/*if(end())
{
MessageBox(g_hwnd,TEXT("you win"),TEXT("great"),MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}*/
else{
//随机生成一个新的2或4放入数组
if (xiaoyici || chendi){ newNum(); }
}
}
break;
//左键done
case 2:
{
//开始数组操作
for (int i = 0; i < 4; i++)
{
for (int a = 0; a < 4; a++){
//1先沉底
for (int b = 0; b < 4; b++)
{
for (int m = 0; m < 4; ++m)
{
for (int k = m; k < 3; ++k)
{
if (arr[i][k] == 0&&arr[i][k+1]!=0)
{
chendi = true;
arr[i][k] = arr[i][k+1];
arr[i][k+1] = 0;
}
}
}
}
//2两两比较
for (int j = a; j < 4; ++j)
{
if (arr[i][j] != 0 && arr[i][j] == arr[i][j+1])
{
xiaoyici = true;
arr[i][j] +=arr[i][j];
arr[i][j+1] = 0;
j++;
a++;
}
else
{
continue;
}
}
}
}//结束数组操作
//判断是否死亡
if (!xiaoyici && isFull())
{
MessageBox(g_hwnd, TEXT("you lose"), TEXT("wrong"), MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}
/*if(end())
{
MessageBox(g_hwnd,TEXT("you win"),TEXT("great"),MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}*/
else{
//随机生成一个新的2或4放入数组
if (xiaoyici || chendi)
{
newNum();
}
}
}
break;
//右键
case 3:
{
//开始数组操作
for (int i = 0; i < 4; i++)
{
for (int a = 0; a < 4; a++)
{
//1先沉底
for (int b = 3; b >= 0; b--)
{
for (int m = 3; m >= 0; --m)
{
for (int k = m; k > 0; --k)
{
if (arr[i][k] == 0&&arr[i][k-1]!=0)
{
chendi = true;
arr[i][k] = arr[i][k-1];
arr[i][k-1] = 0;
}
}
}
}
//2两两比较
for (int j = 3 - a; j >= 0; --j)
{
if (arr[i][j] != 0 && arr[i][j] == arr[i][j-1])
{
xiaoyici = true;
arr[i][j] += arr[i][j];
arr[i][j-1] = 0;
a++;
--j;
}
else
{
continue;
}
}
}
}
//结束数组操作
//判断是否死亡
if (!xiaoyici && isFull())
{
MessageBox(g_hwnd, TEXT("you lose"), TEXT("wrong"), MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}
/*if(end())
{
MessageBox(g_hwnd,TEXT("you win"),TEXT("great"),MB_OK);
SendMessage(g_hwnd,WM_DESTROY,0,0);
}*/
else{
//随机生成一个新的2或4放入数组
if (xiaoyici || chendi){ newNum(); }
}
}
break;
default:
return;
}
}
LRESULT CALLBACK WndProc1(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
Num();
}
break;
case WM_KEYDOWN://控制
{
switch (wParam)
{
//上键
case VK_UP:
{
changeArr(0);
DrawCanvas(hwnd);
}
break;
//下键
case VK_DOWN:
{
changeArr(1);
DrawCanvas(hwnd);
}
break;
//如果左键被按下
case VK_LEFT:
{
changeArr(2);
DrawCanvas(hwnd);
}
break;
//如果右键被按下
case VK_RIGHT:
{
changeArr(3);
DrawCanvas(hwnd);
}
break;
default:
break;
}
}
break;
case WM_DESTROY://关闭应用程序
PostQuitMessage(0);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
这部分代码我是从网上一位那边参考过来的 我觉得他的结构写的不错 很感谢他拿过来和大家分享一下
下面放上game.cpp中新的声明
//全局操作数组
int arr[4][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
以上就是我的2048简单小程序 游戏运行部分来自网上 我看了之后添加了背景音乐及一些图片 子窗口之类的 希望对各位有所帮助 如果有好的建议的希望你们能评论 我一定第一时间回复 谢谢各位
ps:不足之处 请大家多多包涵 多多指教 笔芯