วันพฤหัสบดีที่ 4 สิงหาคม พ.ศ. 2554

การบ้านครั้งที่4

จงเขียนเกมส์ ให้มีรายละเอียดดังนี้
1. Player move by keyboard 8 directions.
2. Player have 2 type missile can change between play game.
3. Game have enemy draw with transformation image and moving to player in up, down, left or right direction.
4. Score over 10 player meet BOSS.
5. Kill boss game is over and show you win.
NOTE: Game have Transformation Image

อธิบายcode



เล่นในPSP







CODE 


#include <cdx_app_wiz.h>


// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
CDXScreen   *Screen     = 0;          // The screen object, every program must have one
CDXInput      *Input        = 0;
FPSmanager    *Fps          = 0;


class Animation {
public:
       Animation( CDXSprite *unit ) {
              this->unit = unit;
       }
       void duration( unsigned int a = 1, unsigned int b = 2, unsigned int delay = 0, bool loop = true ) {
              this->a = a - 1;
              this->b = b - 1;
              this->delay = delay;
              this->loop = loop;
       }
       unsigned int getLastFrame() {
              return this->c_frame+1;
       }
       void shuffle(unsigned int f) {
              this->c_frame = rand() % f;
       }

       unsigned int play() {
              if (!this->loop) {
                     if (this->c_frame < this->a) {
                           this->c_frame = this->a;
                     }
              } else {
                     if (this->c_frame > this->b || this->c_frame < this->a) {
                           this->c_frame = this->a;
                     }
              }
              this->unit->SetFrame(this->c_frame);
              if (this->c_delay >= this->delay || !this->delay) {
                     this->c_delay = 0;
                     this->c_frame += (this->a < this->b) ? 1 : -1;
              } else {
                     this->c_delay++;
              }
              return this->c_frame;
       }
       ~Animation();
private:
       CDXSprite *unit;
       unsigned int a;
       unsigned int b;
       unsigned int delay;
       bool loop;
       unsigned int c_frame;
       unsigned int c_delay;
};

typedef struct
{
       CDXSprite *sp;
       Animation *an;
       int hp;
}Fin;
Fin *j=new Fin();
Fin *bom=new Fin();
Fin *boss=new Fin();

const int $_mon =10 ;

int active_weapon=0;
int score=0;

typedef struct {
       int o, p;
       int acceleration;
       int size;
       CDXSprite *src;
       bool active;
       Animation *t;
} MON;
MON mon[$_mon];
POINT pos;
int u;


typedef struct
{
       CDXSprite     *missile;
       CDXSprite     *missile2;
       POINT pos, di, vel;
       POINT  di2, vel2;
       bool active;
       Animation *a;

}Missile_info;
irr::core::array<Missile_info*> mlist;

bool AddMis( char* name, const POINT& pos, const POINT& di, const POINT& vel );
void UpdateMis(void);
void RemoveMis(void);

bool AddMis( char* name, const POINT& pos, const POINT& di, const POINT& vel )
{
      
       Missile_info* minfo = new Missile_info( );
       minfo->missile = new CDXSprite( );
       minfo->missile->Create( "m.png", 7, 7, 1 );
       minfo->pos = pos;
       minfo->di = di;
       minfo->vel = vel;
       minfo->active = true;
       mlist.push_back( minfo );

       return true;
}

void UpdateMis(void)
{
       for( irr::u32 i = 0; i != mlist.size(); i++ )
       {
              mlist[i]->pos.x += mlist[i]->di.x * mlist[i]->vel.x;
              mlist[i]->pos.y += mlist[i]->di.y * mlist[i]->vel.y;
              mlist[i]->missile->SetPos( mlist[i]->pos.x+95, mlist[i]->pos.y+25 );
              for (int u=0; u < $_mon; u++) {
                     if (mon[u].src->SpriteHit(mlist[i]->missile) && mlist[i]->active && mon[u].active&& score<10) {
                           mon[u].active = false;
                           mlist[i]->active = false;
                           score += 1;
                     }
              }
              if (mlist[i]->missile->SpriteHit(boss->sp) && mlist[i]->active) {
                     boss->hp -= 60;
                     mlist[i]->active = false;
              }
              if (mlist[i]->active) mlist[i]->missile->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
       }
}

