วันเสาร์ที่ 6 สิงหาคม พ.ศ. 2554

Homework 03

Homework 03
‎- ผู้เล่นสามารถเคลื่อนที่ได้​ 8 ทิศทาง
- ผู้เล่นมีมิสไซล์ 2 ชนิด สามารถเปลี่ยนได้ระหว่างเล่​น
- เกมมีศัตรูที่วาดด้วย transformation image (ไม่แน่ใจว่าเป็นภาพเคลื่อน​ไหวหรือเปล่า) และผู้เล่นสามารถเคลื่อนที่​ขิ้น ลง ซ้าย ขวา ได้
- เจอบอสเมื่อผู้เล่นได้ 10 คะแนน
- แสดง You Win เมื่อฆ่าบอสได้



VIDEO GAME REVIEW
CODE DESCRIBE.
PART 1
http://www.youtube.com/watch?v=IyX8QDX9L04&feature=related

PART 2
http://www.youtube.com/watch?v=sKYMOkag_vE&feature=related

SOURCE CODE


#include <cdx_app_wiz.h>
#include<stdlib.h>
#include<stdio.h>

// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
#define widthScreen 480;
#define heightScreen 270;
CDXScreen   *Screen     = 0;    // The screen object, every program must have one
CDXInput *Input = 0;
FPSmanager *Fps = 0;
CDXSprite *Rocket=0;
CDXSprite *missle=0;
CDXSprite *bullet=0;
CDXSprite *r_missle=0;
CDXSprite *enemy_jet=0;
CDXSprite *BG=0;
CDXSprite *boss=0;
//--------------------------------------------------------------------
//Interface
//----------------------------------------------------------------------
CDXSprite *missle_I=0;
CDXSprite *r_missleI=0;
//-------------------------------------------------------------------
// Value
//-------------------------------------------------------------------
int x, y, missle_x, r_missle_x, e_x, e_y, axis=0;
int k,j,e_start=0;
int max_in=50; int max_in_r=5; //number bullet
int delay_out_bullet=5, delay_out_r_bullet=15, delay_enemy=2;
int type=0; //type of bullet
int e_delay=100;
int boss_HP=50, BHP=0, Player_HP=10,PHP=0;
int RHP=10;
int colddown_mis;
//----------------------------------------------------------------
//animate value
//----------------------------------------------------------------
int min_F_M,delay_M,zero_F_M=0; int max_F_M=3;
int xBG;
int delay_BG=3;
//---------------------------------------------------------------------
//Structure
//-----------------------------------------------------------------
typedef struct
{
int x, y;
CDXSprite* missle;
bool Active;
}BULLET;
BULLET _missle; // increase missle
typedef struct
{
int x, y;
CDXSprite* r_missle;
bool Active;
}REDBULLET; // increase red missle
REDBULLET _r_missle;

typedef struct
{
int e_x,e_y;
CDXSprite* enemy_jet;
bool Active;
}ENEMY; // enemy
ENEMY jet_e1,jet_e2,jet_e3,jet_e4,jet_e5,jet_e6,jet_e7,jet_e8,jet_e9;

typedef struct
{
CDXSprite *enemy_jet;
POINT x,y,pos_e, di_e, vel_e;
}Enemy_info;

typedef struct
{
CDXSprite *bullet;
POINT pos, di, vel;
bool Active;
}Bullet_info;
irr::core::array<Bullet_info*> mlist;

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

//----------------------------------------------------------------------
//Function
//----------------------------------------------------------------------

