วันพฤหัสบดีที่ 1 กันยายน พ.ศ. 2554

การบ้านครั้งที่5 : Samurai Island EP2



code เกมส์
#include <cdx_app_wiz.h>
#include <iostream>
#include <math.h>

using namespace std;

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

typedef struct {
       CDXSprite     *image;
       int x, y;
} Background;
Background bgs[20];

int world_x;


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;
};
// class Animation เป็น class ที่ช่วยให้เขียนแอนิเมชั่นง่ายขึ้น


int score=0;
int hp_boss=200;


typedef struct // ตัวแปรทั้งหลายที่ต้องใช้ของbg
{
       CDXSprite *sp;
       Animation *an;
       int x,y;
      
}World;
World *bg=new World();

typedef struct // ตัวแปรทั้งหลายที่ต้องใช้ของฮีโร่
{
       CDXSprite *sp;
       Animation *an;
       char heading;
       bool active;
       int x,y;
      
}Fin;
Fin *rudof=new Fin();
Fin *ninja=new Fin();
int hp_rudof=500;

const int $_mon =10 ;
typedef struct { //ตัวแปรทั้งหลายที่ต้องใช้ของ Enemy
       int o, p;
       int acceleration;
       int size;
       CDXSprite *src;
       bool active;
       Animation *an;
       int x_total;
       int mon_hp;
} MON;
MON mon[$_mon];

//irr::core::array <MON*> m_m;
//int u;


// ------------------------------------------------------------------
// 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 (int i=0; i < 15; i++) {
              MON* mmm = new MON();
              mmm->src = new CDXSprite();
              mmm->src->Create("mark.png", 70, 70, 1);
              if (i > 0) {
                     mmm->o = m_m[i-1]->o + (rand() % 100 + 40);
              } else {
                     mmm->o = (rand() % 100 + 40);
              }
              mmm->p = 200;
              mmm->src->SetPos(mmm->o + 500, mmm->p);
              mmm->active = true;

              m_m.push_back(mmm);
       }
*/
       for (int i=0; i < 15; i++) {
              bgs[i].image = new CDXSprite();
              char buffer[80];
              sprintf(buffer, "images/bg_%02d.png", i+1);
              bgs[i].image->Create(buffer, 1);
              bgs[i].x = 100*i;
              bgs[i].image->SetPosX(bgs[i].x);
       }

       rudof->sp = new CDXSprite();//สร้างฮีโร่
       rudof->sp->Create( "rudof.png",80,80, 1 );
       rudof->an = new Animation(rudof->sp);
       rudof->an->duration(1, 1, 2);

       ninja->sp = new CDXSprite();//สร้างฮีโร่
       ninja->sp->Create( "ninja.png",80,98, 1 );
       ninja->an = new Animation(ninja->sp);
       ninja->an->duration(1, 4, 3);
       ninja->active = true;
      
       if (score <= 19){
       for (int u=0; u < $_mon; u++) { //สร้าง enemy
              mon[u].o = rand () % 480+480; //จุดเกิดแกน x ของ enemy
              mon[u].p = 130 ;//จุดเกิดแกน y ของ enemy
              mon[u].acceleration  = rand () % 1+1;//ความเร่งของ enemy
              mon[u].size = 1;
              mon[u].active = true;
              mon[u].mon_hp = 10;
              mon[u].src = new CDXSprite();
              if (mon[u].size == 1)
              {
                     mon[u].src->Create("mark.png", 70,70,1);//ใส่ภาพ Enemy
                     mon[u].src->SetPos(mon[u].o, mon[u].p);
              }
              mon[u].an = new Animation(mon[u].src);//ใส่ แอนิเมชั่น
              mon[u].an->duration(1,8,1);
       }
       }

       return TRUE;
}

// ------------------------------------------------------------------
// cdx_DeInit - handles cleanup of CDX objects
// ------------------------------------------------------------------
void cdx_DeInit( void )
{
       // TODO: Destroy your CDX objects here


       SAFEDELETE( ninja->sp );

       SAFEDELETE( rudof->sp );
       for (int u=0; u < $_mon; u++) {
              SAFEDELETE( mon[u].src );
       }
       SAFEDELETE( Fps );
       SAFEDELETE( Input );
       SAFEDELETE( Screen );
       for (int i=0; i < 15; i++)
              SAFEDELETE( bgs[i].image );
}

