วันศุกร์ที่ 9 กันยายน พ.ศ. 2554

การบ้านครั้งที่6 : Samurai Island EP3

WZP8 (สั่ง 2 กย. 54 , ส่ง 9 กย. 54)
1.ถ้าส่งเกมที่เราทำได้อยุ่ตอนนี้ได้เกรด C
2.ให้ทำฉาก(ด่าน)เพิ่มเปน 3 ด่าน + ตัวมอนสเตอร์ + บอส
3.ทำให้ครบองค์ประกอบของเกม
*ถ้าทำข้อ 2, 3 จะพิจารณาเกรดอีกครั้ง


CODE GAME


#include <cdx_app_wiz.h>
#include <iostream>


using namespace std;
// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
CDXScreen   *Screen     = 0;          // The screen object, every program must have one
CDXInput      *Input        = 0;
FPSmanager    *Fps          = 0;
CDXSprite * gover = 0;
int score = 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 *image;
       int x, y;
} Background;
Background bgs[20];

int world_x;


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;
int hp_rudof_max = 500;
int hp_boss = 200;
int hp_boss_max = 200;


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


typedef struct {
       char* filename;
       int width, height;
       int a_start, a_stop, a_delay;
} Mon_Filename;

Mon_Filename mon_filename[3];


// ------------------------------------------------------------------
// cdx_Init - handles initialization of the CDX objects
// ------------------------------------------------------------------