void RemoveMis(void)
{
       for( irr::u32 i = 0; i != mlist.size(); ++i )
       {
              if( mlist[i]->pos.x < 0 ||
                     mlist[i]->pos.x > 480 ||
                     mlist[i]->pos.y < 0 ||
                     mlist[i]->pos.y > 272 )
              {
                     SAFEDELETE( mlist[i]->missile );
                     SAFEDELETE( mlist[i] );
                     mlist.erase( i );
                     --i;
              }
       }
}

Missile_info;
irr::core::array<Missile_info*> mlist2;

bool AddMis2( char* name2, const POINT& pos, const POINT& di2, const POINT& vel2 );
void UpdateMis2(void);
void RemoveMis2(void);

bool AddMis2( char* name2, const POINT& pos, const POINT& di2, const POINT& vel2 )
{
       Missile_info* minfo2 = new Missile_info( );
       minfo2->missile2 = new CDXSprite( );
       minfo2->missile2->Create( "m2.png",58,43,1 );
       minfo2->pos = pos;
       minfo2->di2 = di2;
       minfo2->vel2 = vel2;
       minfo2->active = true;
       mlist2.push_back( minfo2 );
       minfo2->a = new Animation(minfo2->missile2);
       minfo2->a->duration(1,7,1);
       return true;
}

void UpdateMis2(void)
{
       for( irr::u32 i = 0; i != mlist2.size(); i++ )
       {
              mlist2[i]->pos.x += mlist2[i]->di2.x * mlist2[i]->vel2.x;
              mlist2[i]->pos.y += mlist2[i]->di2.y * mlist2[i]->vel2.y;
              mlist2[i]->missile2->SetPos( mlist2[i]->pos.x+95, mlist2[i]->pos.y );
              for ( int u=0; u < $_mon; u++) {
                     if (mon[u].src->SpriteHit(mlist2[i]->missile2) && mlist2[i]->active && mon[u].active&& score<10) {
                           mon[u].active = false;
                           mlist2[i]->active = false;
                           score += 1;
                     }
              }

              if (mlist2[i]->missile2->SpriteHit(boss->sp) && mlist2[i]->active) {
                     boss->hp -= 20;
                     mlist2[i]->active = false;
              }



              mlist2[i]->a->play();

              if (mlist2[i]->active) mlist2[i]->missile2->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
       }
}

void RemoveMis2(void)
{
       for( irr::u32 i = 0; i != mlist2.size(); ++i )
       {
              if( mlist2[i]->pos.x < 0 ||
                     mlist2[i]->pos.x > 480 ||
                     mlist2[i]->pos.y < 0 ||
                     mlist2[i]->pos.y > 272 )
              {
                     SAFEDELETE( mlist2[i]->missile2 );
                     SAFEDELETE( mlist2[i] );
                     mlist2.erase( i );
                     --i;
              }
       }
}

// ------------------------------------------------------------------
// 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


       j->sp = new CDXSprite();
       j->sp->Create( "p.png",52,39, 1 );

       j->an = new Animation(j->sp);
       j->an->duration(1,15,1);
       j->hp = 200;


       bom->sp = new CDXSprite();
       bom->sp->Create( "b.png",60,27, 1 );
       bom->an = new Animation(bom->sp);
       bom->an->duration(1,15,1);

       boss->sp = new CDXSprite();
       boss->sp->Create("boss.png",52,111, 1 );
       boss->an = new Animation(boss->sp);
       boss->an->duration(1,2,5);
       boss->hp = 1000;
      


       //---


       srand(time(NULL)); //สร้าง key ในการ Random

       for (u=0; u < $_mon; u++) { //สร้างฟองอากาศ
              mon[u].o = rand () % 480+480;
              mon[u].p = rand () % 200 ;

              mon[u].acceleration  = rand () % 3 + 1;//ความเร่งของลูกบอล random
              mon[u].size = 1;
              mon[u].active = true;
              mon[u].src = new CDXSprite();
              if (mon[u].size == 1)
              {
                     mon[u].src->Create("mon.png", 34,32,1);
                     mon[u].src->SetPos(mon[u].o, mon[u].p);
              }

              mon[u].t = new Animation(mon[u].src);
              mon[u].t ->duration(1,2,2);
             
       }



       return TRUE;
}