// ------------------------------------------------------------------
// 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 (world_x > 0) world_x = 0;
       if (world_x < -1500 + 480) world_x = -1500 + 480;
       for (int i=0; i < 15; i++) {
              bgs[i].image->SetPosX(world_x + bgs[i].x);
              if (bgs[i].x <= (-1)*(world_x - 500))
                     bgs[i].image->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
       }

       //for (int i=0; i < m_m.size(); i++) {
       //     if (m_m[i]->active) {
       //            m_m[i]->o--;
       //            m_m[i]->src->SetPosX(m_m[i]->o);
       //            m_m[i]->src->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
       //     }
       //}
       if (score <= 19){
       for (int u=0; u < $_mon; u++) {

              if (mon[u].src->GetPosX() < 0 || !mon[u].active) //เช็คตำแหน่ง enemy
              {
                     mon[u].o = rand () % 480+480;
                     mon[u].p = 130 ;
                     mon[u].acceleration  = 2;
                     mon[u].size   =  1;
                     mon[u].mon_hp = 10;
                     mon[u].src->SetPos(mon[u].o, mon[u].p);
                     mon[u].active = true;
              }
              if (rudof->sp->SpriteHit(mon[u].src) && mon[u].active && rudof->active && mon[u].o-rudof->x<=100){
                     mon[u].mon_hp-=1;
                     if(mon[u].mon_hp<=0){
                     score +=1;
                     mon[u].active = false;//เช็คชน ตอนฮีโร่ฆ่า enemy
       }
              }
             
              mon[u].an->play();//สั่งให้ enemy เดิน
              mon[u].o = mon[u].src->GetPosX()-mon[u].acceleration;
              mon[u].src->SetPosX( mon[u].o );
              if (mon[u].active) mon[u].src->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
             
       }
       }
       if (rudof->x<-20)rudof->x=-20;
       if (rudof->x>330)rudof->x=330;

      

       for (int u=0; u < $_mon; u++) {
              if ( abs(mon[u].o-rudof->x)<=50&&score<20&&rudof->x >-15){
             
              rudof->x -= 40;
              hp_rudof -=5;

       }
              else if (abs(mon[u].o-rudof->x)<=80&&score<20&&rudof->x <= -20)
              hp_rudof --;
       }



       rudof->active = false;
       if( Input->GetKeyState(SDLK_RIGHT)||
              Input->GetKeyState(CDXKEY_JOYBUTN9)){
              rudof->x += 6;
              rudof->y = 0;
              if (rudof->x>-50){
              world_x -= 4;
              }
              for (int u=0; u < $_mon; u++)
              mon[u].acceleration = 3;
              rudof->an->duration(2,4,2);
              rudof->heading = 'R';
       }
       else if( Input->GetKeyState(SDLK_LEFT)||
              Input->GetKeyState(CDXKEY_JOYBUTN7)){
              rudof->x -= 6;
              rudof->y = 0;
              for (int u=0; u < $_mon; u++)
              mon[u].acceleration = 1;
              if (rudof->x>-50){
              world_x += 4;
              }
              rudof->an->duration(7,9,2);
              rudof->heading = 'L';
       }
      
       /*else if( Input->GetKeyState(SDLK_UP)||
              Input->GetKeyState(CDXKEY_JOYBUTN8)) {
              y -= 5;
       } */
       else if( Input->GetKeyState(SDLK_DOWN)||
              Input->GetKeyState(CDXKEY_JOYBUTN6)) {
              rudof->y = 8;
              for (int u=0; u < $_mon; u++)
              mon[u].acceleration = 2;
              if(rudof->heading == 'R') rudof->an->duration(5, 5);
              if (rudof->heading == 'L') rudof->an->duration(10, 10);
       }
      
       else {
              for (int u=0; u < $_mon; u++)
              mon[u].acceleration = 2;
              rudof->y = 0;
              if (rudof->heading == 'R') {
                     rudof->an->duration(1, 1);
             
              }
              if (rudof->heading == 'L') {
                     rudof->an->duration(6, 6);
                    
              }
       }
       if(Input->GetKeyState(SDLK_z)||Input->GetKeyState(CDXKEY_JOYBUTN3)) {

              rudof->active = true;
              if(rudof->heading == 'R')
              {
                     rudof->an->duration(11, 18,1);
              }

              if (rudof->heading == 'L')
              {
                     rudof->an->duration(21, 28,1);
              }
       }
      
      
       rudof->an->play();
       rudof->sp->SetPos(rudof->x+50,rudof->y+120);
       rudof->sp->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

       if (score >= 20){

       TextXY( Screen->GetBack(), 20, 50, 100, 100, 66, 255, ETA_CENTER, "HP Boss : %d",hp_boss );
       ninja->an->play();
       ninja->sp->SetPos(ninja->x+50,ninja->y+100);
              if (ninja->active) ninja->sp->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
      

       if (rudof->sp->SpriteHit(ninja->sp) && (Input->GetKeyState(SDLK_z)||Input->GetKeyState(CDXKEY_JOYBUTN3)))
       {
              hp_boss--;
       }
       if(hp_boss<=0){
              hp_boss = 0;
              ninja->active = false;//เช็คชน ตอนฮีโร่ฆ่า boss
       }

       if (abs(ninja->x-rudof->x)<=50){
                     rudof->x +=40;
                     hp_rudof-=10;
              }
       }


      
       TextXY( Screen->GetBack(), 20, 20, 100, 100, 66, 255, ETA_CENTER, "Score : %d",score );
       TextXY( Screen->GetBack(), 20, 10, 100, 100, 66, 255, ETA_CENTER, "HP Hero : %d",hp_rudof );



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


ตัวละครในเกมส์


 
 Simulate
 

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

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