鱼C论坛

 找回密码
 立即注册
查看: 2385|回复: 1

模仿写了个win32雷霆战机游戏,加载不了图片

[复制链接]
发表于 2017-7-28 20:37:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
就是消息循环用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);} //这是自动生成的代码,上面那是别人写好的程序用的。时就加载不出敌军的图片但会判断撞没撞。这是怎么回事啊,我估计是消息循环的原因但我不大懂
  1. #include "stdafx.h"
  2. #include "雷霆战机.h"
  3. #include"time.h"
  4. #include<mmsystem.h>
  5. #include<stdlib.h>
  6. #pragma comment(lib,"winmm.lib")

  7. #define MAX_LOADSTRING 100
  8. #define WINDOW_WIDTH 700
  9. #define WINDOW_HEIGHT 600
  10. typedef struct node
  11. {
  12.        
  13.         int x;
  14.         int y;
  15.         struct node* pnext;
  16. }Node;

  17. // 全局变量:
  18. Node* pb;
  19. Node* pe;
  20. HINSTANCE hInst;                                // 当前实例
  21. WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
  22. WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

  23. // 此代码模块中包含的函数的前向声明:
  24. ATOM                MyRegisterClass(HINSTANCE hInstance);
  25. BOOL                InitInstance(HINSTANCE, int);
  26. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  27. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  28. HBITMAP g_hBackbmp, g_hBullet, g_hEnemy, g_hMine;
  29. int h=0;//hight
  30. int g_mineX, g_mineY;
  31. HDC g_hdc, g_memdc, g_bufdc;//设备上下文
  32. DWORD g_mtpre, g_mnow, g_bpre, g_bnow, g_tpre, g_tnow, g_epre, g_enow, g_empre, g_emnow;
  33. DWORD judge1, judge2;
  34. HBITMAP bg;
  35. HWND hWnd;
  36. int u = 0;
  37. //函数的声明
  38. BOOL GameInit(HWND hWnd);
  39. void game_Paint();
  40. void game_clear(HWND hwnd);
  41. void createlist();
  42. void AddNode(Node* P, int n);
  43. void Move(Node* P, int a);
  44. void DeleteNode(int a);
  45. void shoot(HWND hWnd);
  46. int APIENTRY wWinMain( HINSTANCE hInstance,   //_In_  _In_opt_
  47.                      HINSTANCE hPrevInstance,//
  48.                       LPWSTR    lpCmdLine,//_In_
  49.                       int       nCmdShow)//_In_
  50. {
  51.    // UNREFERENCED_PARAMETER(hPrevInstance);//unreferenced_parameter
  52.     //UNREFERENCED_PARAMETER(lpCmdLine);//

  53.     // TODO: 在此放置代码。

  54.     // 初始化全局字符串
  55.     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  56.     LoadStringW(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
  57.     MyRegisterClass(hInstance);
  58.        
  59.     // 执行应用程序初始化:
  60.     if (!InitInstance (hInstance, nCmdShow))
  61.     {
  62.         return FALSE;
  63.     }


  64.         if (!GameInit(hWnd))
  65.                 return -1;
  66.        

  67.         PlaySound(L"./素材/痴心不改—金池.wav", NULL, SND_FILENAME | SND_LOOP | SND_ASYNC);

  68.     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));
  69.         MSG msg = { 0 };
  70.     // 主消息循环:
  71.         //while (GetMessage(&msg, nullptr, 0, 0))
  72.         while(msg.message != WM_QUIT)
  73.         {
  74.                 /*if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  75.                 {

  76.                         TranslateMessage(&msg);
  77.                         DispatchMessage(&msg);
  78.                 }*/
  79.                 if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))//消息队列去获得消息        PM_REMOVE
  80.                 {
  81.                        
  82.                         TranslateMessage(&msg);  
  83.                         DispatchMessage(&msg);   
  84.                 }
  85.                 else
  86.                 {
  87.                         g_mnow = GetTickCount();
  88.                         g_tnow = GetTickCount();
  89.                         g_emnow = GetTickCount();
  90.                         judge2 = GetTickCount();
  91.                         if (g_mnow - g_mtpre >= 5)
  92.                         {
  93.                                 Move(pb, 1);
  94.                                 g_mtpre = g_mnow;
  95.                         }
  96.                         if (g_emnow - g_empre >= 20)//敌军
  97.                         {
  98.                                 Move(pe, 2);
  99.                                 g_emnow = g_empre;
  100.                         }
  101.                         if (g_tnow - g_tpre >= 10)
  102.                                 game_Paint();
  103.                        
  104.                        
  105.                                 shoot(hWnd);
  106.                                
  107.                        
  108.                 }
  109.                 }
  110.        
  111.   
  112.         // game_clear(hWnd);
  113.          return (int) msg.wParam;
  114. }



  115. //
  116. //  函数: MyRegisterClass()
  117. //
  118. //  目的: 注册窗口类。
  119. //
  120. ATOM MyRegisterClass(HINSTANCE hInstance)
  121. {
  122.     WNDCLASSEXW wcex;

  123.     wcex.cbSize = sizeof(WNDCLASSEX);

  124.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  125.     wcex.lpfnWndProc    = WndProc;
  126.     wcex.cbClsExtra     = 0;
  127.     wcex.cbWndExtra     = 0;
  128.     wcex.hInstance      = hInstance;
  129.     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
  130.     wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
  131.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  132.     wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_MY);
  133.     wcex.lpszClassName  = szWindowClass;
  134.     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

  135.     return RegisterClassExW(&wcex);
  136. }

  137. //
  138. //   函数: InitInstance(HINSTANCE, int)
  139. //
  140. //   目的: 保存实例句柄并创建主窗口
  141. //
  142. //   注释:
  143. //
  144. //        在此函数中,我们在全局变量中保存实例句柄并
  145. //        创建和显示主程序窗口。
  146. //
  147. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  148. {
  149.    hInst = hInstance; // 将实例句柄存储在全局变量中

  150.    hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  151.           300, 80, 700,600, nullptr, nullptr, hInstance, nullptr);//CW_USEDEFAULT

  152.    if (!hWnd)
  153.    {
  154.       return FALSE;
  155.    }
  156.    ShowWindow(hWnd, nCmdShow);
  157.    UpdateWindow(hWnd);

  158.    return TRUE;
  159. }

  160. //
  161. //  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
  162. //
  163. //  目的:    处理主窗口的消息。
  164. //
  165. //  WM_COMMAND  - 处理应用程序菜单
  166. //  WM_PAINT    - 绘制主窗口
  167. //  WM_DESTROY  - 发送退出消息并返回
  168. //
  169. //
  170. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  171. {
  172.     switch (message)
  173.     {
  174.     case WM_COMMAND:
  175.         {
  176.             int wmId = LOWORD(wParam);
  177.             // 分析菜单选择:
  178.             switch (wmId)
  179.             {
  180.             case IDM_ABOUT:
  181.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  182.                 break;
  183.             case IDM_EXIT:
  184.                 DestroyWindow(hWnd);
  185.                 break;
  186.             default:
  187.                 return DefWindowProc(hWnd, message, wParam, lParam);
  188.             }
  189.         }
  190.         break;
  191. case WM_PAINT:
  192.         {
  193.             PAINTSTRUCT ps;
  194.             HDC hdc = BeginPaint(hWnd, &ps);
  195.             // TODO: 在此处添加使用 hdc 的任何绘图代码...
  196.             EndPaint(hWnd, &ps);
  197.         }
  198.         break;
  199.     case WM_DESTROY:
  200.         PostQuitMessage(0);
  201.         break;
  202.         case WM_CREATE://WM_
  203.                 createlist();
  204.                 break;
  205.         case WM_KEYDOWN:  //接受键盘 keydowm
  206.                 switch (wParam)
  207.                 {
  208.                 case VK_UP:
  209.                         g_mineY -= 7;
  210.                         break;
  211.                 case VK_DOWN:
  212.                         g_mineY += 7;
  213.                         break;
  214.                 case VK_LEFT:
  215.                         g_mineX -= 7;
  216.                         break;
  217.                 case VK_RIGHT:
  218.                         g_mineX += 7;
  219.                         break;
  220.                 }
  221.                 break;
  222.     default:
  223.         return DefWindowProc(hWnd, message, wParam, lParam);
  224.     }
  225.     return 0;
  226. }

  227. // “关于”框的消息处理程序。
  228. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  229. {
  230.     UNREFERENCED_PARAMETER(lParam);
  231.     switch (message)
  232.     {
  233.     case WM_INITDIALOG:
  234.         return (INT_PTR)TRUE;

  235.     case WM_COMMAND:
  236.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  237.         {
  238.             EndDialog(hDlg, LOWORD(wParam));
  239.             return (INT_PTR)TRUE;
  240.         }
  241.         break;
  242.     }
  243.     return (INT_PTR)FALSE;
  244. }
  245. BOOL GameInit(HWND hWnd)
  246. {
  247.         srand((unsigned int)time(NULL));
  248.         g_hdc = GetDC(hWnd);//得到窗口环境句柄
  249.         g_memdc = CreateCompatibleDC(g_hdc);//创建兼容设备环境
  250.         g_bufdc = CreateCompatibleDC(g_hdc);
  251.         bg = CreateCompatibleBitmap(g_hdc, WINDOW_WIDTH, WINDOW_HEIGHT);//画布
  252.         //(HBITMAP)LoadImage(NULL, L"./素材/BG2.bmp", IMAGE_BITMAP, WINDOW_WIDTH, WINDOW_HEIGHT, LR_LOADFROMFILE);
  253.         g_hMine = (HBITMAP)LoadImage(NULL, L"./素材/mine.bmp", IMAGE_BITMAP, 200, 100, LR_LOADFROMFILE);
  254.         g_hBullet=(HBITMAP)LoadImage(NULL, L"./素材/bullet.bmp", IMAGE_BITMAP,120,25,LR_LOADFROMFILE);
  255.         g_hEnemy=(HBITMAP)LoadImage(NULL, L"./素材/enemy.bmp", IMAGE_BITMAP, 100, 34, LR_LOADFROMFILE);
  256.         g_hBackbmp= (HBITMAP)LoadImage(NULL, L"./素材/BG2.bmp", IMAGE_BITMAP, 800, 2408, LR_LOADFROMFILE);
  257.        
  258.         SelectObject(g_memdc, bg);//SelectObject
  259.         g_mineX = (WINDOW_WIDTH - 100) / 2;
  260.         g_mineY = WINDOW_HEIGHT - 150;

  261.         return TRUE;

  262. }
  263. void game_Paint()//游戏的绘制
  264. {
  265.         SelectObject(g_bufdc, g_hBackbmp);
  266.         BitBlt(g_memdc, 0, 0, WINDOW_WIDTH, h, g_bufdc,0, 2048-h, SRCCOPY);
  267.         BitBlt(g_memdc,0,h,WINDOW_WIDTH,WINDOW_HEIGHT-h,g_bufdc,0,0,SRCCOPY);
  268.         if (++h >= 2048)
  269.         {
  270.                 h = 0;
  271.         }
  272. //透明绘图
  273.         g_bnow = GetTickCount();
  274.         if (g_bnow - g_bpre >= 500)
  275.         {
  276.                 AddNode(pb, 1);
  277.                 g_bpre = g_bnow;
  278.         }
  279.         Node *pbtemporary = pb->pnext;//画子弹
  280.         SelectObject(g_bufdc, g_hBullet);
  281.         while (pbtemporary != NULL)
  282.         {
  283.                 BitBlt(g_memdc,pbtemporary->x,pbtemporary->y,60,25,g_bufdc,60,0,SRCAND);//srcand        把黑的刷到目标文件,白色不刷
  284.                 BitBlt(g_memdc, pbtemporary->x, pbtemporary->y, 60, 25, g_bufdc, 0, 0, SRCPAINT);//srcpaint 白色区域刷到目标文件中,黑色不刷
  285.                 pbtemporary = pbtemporary->pnext;
  286.         }

  287.         g_enow = GetTickCount();//enemy
  288.         if (g_enow - g_epre>=1000)
  289.         {
  290.                 AddNode(pe, 2);
  291.                 g_epre = g_enow;
  292.         }
  293.         Node *petemp = pe->pnext;
  294.         SelectObject(g_bufdc, g_hEnemy);//画敌军
  295.         while (petemp != NULL)
  296.         {
  297.                 BitBlt(g_memdc, petemp->x, petemp->y, 50, 34, g_bufdc, 0, 0, SRCAND);
  298.                 BitBlt(g_memdc, petemp->x, petemp->y, 50, 34, g_bufdc,50,0, SRCPAINT);
  299.                 petemp = petemp->pnext;
  300.         }
  301.         SelectObject(g_bufdc, g_hMine);
  302.         BitBlt(g_memdc, g_mineX, g_mineY, 100, 100, g_bufdc,0,0, SRCAND);
  303.         BitBlt(g_memdc, g_mineX, g_mineY, 100, 100, g_bufdc, 100, 0, SRCPAINT);

  304.         BitBlt(g_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, g_memdc, 0, 0, SRCCOPY);
  305.         g_tpre = GetTickCount();
  306. }
  307. void game_clear(HWND hwnd)//资源清理
  308. {
  309.         ReleaseDC(hwnd,g_hdc);
  310.         DeleteObject(g_bufdc);
  311.         DeleteObject(g_memdc);
  312.         DeleteObject(bg);
  313.         return;
  314. }
  315. void createlist()
  316. {
  317.         pb = (Node*)malloc(sizeof(Node));
  318.         pb->pnext = NULL;
  319.         pe = (Node*)malloc(sizeof(Node));
  320.         pe->pnext = NULL;
  321.        
  322.         g_mtpre = GetTickCount();
  323.         g_bpre = GetTickCount();
  324.         g_epre = GetTickCount();
  325.         g_empre = GetTickCount();
  326.         judge1 = GetTickCount();
  327. }
  328. void AddNode(Node* P,int n)
  329. {
  330.         Node* pnew = (Node*)malloc(sizeof(Node));
  331.         if (n==1)
  332.         {
  333.                 pnew->x = g_mineX + 20;
  334.                 pnew->y = g_mineY;
  335.         }
  336.         else if (n == 2)//enemy
  337.         {
  338.                 pnew->x = rand() %( WINDOW_WIDTH-10);
  339.                 pnew->y = 0;
  340.         }
  341.         pnew->pnext = P->pnext;
  342.         P->pnext = pnew;
  343. }

  344. void Move(Node* P,int a)//移动   
  345. {
  346.         if (a == 1)
  347.         {
  348.                 P = pb->pnext;
  349.                 while (P != NULL)
  350.                 {
  351.                         P->y -= 5;
  352.                         P = P->pnext;
  353.                 }
  354.         }
  355.         if (a == 2)
  356.         {
  357.                 P = pe->pnext;
  358.                 while (P != NULL)
  359.                 {
  360.                         P->y += 5;
  361.                         P = P->pnext;
  362.                 }
  363.         }
  364.         DeleteNode(a);
  365. }

  366. void shoot(HWND hWnd)
  367. {
  368.         //判断与敌机是否相撞
  369.         //击中敌机子弹就消失
  370.         Node * pbfront= pb->pnext, *pblater=pb;
  371.         Node * pefront, *pelater=pe;
  372.         pefront = pe->pnext;
  373.         while (pefront != NULL)
  374.         {
  375.                 if (pefront->x >= g_mineX -100 && pefront->x <= g_mineX + 100 && pefront->y >= g_mineY + 34 && pefront->y<= g_mineY + 100)
  376.                 {
  377.                         MessageBox(hWnd, L"你怎么这么笨,这也能撞死!", L"消息", MB_OK);
  378.                         PostQuitMessage(0);
  379.                         break;
  380.                 }
  381.                 else
  382.                 {
  383.                         pefront = pefront->pnext;
  384.                 }
  385.         }
  386.         //子弹击中敌机没有
  387.         pefront = pe->pnext;
  388.         while (pefront != NULL)//每一个敌军
  389.         {
  390.                 pbfront = pb->pnext;
  391.                 pblater = pb;
  392.                 while (pbfront != NULL)//每一个子弹
  393.                 {
  394.                         if (pbfront->x >= pefront->x - 100 && pbfront->x <= pefront->x + 100 && pbfront->y <= pefront->y + 34 && pbfront->y >= pefront->y - 34)
  395.                         {
  396.                                 pblater->pnext = pbfront->pnext;
  397.                                 free(pbfront);
  398.                                 pelater->pnext = pefront->pnext;
  399.                                 free(pefront);
  400.                                 break;
  401.                         }
  402.                         else
  403.                         {
  404.                                 pbfront = pbfront->pnext;
  405.                                 pblater = pblater->pnext;
  406.                         }
  407.                 }
  408.                 pefront = pelater->pnext;//把链表接起来
  409.                 if (pefront != NULL)
  410.                 {
  411.                         pefront = pefront->pnext;
  412.                         pelater = pelater->pnext;
  413.                 }

  414.         }
  415. }

  416. void DeleteNode(int a)
  417. {
  418.         if (a == 1)
  419.         {
  420.                 Node* P = pb->pnext;
  421.                 Node*P1 = pb;
  422.                 while (P != NULL)
  423.                 {
  424.                         if (P != NULL&&P->y <= 0)
  425.                         {
  426.                                 P1->pnext = P->pnext;
  427.                                 free(P);
  428.                                 P = P1->pnext;
  429.                         }
  430.                         if (P != NULL)
  431.                         {
  432.                                 P1 = P1->pnext;
  433.                                 P = P->pnext;
  434.                         }
  435.                 }
  436.         }
  437.         else if (a == 2)
  438.         {
  439.             Node* P = pe->pnext;
  440.                 Node* P1 = pe;
  441.                 while (P != NULL)
  442.                 {
  443.                         if (P != NULL&&P->y >= 700)
  444.                         {
  445.                                 P1->pnext = P->pnext;
  446.                                 //pe->pnext = P->pnext;
  447.                                 free(P);
  448.                                 P = P1->pnext;
  449.                         }
  450.                         if (P != NULL)
  451.                         {
  452.                                 P1 = P1->pnext;
  453.                                 P = P->pnext;
  454.                         }
  455.                 }
  456.         }
  457. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-1 09:00:06 | 显示全部楼层
问题解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 11:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表