8/12/2011

Update#1 Prince Drangon Game

การบ้านครั้งที่ 1 (Project Prince Dragon Game)
สั่งวันที่ 5 ส.ค. 54   
กำหนดส่ง 12ส.ค. 54

1 ต้องตัวละครสามารถเคลื่อนที่ ซ้าย ขวา บน ล่าง ได้
2 ตัวละคร (มังกร) ต้องพ่นไฟซึ่งเป็นอาวุธที่ใช้ในการต่อสู้ได้ โดยไฟสามารถเปลี่ยนได้ 2 แบบ
3 ตัวMonsterสามารถเช็คชนกับมังกร
การบ้านที่ส่งเป็นตัวอย่างแสดงให้เห็นว่าสามารถเขียนโค้ดให้ทำตามคำสั่งที่กำหนดไว้ได้ ฉากและตัวละครจริงจะมีการพัฒนาเพิ่มเติมมากกว่านี้ 
Play in simulator







ฉากในเกม



ด่านที่ 1


 ด่านที่ 2


Code Program

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

CDXSnd* bgsound;

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;
}
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 *img;
Animation *animate;
int hp;
bool active;
}mySprite;

mySprite *boss=new mySprite();
mySprite *dragon=new mySprite();

int active_weapon=0;
int score=0;



typedef struct 
{
CDXSprite *missile;
POINT pos, di, vel;
bool active;
Animation *animate;

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

bool AddMis();
void UpdateMis(void);
void RemoveMis(void);


bool AddMis()
{
Missile_info* minfo = new Missile_info( );

if (!active_weapon) {
minfo->missile = new CDXSprite();
minfo->missile->Create("new_fire.png", 60, 737, 4);
minfo->di.x = 1;
minfo->vel.x = 10;
boss->hp -= 100;
//score += 10;
minfo->pos.x = pos.x;
minfo->pos.y = pos.y - 5;

minfo->animate = new Animation(minfo->missile);
minfo->animate->duration(1, 4, 2);
} else {
minfo->missile = new CDXSprite();
minfo->missile->Create("bigfire2.png", 50, 87, 4);
minfo->di.x = 1;
minfo->vel.x = 10;
boss->hp -= 250;
//score += 50;
minfo->pos.x = pos.x;
minfo->pos.y = pos.y - 10 ;

minfo->animate = new Animation(minfo->missile);
minfo->animate->duration(1, 4, 3);
}

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+80, mlist[i]->pos.y- 10 );

if (mlist[i]->missile->SpriteHit(boss->img) && mlist[i]->active && boss->active) {
mlist[i]->active = false;
score += 20;
}



if (mlist[i]->active) {
if (mlist[i]->animate) mlist[i]->animate->play();
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;
}
}
}
typedef struct Animate_jump {

                int _delay;

                int delay;

                bool stop;

} Animate_jump;

Animate_jump frame;

typedef struct Sprite {

                int x, y;

                int speed;
        

                Animate_jump animate_jump;

                CDXSprite *sprite;

} Sprite;


// ------------------------------------------------------------------

// cdx_Init - handles initialization of the CDX objects

// ------------------------------------------------------------------

void animate2 (CDXSprite *s, Animate_jump *animate_jump) {

                if (!animate_jump->_frame || (animate_jump->_frame > animate_jump->last || animate_jump->_frame < animate_jump->first)) {

                                animate_jump->_frame = animate_jump->first;

                }

                s->SetFrame(animate_jump->_frame);

                if (animate_jump->_delay >= animate_jump->delay || !animate_jump->delay) {

                                animate_jump->_delay = 0;

                                animate_jump->_frame++;

                } else {

                                animate_jump->_delay++;

                }

                if (animate_jump->_frame > animate_jump->last) {

                                if (!animate_jump->stop) animate_jump->_frame = animate_jump->first;

                }

}

Sprite ooo;

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

dragon->img = new CDXSprite();
dragon->img->Create( "12.png", 70, 56, 4);
dragon->animate = new Animation(dragon->img);
dragon->animate->duration(1, 4, 3);
dragon->hp = 200;
dragon->active = true;

