Windows 游戏编程大师技巧第四章第4个例子

本程序演示了一个简单的椭圆动画游戏,通过不断更新椭圆位置实现动态效果,并在碰撞到窗口边界时改变方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1.  // DEMO4_4.CPP  - Ellipse Animation Demo
  2. // INCLUDES ///////////////////////////////////////////////
  3. #define WIN32_LEAN_AND_MEAN  // just say no to MFC
  4. #include <windows.h>   // include all the windows headers
  5. #include <windowsx.h>  // include useful macros
  6. #include <mmsystem.h>  // very important and include WINMM.LIB too!
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. // DEFINES ////////////////////////////////////////////////
  11. // defines for windows 
  12. #define WINDOW_CLASS_NAME "WINCLASS1"
  13. #define WINDOW_WIDTH  400
  14. #define WINDOW_HEIGHT 300
  15. // MACROS /////////////////////////////////////////////////
  16. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  17. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  18. // GLOBALS ////////////////////////////////////////////////
  19. HWND      main_window_handle = NULL; // globally track main window
  20. HINSTANCE hinstance_app      = NULL; // globally track hinstance
  21. char buffer[80];                     // general printing buffer
  22. // FUNCTIONS //////////////////////////////////////////////
  23. LRESULT CALLBACK WindowProc(HWND hwnd, 
  24.           UINT msg, 
  25.                             WPARAM wparam, 
  26.                             LPARAM lparam)
  27. {
  28. // this is the main message handler of the system
  29. PAINTSTRUCT  ps;  // used in WM_PAINT
  30. HDC    hdc; // handle to a device context
  31. char buffer[80];        // used to print strings
  32. // what is the message 
  33. switch(msg)
  34.  { 
  35.  case WM_CREATE: 
  36.         {
  37.   // do initialization stuff here
  38.         // return success
  39.   return(0);
  40.   } break;
  41.    
  42.  case WM_PAINT: 
  43.   {
  44.   // simply validate the window 
  45.         hdc = BeginPaint(hwnd,&ps);  
  46.         
  47.         // end painting
  48.         EndPaint(hwnd,&ps);
  49.         // return success
  50.   return(0);
  51.      } break;
  52.  case WM_DESTROY: 
  53.   {
  54.   // kill the application, this sends a WM_QUIT message 
  55.   PostQuitMessage(0);
  56.         // return success
  57.   return(0);
  58.   } break;
  59.  default:break;
  60.     } // end switch
  61. // process any messages that we didn't take care of 
  62. return (DefWindowProc(hwnd, msg, wparam, lparam));
  63. // end WinProc
  64. // WINMAIN ////////////////////////////////////////////////
  65. int WINAPI WinMain( HINSTANCE hinstance,
  66.      HINSTANCE hprevinstance,
  67.      LPSTR lpcmdline,
  68.      int ncmdshow)
  69. {
  70. WNDCLASSEX winclass; // this will hold the class we create
  71. HWND    hwnd;  // generic window handle
  72. MSG     msg;   // generic message
  73. HDC        hdc;      // graphics device context
  74. // first fill in the window class stucture
  75. winclass.cbSize         = sizeof(WNDCLASSEX);
  76. winclass.style   = CS_DBLCLKS | CS_OWNDC | 
  77.                           CS_HREDRAW | CS_VREDRAW;
  78. winclass.lpfnWndProc = WindowProc;
  79. winclass.cbClsExtra  = 0;
  80. winclass.cbWndExtra  = 0;
  81. winclass.hInstance  = hinstance;
  82. winclass.hIcon   = LoadIcon(NULL, IDI_APPLICATION);
  83. winclass.hCursor  = LoadCursor(NULL, IDC_ARROW); 
  84. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  85. winclass.lpszMenuName = NULL;
  86. winclass.lpszClassName = WINDOW_CLASS_NAME;
  87. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  88. // save hinstance in global
  89. hinstance_app = hinstance;
  90. // register the window class
  91. if (!RegisterClassEx(&winclass))
  92.  return(0);
  93. // create the window
  94. if (!(hwnd = CreateWindowEx(NULL,                // extended style
  95.                             WINDOW_CLASS_NAME,   // class
  96.           "Ellipse Animation Demo"// title
  97.           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  98.            0,0,   // initial x,y
  99.           WINDOW_WIDTH, // initial width
  100.                             WINDOW_HEIGHT,// initial height
  101.           NULL,   // handle to parent 
  102.           NULL,   // handle to menu
  103.           hinstance,// instance of this application
  104.           NULL))) // extra creation parms
  105. return(0);
  106. // save main window handle
  107. main_window_handle = hwnd;
  108. // get the graphics device context once this time and keep it
  109. // this is possible due to the CS_OWNDC flag
  110. hdc = GetDC(hwnd);
  111. // seed random number generator
  112. srand(GetTickCount());
  113. // create the pens and brushes
  114. HPEN   white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
  115. HPEN   black_pen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
  116. HBRUSH green_brush = CreateSolidBrush(RGB(0,255,0));
  117. HBRUSH black_brush = CreateSolidBrush(RGB(0,0,0));
  118. // starting position of ball
  119. int ball_x = WINDOW_WIDTH/2;
  120. int ball_y = WINDOW_HEIGHT/2;
  121. // initial velocity of ball
  122. int xv = -4+rand()%8;
  123. int yv = -4+rand()%8;
  124. // enter main event loop, but this time we use PeekMessage()
  125. // instead of GetMessage() to retrieve messages
  126. while(TRUE)
  127.  {
  128.     // test if there is a message in queue, if so get it
  129.  if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  130.     { 
  131.     // test if this is a quit
  132.        if (msg.message == WM_QUIT)
  133.            break;
  134.     // translate any accelerator keys
  135.     TranslateMessage(&msg);
  136.     // send the message to the window proc
  137.     DispatchMessage(&msg);
  138.     } // end if
  139.     // ERASE the last position of the ball
  140.     
  141.     // first select the black pen and brush into context
  142.     SelectObject(hdc, black_pen);
  143.     SelectObject(hdc, black_brush);
  144.     // draw the ball
  145.     Ellipse(hdc, ball_x, ball_y, ball_x + 32, ball_y + 32);
  146.     
  147.     // MOVE the ball
  148.     ball_x+=xv;
  149.     ball_y+=yv;
  150.     // test for collisions, first x-axis
  151.     if (ball_x < 0 || ball_x > WINDOW_WIDTH - 32)
  152.        { 
  153.        // invert x-velocity of ball
  154.        xv=-xv;
  155.        // push ball back
  156.        ball_x+=xv;
  157.        } // end if
  158.     else
  159.     // test for y-axis collisions
  160.     if (ball_y < 0 || ball_y > WINDOW_HEIGHT - 32)
  161.        { 
  162.        // invert y-velocity of ball
  163.        yv=-yv;
  164.        // push ball back
  165.        ball_y+=yv;
  166.        } // end if
  167.     // now select the green and white colors for brush and pen
  168.     SelectObject(hdc, white_pen);
  169.     SelectObject(hdc, green_brush);
  170.     // DRAW the ball
  171.     Ellipse(hdc, ball_x, ball_y, ball_x + 32, ball_y + 32);
  172.     // main game processing goes here
  173.     if (KEYDOWN(VK_ESCAPE))
  174.        SendMessage(hwnd, WM_CLOSE, 0,0);
  175.     // slow system down a little
  176.     Sleep(10); 
  177.        
  178.  } // end while
  179. // delete all the objects
  180. DeleteObject(white_pen);
  181. DeleteObject(black_pen);
  182. DeleteObject(green_brush);
  183. DeleteObject(black_brush);
  184. // release the device context
  185. ReleaseDC(hwnd,hdc);
  186. // return to Windows like this
  187. return(msg.wParam);
  188. // end WinMain
  189. ///////////////////////////////////////////////////////////

 

这个程序已经初见游戏的端倪了.

   SelectObject(hdc, black_pen);
    SelectObject(hdc, black_brush);

    // draw the ball
    Ellipse(hdc, ball_x, ball_y, ball_x + 32, ball_y + 32);
   
    // MOVE the ball
    ball_x+=xv;
    ball_y+=yv;

    // test for collisions, first x-axis
    if (ball_x < 0 || ball_x > WINDOW_WIDTH - 32)
       {
       // invert x-velocity of ball
       xv=-xv;

       // push ball back
       ball_x+=xv;

       } // end if
    else
    // test for y-axis collisions
    if (ball_y < 0 || ball_y > WINDOW_HEIGHT - 32)
       {
       // invert y-velocity of ball
       yv=-yv;

       // push ball back
       ball_y+=yv;

       } // end if

    // now select the green and white colors for brush and pen
    SelectObject(hdc, white_pen);
    SelectObject(hdc, green_brush);

    // DRAW the ball
    Ellipse(hdc, ball_x, ball_y, ball_x + 32, ball_y + 32);

    // main game processing goes here
    if (KEYDOWN(VK_ESCAPE))
       SendMessage(hwnd, WM_CLOSE, 0,0);

    // slow system down

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值