พิซซ่าหน้าเห็ดเผาะ
Location :
กรุงเทพฯ Thailand

[ดู Profile ทั้งหมด]

ให้ทิปเจ้าของ Blog [?]
ฝากข้อความหลังไมค์
Rss Feed
Smember
ผู้ติดตามบล็อก : 1 คน [?]




Group Blog
 
<<
สิงหาคม 2554
 
 123456
78910111213
14151617181920
21222324252627
28293031 
 
4 สิงหาคม 2554
 
All Blogs
 
Friends' blogs
[Add พิซซ่าหน้าเห็ดเผาะ's blog to your web]
Links
 

 

game Midterm

game Midterm



Thanks: Ro ฝากรูป


Thanks: Ro ฝากรูป


Thanks: Ro ฝากรูป


Thanks: Ro ฝากรูป


Thanks: Ro ฝากรูป

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








codeเกม

#include

//ส่วนของการประกาศตัวแปร


CDXScreen *Screen = 0;
CDXInput *Input = 0;
FPSmanager *Fps = 0;

//ประตัวแปรประเภทเสียง

CDXSnd *win;
CDXSnd *theme;
CDXSnd *hit;

//ประกาศตัวแปรประเภทสไปท์ (รูปภาพ)

CDXSprite *bg = 0;
CDXSprite *pac = 0;
CDXSprite *mistype = 0;
CDXSprite *ball = 0;

int x,y,i,j=1,charstate=0;
float score=0;
int a; //ใช้เก็บค่าตอนเปลี่ยนรูปตัวละคร
int bat_A[] = {10,50,90,130,170,210,250};

bool mistypes=true;
POINT pos;

typedef struct
{
CDXSprite *ball;
POINT pos, di, vel;
bool active;
}ball_info;

irr::core::array flist;

bool Addball( char* name, const POINT& pos, const POINT& di, const POINT& vel );
void Updateball(void);
void Removeball(void);

bool Addball( char* name, const POINT& pos, const POINT& di, const POINT& vel )
{
ball_info* finfo = new ball_info( );
finfo->ball = new CDXSprite( );
finfo->ball->Create( name, 1 );
finfo->pos = pos;
finfo->di = di;
finfo->vel = vel;
finfo->active = true;
flist.push_back( finfo );
return true;
}

void Updateball(void)
{
for( irr::u32 i = 0; i != flist.size(); i++ )
{
flist[i]->pos.x += flist[i]->di.x * flist[i]->vel.x;
flist[i]->pos.y += flist[i]->di.y * flist[i]->vel.y;
flist[i]->ball->SetPos( flist[i]->pos.x+pac->GetWidth()-5, flist[i]->pos.y+20 );
if(flist[i]->active)
flist[i]->ball->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
}
}

void Removeball(void)
{
for( irr::u32 i = 0; i != flist.size(); ++i )
{
if( flist[i]->pos.x < 0 ||
flist[i]->pos.x > 480 ||
flist[i]->pos.y < 0 ||
flist[i]->pos.y > 272 )
{
SAFEDELETE( flist[i]->ball );
SAFEDELETE( flist[i] );
flist.erase( i );
--i;
}
}
}

const int Bat_R = 10;

typedef struct Animate {
int _frame;
int _delay;
int first;
int last;
int delay;
bool stop;
} Animate;

typedef struct Boss_S { //สร้างตัวแปร Boss_S แบบโครงสร้าง
int hp;
Animate animate;
CDXSprite *sprite;
} Boss_S;

typedef struct bat { //สร้างตัวแปร bat แบบโครงสร้าง
int x, y;
int acceleration;
int size;
CDXSprite *src;
Sprite b;
bool active ;
} bat;

Boss_S boss;
bat bat[Bat_R];


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


bg = new CDXSprite();
bg->Create("bg.jpg", 1);

pac = new CDXSprite( );
pac->Create( "pac.png", 50,50,24 );

mistype = new CDXSprite();
mistype->Create( "ball.png",20,19,1);


win = new CDXSnd( );
win->Create( );
win->Play( "win.wav" );

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

hit = new CDXSnd( );
hit->Create( );
hit->Play( "hit.wav" );

boss.sprite = new CDXSprite( );
boss.sprite->Create( "boss.png", 150,115,1);
boss.hp=1000;


srand(time(NULL)); // คำสั่ง Ramdon

for (i=0; i < Bat_R; i++) {
bat[i].x = rand () % 480+480;
bat[i].y = bat_A[rand()%6] ;
bat[i].active = true;
bat[i].acceleration = rand () % 3 + 2;

bat[i].b.sprite=new CDXSprite();
bat[i].b.sprite->Create("bat.png", 50,50,1);
bat[i].b.sprite->SetPos(bat[i].x, bat[i].y);
}
return TRUE;
}


