วันศุกร์ที่ 8 กรกฎาคม พ.ศ. 2554

Pacman Version 2

ทดสอบเกมและอธิบายโค้ด





โค้ดโปรแกรม

#include<stdio.h>
#include<conio.h>
#include <cdx_app_wiz.h>

// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
CDXScreen   *Screen     = 0;      // The screen object, every program must have one
CDXInput    *Input            = 0;
FPSmanager  *Fps        = 0;
CDXSprite *BG=0;
CDXSprite *pac1=0;
CDXSnd*     g_snd;
int MaxFrame =15;
int MinFrame =0;
int ZeroFrame=MinFrame;
int MaxFrameCoin=15;
int MinFrameCoin=0;
int ZeroFrameCoin =MinFrameCoin;
int delayCoin,delayBG=0;
int MaxFrameBG=11;
int MinFrameBG=0;
int ZeroFrameBG=MinFrameBG;

char loop;
int x, y, i, j, c;
typedef struct
{
      int x, y;
      CDXSprite* coin;
      bool Active;
}COININFO;

COININFO mycoin[10][8];
bool still=false;
int a=0;
int score,points,num,k=0;
// ------------------------------------------------------------------
// cdx_Init - handles initialization of the CDX objects
// ------------------------------------------------------------------
BOOL cdx_Init()
{
      Screen = new CDXScreen();
      Screen->Create( );
      Screen->CreateWindowed( 480, 272, 32, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_INIT_TIMER );

      Input = new CDXInput( );
      Input->Create( );

      Fps = new FPSmanager( );
      SDL_initFramerate( Fps );
      SDL_setFramerate( Fps, 30 );

      // TODO: Initialize your own CDX objects here

      for( i = 0; i < 10; i++ )
      for( j = 0; j < 8; j++ )
      {
            mycoin[i][j].coin = new CDXSprite( );
            mycoin[i][j].coin->Create( "coin.jpg",25,25,14 );
            mycoin[i][j].x = 40*(i+1);
            mycoin[i][j].y = 30*(j+1);
                 
            mycoin[i][j].coin->SetPos( mycoin[i][j].x, mycoin[i][j].y );
            mycoin[i][j].Active = true;
           
      }
      g_snd = new CDXSnd( );
      g_snd->Create( );

      pac1 = new CDXSprite();
      pac1->Create("pac1.png",25,25,15);

      BG = new CDXSprite();
      BG -> Create("bg.png",480,272,11);
           

      return TRUE;
}

// ------------------------------------------------------------------
// cdx_DeInit - handles cleanup of CDX objects
// ------------------------------------------------------------------
void cdx_DeInit( void )
{
      // TODO: Destroy your CDX objects here
      for( i = 0; i < 10; i++ )
            for( j = 0; j < 8; j++ )
            SAFEDELETE( mycoin[i][j].coin );
      SAFEDELETE(pac1);
      SAFEDELETE(BG);
      SAFEDELETE( g_snd );

      SAFEDELETE( Fps );
      SAFEDELETE( Input );
      SAFEDELETE( Screen );
}