// ------------------------------------------------------------------
// cdx_DeInit - handles cleanup of CDX objects
// ------------------------------------------------------------------
void cdx_DeInit( void )
{
       // TODO: Destroy your CDX objects here
        
       for( irr::u32 i = 0; i != mlist.size(); ++i )
       {
              SAFEDELETE( mlist[i]->missile );
              SAFEDELETE( mlist[i] );
              mlist.erase( i );
              --i;
       }

       for( irr::u32 i = 0; i != mlist2.size(); ++i )
       {
              SAFEDELETE( mlist2[i]->missile2 );
              SAFEDELETE( mlist2[i] );
              mlist2.erase( i );
              --i;
       }


       for (u=0; u < $_mon; u++) {
              SAFEDELETE( mon[u].src );
       }

       SAFEDELETE( j->sp );
       SAFEDELETE( bom->sp );
       SAFEDELETE( boss->sp );
       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


       for (u=0; u < $_mon; u++) {

              if (mon[u].src->GetPosX() < 0 || !mon[u].active) //เช็คตำแหน่งฟองว่าเลยจอหรื่อไม่

              {

                     mon[u].o = rand () % 480+480;
                     mon[u].p = rand () % 200 ;
                     mon[u].acceleration  = rand () % 3 + 1;
                     mon[u].size   =  1;

                     mon[u].src->SetPos(mon[u].o, mon[u].p);

                     mon[u].active = true;

              }
             
              mon[u].t->play();
              mon[u].o = mon[u].src->GetPosX()-mon[u].acceleration;//เซ็ทตำแหน่งให้ฟองลอยไปข้างบน
              mon[u].src->SetPosX( mon[u].o );//เซ็ทตำแหน่งฟองอากาศ
              if (mon[u].active && score < 10) mon[u].src->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
       }
             
       if( Input->GetKeyState(SDLK_RIGHT)||
              Input->GetKeyState(CDXKEY_JOYBUTN9))
              pos.x += 5;

       if( Input->GetKeyState(SDLK_LEFT)||
              Input->GetKeyState(CDXKEY_JOYBUTN7))
              pos.x -= 5;

       if( Input->GetKeyState(SDLK_UP)||
              Input->GetKeyState(CDXKEY_JOYBUTN8))
              pos.y -= 5;

       if( Input->GetKeyState(SDLK_DOWN)||
              Input->GetKeyState(CDXKEY_JOYBUTN6))
              pos.y += 5;

       if (pos.x<0)pos.x=0;
       if (pos.x>450)pos.x=450;
       if (pos.y<0)pos.y=0;
       if (pos.y>230)pos.y=230;

       j->an->play();
       j->sp->SetPos( pos.x+52, pos.y );
       j->sp->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

       bom->an->play();
       bom->sp->SetPos( pos.x, pos.y+18 );
       bom->sp->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
      
       TextXY( Screen->GetBack(), 380, 20, 233, 227, 66, 255, "score : %d", score );

       POINT di = { 1, 0 };
       POINT vel = { 15, 0 };
       POINT di2 = { 1, 0 };
       POINT vel2 = { 7, 0 };

       UpdateMis( );
       RemoveMis( );
       UpdateMis2( );
       RemoveMis2( );

       if( Input->GetKeyState(SDLK_z) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS ) {
              if (active_weapon == 0) {
                     AddMis2( "m2.png", pos,di2,vel2 );
              } else if (active_weapon == 1) {
                     AddMis( "m.png", pos, di, vel );
              }
       }
      
       if( Input->GetKeyState(SDLK_x) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN0) == CDXKEY_PRESS ) {

              if (active_weapon < 1) {
                     active_weapon++;
              } else {
                     active_weapon = 0;
              }

       }

       if(score==10 && boss->hp > 0)
       {
              boss->an->play();
              boss->sp->SetPos( 350, 50 );
              boss->sp->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
       }

       if (boss->hp <= 0) {
              TextXY(Screen->GetBack(), 0, 100, 255, 255, 255, 255, ETA_CENTER, "You Won !!!!!!!!");
       }

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


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

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