ball.cpp
上传用户:szsg028
上传日期:2022-08-01
资源大小:12k
文件大小:2k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. #include "ball.h"
  2. ball::ball()
  3. {
  4. m_fVelocidad = 0;
  5. m_Cancha = 0;
  6. m_cDirectionX = 1;
  7. m_cDirectionY = 1;
  8. srand( time(0) );
  9. m_brushBlanco = (HBRUSH)GetStockObject(WHITE_BRUSH);
  10. //m_brushBlanco = (HBRUSH)CreateSolidBrush(RGB(255,0,0));
  11. m_penBlanco = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
  12. }
  13. ball::~ball()
  14. {
  15. DeleteObject(m_brushBlanco);
  16. DeleteObject(m_penBlanco);
  17. }
  18. void ball::draw(HDC hDC)
  19. {
  20. HBRUSH hOldBrush;
  21. HPEN hOldPen;
  22. hOldBrush = (HBRUSH)SelectObject(hDC, m_brushBlanco);
  23. hOldPen = (HPEN)SelectObject(hDC, m_penBlanco);
  24. Ellipse(hDC, getX() - getWidth()/2, getY() - getHeight()/2, getX() + getWidth()/2, getY() + getHeight()/2);
  25. SelectObject(hDC, hOldBrush);
  26. SelectObject(hDC, hOldPen);
  27. }
  28. void ball::move(float dt)
  29. {
  30. mY += dt*m_fVelocidad*m_cDirectionY;
  31. mX += dt*m_fVelocidad*m_cDirectionX;
  32. if ( mY-mHeight/2 < m_Cancha->getMarginTop() )
  33. {
  34. mY = m_Cancha->getMarginTop() + mHeight/2;
  35. m_cDirectionY = -m_cDirectionY;
  36. }
  37. if ( mY+mHeight/2 > m_Cancha->getMarginBottom() )
  38. {
  39. mY = m_Cancha->getMarginBottom() - mHeight/2;
  40. m_cDirectionY = -m_cDirectionY;
  41. }
  42. }
  43. void ball::bounce()
  44. {
  45. m_cDirectionX = -m_cDirectionX;
  46. }
  47. void ball::randomizeDirections()
  48. {
  49. int r = 0;
  50. //randomizo eje x
  51. r = rand() % 2;
  52. if (r == 0) 
  53. m_cDirectionX = -1;
  54. else
  55. m_cDirectionX = 1;
  56. //randomizo eje y
  57. r = rand() % 2;
  58. if (r == 0) 
  59. m_cDirectionY = -1;
  60. else
  61. m_cDirectionY = 1;
  62. }
  63. void ball::setVelocidad(float valor)
  64. {
  65. m_fVelocidad = valor;
  66. }
  67. void ball::setCancha(court* cancha)
  68. {
  69. m_Cancha = cancha;
  70. }
  71. void ball::centrarEnCancha()
  72. {
  73. setPosition(m_Cancha->getWidth()/2, m_Cancha->getHeight()/2);
  74. }