CPaddle.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:8k
源码类别:

游戏

开发平台:

Visual C++

  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// File:    CPaddle.cpp
  3. /// Purpose: Definition of CPaddle Class Members
  4. ///////////////////////////////////////////////////////////////////////////////
  5. /// Configuation Management
  6. /// 
  7. /// Who         When          Description
  8. /// ===========================================================================
  9. /// R. Walter   28-Dec-2003   Initial Version/Release
  10. ///
  11. ///////////////////////////////////////////////////////////////////////////////
  12. /// Copyright 2003: Robert Walter   All rights reserved
  13. ///////////////////////////////////////////////////////////////////////////////
  14. /// APPLICATION DEFINES ///////////////////////////////////////////////////////
  15. #define WIN32_LEAN_AND_MEAN /** no MFC **/
  16. /// HEADER FILE INCLUDES //////////////////////////////////////////////////////
  17. #include "CPaddle.h"
  18. ///////////////////////////////////////////////////////////////////////////////
  19. /// Method:  CPaddle --> default constructor
  20. /// Purpose: Initialize member variables
  21. ///////////////////////////////////////////////////////////////////////////////
  22. /// Receives: nothing
  23. /// Returns:  nothing
  24. ///////////////////////////////////////////////////////////////////////////////
  25. CPaddle::CPaddle()
  26. {
  27.     return;
  28. }
  29. ///////////////////////////////////////////////////////////////////////////////
  30. /// Method:  ~CPaddle --> class destructor
  31. /// Purpose: Uninitialize member variables
  32. ///////////////////////////////////////////////////////////////////////////////
  33. /// Receives: nothing
  34. /// Returns:  nothing
  35. ///////////////////////////////////////////////////////////////////////////////
  36. CPaddle::~CPaddle()
  37. {
  38. return;
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. /// Method:  GetXPos
  42. /// Purpose: Retrieve the left most coordinate (in pixels) of the current
  43. ///          paddle location
  44. ///////////////////////////////////////////////////////////////////////////////
  45. /// Receives: nothing
  46. /// Returns:  X coordinate of paddle (integer)
  47. ///////////////////////////////////////////////////////////////////////////////
  48. int CPaddle::GetXPos()
  49. {
  50. return(m_x_pos);
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. /// Method:  GetXVel
  54. /// Purpose: Retrieve the speed the paddle is moving across the X axis
  55. ///          (positive indicates moving right, negative moving left)
  56. ///////////////////////////////////////////////////////////////////////////////
  57. /// Receives: nothing
  58. /// Returns:  current X velocity of paddle (integer)
  59. ///////////////////////////////////////////////////////////////////////////////
  60. int CPaddle::GetXVel()
  61. {
  62. return(m_x_vel);
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. /// Method:  GetLength
  66. /// Purpose: Retrieve the current length (in pixels) of the paddle
  67. ///////////////////////////////////////////////////////////////////////////////
  68. /// Receives: nothing
  69. /// Returns:  length (in pixels) of the paddle (integer)
  70. ///////////////////////////////////////////////////////////////////////////////
  71. int CPaddle::GetLength()
  72. {
  73. return(m_length);
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////
  76. /// Method:  GetPaddleRects
  77. /// Purpose: Retrieve the count and RECT structures of the actual paddles
  78. ///          in the playing area.  NOTE: there may be more than one RECT
  79. ///////////////////////////////////////////////////////////////////////////////
  80. /// Receives: nothing
  81. /// Returns:  count of paddle rectangles -> through a integer pointer
  82. ///           array of paddle RECTs -> through a RECT pointer
  83. ///////////////////////////////////////////////////////////////////////////////
  84. void CPaddle::GetPaddleRects(int* v_rect_cnt, RECT* v_rects)
  85. {
  86. *v_rect_cnt = m_cnt;
  87. v_rects[0].top = m_bottom_limit - m_height;
  88. v_rects[0].left = m_x_pos;
  89. v_rects[0].right = (m_x_pos + m_length);
  90. v_rects[0].bottom = m_bottom_limit;
  91. if (m_cnt == 2)
  92. {
  93.         v_rects[1].top = v_rects[0].top - m_hor_space - m_height;
  94. v_rects[1].left = m_x_pos;
  95. v_rects[1].right = (m_x_pos + m_length);
  96. v_rects[1].bottom = v_rects[0].top - m_hor_space;
  97. }
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////
  100. /// Method:  SetBaseProperties
  101. /// Purpose: Initializes the core set of properties that dictates how the 
  102. ///          paddle will look in the playing area
  103. ///////////////////////////////////////////////////////////////////////////////
  104. /// Receives: count of paddles (integer)
  105. ///           amount of horizontal space (in pixels) between the paddles (integer)
  106. ///           length of the paddles (in pixels) (integer)
  107. ///           height of the paddles (in pixels) (integer)
  108. /// Returns:  none
  109. ///////////////////////////////////////////////////////////////////////////////
  110. void CPaddle::SetBaseProperties(int const p_cnt, int const p_hor_space, int const p_length, int const p_height)
  111. {
  112. m_cnt = p_cnt;
  113. m_hor_space = p_hor_space;
  114. m_length = p_length;
  115. m_height = p_height;
  116. }
  117. ///////////////////////////////////////////////////////////////////////////////
  118. /// Method:  SetPaddleLimits
  119. /// Purpose: Initializes the bounds the paddle can move between in the playing
  120. ///          area (ie. how far left, how far right, etc.)
  121. ///////////////////////////////////////////////////////////////////////////////
  122. /// Receives: left boundary of playing area (integer)
  123. ///           right boundary of playing area (integer)
  124. ///           bottom boundary of playing area (integer)
  125. ///////////////////////////////////////////////////////////////////////////////
  126. void CPaddle::SetPaddleLimits(int const p_min_limit, int const p_max_limit, int const p_bottom_limit)
  127. {
  128. m_min_limit = p_min_limit;
  129. m_max_limit = p_max_limit;
  130. m_bottom_limit = p_bottom_limit;
  131. // calculate initial paddle position
  132. m_x_pos = ((p_max_limit - p_min_limit) - (m_length / 2)) / 2;
  133. m_x_vel = 0;
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////
  136. /// Method:  SetXPos
  137. /// Purpose: Set the current X position of the paddle
  138. ///////////////////////////////////////////////////////////////////////////////
  139. /// Receives: X coordinate of paddle (integer)
  140. /// Returns:  none
  141. ///////////////////////////////////////////////////////////////////////////////
  142. void CPaddle::SetXPos(int const p_x_pos)
  143. {
  144. m_x_pos = p_x_pos;
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////
  147. /// Method:  SetXVel
  148. /// Purpose: Set the speed the paddle is moving across the X axis
  149. ///          (positive indicates moving right, negative moving left)
  150. ///////////////////////////////////////////////////////////////////////////////
  151. /// Receives: new X velocity of paddle (integer)
  152. /// Returns:  none
  153. ///////////////////////////////////////////////////////////////////////////////
  154. void CPaddle::SetXVel(int const p_x_vel)
  155. {
  156. m_x_vel = p_x_vel;
  157. }
  158. bool CPaddle::HitPaddle(CBall p_ball)
  159. {
  160. RECT l_rects[2];
  161. int  l_rect_cnt;
  162. GetPaddleRects(&l_rect_cnt, l_rects);
  163. for (int i = 0; i < l_rect_cnt; i++)
  164. {
  165.        int l_ball_x = 0;
  166.    int l_ball_y = 0;
  167.    int l_ball_bottom = 0;
  168.    int l_ball_right = 0;
  169.    l_ball_x = p_ball.GetXPos();
  170.    l_ball_y = p_ball.GetYPos();
  171.    l_ball_bottom = l_ball_y + BALL_HEIGHT;
  172.    l_ball_right = l_ball_x + BALL_WIDTH;
  173.    if ( (l_ball_x >= l_rects[i].left) && (l_ball_x <= l_rects[i].right) && (l_ball_bottom >= l_rects[i].top) ) 
  174.    {
  175.    return(true);
  176.    }
  177.    if ( (l_ball_right >= l_rects[i].left) && (l_ball_right <= l_rects[i].right) && (l_ball_bottom >= l_rects[i].top) ) 
  178.    {
  179.    return(true);
  180.    }
  181.     }
  182. return(false);
  183. }
  184. bool CPaddle::IsPaddleAtPosition(int p_x, int p_y, RECT* p_rect)
  185. {
  186. RECT l_rects[2];
  187. int  l_rect_cnt;
  188. GetPaddleRects(&l_rect_cnt, l_rects);
  189. for (int i = 0; i < l_rect_cnt; i++)
  190. {
  191.        if ( (p_x >= l_rects[i].left) && (p_x <= l_rects[i].right) && (p_y >= l_rects[i].top) && (p_y <= l_rects[i].bottom) )
  192.    {
  193.    *p_rect = l_rects[i];
  194.    return(true);
  195.    }
  196.     }
  197. return(false);
  198. }
  199. void CPaddle::SetLength(int const p_length)
  200. {
  201. m_length = p_length;
  202. }
  203. /// END OF FILE ///////////////////////////////////////////////////////////////