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.
โจทย์ไทย
- ผู้เล่นสามารถเคลื่อนที่ได้ 8 ทิศทาง
- ผู้เล่นมีมิสไซล์ 2 ชนิด สามารถเปลี่ยนได้ระหว่างเล่น
- เกมมีศัตรูที่วาดด้วย transformation image และผู้เล่นสามารถเคลื่อนที่ขิ้น
- เจอบอสเมื่อผู้เล่นได้ 10 คะแนน
- แสดง You Win เมื่อฆ่าบอสได้
RUNGAME
CODE
#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 *mu = 0;
CDXSnd *g_snd;
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_f
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 *boss=new mySprite();
const int $_mon =10 ;
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 spe.png", 25, 27, 5);
minfo->di.x = 1;
minfo->vel.x = 7;
minfo->pos.x = pos.x;
minfo->pos.y = pos.y + 25;
minfo->animate = new Animation(minfo->missile);
minfo->animate->duration(1, 5, 2);
} else {
minfo->missile = new CDXSprite();
minfo->missile->Create("bigfir
minfo->di.x = 1;
minfo->vel.x = 3;
minfo->pos.x = pos.x;
minfo->pos.y = pos.y + 5;
minfo->animate = new Animation(minfo->missile);
minfo->animate->duration(1, 4, 2);
}
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(mli
monster[u].active = false;
mlist[i]->active = false;
score += 1;
}
}
if (mlist[i]->missile->SpriteHit(
boss->hp -= 20;
mlist[i]->active = false;
}
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
g_snd = new CDXSnd();
g_snd->Create();
g_snd->Play("sound.ogg");
mu = new CDXSprite( );
mu->Create( "bg.jpg", 1 );
j->img = new CDXSprite();
j->img->Create( "player.png", 75, 50, 2 );
j->animate = new Animation(j->img);
j->animate->duration(1, 2, 2);
j->hp = 200;
srand(time(NULL)); //สร้าง key ในการ Random
for (u=0; u < $_mon; u++) { //สร้างลูกน้อง
monster[u].x = rand () % 480+480;
monster[u].y = rand () % 200 ;
monster[u].acceleration = rand () % 3 + 1;//ความเร่งของลูกน้อง random
monster[u].size = 1;
monster[u].active = true;
monster[u].src = new CDXSprite();
if (monster[u].size == 1)
{
monster[u].src->Create("enermy
monster[u].src->SetPos(monster
}
monster[u].animate = new Animation(monster[u].src);
monster[u].animate ->duration(1, 3, 2);
}
boss->img = new CDXSprite();
boss->img->Create("boss.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 );
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( mu );
SAFEDELETE( g_snd );
for (u=0; u < $_mon; u++) {
SAFEDELETE( monster[u].src );
}
SAFEDELETE( j->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
mu->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
monster[u].active = true;
}
monster[u].animate->play();
monster[u].x = monster[u].src->GetPosX()-mons
monster[u].src->SetPosX( monster[u].x );//เซ็ทตำแหน่งลูกน้อง
if (monster[u].active && score < 10) monster[u].src->Draw(Screen->G
}
if( Input->GetKeyState(SDLK_RIGHT)
Input->GetKeyState(CDXKEY_JOYB
pos.x += 5;
if( Input->GetKeyState(SDLK_LEFT)|
Input->GetKeyState(CDXKEY_JOYB
pos.x -= 5;
if( Input->GetKeyState(SDLK_UP)||
Input->GetKeyState(CDXKEY_JOYB
pos.y -= 5;
if( Input->GetKeyState(SDLK_DOWN)|
Input->GetKeyState(CDXKEY_JOYB
pos.y += 5;
if (pos.x<0)pos.x=0;
if (pos.x> (480 - j->img->GetBlockWidth()))pos.x
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(), 380, 20, 233, 227, 66, 255, "score : %d", score ); // แสดง คะเเนนที่ ฆ่าลูกน้องไป
if( Input->GetKeyState(SDLK_z) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYB
AddMis();
}
UpdateMis( );
RemoveMis( );
if( Input->GetKeyState(SDLK_x) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYB
if (active_weapon < 1) {
active_weapon++;
} else {
active_weapon = 0;
}
}
if(score >= 10 && boss->hp > 0)
{
boss->animate->play();
boss->img->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
}
if (boss->hp <= 0) {
boss->active = false;
TextXY(Screen->GetBack(), 0, 100, 255, 255, 255, 255, ETA_CENTER, "You Winner!");// แสดงข้อความชนะ
}
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
ไม่มีความคิดเห็น:
แสดงความคิดเห็น