ball.cpp
资源名称:pong.rar [点击查看]
上传用户:szsg028
上传日期:2022-08-01
资源大小:12k
文件大小:2k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- #include "ball.h"
- ball::ball()
- {
- m_fVelocidad = 0;
- m_Cancha = 0;
- m_cDirectionX = 1;
- m_cDirectionY = 1;
- srand( time(0) );
- m_brushBlanco = (HBRUSH)GetStockObject(WHITE_BRUSH);
- //m_brushBlanco = (HBRUSH)CreateSolidBrush(RGB(255,0,0));
- m_penBlanco = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
- }
- ball::~ball()
- {
- DeleteObject(m_brushBlanco);
- DeleteObject(m_penBlanco);
- }
- void ball::draw(HDC hDC)
- {
- HBRUSH hOldBrush;
- HPEN hOldPen;
- hOldBrush = (HBRUSH)SelectObject(hDC, m_brushBlanco);
- hOldPen = (HPEN)SelectObject(hDC, m_penBlanco);
- Ellipse(hDC, getX() - getWidth()/2, getY() - getHeight()/2, getX() + getWidth()/2, getY() + getHeight()/2);
- SelectObject(hDC, hOldBrush);
- SelectObject(hDC, hOldPen);
- }
- void ball::move(float dt)
- {
- mY += dt*m_fVelocidad*m_cDirectionY;
- mX += dt*m_fVelocidad*m_cDirectionX;
- if ( mY-mHeight/2 < m_Cancha->getMarginTop() )
- {
- mY = m_Cancha->getMarginTop() + mHeight/2;
- m_cDirectionY = -m_cDirectionY;
- }
- if ( mY+mHeight/2 > m_Cancha->getMarginBottom() )
- {
- mY = m_Cancha->getMarginBottom() - mHeight/2;
- m_cDirectionY = -m_cDirectionY;
- }
- }
- void ball::bounce()
- {
- m_cDirectionX = -m_cDirectionX;
- }
- void ball::randomizeDirections()
- {
- int r = 0;
- //randomizo eje x
- r = rand() % 2;
- if (r == 0)
- m_cDirectionX = -1;
- else
- m_cDirectionX = 1;
- //randomizo eje y
- r = rand() % 2;
- if (r == 0)
- m_cDirectionY = -1;
- else
- m_cDirectionY = 1;
- }
- void ball::setVelocidad(float valor)
- {
- m_fVelocidad = valor;
- }
- void ball::setCancha(court* cancha)
- {
- m_Cancha = cancha;
- }
- void ball::centrarEnCancha()
- {
- setPosition(m_Cancha->getWidth()/2, m_Cancha->getHeight()/2);
- }