是照着书上做的小游戏 想加入背景音乐和给背景换个颜色 还有加一个计时然后显示最终分数的效果 该怎么写呢
代码和现在的运行效果如下
// WindowsProject1.cpp : 定义应用程序的入口点。
//
#include "windows.h"
#include <cstdio>
#include "framework.h"
#include "WindowsProject1.h"
#include"windef.h"
#include "stdbool.h"
#include "stdio.h"
#pragma comment(lib,"Winmm.lib")
#define MAX_LOADSTRING 100
#define _CRT_SECURE_NO_WARNINGS
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int left = 100, top = 20, right = left + 250, bottom = top + 400;
char c1, c2;
int x = -1, y = -1;
int iScoring = 0, iFail = 0;
int gameover = 0;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));
MSG msg;
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW 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 = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目标: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void DrawBk(HDC hdc, int x1, int y1, int x2, int y2)
{
Rectangle(hdc, x1, y1, x2, y2);
char s[100] = "ABCDEFGHIJKIMNOPQRSTUVWXYZ";
TextOutA(hdc, x1 + 5, y2 - 25, s, strlen(s));
}
void ShowScoring(HDC hdc, int x, int y, int iScoring, int iFail)
{
char szTemp[32];
TextOutA(hdc, x, y, "当前得分:", strlen("当前得分:"));
y = y + 20;
sprintf_s(szTemp, "%d", iScoring);
TextOutA(hdc, x, y, szTemp, strlen(szTemp));
y = y + 20;
TextOutA(hdc, x, y, "当前失误:", strlen("当前失误:"));
y = y + 20;
sprintf_s(szTemp, "%d", iFail);
TextOutA(hdc, x, y, szTemp, strlen(szTemp));
}
void GameOver(HDC hdc, int x, int y)
{
COLORREF OldColor, NewColor = RGB(rand() % 255, rand() % 255, rand() % 255);
OldColor = SetTextColor(hdc, NewColor);
TextOutA(hdc, x, y, "GAME OVER !", strlen("GAME OVER !"));
SetTextColor(hdc, OldColor);
}
void Fire(HDC hdc, int x, int y1, int y2)
{
HPEN holdPen, hNewPen = CreatePen(PS_DASHDOTDOT, 1, RGB(21, 104, 255));
holdPen = (HPEN)SelectObject(hdc, hNewPen);
MoveToEx(hdc, x, y1, NULL);
LineTo(hdc, x, y2);
Sleep(100);
HPEN hNewPen2 = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
SelectObject(hdc, hNewPen2);
MoveToEx(hdc, x, y1, NULL);
LineTo(hdc, x, y2);
SelectObject(hdc, holdPen);
DeleteObject(hNewPen);
DeleteObject(hNewPen2);
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目标: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_TIMER:
switch (wParam)
{
case 1:
y = y + iScoring / 10 + 1;
if (y > bottom - 40)
{
gameover = 1;
KillTimer(hWnd, 1);
SetTimer(hWnd, 2, 300, NULL);
}
InvalidateRect(hWnd, 0, 0);
break;
case 2:
InvalidateRect(hWnd, 0, 0);
break;
}
break;
case WM_CHAR:
{
c2 = (wParam >= 'a' && wParam <= 'z') ? wParam + 'A' - 'a' : wParam;
HDC hdc = GetDC(hWnd);
Fire(hdc, left + 5 + (c2 - 'A') * 9 + 4, top, bottom);
ReleaseDC(hWnd, hdc);
if (c2 == c1)
{
c1 = rand() % 26 + 'A';
x = left + 5 + (c1 - 'A') * 9;
y = top;
iScoring = iScoring + 1;
}
else
iFail = iFail + 1;
}
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case ID_START:
if (gameover == 1)
KillTimer(hWnd, 2);
gameover = 0;
iScoring = 0;
iFail = 0;
c1 = rand() % 26 + 'A';
x = left + 5 + (c1 - 'A') * 9;
y = top;
SetTimer(hWnd, 1, 10, NULL);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
DrawBk(hdc, left, top, right, bottom);
ShowScoring(hdc, right + 20, top + 50, iScoring, iFail);
if (gameover)
GameOver(hdc, left + 80, top + 130);
else
{
char szTemp[8];
sprintf_s(szTemp, "%c", c1);
TextOutA(hdc, x, y, szTemp, strlen(szTemp));
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
这里有一个java版本的,可以参考一下 https://blog.csdn.net/luoxueyong/article/details/128123006
计时的可以用定时器(将定时器间隔设为1秒,到时间后触发定时器事件更新显示的时间即可):
C++项目加背景音乐的贪吃蛇小游戏详细教程
和你要的效果是一样的,如有帮助,望采纳
https://blog.csdn.net/weixin_44611048/article/details/104886484