模仿写了个win32雷霆战机游戏,加载不了图片
就是消息循环用while(msg.message != WM_QUIT) if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {TranslateMessage(&msg); DispatchMessage(&msg);} 敌军图片一加载出来就消失,子弹正常,当用while (GetMessage(&msg, nullptr, 0, 0)) if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {TranslateMessage(&msg); DispatchMessage(&msg);} //这是自动生成的代码,上面那是别人写好的程序用的。时就加载不出敌军的图片但会判断撞没撞。这是怎么回事啊,我估计是消息循环的原因但我不大懂#include "stdafx.h"
#include "雷霆战机.h"
#include"time.h"
#include<mmsystem.h>
#include<stdlib.h>
#pragma comment(lib,"winmm.lib")
#define MAX_LOADSTRING 100
#define WINDOW_WIDTH 700
#define WINDOW_HEIGHT 600
typedef struct node
{
int x;
int y;
struct node* pnext;
}Node;
// 全局变量:
Node* pb;
Node* pe;
HINSTANCE hInst; // 当前实例
WCHAR szTitle; // 标题栏文本
WCHAR szWindowClass; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HBITMAP g_hBackbmp, g_hBullet, g_hEnemy, g_hMine;
int h=0;//hight
int g_mineX, g_mineY;
HDC g_hdc, g_memdc, g_bufdc;//设备上下文
DWORD g_mtpre, g_mnow, g_bpre, g_bnow, g_tpre, g_tnow, g_epre, g_enow, g_empre, g_emnow;
DWORD judge1, judge2;
HBITMAP bg;
HWND hWnd;
int u = 0;
//函数的声明
BOOL GameInit(HWND hWnd);
void game_Paint();
void game_clear(HWND hwnd);
void createlist();
void AddNode(Node* P, int n);
void Move(Node* P, int a);
void DeleteNode(int a);
void shoot(HWND hWnd);
int APIENTRY wWinMain( HINSTANCE hInstance, //_In__In_opt_
HINSTANCE hPrevInstance,//
LPWSTR lpCmdLine,//_In_
int nCmdShow)//_In_
{
// UNREFERENCED_PARAMETER(hPrevInstance);//unreferenced_parameter
//UNREFERENCED_PARAMETER(lpCmdLine);//
// TODO: 在此放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
if (!GameInit(hWnd))
return -1;
PlaySound(L"./素材/痴心不改—金池.wav", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));
MSG msg = { 0 };
// 主消息循环:
//while (GetMessage(&msg, nullptr, 0, 0))
while(msg.message != WM_QUIT)
{
/*if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}*/
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))//消息队列去获得消息 PM_REMOVE
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
g_mnow = GetTickCount();
g_tnow = GetTickCount();
g_emnow = GetTickCount();
judge2 = GetTickCount();
if (g_mnow - g_mtpre >= 5)
{
Move(pb, 1);
g_mtpre = g_mnow;
}
if (g_emnow - g_empre >= 20)//敌军
{
Move(pe, 2);
g_emnow = g_empre;
}
if (g_tnow - g_tpre >= 10)
game_Paint();
shoot(hWnd);
}
}
// game_clear(hWnd);
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_MY));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MY);
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 = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
300, 80, 700,600, nullptr, nullptr, hInstance, nullptr);//CW_USEDEFAULT
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
//函数: 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_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
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 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE://WM_
createlist();
break;
case WM_KEYDOWN://接受键盘 keydowm
switch (wParam)
{
case VK_UP:
g_mineY -= 7;
break;
case VK_DOWN:
g_mineY += 7;
break;
case VK_LEFT:
g_mineX -= 7;
break;
case VK_RIGHT:
g_mineX += 7;
break;
}
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;
}
BOOL GameInit(HWND hWnd)
{
srand((unsigned int)time(NULL));
g_hdc = GetDC(hWnd);//得到窗口环境句柄
g_memdc = CreateCompatibleDC(g_hdc);//创建兼容设备环境
g_bufdc = CreateCompatibleDC(g_hdc);
bg = CreateCompatibleBitmap(g_hdc, WINDOW_WIDTH, WINDOW_HEIGHT);//画布
//(HBITMAP)LoadImage(NULL, L"./素材/BG2.bmp", IMAGE_BITMAP, WINDOW_WIDTH, WINDOW_HEIGHT, LR_LOADFROMFILE);
g_hMine = (HBITMAP)LoadImage(NULL, L"./素材/mine.bmp", IMAGE_BITMAP, 200, 100, LR_LOADFROMFILE);
g_hBullet=(HBITMAP)LoadImage(NULL, L"./素材/bullet.bmp", IMAGE_BITMAP,120,25,LR_LOADFROMFILE);
g_hEnemy=(HBITMAP)LoadImage(NULL, L"./素材/enemy.bmp", IMAGE_BITMAP, 100, 34, LR_LOADFROMFILE);
g_hBackbmp= (HBITMAP)LoadImage(NULL, L"./素材/BG2.bmp", IMAGE_BITMAP, 800, 2408, LR_LOADFROMFILE);
SelectObject(g_memdc, bg);//SelectObject
g_mineX = (WINDOW_WIDTH - 100) / 2;
g_mineY = WINDOW_HEIGHT - 150;
return TRUE;
}
void game_Paint()//游戏的绘制
{
SelectObject(g_bufdc, g_hBackbmp);
BitBlt(g_memdc, 0, 0, WINDOW_WIDTH, h, g_bufdc,0, 2048-h, SRCCOPY);
BitBlt(g_memdc,0,h,WINDOW_WIDTH,WINDOW_HEIGHT-h,g_bufdc,0,0,SRCCOPY);
if (++h >= 2048)
{
h = 0;
}
//透明绘图
g_bnow = GetTickCount();
if (g_bnow - g_bpre >= 500)
{
AddNode(pb, 1);
g_bpre = g_bnow;
}
Node *pbtemporary = pb->pnext;//画子弹
SelectObject(g_bufdc, g_hBullet);
while (pbtemporary != NULL)
{
BitBlt(g_memdc,pbtemporary->x,pbtemporary->y,60,25,g_bufdc,60,0,SRCAND);//srcand 把黑的刷到目标文件,白色不刷
BitBlt(g_memdc, pbtemporary->x, pbtemporary->y, 60, 25, g_bufdc, 0, 0, SRCPAINT);//srcpaint 白色区域刷到目标文件中,黑色不刷
pbtemporary = pbtemporary->pnext;
}
g_enow = GetTickCount();//enemy
if (g_enow - g_epre>=1000)
{
AddNode(pe, 2);
g_epre = g_enow;
}
Node *petemp = pe->pnext;
SelectObject(g_bufdc, g_hEnemy);//画敌军
while (petemp != NULL)
{
BitBlt(g_memdc, petemp->x, petemp->y, 50, 34, g_bufdc, 0, 0, SRCAND);
BitBlt(g_memdc, petemp->x, petemp->y, 50, 34, g_bufdc,50,0, SRCPAINT);
petemp = petemp->pnext;
}
SelectObject(g_bufdc, g_hMine);
BitBlt(g_memdc, g_mineX, g_mineY, 100, 100, g_bufdc,0,0, SRCAND);
BitBlt(g_memdc, g_mineX, g_mineY, 100, 100, g_bufdc, 100, 0, SRCPAINT);
BitBlt(g_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, g_memdc, 0, 0, SRCCOPY);
g_tpre = GetTickCount();
}
void game_clear(HWND hwnd)//资源清理
{
ReleaseDC(hwnd,g_hdc);
DeleteObject(g_bufdc);
DeleteObject(g_memdc);
DeleteObject(bg);
return;
}
void createlist()
{
pb = (Node*)malloc(sizeof(Node));
pb->pnext = NULL;
pe = (Node*)malloc(sizeof(Node));
pe->pnext = NULL;
g_mtpre = GetTickCount();
g_bpre = GetTickCount();
g_epre = GetTickCount();
g_empre = GetTickCount();
judge1 = GetTickCount();
}
void AddNode(Node* P,int n)
{
Node* pnew = (Node*)malloc(sizeof(Node));
if (n==1)
{
pnew->x = g_mineX + 20;
pnew->y = g_mineY;
}
else if (n == 2)//enemy
{
pnew->x = rand() %( WINDOW_WIDTH-10);
pnew->y = 0;
}
pnew->pnext = P->pnext;
P->pnext = pnew;
}
void Move(Node* P,int a)//移动
{
if (a == 1)
{
P = pb->pnext;
while (P != NULL)
{
P->y -= 5;
P = P->pnext;
}
}
if (a == 2)
{
P = pe->pnext;
while (P != NULL)
{
P->y += 5;
P = P->pnext;
}
}
DeleteNode(a);
}
void shoot(HWND hWnd)
{
//判断与敌机是否相撞
//击中敌机子弹就消失
Node * pbfront= pb->pnext, *pblater=pb;
Node * pefront, *pelater=pe;
pefront = pe->pnext;
while (pefront != NULL)
{
if (pefront->x >= g_mineX -100 && pefront->x <= g_mineX + 100 && pefront->y >= g_mineY + 34 && pefront->y<= g_mineY + 100)
{
MessageBox(hWnd, L"你怎么这么笨,这也能撞死!", L"消息", MB_OK);
PostQuitMessage(0);
break;
}
else
{
pefront = pefront->pnext;
}
}
//子弹击中敌机没有
pefront = pe->pnext;
while (pefront != NULL)//每一个敌军
{
pbfront = pb->pnext;
pblater = pb;
while (pbfront != NULL)//每一个子弹
{
if (pbfront->x >= pefront->x - 100 && pbfront->x <= pefront->x + 100 && pbfront->y <= pefront->y + 34 && pbfront->y >= pefront->y - 34)
{
pblater->pnext = pbfront->pnext;
free(pbfront);
pelater->pnext = pefront->pnext;
free(pefront);
break;
}
else
{
pbfront = pbfront->pnext;
pblater = pblater->pnext;
}
}
pefront = pelater->pnext;//把链表接起来
if (pefront != NULL)
{
pefront = pefront->pnext;
pelater = pelater->pnext;
}
}
}
void DeleteNode(int a)
{
if (a == 1)
{
Node* P = pb->pnext;
Node*P1 = pb;
while (P != NULL)
{
if (P != NULL&&P->y <= 0)
{
P1->pnext = P->pnext;
free(P);
P = P1->pnext;
}
if (P != NULL)
{
P1 = P1->pnext;
P = P->pnext;
}
}
}
else if (a == 2)
{
Node* P = pe->pnext;
Node* P1 = pe;
while (P != NULL)
{
if (P != NULL&&P->y >= 700)
{
P1->pnext = P->pnext;
//pe->pnext = P->pnext;
free(P);
P = P1->pnext;
}
if (P != NULL)
{
P1 = P1->pnext;
P = P->pnext;
}
}
}
}
问题解决了
页:
[1]