8/11/2011

Homework#3 Shooting game

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

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


ScreenShot



Start  Game


Play Game (Missile1)


 Play Game (Missile2)


You Win


You Lose


อธิบายการทำงานของโปรแกรม


Play in simulator



Play in PSP

 

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;

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 *j=new mySprite();
mySprite *kk=new mySprite();
mySprite *boss=new mySprite();
mySprite *BG=new mySprite();


const int $_mon =20 ;
int active_weapon=0;
int score=0;

typedef struct {
int x, y;
int acceleration;
int size;
CDXSprite *src;
bool active;
Animation *animate;
} MON;
MON monster[$_mon];
POINT pos;
int u;

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("fire spe2.png", 25, 30, 5);
minfo->di.x = 1;
minfo->vel.x = 7;
//boss->hp -= 20;
minfo->pos.x = pos.x;
minfo->pos.y = pos.y + 25;

minfo->animate = new Animation(minfo->missile);
minfo->animate->duration(1, 5, 1);
} else {
minfo->missile = new CDXSprite();
minfo->missile->Create("bigfire.png", 61, 60, 4);
minfo->di.x = 1;
minfo->vel.x = 3;
//boss->hp -= 50;
minfo->pos.x = pos.x;
minfo->pos.y = pos.y + 5;

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

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 );
for ( int u=0; u < $_mon; u++) {
if (monster[u].src->SpriteHit(mlist[i]->missile) && mlist[i]->active && monster[u].active && score < 100) {
kk->active = true;
kk->animate->play();
monster[u].active = false;
mlist[i]->active = false;
score += 5;
}
}

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


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

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

srand(time(NULL)); //สร้าง Random

for (u=0; u < $_mon; u++) { //create monster
monster[u].x = rand () % 480+480;
monster[u].y = rand () % 200 ;

monster[u].acceleration = rand () % 3 + 1;
monster[u].size = 1;
monster[u].active = true;
monster[u].src = new CDXSprite();

if (monster[u].size == 1) 
{
monster[u].src->Create("enemy.png", 30, 30, 3);
monster[u].src->SetPos(monster[u].x, monster[u].y);
}

monster[u].animate = new Animation(monster[u].src);
monster[u].animate ->duration(1, 3, 2);
}

boss->img = new CDXSprite();
boss->img->Create("boss2.png", 180, 143, 5);

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

kk->img = new CDXSprite();
kk->img->Create("player3.png", 75, 50, 12);
kk->img->SetPos(pos.x,pos.y);

kk->animate = new Animation(kk->img);
kk->animate->duration(1, 12, 10);
kk->active = true;

j->img = new CDXSprite();
j->img->Create( "player2.png", 75, 50, 2 );
j->animate = new Animation(j->img);
j->animate->duration(1, 2, 2);
j->hp = 200;
j->active = true;
BG->img = new CDXSprite();
BG->img->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 );

for (u=0; u < $_mon; u++) {
SAFEDELETE( monster[u].src );
}
SAFEDELETE( j->img );
SAFEDELETE( kk->img );
SAFEDELETE( BG->img );
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->img->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

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

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

monster[u].x = rand () % 480+480;
monster[u].y = rand () % 200 ;
monster[u].acceleration = rand () % 3 + 1;
monster[u].size =  1;
monster[u].src->SetPos(monster[u].x, monster[u].y);
monster[u].active = true;
}
monster[u].animate->play();
monster[u].x = monster[u].src->GetPosX()-monster[u].acceleration;//เซ็ทตำแหน่งให้ฟองลอยไปข้างบน
monster[u].src->SetPosX( monster[u].x );//เซ็ทตำแหน่งฟองอากาศ
if (monster[u].active && score < 100) monster[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> (480 - j->img->GetBlockWidth()))pos.x= 480 - j->img->GetBlockWidth();
if (pos.y<0)pos.y=0;
if (pos.y>230)pos.y=230;

j->animate->play();
j->img->SetPos( pos.x, pos.y );
j->img->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

TextXY( Screen->GetBack(), 195, 20, 100, 100, 66, 255, "Score : %d", score );
TextXY( Screen->GetBack(), 50, 20, 150, 150, 66, 255, "Player : %d", j->hp );
if(score >= 100)
TextXY( Screen->GetBack(), 360, 20, 200, 200, 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;
}
}

if(score >= 100 && 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 >= 100) {
boss->active = true;
/////////ชนแล้วกระพริบ/////////////

if( j->img->SpriteHit(boss->img)){ //ชนกับบอส
kk->active = true;
j->active = true;
kk->animate->play();
j->hp -= 10;
kk->img->SetPos( pos.x, pos.y );
kk->img->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
}

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

if( j->img->SpriteHit(boss->img)){ //ชนกับบอส
j->hp -= 10;
j->img->SetPos( pos.x=0, pos.y=135 );
j->img->Draw( Screen->GetBack(), 0, 135, CDXBLT_TRANS );
}

if (j->hp <= 0)
{
j->active = false;
Screen->GetBackEx()->Fill(0);
TextXY( Screen->GetBack(), 0, 130, 255, 255, 255, 255, ETA_CENTER,"GAME OVER");
}
}
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)