int stage = 1;
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++) {
              bgs[i].image = new CDXSprite();
              char buffer[80];
              sprintf(buffer, "images/bg_%02d_ (%d).png", stage, 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);
       rudof->active = 1;
       rudof->x=50;
       rudof->y=120;
       rudof->sp->SetPos(rudof->x, rudof->y );

       gover = new CDXSprite();
       gover->Create("images/gameover.png",480,272,1);

       mon_filename[0].filename = "mark.png";
       mon_filename[1].filename = "mark.png";
       mon_filename[2].filename = "mark.png";

       mon_filename[0].width = 70;
       mon_filename[1].width = 70;
       mon_filename[2].width = 70;

       mon_filename[0].height = 70;
       mon_filename[1].height = 70;
       mon_filename[2].height = 70;

       mon_filename[0].a_start = 1;
       mon_filename[1].a_start = 1;
       mon_filename[2].a_start = 1;

       mon_filename[0].a_stop = 8;
       mon_filename[1].a_stop = 8;
       mon_filename[2].a_stop = 8;

       mon_filename[0].a_delay = 1;
       mon_filename[1].a_delay = 1;
       mon_filename[2].a_delay = 1;

       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 = 2*(stage*2); //ความเร่งของ 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);
              }
       }

       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;
       ninja->x =350;
       ninja->y =100;


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


       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*(stage*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;
                     }

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

              }
       }


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

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


       if (rudof->x < 0)
              rudof->x = 0;
       if (rudof->x > 400)
              rudof->x = 400;

       if (Input->GetKeyState(SDLK_RIGHT) || Input->GetKeyState(CDXKEY_JOYBUTN9)) {
              rudof->x += 6;
              if (rudof->x > -50) {
                     world_x -= 5;
              }
              for (int u = 0; u < $_mon; u++)
                     mon[u].acceleration = 5;
              rudof->an->duration(2, 4, 2);
              rudof->heading = 'R';
       } else if (rudof->x > 20&&Input->GetKeyState(SDLK_LEFT) || Input->GetKeyState(CDXKEY_JOYBUTN7)) {
              rudof->x -= 6;
              world_x += 5;
             
              for (int u = 0; u < $_mon; u++)
                     mon[u].acceleration = -5;
      
              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)) {
              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;

              if (rudof->heading == 'R') {
                     rudof->an->duration(1, 1);

              }
              if (rudof->heading == 'L') {
                     rudof->an->duration(6, 6);

              }
       }
       for (int u = 0; u < $_mon; u++) {
              if (mon[u].active && (Input->GetKeyState(SDLK_z) || Input->GetKeyState(CDXKEY_JOYBUTN3))) {
             
                     if (rudof->heading == 'R') {
                           rudof->an->duration(11, 18, 1);
                     }

                     if (rudof->heading == 'L') {
                           rudof->an->duration(21, 28, 1);
                     }
                     if (mon[u].mon_hp <= 0) {
                           score += 1;
                           mon[u].active = false; //เช็คชน ตอนฮีโร่ฆ่า enemy
                     }
              }
       }
       if (rudof->active) {
              if (hp_rudof <= 0) hp_rudof = 0;
              int hp = (float (hp_rudof)/( 100*(float (hp_rudof_max)/100))) * 100;
              int color;

              if (hp >=  0) color = 0xff0000FF;
              if (hp >= 15) color = 0xff5400FF;
              if (hp >= 40) color = 0xffc000FF;
              if (hp >= 75) color = 0x72ec10FF;

              boxColor(
                     Screen->GetBack(),
                     rudof->x - 1,
                     rudof->y - 1,
                     rudof->x + 1 + rudof->sp->GetBlockWidth(),
                     rudof->y + 3,
                     0x000000C8
              );
              boxColor(
                     Screen->GetBack(),
                     rudof->x,
                     rudof->y,
                     rudof->x + (hp * (float (rudof->sp->GetBlockWidth()) / 100)),
                     rudof->y + 2,
                     color
              );
              rudof->an->play();
              rudof->sp->SetPos(rudof->x, rudof->y );
              rudof->sp->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
       }

       if (score >= 20) {

              if (ninja->active)
              {
              int hp = (float (hp_boss)/( 100*(float (hp_boss_max)/100))) * 100;
              int color;

              if (hp >=  0) color = 0xff0000FF;
              if (hp >= 15) color = 0xff5400FF;
              if (hp >= 40) color = 0xffc000FF;
              if (hp >= 75) color = 0x72ec10FF;

              boxColor(
                     Screen->GetBack(),
                     ninja->x - 1,
                     ninja->y - 1,
                     ninja->x + 1 + ninja->sp->GetBlockWidth(),
                     ninja->y + 3,
                     0x000000C8
              );
              boxColor(
                     Screen->GetBack(),
                     ninja->x,
                     ninja->y,
                     ninja->x + (hp * (float (ninja->sp->GetBlockWidth()) / 100)),
                     ninja->y + 2,
                     color
              );
              TextXY(Screen->GetBack(), 20, 50, 100, 100, 66, 255, ETA_CENTER, "HP Boss : %d", hp_boss);
              ninja->an->play();
              ninja->sp->SetPos(ninja->x, ninja->y);
              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 (rudof->active&&abs(ninja->x - rudof->x) <= 50 && ninja->active) {
                     rudof->x -= 40;
                     hp_rudof -= 10;
              }
              }
              if (Input->GetKeyState(SDLK_h) == CDXKEY_PRESS && hp_boss == 0) {
                     rudof->heading='R';
                     world_x=0;
                     rudof->x=50;
                     ninja->active=true;
                     hp_boss = 200 * (stage+1);
                     hp_boss_max = 200 * (stage+1);
                     stage++;
                     score = 0;
                     for (int i = 0; i < 15; i++) {
                           SAFEDELETE(bgs[i].image);
                           bgs[i].image = new CDXSprite();
                           char buffer[80];
                           sprintf(buffer, "images/bg_%02d_ (%d).png", stage, i + 1);
                           bgs[i].image->Create(buffer, 1);
                           bgs[i].x = 100 * i;
                           bgs[i].image->SetPosX(bgs[i].x);
                     }
                     for (int u = 0; u < $_mon; u++) { //สร้าง enemy
                           SAFEDELETE(mon[u].src);
                           mon[u].o = rand() % 480 + 480; //จุดเกิดแกน x ของ enemy
                           mon[u].p = 130; //จุดเกิดแกน y ของ enemy
                           mon[u].acceleration = 2*(stage*2); //ความเร่งของ 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(mon_filename[stage-1].filename, mon_filename[stage-1].width, mon_filename[stage-1].height, 1); //ใส่ภาพ Enemy
                                  mon[u].src->SetPos(mon[u].o, mon[u].p);
                           }
                           mon[u].an = new Animation(mon[u].src); //ใส่ แอนิเมชั่น
                           mon[u].an->duration(mon_filename[stage-1].a_start, mon_filename[stage-1].a_stop, mon_filename[stage-1].a_delay);
                     }
              }
      
       }

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

              if (hp_rudof<=0) {
              hp_rudof==0;
              rudof->active=false;
              gover->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;
}


Simulate

 

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

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