bool AddMis( char* name, const POINT& pos, const POINT& di, const POINT& vel )
{
Bullet_info* minfo = new Bullet_info( );
if(type==0){
minfo->bullet = new CDXSprite( );
minfo->bullet->Create( "pic/bullet.pcx", 0 );
}
if(type==1){
minfo->bullet = new CDXSprite( );
minfo->bullet->Create( "pic/r_bullet.png", 0 );
}

minfo->pos = pos;
minfo->di = di;
minfo->vel = vel;
mlist.push_back( minfo );
return true;
}
void UpdateMis(void)
{
for( irr::u32 i = 0; i != mlist.size(); i++ ){
if(boss_HP!=0){
 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]->bullet->SetPos( mlist[i]->pos.x, mlist[i]->pos.y );
 mlist[i]->bullet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
 if( mlist[i]->bullet->SpriteHit(boss)){
mlist[i]-> Active = false;
boss_HP=boss_HP-1;
 }
}
if(boss_HP==0)
BHP=1;
}
}
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]->bullet );
SAFEDELETE( mlist[i] );
mlist.erase( i );
--i;
}
}
}

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
Rocket=new CDXSprite();
Rocket->Create("pic/Rocket.pcx",0);

BG=new CDXSprite();
BG->Create("pic/BG.jpg",0);

_r_missle.r_missle = new CDXSprite( );
_r_missle.r_missle->Create( "pic/r_bullet.png", 0 );

missle_I=new CDXSprite();
missle_I->Create("pic/bullet.pcx",0);

r_missleI=new CDXSprite();
r_missleI->Create("pic/r_bullet.png",0);

jet_e1.enemy_jet = new CDXSprite( );
jet_e1.enemy_jet->Create( "pic/metero.png",30,30,3 );
jet_e1.Active = true;
jet_e2.enemy_jet = new CDXSprite( );
jet_e2.enemy_jet->Create( "pic/metero.png",30,30,3 );
jet_e2.Active = true;
jet_e3.enemy_jet = new CDXSprite( );
jet_e3.enemy_jet->Create( "pic/metero.png",30,30,3 );
jet_e3.Active = true;
jet_e4.enemy_jet = new CDXSprite( );
jet_e4.enemy_jet->Create( "pic/metero.png",30,30,3 );
jet_e4.Active = true;
jet_e5.enemy_jet = new CDXSprite( );
jet_e5.enemy_jet->Create( "pic/metero.png",30,30,3);
jet_e5.Active = true;
jet_e6.enemy_jet = new CDXSprite( );
jet_e6.enemy_jet->Create( "pic/metero.png",30,30,3);
jet_e6.Active = true;
jet_e7.enemy_jet = new CDXSprite( );
jet_e7.enemy_jet->Create( "pic/metero.png",30,30,3 );
jet_e7.Active = true;
jet_e8.enemy_jet = new CDXSprite( );
jet_e8.enemy_jet->Create( "pic/metero.png",30,30,3 );
jet_e8.Active = true;
jet_e9.enemy_jet = new CDXSprite( );
jet_e9.enemy_jet->Create( "pic/metero.png",30,30,3);
jet_e9.Active = true;

_missle.missle = new CDXSprite( );
_missle.missle->Create( "pic/bullet.pcx",0 );

boss=new CDXSprite();
boss->Create("pic/boss.png",0);

return TRUE;
}

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

SAFEDELETE( _missle.missle );
SAFEDELETE( _r_missle.r_missle );
// Decrese _missle

for( irr::u32 i = 0; i != mlist.size(); ++i )
{
SAFEDELETE( mlist[i]->bullet );
if(type==1){
SAFEDELETE( mlist[i]->bullet );
}
SAFEDELETE( mlist[i] );
mlist.erase( i );
--i;
} // Decrese bullet

SAFEDELETE(BG);
SAFEDELETE( jet_e1.enemy_jet );
SAFEDELETE( jet_e2.enemy_jet );
SAFEDELETE( jet_e3.enemy_jet );
SAFEDELETE( jet_e4.enemy_jet );
SAFEDELETE( jet_e5.enemy_jet );
SAFEDELETE( jet_e6.enemy_jet );
SAFEDELETE( jet_e7.enemy_jet );
SAFEDELETE( jet_e8.enemy_jet );
SAFEDELETE( jet_e9.enemy_jet );

