[c++] [win32] jeux de shoot ( space shoot )

Description

Voila un petit jeux vidéo. C'est très simple, il faut shooter le max d'aliens. J'ai crée ce jeux pour tester mon moteur 2D. Donc, les sources, en plus du jeux, sont un moteur de jeux 2D entièrement écrit avec l'API Win32 et en C++. J'utilise aussi la bibliothèque Fmod ( qui est gratuite tant que 'elle n'est pas utilisée pour un usage commercial).
Je met en exemple la class Sprite ci dessous.

Source / Exemple :


//------------------------------
// sprites.cpp
//------------------------------

#include <windows.h>
#include <fstream>
#include "sprites.h"

namespace G2D
{

//bool cSprite::mCollision = false;

cSprite::cSprite(HDC hdc, bool nodes)
{

	mBitmap = new cBitmap(hdc);
	mNextSprite = NULL;
	mHdc = hdc;

	mPosX = 0;
	mPosY = 0;
	mWidth = 0;
	mHeight = 0;
	mNumFrame = 0;
	mNumColumn = 0;
	mFrameDelay = 0;
	mNodes = nodes;
    mVisible = true;
	mAlive = true;
	mLastDisplay = true;
}

cSprite::cSprite(HDC hdc, RECT rect, eReaction reaction, bool nodes)
{
	mBitmap = new cBitmap(hdc);
	mNextSprite = NULL;
	mHdc = hdc;
    mLimiteRect = rect;
	mReaction = reaction;
	mNodes = nodes;
    mVisible = true;
	mAlive = true;
	mLastDisplay = true;

    mPosX = 0;
	mPosY = 0;
    mWidth = 0;
	mHeight = 0;
	mNumFrame = 0;
	mNumColumn = 0;
	mFrameDelay = 0;
}

cSprite::~cSprite()
{
	
	delete mBitmap;
	if(mNextSprite)
		delete mNextSprite;	
}

void cSprite::LoadSprite(std::string name, int x, int y, int frame, int column , int delay )
{
   
	if(!mNodes)
	{ 
		mPosX = x;
		mPosY = y;
		mNumFrame = frame;
		mNumColumn = column;
		mFrameDelay = delay;
		mCurrentFrame = 0;
		mCountFrameDelay = 0;
		mCurrentColumn = 0;
		
		mBitmap->LoadBitmap(name.c_str());
		mWidth = mBitmap->GetBitmapWidth();
		mHeight = mBitmap->GetBitmapHeight();

		static_cast<int>(mFrameWidth) = (mWidth/mNumFrame);
		static_cast<int>(mFrameHeight) = (mHeight/mNumColumn);
		
		// for pixel perfect collision
		//------------------------------------------------------
        mT = new bool*[mWidth];
        for(int i=0;i<mWidth;i++)
			mT[i] = new bool[mHeight];

        for(int i=0;i<mWidth;i++)
	        for(int j=0;j<mHeight;j++)
	        {
		        if(GetPixel(mBitmap->GetMemHdc(), i, j) == RGB(255, 0, 255))
					mT[i][j] = false;
				else
					mT[i][j] = true;   
			}
		//-------------------------------------------------------

        SetRect(&mCollisionRect,x, y, x+mWidth, y+mHeight);
		mNextSprite = new cSprite(mHdc, true);
		return;
	}

	if(!mNextSprite)
	{
		mPosX = x;
	    mPosY = y;
		mNumFrame = frame;
		mNumColumn = column;
		mFrameDelay = delay;
		mCurrentFrame = 0;
		mBitmap->LoadBitmap(name.c_str());
		mWidth = mBitmap->GetBitmapWidth();
		mHeight = mBitmap->GetBitmapHeight();

        // for pixel perfect collision
		//------------------------------------------------------
        mT = new bool*[mWidth];
        for(int i=0;i<mWidth;i++)
			mT[i] = new bool[mHeight];

        for(int i=0;i<mWidth;i++)
	        for(int j=0;j<mHeight;j++)
	        {
		        if(GetPixel(mBitmap->GetMemHdc(), i, j) == RGB(255, 0, 255))
					mT[i][j] = false;
				else
					mT[i][j] = true;   
			}
		//-------------------------------------------------------

	    SetRect(&mCollisionRect,x, y, x+mWidth, y+mHeight);
		mNextSprite = new cSprite(mHdc, mLimiteRect, mReaction, true);
		return;
	}
	else
		mNextSprite->LoadSprite(name, x, y, frame, column, delay);
}

//--------------------------------------------
// Display a sprite
//--------------------------------------------
void cSprite::DisplaySprite()
{
	if(mNextSprite)
	{
		if(mAlive)
		{
		    if(mVisible || (!mVisible && mLastDisplay))
		    {
                 mBitmap->DisplayBitmap(mPosX, mPosY);
                 SetRect(&mCollisionRect,mPosX, mPosY, mPosX+mWidth, mPosY+mHeight);
			    if(!mVisible)
				    mLastDisplay = false;
		    }
	        mNextSprite->DisplaySprite();
		}
	}
}

//----------------------------------------------
// Display a transparent sprite
//----------------------------------------------
void cSprite::DisplaySprite(int R, int G, int B)
{
	if(mNextSprite)
	{
		if(mAlive)
		{
		    if(mVisible || (!mVisible && mLastDisplay))
		    {
             mBitmap->DisplayBitmap(mPosX, mPosY, R, G, B);
             SetRect(&mCollisionRect,mPosX, mPosY, mPosX+mWidth, mPosY+mHeight);
			    if(!mVisible)
				    mLastDisplay = false;
		    }
	        mNextSprite->DisplaySprite();
		}
	}
}

//-----------------------------------------------------------
// Display a transparent sprite with a BYTE[] as argument
//-----------------------------------------------------------
void cSprite::DisplaySprite(BYTE color[])
{
	if(this)
	{
	if(mNextSprite)
	{
		if(mAlive)
		{
		    if(mVisible || (!mVisible && mLastDisplay))
		    {
			    if(mNumFrame == 1)
			    {
                    mBitmap->DisplayBitmap(mPosX, mPosY, color);
                    SetRect(&mCollisionRect,mPosX, mPosY, mPosX+mWidth, mPosY+mHeight);
			        if(!mVisible)
				    mLastDisplay = false;
		        }
			    else
			    {
					mBitmap->DisplayPartBitmap(mPosX, mPosY, (mFrameWidth*mCurrentFrame), (mFrameHeight*mCurrentColumn),
					                       mFrameWidth, mFrameHeight, color);

				    if(mCountFrameDelay == mFrameDelay)
				    {
					     if((mCurrentFrame++) == (mNumFrame+1))
					     mCurrentFrame = 0; 

                         mCountFrameDelay = 0;
			        }
					else
						mCountFrameDelay++;
				}
			}
		}
	}
	mNextSprite->DisplaySprite();
	}
}

void cSprite::MooveSprite(int x, int y)
{
	if(mNextSprite)
	{
	  if(mReaction == SR_NOREACTIONS)
	  {
		  mPosX += x;
		  mPosY += y;
          SetRect(&mCollisionRect,mPosX, mPosY, mPosX+mWidth, mPosY+mHeight);
		  return;
	  }

	  if(mReaction == SR_STOP)
	  {
		  if(mLimiteRect.top<(mPosX+x) && mLimiteRect.bottom>(mPosX+x+mWidth) &&
		     mLimiteRect.left<(mPosY+y) && mLimiteRect.right>(mPosY+y+mHeight))
		  {
			  mPosX += x;
	          mPosY += y;
			  SetRect(&mCollisionRect,mPosX, mPosY, mPosX+mWidth, mPosY+mHeight);
			  return;
		  }
	  }

	  if(mReaction == SR_DELETE)
	  {
		  if(mLimiteRect.top<(mPosX+x) && mLimiteRect.bottom>(mPosX+x+mWidth) &&
		     mLimiteRect.left<(mPosY+y) && mLimiteRect.right>(mPosY+y+mHeight))
		  { 
			  mPosX += x;
		      mPosY += y;
			  SetRect(&mCollisionRect,mPosX, mPosY, mPosX+mWidth, mPosY+mHeight);
		  }
		  else
			  mVisible = false;
	  }
	  
	  
	  mNextSprite->MooveSprite(x, y);
	}
}

void cSprite::UpdateFrame()
{
     
    
}

INIT CreateSpriteObject(HDC hdc, bool nodes, cSprite** sprite)
{

  • sprite = new cSprite(hdc, nodes);
if(*sprite!= NULL) return G2D_OK; else return G2D_FAILED; } INIT CreateSpriteObject(HDC hdc, RECT rect_limite, eReaction reaction, bool nodes, cSprite** sprite) {
  • sprite = new cSprite(hdc, rect_limite, reaction, nodes);
if(*sprite!=NULL) return G2D_OK; else return G2D_FAILED; } } //namespace G2D

Conclusion :


Dîtes moi ce que vous en pensez.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (cs_nikau)