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!
OptionsMMAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
0
Prophecy-OnlineCommented:
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.
0
SnurreCommented:
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)
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

OptionsMMAuthor Commented:
Snurre,

Posting the Sprites would be great!  thanks...
0
SnurreCommented:
OK, heres the H File
----

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

#ifndef _SPRITE_H_
#define _SPRITE_H_

#pragma once

#include <windows.h>
#include "Bitmap.h"

/*************************************************************************************/
// Manuella Data Typer!
/*************************************************************************************/
typedef WORD        SPRITEACTION;
const SPRITEACTION  SA_NONE      = 0x0000L,
                    SA_KILL      = 0x0001L,
                    SA_ADDSPRITE = 0x0002L;

typedef WORD        BOUNDSACTION;
const BOUNDSACTION  BA_STOP   = 0,
                    BA_WRAP   = 1,
                    BA_BOUNCE = 2,
                    BA_DIE    = 3;


class Sprite
{
protected:
  // Medlems Variabler
  Bitmap*       m_pBitmap;
  int           m_iNumFrames, m_iCurFrame;
  int           m_iFrameDelay, m_iFrameTrigger;
  RECT          m_rcPosition,
                m_rcCollision;
  POINT         m_ptVelocity;
  int           m_iZOrder;
  RECT          m_rcBounds;
  BOUNDSACTION  m_baBoundsAction;
  BOOL          m_bHidden;
  BOOL          m_bDying;
  BOOL          m_bOneCycle;

  // Hjälp Metoder
  void          UpdateFrame();
  virtual void  CalcCollisionRect();

public:
  // Konstruktorer / Destruktor
  Sprite(Bitmap* pBitmap);
  Sprite(Bitmap* pBitmap, RECT &rcBounds, BOUNDSACTION baBoundsAction = BA_STOP);
  Sprite(Bitmap* pBitmap, POINT ptPosition, POINT ptVelocity, int iZOrder,
                              RECT &rcBounds, BOUNDSACTION baBoundsAction = BA_STOP);
  virtual ~Sprite();

  // Generella Metoder
  virtual SPRITEACTION  Update();
  virtual Sprite*       AddSprite();
  void                  Draw(HDC hDC);
  BOOL                  IsPointInside(int x, int y);
  BOOL                  TestCollision(Sprite* pTestSprite);
  void                  Kill()      { m_bDying = TRUE; };

  // Åtkomst Metoder
  Bitmap* GetBitmap()               { return m_pBitmap; };
  void    SetNumFrames(int iNumFrames, BOOL bOneCycle = FALSE);
  void    SetFrameDelay(int iFrameDelay) { m_iFrameDelay = iFrameDelay; };
  RECT&   GetPosition()             { return m_rcPosition; };
  void    SetPosition(int x, int y);
  void    SetPosition(POINT ptPosition);
  void    SetPosition(RECT& rcPosition);
  void    OffsetPosition(int x, int y);
  RECT&   GetCollision()            { return m_rcCollision; };
  POINT   GetVelocity()             { return m_ptVelocity; };
  void    SetVelocity(int x, int y);
  void    SetVelocity(POINT ptVelocity);
  BOOL    GetZOrder()               { return m_iZOrder; };
  void    SetZOrder(int iZOrder)    { m_iZOrder = iZOrder; };
  void    SetBounds(RECT& rcBounds) { CopyRect(&m_rcBounds, &rcBounds); };
  void    SetBoundsAction(BOUNDSACTION ba) { m_baBoundsAction = ba; };
  BOOL    IsHidden()                { return m_bHidden; };
  void    SetHidden(BOOL bHidden)   { m_bHidden = bHidden; };
  int     GetWidth()
    { return (m_pBitmap->GetWidth() / m_iNumFrames); };
  int     GetHeight()               { return m_pBitmap->GetHeight(); };

  void SetBitmap(Bitmap *pBitmap) { m_pBitmap = pBitmap; };


};

/*************************************************************************************/
// Sprite Inline Hjälp Metod
/*************************************************************************************/
inline void Sprite::UpdateFrame()
{
  if ((m_iFrameDelay >= 0) && (--m_iFrameTrigger <= 0))
  {
    // Nollställ Frame Triggern!
    m_iFrameTrigger = m_iFrameDelay;

    // Minska Framen
    if (++m_iCurFrame >= m_iNumFrames)
    {
      // Ifall det är en 1frames animation, DÖDA Spriten
      if (m_bOneCycle)
        m_bDying = TRUE;
      else
        m_iCurFrame = 0;
    }
  }
}

inline void Sprite::CalcCollisionRect()
{
  int iXShrink = (m_rcPosition.left - m_rcPosition.right) / 12;
  int iYShrink = (m_rcPosition.top - m_rcPosition.bottom) / 12;
  CopyRect(&m_rcCollision, &m_rcPosition);
  InflateRect(&m_rcCollision, iXShrink, iYShrink);
}

/*************************************************************************************/
// Sprite Inline Generella Metoder
/*************************************************************************************/
inline BOOL Sprite::TestCollision(Sprite* pTestSprite)
{
  RECT& rcTest = pTestSprite->GetCollision();
  return m_rcCollision.left <= rcTest.right &&
         rcTest.left <= m_rcCollision.right &&
         m_rcCollision.top <= rcTest.bottom &&
         rcTest.top <= m_rcCollision.bottom;
}

inline BOOL Sprite::IsPointInside(int x, int y)
{
  POINT ptPoint;
  ptPoint.x = x;
  ptPoint.y = y;
  return PtInRect(&m_rcPosition, ptPoint);
}

/*************************************************************************************/
// Sprite Inline Åtkomst Metoder
/*************************************************************************************/
inline void Sprite::SetNumFrames(int iNumFrames, BOOL bOneCycle)
{
  // Ange antal nummer med Frames och OM DE SKALL SPELAS I EN ENDA HEL SEKVENS
  m_iNumFrames = iNumFrames;
  m_bOneCycle = bOneCycle;

  // Räkna ut ny position
  RECT rect = GetPosition();
  rect.right = rect.left + ((rect.right - rect.left) / iNumFrames);
  SetPosition(rect);
}

inline void Sprite::SetPosition(int x, int y)
{
  OffsetRect(&m_rcPosition, x - m_rcPosition.left, y - m_rcPosition.top);
  CalcCollisionRect();
}

inline void Sprite::SetPosition(POINT ptPosition)
{
  OffsetRect(&m_rcPosition, ptPosition.x - m_rcPosition.left,
    ptPosition.y - m_rcPosition.top);
  CalcCollisionRect();
}

inline void Sprite::SetPosition(RECT& rcPosition)
{
  CopyRect(&m_rcPosition, &rcPosition);
  CalcCollisionRect();
}

inline void Sprite::OffsetPosition(int x, int y)
{
  OffsetRect(&m_rcPosition, x, y);
  CalcCollisionRect();
}

inline void Sprite::SetVelocity(int x, int y)
{
  m_ptVelocity.x = x;
  m_ptVelocity.y = y;
}

inline void Sprite::SetVelocity(POINT ptVelocity)
{
  m_ptVelocity.x = ptVelocity.x;
  m_ptVelocity.y = ptVelocity.y;
}


#endif
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SnurreCommented:
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);
  }
}
0
SnurreCommented:
OK, theres the Code, I hope you have any use of it... And if it's something you whant explained, yust ask.
0
OptionsMMAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.