void animate (CDXSprite *s, Animate *animate) {

if (!animate->_frame || (animate->_frame > animate->last || animate->_frame < animate->first))
{animate->_frame = animate->first;}
s->SetFrame(animate->_frame);
if (animate->_delay >= animate->delay || !animate->delay)
{ animate->_delay = 0;
animate->_frame++; }
else { animate->_delay++; }
if (animate->_frame > animate->last)
{
if (!animate->stop) animate->_frame = animate->first;
}
}

// -----------------------------------------------------------------
// cdx_DeInit - handles cleanup of CDX objects คืนหน่วยความจำ
// -----------------------------------------------------------------

void cdx_DeInit( void )
{
// TODO: Destroy your CDX objects here

for( irr::u32 i = 0; i != flist.size(); ++i )
{
SAFEDELETE( flist[i]->ball );
SAFEDELETE( flist[i] );
flist.erase( i );
--i;
}

for (i=0; i < Bat_R; i++)
{ SAFEDELETE( bat[i].b.sprite ); }


SAFEDELETE( ball );

SAFEDELETE( boss.sprite );
SAFEDELETE( hit );
SAFEDELETE( theme );
SAFEDELETE( win );

SAFEDELETE( pac );
SAFEDELETE( bg );

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_F1)||Input->GetKeyState(CDXKEY_JOYBUTN11) ){
win->Stop( "win.wav" );
boss.hp=1000;
score=0;
pac->SetPos( pos.x=0, pos.y=0 );
theme->Stop( "theme.ogg" );
theme->Play( "theme.ogg" );
for (i=0; i < Bat_R; i++) {
bat[i].active = false
}
}

TextXY( Screen->GetBack(), 12,260,1,1,1,1000,"Press Z");
TextXY( Screen->GetBack(), 10,20,1,1,1,1000," Score : %.1f",score);

if(score<12){
for (i=0; i < Bat_R; i++) {
if (bat[i].b.sprite->GetPosX() < -50) {
bat[i].x = rand () % 480+480;
bat[i].y = bat_A[rand()%6] ;
bat[i].acceleration = rand()% 3 + 2;
SAFEDELETE( bat[i].src );

bat[i].b.sprite = new CDXSprite();
bat[i].active = true;
bat[i].b.animate._frame = rand()% 3;
bat[i].b.sprite->Create("bat.png",50,50,1);
bat[i].b.sprite->SetPos(bat[i].x, bat[i].y);

} else {
bat[i].x = bat[i].b.sprite->GetPosX()-bat[i].acceleration;
}

bat[i].b.sprite->SetPosX( bat[i].x );
bat[i].b.animate.first=0; // การอนิเมท การขยับของภาพ
bat[i].b.animate.last=2;
bat[i].b.animate.delay=2;
animate(bat[i].b.sprite,&bat[i].b.animate);
if(bat[i].active)
bat[i].b.sprite->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
}


for( irr::u32 i = 0; i != flist.size(); i++ )
for( j = 0; j < Bat_R; j++ )
{
if( bat[j].b.sprite->SpriteHit(flist[i]->ball) && flist[i]->active && bat[j].active)
{
bat[j].active = false;
if(mistypes == true)
flist[i]->active = false;
score=score+0.5;
}

}

}


if(boss.hp>0)mistype->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

if(boss.hp<=0){
win->Play( "win.wav" );
TextXY( Screen->GetBack(), 0,130,1,1,1,1000,ETA_CENTER," YOU WIN ");
TextXY( Screen->GetBack(), 0,150,1,1,1,1000,ETA_CENTER,"Press F1 to play again");
}