// ------------------------------------------------------------------
// cdx_DoFrame - performs drawing of the current frame
// ------------------------------------------------------------------
void cdx_DoFrame()
{
      Input->Update( );

      Screen->GetBackEx()->Fill(0);

      // TODO: Add code to draw your objects during each frame
     
            g_snd->Play( "siren.ogg" );
            if(x<=0) x=0;
            if (x>=450)
                  x=450;
            if(y<=0)y=0;
            if(y>=245)
                  y=245;

            pac1->SetPos( x, y );

            delayBG++;
            if(ZeroFrameBG<=MaxFrameBG){
                  BG->SetPos(0,0);
                  BG->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                  BG->SetFrame(MinFrameBG);
                  if(delayBG==4){
                        MinFrameBG++;
                        delayBG=0;
                        if(MinFrameBG>=11){
                              MinFrameBG=0;
                        }
                  }
            }

            switch(a){
                  case 1:if(ZeroFrame<=MaxFrame){
                              pac1->SetFrame(MinFrame);
                              pac1->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                              MinFrame++;
                              if(MinFrame>=2){
                                    MinFrame=0;
                              }
                        }
                        break;
                  case 2:if(ZeroFrame<=MaxFrame){
                             
                              pac1->SetFrame(MinFrame+1);
                              pac1->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                              MinFrame++;
                              if(MinFrame>=3){
                                    MinFrame=1;
                              }
                        }
                        break;
                  case 3:if(ZeroFrame<=MaxFrame){
                              pac1->SetFrame(MinFrame+2);
                              pac1->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                              MinFrame++;
                              if(MinFrame>=4){
                                    MinFrame=2;
                              }
                        }
                        break;
                  case 4:if(ZeroFrame<=MaxFrame){
                              pac1->SetFrame(MinFrame+3);
                              pac1->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                              MinFrame++;
                              if(MinFrame>=5){
                                    MinFrame=3;
                              }
                        }
                        break;
                  case 0:pac1->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );break;
                  case 9: break;
            }
            a=0;
            if(num!=80){
                  if( Input->GetKeyState(SDLK_RIGHT)||Input->GetKeyState(CDXKEY_JOYBUTN9)){
                        x += 5;a=1;
                  }//right
                  if( Input->GetKeyState(SDLK_LEFT)||Input->GetKeyState(CDXKEY_JOYBUTN7)){
                        x -= 5;a=2;
                  }//left
                  if( Input->GetKeyState(SDLK_UP)||Input->GetKeyState(CDXKEY_JOYBUTN8)){
                        y -= 5;a=3;
                  }//up
                  if( Input->GetKeyState(SDLK_DOWN)|| Input->GetKeyState(CDXKEY_JOYBUTN6)){
                        y += 5; a=4;
                  }//down
            }//end if
     
            delayCoin++;
            for( i = 0; i < 10; i ++ ){
                  for( j = 0; j < 8; j++ )
                  {
                       
                        if( pac1->SpriteHit(mycoin[i][j].coin) && mycoin[i][j].Active ){
                              mycoin[i][j].Active = false;
                              points=points+10;
                              score=points;
                              num=num+1;
                              g_snd->Play( "munch_a.wav" );
                        }
                        if( mycoin[i][j].Active ){
                              if(ZeroFrameCoin<=MaxFrameCoin){
                                    mycoin[i][j].coin->SetFrame(MinFrameCoin);
                                    mycoin[i][j].coin->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                                   
                                    if(delayCoin==2){
                                          MinFrameCoin++;
                                          delayCoin=0;
                                    }//if delay
                                    if(MinFrameCoin>=15){
                                          MinFrameCoin=ZeroFrameCoin;
                                    }//if reset delay
                              }
                        }
                  }
            }
            score=points+score;
            TextXY( Screen->GetBack(), 20, 20, 233, 227, 66, 255, ETA_RIGHT, "Score : %d",points);
            if(num==80){
                  a=9;
                  TextXY(Screen->GetBack(),50,70,233,227,66,255,ETA_CENTER,"You Win.");
                  pac1->SetPos( 230, 90 );
                  pac1->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
                  TextXY(Screen->GetBack(),50,130,233,227,66,255,ETA_CENTER,"Congratulation.Your score : %d",points);
                  TextXY(Screen->GetBack(),50,150,233,227,66,255,ETA_CENTER,"Press start or F1 for start new game.");    
                 
            }
            if(Input->GetKeyState(SDLK_F1)||Input->GetKeyState(CDXKEY_JOYBUTN11)){
                        for( i = 0; i < 10; i++ )
                              for( j = 0; j < 8; j++ ){
                                    mycoin[i][j].x = 40*(i+1);
                                    mycoin[i][j].y = 30*(j+1);
                                    mycoin[i][j].Active = true;
                              }
                  pac1->SetPos(x,y);
                  pac1->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
                  x=0;y=0;score=0;points=0;num=0;k=0;delayCoin=0;
            }// end if
            Screen->Flip( 0, 0, 1 );
            SDL_framerateDelay( Fps );
}

int main( int argc, char* args[] )
{
#ifdef _PSP

#ifndef NDEBUG
      pspDebugScreenInit( );
#endif

      SetupCallbacks( );
#endif     

      cdx_Init();

      while(1){
                  #ifdef WIN32
                        if( Input->GetKeyState(SDLK_ESCAPE) )
                              break;
                              #endif
                              cdx_DoFrame();
      }
            cdx_DeInit();

            return 0;
 }


ไม่มีความคิดเห็น:

แสดงความคิดเห็น