Solved

Pong 500 points

Posted on 2003-12-07
8
640 Views
Last Modified: 2010-04-07
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!
0
Comment
Question by:OptionsMM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9894608
0
 

Expert Comment

by:Prophecy-Online
ID: 9933942
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
 
LVL 2

Expert Comment

by:Snurre
ID: 9938971
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:OptionsMM
ID: 9981713
Snurre,

Posting the Sprites would be great!  thanks...
0
 
LVL 2

Accepted Solution

by:
Snurre earned 500 total points
ID: 9982365
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
 
LVL 2

Expert Comment

by:Snurre
ID: 9982367
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
 
LVL 2

Expert Comment

by:Snurre
ID: 9982382
OK, theres the Code, I hope you have any use of it... And if it's something you whant explained, yust ask.
0
 

Author Comment

by:OptionsMM
ID: 9983038
Thanks!
0

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question