SAFEDELETE(boss);
SAFEDELETE(missle_I);
SAFEDELETE(Rocket);
SAFEDELETE( Fps );
SAFEDELETE( Input );
SAFEDELETE( Screen );
}
// ------------------------------------------------------------------
// cdx_DoFrame - performs drawing of the current frame
// ------------------------------------------------------------------
void cdx_DoFrame()
{
Input->Update( );

Screen->GetBackEx()->Fill(0);
if(x<=0) x=0;
if (x>=450)
x=450;
if(y<=0)y=0;
if(y>=245)
y=245;

// TODO: Add code to draw your objects during each frame
//----------------BG----------------------------
BG->SetPos(xBG,0);
BG->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
xBG--;
if(xBG==-1890){
xBG=480;
}
e_x=420;
if(e_start==0){
x=0;y=125;
}
//-----------------------------------------------
//boss
//-----------------------------------------------
if(BHP!=1){
boss->SetPos(e_x,e_y);
boss->Draw(Screen->GetBack(),0,0,CDXBLT_TRANSHFLIP);

switch(axis){
case 0:e_y=e_y+5;break;
case 1:e_y=e_y-5;break;
}
if(e_y==0){
axis=0;
}
if(e_y==270){
axis=1;
}
//-----------------------------------------------------------------------
// Control
//----------------------------------------------------------------------
if(Player_HP>0){
Rocket->SetPos(x,y);
Rocket->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
if( Input->GetKeyState(SDLK_RIGHT)|| Input->GetKeyState(CDXKEY_JOYBUTN9))
x += 8;
if( Input->GetKeyState(SDLK_LEFT)|| Input->GetKeyState(CDXKEY_JOYBUTN7))
x -= 8;
if( Input->GetKeyState(SDLK_UP)|| Input->GetKeyState(CDXKEY_JOYBUTN8))
y -= 8;
if( Input->GetKeyState(SDLK_DOWN)|| Input->GetKeyState(CDXKEY_JOYBUTN6))
y += 8;
//----------------------------------------------------------------------------
//event
//----------------------------------------------------------------------------
//------------------------------------------------------------------------
//Emeny
//------------------------------------------------------------------------

jet_e1.enemy_jet->SetPos(jet_e1.e_x,jet_e1.e_y);
jet_e2.enemy_jet->SetPos(jet_e2.e_x,jet_e2.e_y);
jet_e3.enemy_jet->SetPos(jet_e3.e_x,jet_e3.e_y);
jet_e4.enemy_jet->SetPos(jet_e4.e_x,jet_e4.e_y);
jet_e5.enemy_jet->SetPos(jet_e5.e_x,jet_e5.e_y);
jet_e6.enemy_jet->SetPos(jet_e6.e_x,jet_e6.e_y);
jet_e7.enemy_jet->SetPos(jet_e7.e_x,jet_e7.e_y);
jet_e8.enemy_jet->SetPos(jet_e8.e_x,jet_e8.e_y);
jet_e9.enemy_jet->SetPos(jet_e9.e_x,jet_e9.e_y);

delay_M++;
if(zero_F_M<=max_F_M){
jet_e1.enemy_jet->SetFrame(min_F_M);
jet_e2.enemy_jet->SetFrame(min_F_M);
jet_e3.enemy_jet->SetFrame(min_F_M);
jet_e4.enemy_jet->SetFrame(min_F_M);
jet_e5.enemy_jet->SetFrame(min_F_M);
jet_e6.enemy_jet->SetFrame(min_F_M);
jet_e7.enemy_jet->SetFrame(min_F_M);
jet_e8.enemy_jet->SetFrame(min_F_M);
jet_e9.enemy_jet->SetFrame(min_F_M);
if(delay_M==4){
min_F_M++;
delay_M=0;
if(min_F_M>=max_F_M){
min_F_M=0;
}
}
}

jet_e1.e_x--; jet_e2.e_x--; jet_e3.e_x--; jet_e4.e_x--; jet_e5.e_x--; jet_e6.e_x--; jet_e7.e_x--; jet_e8.e_x--; jet_e9.e_x--;

if(jet_e1.e_x<=-40 && jet_e2.e_x<=-40 && jet_e3.e_x<=-40){
jet_e1.e_x=460+rand() %160; jet_e2.e_x=460+rand() %160; jet_e3.e_x=460+rand() %160;
}
if(jet_e4.e_x<=-40 && jet_e5.e_x<=-40 && jet_e6.e_x<=-40){
jet_e4.e_x=460+rand() %160; jet_e5.e_x=460+rand() %160; jet_e6.e_x=460+rand() %160;
}
if(jet_e7.e_x<=-40 && jet_e8.e_x<=-40 && jet_e9.e_x<=-40){
jet_e7.e_x=460+rand() %160; jet_e8.e_x=460+rand() %160; jet_e9.e_x=460+rand() %160;
}

if( Rocket->SpriteHit(jet_e1.enemy_jet)){ jet_e1.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e2.enemy_jet)){ jet_e2.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e3.enemy_jet)){ jet_e3.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e4.enemy_jet)){ jet_e4.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e5.enemy_jet)){ jet_e5.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e6.enemy_jet)){ jet_e6.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e7.enemy_jet)){ jet_e7.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e8.enemy_jet)){ jet_e8.Active = false; PHP=1;}
if( Rocket->SpriteHit(jet_e9.enemy_jet)){ jet_e9.Active = false; PHP=1;}

