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

การบ้านครั้งที่3 วันที่ 1 กรกฏาคม 2554

PACMAN V.2.0

 screenshot
อธิบาย code


รันpacman ลงPSP 
code



#include <cdx_app_wiz.h>
#include <time.h>

// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
CDXScreen   *Screen     = 0;          // The screen object, every program must have one
CDXInput      *Input        = 0;
FPSmanager    *Fps          = 0;
CDXSprite     * pac         = 0;
CDXSprite     * bg          = 0;
CDXSnd        *ss;
CDXSnd        *waka;
CDXSnd        *win;

int s=0;
int x, y, i, j, c,a,d;
time_t start,end;


typedef struct
{
       int x,y;
       CDXSprite* ball;
       bool Active;
}BALLINFO;
BALLINFO myball[10][8];

// ------------------------------------------------------------------
// 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
      
       s = 0;
       x=5;
       y=5;

       ss = new CDXSnd( );
       ss->Create( );
      
       time (&start);

      
       ss->Play( "ss.wav" );

       waka = new CDXSnd( );
       waka->Create( );

       win = new CDXSnd( );
       win->Create();

       bg = new CDXSprite( );
       bg->Create( "bg.jpg", 1 );

       pac = new CDXSprite( );
       pac->Create( "pac.png",32,32,12 );

       for( i = 0; i < 10; i++ )
       for( j = 0; j < 8; j++ )
       {
              myball[i][j].ball = new CDXSprite( );
              myball[i][j].ball->Create( "ball.png", 1 );
              myball[i][j].x = 42*(i+1);
              myball[i][j].y = 24*(j+1);

              myball[i][j].ball->SetPos( myball[i][j].x, myball[i][j].y);
      
              myball[i][j].Active = true;
       }


      

       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( myball[i][j].ball );
       SAFEDELETE(pac);
       SAFEDELETE(bg);
       SAFEDELETE( ss );
       SAFEDELETE( waka );
       SAFEDELETE( win );

       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
      
       if ( Input->GetKeyState(SDLK_SPACE) || Input->GetKeyState(CDXKEY_JOYBUTN11) ) {

              s = 0;
              x=5;
              y=5;
             
              win->Stop("win.wav");
              ss->Stop( "ss.wav" );
              ss->Play( "ss.wav" );
              time (&start);

              for( i = 0; i < 10; i++ ) {
                     for( j = 0; j < 8; j++ ) {
                     myball[i][j].ball->SetPos( myball[i][j].x, myball[i][j].y );
                     myball[i][j].Active = true;
                     }
              }
       }

       time (&end);
      
       bg->SetPos( 0, 0 );
       bg->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );


       if( difftime (end,start) >= 5.0 ) {

       if( Input->GetKeyState(SDLK_RIGHT)||Input->GetKeyState(CDXKEY_JOYBUTN9))
       {
              if (d>0){
                    
              d=0;
              x += 12;
              pac->SetFrame( a++ );
              if (a>2)
              {a=0;}
              }}
       d++;
      


       if( Input->GetKeyState(SDLK_LEFT)||Input->GetKeyState(CDXKEY_JOYBUTN7))
       {
              if (d>0){
                    
              d=0;
              x -= 12;
              pac->SetFrame( a++ );
              if (a>5)
              {a=3;}
              }}
       d++;


       if( Input->GetKeyState(SDLK_UP)||Input->GetKeyState(CDXKEY_JOYBUTN8))
       {
              if (d>0){
                    
              d=0;
              y -= 12;
              pac->SetFrame( a++ );
              if (a>8)
              {a=6;}
              }}
       d++;

       if( Input->GetKeyState(SDLK_DOWN)||Input->GetKeyState(CDXKEY_JOYBUTN6))
       {
              if (d>0){
                    
              d=0;
              y += 12;
              pac->SetFrame( a++ );
              if (a>11)
              {a=9;}
              }}
       d++;
       }
      
      
       if (x<0)x=0;
       if (x>450)x=450;
       if (y<0)y=0;
       if (y>240)y=240;
      


       for( i = 0; i < 10; i ++ )
              for( j = 0; j < 8; j++ )
              {
                     if( myball[i][j].Active )
                           myball[i][j].ball->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
                    

                     if( pac->SpriteHit(myball[i][j].ball) && myball[i][j].Active ){
                           myball[i][j].Active = false;
                           s=s+100;
                           waka->Stop( "waka.wav" );
                           waka->Play( "waka.wav" );
                     }

              }

              TextXY( Screen->GetBack(), 150, 10, 233, 227, 66, 255,ETA_CENTER, "score = %d",s );
              if (s==8000)
      
              {
                     win->Play( "win.wav" );
                     x=230;
                     y=126;
                    
              TextXY( Screen->GetBack(), 150, 50, 233, 227, 66, 255,ETA_CENTER, "You Win!!" );
              TextXY( Screen->GetBack(), 150, 170, 233, 227, 66, 255,ETA_CENTER, "Press START to Play again." );}

      
       pac->SetPos( x, y );
       pac->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
      
      

       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;
}