if(score>=10){

TextXY( Screen->GetBack(), 350,20,1,1,1,1000,"Boss HP : %d",boss.hp);

boss.animate.first=0; //การขยับของบอส
boss.animate.last=2;
boss.animate.delay=5;
boss.sprite->SetPos(320,80); // เซทตำแหน่งที่บอสออก
animate(boss.sprite,&boss.animate);

if(boss.hp>0) {
boss.sprite->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
if( pac->SpriteHit(boss.sprite)) //เป็นการเช็คชน ถ้าหากผู้เล่นไปชนบอสจะแสดง GAME OVER
{
Screen->GetBackEx()->Fill(0);
TextXY( Screen->GetBack(), 0,130,1000,1000,1000,1000,ETA_CENTER,"GAME OVER");
TextXY( Screen->GetBack(), 0,150,1000,1000,1000,1000,ETA_CENTER,"Press F1 to play again");
}


for( irr::u32 i = 0; i != flist.size(); i++ )
{
if( boss.sprite->SpriteHit(flist[i]->ball)&& flist[i]->active )
{
flist[i]->active = false;
boss.hp-=100;
}
}
}
}
mistype->SetPos(25,230);

if(mistypes){ mistype->Create( "ball1.png",20,19,1); }
else
mistype->Create( "ball2.png",20,20,1);
POINT di = { 1, 0 };
POINT vel = { 10, 0 };






if( Input->GetKeyState(SDLK_SPACE) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN3) == CDXKEY_PRESS )
{
if(mistypes)
Addball( "ball1.png", pos, di, vel );

else
Addball( "ball2.png", pos, di, vel );
}

// เซทปุ่มกดเพื่อใช้สำหรับเปลี่ยนประเภทกระสุน

if( Input->GetKeyState(SDLK_z) == CDXKEY_PRESS || Input->GetKeyState(CDXKEY_JOYBUTN1) == CDXKEY_PRESS )
{ mistypes = !mistypes; }

// ส่วนของเสียงยิง

if( Input->GetKeyState(SDLK_SPACE) == 2 || Input->GetKeyState(CDXKEY_JOYBUTN3) == 2)
{ hit->Play( "hit.wav" ); }

Updateball( );
Removeball( );


if (Input->GetKeyState(SDLK_RIGHT)||Input->GetKeyState(CDXKEY_JOYBUTN9))
{ pos.x += 6;
pac->SetFrame( a++ );
if (a>2)
{a=0;}
}

if (Input->GetKeyState(SDLK_LEFT)||Input->GetKeyState(CDXKEY_JOYBUTN7))
{ pos.x -= 6;
pac->SetFrame( a++ );
if (a>5)
{a=3;}
}

if (Input->GetKeyState(SDLK_UP)||Input->GetKeyState(CDXKEY_JOYBUTN8))
{ pos.y -= 6;
pac->SetFrame( a++ );
if (a>8)
{a=6;}
}

if (Input->GetKeyState(SDLK_DOWN)||Input->GetKeyState(CDXKEY_JOYBUTN6))
{ pos.y += 6;
pac->SetFrame( a++ );
if (a>11)
{a=9;}
}

if ((Input->GetKeyState(SDLK_UP)) && (Input->GetKeyState(SDLK_LEFT)))
{ pac->SetFrame( a++ );
if (a>14)
{a=12;}
}

if ((Input->GetKeyState(SDLK_LEFT)) && (Input->GetKeyState(SDLK_DOWN)))
{ pac->SetFrame( a++ );
if (a>17)
{a=15;}
}

if ((Input->GetKeyState(SDLK_RIGHT)) && (Input->GetKeyState(SDLK_UP)))
{ pac->SetFrame( a++ );
if (a>20)
{a=18;}
}

if ((Input->GetKeyState(SDLK_RIGHT)) && (Input->GetKeyState(SDLK_DOWN)))
{ pac->SetFrame( a++ );
if (a>23)
{a=21;}
}

pac->SetPos( pos.x, pos.y );
pac->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );

if( pos.x < 0 )
{ pos.x = 0; }
if( pos.x > 420)
{ pos.x = 420; }
if( pos.y < 0 )
{ pos.y = 0; }
if( pos.y >225)
{ pos.y = 225; }


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





 

Create Date : 04 สิงหาคม 2554
0 comments
Last Update : 5 สิงหาคม 2554 15:05:31 น.
Counter : 291 Pageviews.

ชื่อ : * blog นี้ comment ได้เฉพาะสมาชิก
Comment :
  *ส่วน comment ไม่สามารถใช้ javascript และ style sheet
 
รหัสส่งข้อความ
กรุณายืนยันรหัสส่งข้อความ

 Pantip.com | PantipMarket.com | Pantown.com | © 2004 BlogGang.com allrights reserved.