jet_e1.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e2.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e3.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e4.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e5.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e6.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e7.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e8.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );
jet_e9.enemy_jet->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANSHFLIP );

if(e_start==0){
jet_e1.e_x=72; jet_e1.e_y=40; jet_e4.e_x=184; jet_e4.e_y=80; jet_e7.e_x=376; jet_e7.e_y=32;
jet_e2.e_x=50; jet_e2.e_y=140; jet_e5.e_x=240; jet_e5.e_y=128; jet_e8.e_x=408; jet_e8.e_y=136;
jet_e3.e_x=80; jet_e3.e_y=200; jet_e6.e_x=176; jet_e6.e_y=192; jet_e9.e_x=328; jet_e9.e_y=176;
e_start=1;
}//end e_start
e_delay--;
if(e_delay==100&&e_start==1){
jet_e1.e_x=rand() %160; jet_e1.e_y=32+rand()%56;
jet_e2.e_x=rand() %160; jet_e2.e_y=89+rand()%56;
jet_e3.e_x=rand() %160; jet_e3.e_y=146+rand()%56;
jet_e4.e_x=200+rand()%160; jet_e4.e_y=32+rand()%56;
jet_e5.e_x=200+rand()&160; jet_e5.e_y=89+rand()%56;
jet_e6.e_x=200+rand()&160; jet_e6.e_y=146+rand()%56;
jet_e7.e_x=430+rand()%160; jet_e7.e_y=32+rand()%56;
jet_e8.e_x=430+rand()%160; jet_e8.e_y=89+rand()%56;
jet_e9.e_x=430+rand()%160; jet_e9.e_y=146+rand()%56;
}//end_e_delay
if(PHP==1){
Player_HP=Player_HP-1;
PHP=0;
}//end PHP.
//------------------------------------------------------------------------
//random_missle_out_increase
//------------------------------------------------------------------------
delay_out_bullet--;
missle_x=missle_x--;
_missle.missle->SetPos( missle_x, _missle.y+5 );

if(delay_out_bullet==0){
_missle.y = rand() %heightScreen;
_missle.Active = true;
missle_x=480;
delay_out_bullet=100;
_missle.missle->Draw(Screen->GetBack(),0,0,CDXBLT_TRANSHFLIP);
}
if(Rocket->SpriteHit(_missle.missle)){
_missle.Active = false;
max_in=max_in+10;
}
if( _missle.Active ){
_missle.missle->Draw( Screen->GetBack(), 0, 0,CDXBLT_TRANSHFLIP );
}