//////////sound/////////////

bgsound = new CDXSnd();
bgsound->Create();
bgsound->Play("mu_hardyz.ogg");

//////////////////////////////
              
boss->img = new CDXSprite();
boss->img->Create("boss2.png", 200, 158, 5);

boss->animate = new Animation(boss->img);
boss->animate->duration(1, 5, 5);
boss->hp = 3000;
boss->active = true;
boss->img->SetPos( 300, +100 );

                bg = new CDXSprite();

                bg->Create("bg.pcx",480,272,1);
                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;
}

SAFEDELETE( boss->img );

                SAFEDELETE( bg );

                SAFEDELETE( dragon );

                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

                bg->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

                if( Input->GetKeyState(SDLK_RIGHT)||

                                Input->GetKeyState(CDXKEY_JOYBUTN9))

                                pos.x += 6;

                if( Input->GetKeyState(SDLK_LEFT)||

                                Input->GetKeyState(CDXKEY_JOYBUTN7))

                                pos.x -= 6;

                if( Input->GetKeyState(SDLK_UP)||

                                Input->GetKeyState(CDXKEY_JOYBUTN8))
                               
                                {

                                if(jump==false)

                                upspeed -=15;

                                jump=true;

                                }
               
               if( pos.x < 0 )

                { pos.x = 0; }

                if(pos.x > (480-dragon->img->GetWidth() ))

                { pos.x =  480-dragon->img->GetWidth() ; }

               if(pos.y < 0 )

                { pos.y = 0; }

                if(pos.y > (272-dragon->img->GetHeight() ))

                { pos.y = 272-dragon->img->GetHeight() ;}

TextXY( Screen->GetBack(), 195, 20, 20, 100, 66, 255, "Score : %d", score );
TextXY( Screen->GetBack(), 40, 20, 32, 30, 100, 255, "Player : %d", dragon->hp );
TextXY( Screen->GetBack(), 350, 20, 250,20, 66, 255, "Boss : %d", boss->hp );


if( Input->GetKeyState(SDLK_z) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS ) {
AddMis();
}
UpdateMis( );
RemoveMis( );

if( Input->GetKeyState(SDLK_x) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN0) == CDXKEY_PRESS ) {
if (active_weapon < 1) {
active_weapon++;
} else {
active_weapon = 0;
}
}
                animate2(dragon->img,&frame);

                dragon->img->SetPos( pos.x, pos.y );

dragon->img->Draw( Screen->GetBack(), 0, 15, CDXBLT_TRANS );

 if(score >= 0 && boss->hp > 0)
{
boss->active = true;
boss->animate->play();
boss->img->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
}
if (boss->hp <= 0) {
boss->active = false;
Screen->GetBackEx()->Fill(0);
TextXY(Screen->GetBack(), 0, 130, 255, 255, 255, 255, ETA_CENTER, "You're The Winner !!!!!");
}


if(score >= 0) {
boss->active = true;
/////////ชนแล้วกระพริบ/////////////

if( dragon->img->SpriteHit(boss->img)){ //ชนกับบอส
dragon->active = false;
dragon->img->SetDelay(2);
dragon->active = true;
}

/////////////////////////////

/////////ชนแล้วเด้ง/////////////

if( dragon->img->SpriteHit(boss->img)){ //ชนกับบอส
dragon->hp -= 50;
dragon->img->SetPos( pos.x= pos.x-100, pos.y );
}

//////////////////////////
if (dragon->hp <= 0)
{
dragon->active = false;
Screen->GetBackEx()->Fill(0);
TextXY( Screen->GetBack(), 0, 130, 255, 255, 255, 255, ETA_CENTER,"GAME OVER");
}
}

///////// Start new game///////////////
  if( Input->GetKeyState(SDLK_SPACE)||Input->GetKeyState(CDXKEY_JOYBUTN11)){
charstate=0,upspeed,gravity=1;
active_weapon=0;
score=0;
dragon->hp = 200;
boss->hp = 3000;
upspeed =0;
//pos.y+=upspeed;
dragon->img->SetPos( x=0, y=15 );
  }

                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;

}

Code