Link to home
Start Free TrialLog in
Avatar of OptionsMM
OptionsMM

asked on

Pong 500 points

Hi Experts!

I am going to learn Visual C++ (I already know VB pretty well.)  My first real project is going to be to build a PONG game.  very simple like the atari version.  I am expecting this will take several months of work but I think it will be a great learning experience.

So how do I build it?  What vaiables would you decalare?  How about the boundaries?  How about player 1 or player 2?  How would you program the events?

500 points for a detailed blueprint.... something for me to get started on.  The one with the best summary gets the points!

Thanks!
Avatar of sunnycoder
sunnycoder
Flag of India image

Avatar of Prophecy-Online
Prophecy-Online

Yeh, there are tons of places where they have the OpenSource for pong games in all types of languages if you download some of the source Im sure you would be able to use it to look at your own project and know how to do it, it is the way that I learnt vb, by looking at open source and seeing what each piece did and then everntually started to program things for myself.
Well, are you going to use OpenGL och DirectX or plain GDI?

If you are going to use plain GDI the first thing you need is Sprites. This is for handling Graphical movment in the Game.
Then you also need a class to read Bitmaps to use with your Sprite class and for other Graphics.
You also need a class that handles the mouse/keyboard events.
And to handling the players you would need a player class.

If you are going to use OpenGL, you would need the classes named abov but with changes. And the Same goes for DirectX.


I have already made my own Bitmap, Sprite classes for handling this stuff. I Coul'd post them for you if you like.

I hope you find what you are looking for. Happy Programming. :o)
Avatar of OptionsMM

ASKER

Snurre,

Posting the Sprites would be great!  thanks...
ASKER CERTIFIED SOLUTION
Avatar of Snurre
Snurre

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And heres the CPP File
---

/*************************************************************************************/
//      Sprite.cpp
//      Hantera Animering av Bilder
//      Skriven av: Marcus Grenängen MO Soft 2002
/*************************************************************************************/
#include "Sprite.h"