delay_out_r_bullet--;
r_missle_x=r_missle_x--;
_r_missle.r_missle->SetPos( r_missle_x, _r_missle.y );

if(delay_out_r_bullet==0){
_r_missle.y = rand() % heightScreen;
_r_missle.Active = true;
r_missle_x=480;
delay_out_r_bullet=500;
_r_missle.r_missle->Draw(Screen->GetBack(),0,0,CDXBLT_TRANSHFLIP);
}
if(Rocket->SpriteHit(_r_missle.r_missle)){
_r_missle.Active = false;
max_in_r = max_in_r +2;
}
if( _r_missle.Active ){
_r_missle.r_missle->Draw( Screen->GetBack(), 0, 0,CDXBLT_TRANSHFLIP );
}
//-------------------------------------------------------------------------------
//missle
//-------------------------------------------------------------------------------
POINT di = { 1, 0 };
POINT vel = { 10, 0 };
POINT pos={x,y};

if(type==0){
if(max_in!=0&&BHP!=1){
if( Input->GetKeyState(SDLK_SPACE) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS ){
AddMis( "pic/bullet.pcx", pos, di, vel );
max_in=max_in-1;
}
UpdateMis();
RemoveMis();
}
if(max_in==0&&BHP!=1){
if( Input->GetKeyState(SDLK_SPACE) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS ){
TextXY(Screen->GetBack(),50,100,233,227,66,255,ETA_CENTER,"You must fill youe bullet.");
}
}
}//end type0
if(type==1){
if(max_in_r!=0&&BHP!=1){
if( Input->GetKeyState(SDLK_SPACE) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS ){
AddMis( "pic/bullet.pcx", pos, di, vel );
max_in_r=max_in_r-1;
}
UpdateMis();
RemoveMis();
}
if(max_in_r==0&&BHP!=1){
if( Input->GetKeyState(SDLK_SPACE) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS ){
TextXY(Screen->GetBack(),50,100,233,227,66,255,ETA_CENTER,"You must fill youe bullet.");
}
}
}//end type1


if(Input->GetKeyState(SDLK_F1) == CDXKEY_PRESS){
type=type+1;
if(type>1)
type=0;
}
}//end Player_HP
}//end BHP
if(Input->GetKeyState(SDLK_F2) == CDXKEY_PRESS){
x, y, missle_x, r_missle_x, e_x, e_y, axis=0;
k,j,e_start=0;
max_in=50;  max_in_r=5; //number bullet
delay_out_bullet=5, delay_out_r_bullet=15, delay_enemy=2;
type=0; //type of bullet
e_delay=100;
boss_HP=50, BHP=0, Player_HP=10,PHP=0;
RHP=10;
colddown_mis=0;
}
//---------------------------------------------------------
//Picture
//----------------------------------------------------
missle_I->SetPos(20,235);
missle_I->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
r_missleI->SetPos(56,235);
r_missleI->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
//-----------------------------------------------------------------------------------
//text
//-----------------------------------------------------------------------------------
TextXY(Screen->GetBack(),30,250,233,227,66,255,"%d",max_in);
TextXY(Screen->GetBack(),65,250,233,227,66,255,"%d",max_in_r);
TextXY(Screen->GetBack(),360,8,233,227,66,255,"Boss HP : %d",boss_HP);
TextXY(Screen->GetBack(),10,8,233,227,66,255,"Your HP : %d",Player_HP);
if(Player_HP==0){
Rocket->SetPos(220,130);
Rocket->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
TextXY(Screen->GetBack(),10,160,233,227,66,255,ETA_CENTER,"You Lost. Press F2 for continous.");
}
if(boss_HP==0){
Rocket->SetPos(220,130);
Rocket->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
TextXY(Screen->GetBack(),10,160,233,227,66,255,ETA_CENTER,"You Win. Press F2 for continous.");
}
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;
}

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

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