/*************************************************************************************/
// Sprite Konstruktor / Destruktor
/*************************************************************************************/
Sprite::Sprite(Bitmap* pBitmap)
{
  // Installera Medlems Variablerna
  m_pBitmap = pBitmap;
  m_iNumFrames = 1;
  m_iCurFrame = m_iFrameDelay = m_iFrameTrigger = 0;
  SetRect(&m_rcPosition, 0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
  CalcCollisionRect();
  m_ptVelocity.x = m_ptVelocity.y = 0;
  m_iZOrder = 0;
  SetRect(&m_rcBounds, 0, 0, 640, 480);
  m_baBoundsAction = BA_STOP;
  m_bHidden = FALSE;
  m_bDying = FALSE;
  m_bOneCycle = FALSE;
}

Sprite::Sprite(Bitmap* pBitmap, RECT& rcBounds, BOUNDSACTION baBoundsAction)
{
  // Räkna ut en Slumpmässig Position
  int iXPos = rand() % (rcBounds.right - rcBounds.left);
  int iYPos = rand() % (rcBounds.bottom - rcBounds.top);

  // Installera Medlems Variablerna
  m_pBitmap = pBitmap;
  m_iNumFrames = 1;
  m_iCurFrame = m_iFrameDelay = m_iFrameTrigger = 0;
  SetRect(&m_rcPosition, iXPos, iYPos, iXPos + pBitmap->GetWidth(),
    iYPos + pBitmap->GetHeight());
  CalcCollisionRect();
  m_ptVelocity.x = m_ptVelocity.y = 0;
  m_iZOrder = 0;
  CopyRect(&m_rcBounds, &rcBounds);
  m_baBoundsAction = baBoundsAction;
  m_bHidden = FALSE;
  m_bDying = FALSE;
  m_bOneCycle = FALSE;
}

Sprite::Sprite(Bitmap* pBitmap, POINT ptPosition, POINT ptVelocity, int iZOrder,
    RECT& rcBounds, BOUNDSACTION baBoundsAction)
{
  // Installera Medlems Variablerna
  m_pBitmap = pBitmap;
  m_iNumFrames = 1;
  m_iCurFrame = m_iFrameDelay = m_iFrameTrigger = 0;
  SetRect(&m_rcPosition, ptPosition.x, ptPosition.y, pBitmap->GetWidth(),
    pBitmap->GetHeight());
  CalcCollisionRect();
  m_ptVelocity = ptPosition;
  m_iZOrder = iZOrder;
  CopyRect(&m_rcBounds, &rcBounds);
  m_baBoundsAction = baBoundsAction;
  m_bHidden = FALSE;
  m_bDying = FALSE;
  m_bOneCycle = FALSE;
}

Sprite::~Sprite()
{
}

/*************************************************************************************/
// Sprite Generella Metoder
/*************************************************************************************/
SPRITEACTION Sprite::Update()
{
  // Kolla ifall Spriten måste Dödas
  if (m_bDying)
    return SA_KILL;

  // Uppdatera Bildrutan
  UpdateFrame();

  // Updatera Positionen
  POINT ptNewPosition, ptSpriteSize, ptBoundsSize;
  ptNewPosition.x = m_rcPosition.left + m_ptVelocity.x;
  ptNewPosition.y = m_rcPosition.top + m_ptVelocity.y;
  ptSpriteSize.x = m_rcPosition.right - m_rcPosition.left;
  ptSpriteSize.y = m_rcPosition.bottom - m_rcPosition.top;
  ptBoundsSize.x = m_rcBounds.right - m_rcBounds.left;
  ptBoundsSize.y = m_rcBounds.bottom - m_rcBounds.top;

  // Kolla...!
  // Slå Ihop???
  if (m_baBoundsAction == BA_WRAP)
  {
    if ((ptNewPosition.x + ptSpriteSize.x) < m_rcBounds.left)
      ptNewPosition.x = m_rcBounds.right;
    else if (ptNewPosition.x > m_rcBounds.right)
      ptNewPosition.x = m_rcBounds.left - ptSpriteSize.x;
    if ((ptNewPosition.y + ptSpriteSize.y) < m_rcBounds.top)
      ptNewPosition.y = m_rcBounds.bottom;
    else if (ptNewPosition.y > m_rcBounds.bottom)
      ptNewPosition.y = m_rcBounds.top - ptSpriteSize.y;
  }
  // Stutsa?
  else if (m_baBoundsAction == BA_BOUNCE)
  {
    BOOL bBounce = FALSE;
    POINT ptNewVelocity = m_ptVelocity;
    if (ptNewPosition.x < m_rcBounds.left)
    {
      bBounce = TRUE;
      ptNewPosition.x = m_rcBounds.left;
      ptNewVelocity.x = -ptNewVelocity.x;
    }
    else if ((ptNewPosition.x + ptSpriteSize.x) > m_rcBounds.right)
    {
      bBounce = TRUE;
      ptNewPosition.x = m_rcBounds.right - ptSpriteSize.x;
      ptNewVelocity.x = -ptNewVelocity.x;
    }
    if (ptNewPosition.y < m_rcBounds.top)
    {
      bBounce = TRUE;
      ptNewPosition.y = m_rcBounds.top;
      ptNewVelocity.y = -ptNewVelocity.y;
    }
    else if ((ptNewPosition.y + ptSpriteSize.y) > m_rcBounds.bottom)
    {
      bBounce = TRUE;
      ptNewPosition.y = m_rcBounds.bottom - ptSpriteSize.y;
      ptNewVelocity.y = -ptNewVelocity.y;
    }
    if (bBounce)
      SetVelocity(ptNewVelocity);
  }
  // DÖ?
  else if (m_baBoundsAction == BA_DIE)
  {
    if ((ptNewPosition.x + ptSpriteSize.x) < m_rcBounds.left ||
      ptNewPosition.x > m_rcBounds.right ||
      (ptNewPosition.y + ptSpriteSize.y) < m_rcBounds.top ||
      ptNewPosition.y > m_rcBounds.bottom)
      return SA_KILL;
  }
  // Stoppa (Standard Läge)
  else
  {
    if (ptNewPosition.x  < m_rcBounds.left ||
      ptNewPosition.x > (m_rcBounds.right - ptSpriteSize.x))
    {
      ptNewPosition.x = max(m_rcBounds.left, min(ptNewPosition.x,
        m_rcBounds.right - ptSpriteSize.x));
      SetVelocity(0, 0);
    }
    if (ptNewPosition.y  < m_rcBounds.top ||
      ptNewPosition.y > (m_rcBounds.bottom - ptSpriteSize.y))
    {
      ptNewPosition.y = max(m_rcBounds.top, min(ptNewPosition.y,
        m_rcBounds.bottom - ptSpriteSize.y));
      SetVelocity(0, 0);
    }
  }
  SetPosition(ptNewPosition);
 
  return SA_NONE;
}

Sprite* Sprite::AddSprite()
{
  return NULL;
}

void Sprite::Draw(HDC hDC)
{
  // Rita Spriten ifall den inte är Dold!
  if (m_pBitmap != NULL && !m_bHidden)
  {
    // Rita den aktuella Bildrutan!
    if (m_iNumFrames == 1)
      m_pBitmap->Draw(hDC, m_rcPosition.left, m_rcPosition.top, TRUE);
    else
      m_pBitmap->DrawPart(hDC, m_rcPosition.left, m_rcPosition.top,
        m_iCurFrame * GetWidth(), 0, GetWidth(), GetHeight(), TRUE);
  }
}
OK, theres the Code, I hope you have any use of it... And if it's something you whant explained, yust ask.
Thanks!