จงเขียนเกม PACMAN โดยมีรายละเอียดดังนี้
1. มีลูกบอลทั้งหมด 80 ลูก
2. มีการเก็บคะแนนและแสดงคะแนนด้านซ้ายบน
3. หากกินลูกบอลหมดจะขึ้นคำว่า YOU WIN
screen shot
2. มีการเก็บคะแนนและแสดงคะแนนด้านซ้ายบน
3. หากกินลูกบอลหมดจะขึ้นคำว่า YOU WIN
screen shot
Start Game
อธิบายการทำงานของโปรแกรม
Play in simulator
Play in PSP
#include <cdx_app_wiz.h>
#include <time.h>
// ------------------------------------------------------------------
// CDX Objects
// ------------------------------------------------------------------
CDXScreen *Screen = 0; // The screen object, every program must have one
CDXInput *Input = 0;
FPSmanager *Fps = 0;
CDXSprite * BG = 0;
CDXSprite * pac = 0;
CDXSnd* bgsound;
int x, y, i, j;
int score = 0 ,countball = 0;
double playtime=30, remaining_time=30;
time_t start, end;
typedef struct
{
int x, y;
CDXSprite* ball;
bool Active;
}BALLINFO;
BALLINFO myball[10][8];
// ------------------------------------------------------------------
// 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.pcx", 1 );
pac = new CDXSprite( );
pac->Create( "pac.png",50,50, 1 );
bgsound = new CDXSnd();
bgsound->Create();
bgsound->Play("road to myself.ogg");
time(&start);
for( i = 0; i < 10; i++ )
for( j = 0; j < 8; j++ )
{
myball[i][j].ball = new CDXSprite( );
myball[i][j].ball->Create( "ball.png", 1 );
myball[i][j].x = 40*(i+1);
myball[i][j].y = 30*(j+1);
myball[i][j].ball->SetPos( myball[i][j].x+15, myball[i][j].y );
myball[i][j].Active = true;
}
return TRUE;
}
// ------------------------------------------------------------------
// cdx_DeInit - handles cleanup of CDX objects
// ------------------------------------------------------------------
void cdx_DeInit( void )
{
// TODO: Destroy your CDX objects here
for( i = 0; i < 10; i++ )
for( j = 0; j < 8; j++ )
SAFEDELETE( myball[i][j].ball );
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
if (remaining_time > 0 && countball < 80) {
if( Input->GetKeyState(SDLK_RIGHT)||
Input->GetKeyState(CDXKEY_JOYBUTN9)){
x += 5;
pac->SetFrame(0);}
if( Input->GetKeyState(SDLK_LEFT)||
Input->GetKeyState(CDXKEY_JOYBUTN7)){
x -= 5;
pac->SetFrame(1);}
if( Input->GetKeyState(SDLK_UP)||
Input->GetKeyState(CDXKEY_JOYBUTN8)){
y -= 5;
}
if( Input->GetKeyState(SDLK_DOWN)||
Input->GetKeyState(CDXKEY_JOYBUTN6)){
y += 5;
}
}
BG->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
if (x<0)x=0;
if (x>=440)x=440;
if (y<0)y=0;
if (y>=220)y=220;
pac->SetPos( x, y );
pac->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
for( i = 0; i < 10; i ++ ){
for( j = 0; j < 8; j++ )
{
if( pac->SpriteHit(myball[i][j].ball) && myball[i][j].Active ) {
myball[i][j].Active = false;
score += 20;
countball++;
}
if( myball[i][j].Active )
myball[i][j].ball->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
}
}// for j3
if( countball == 80 ) {
TextXY( Screen->GetBack(), 200, 125, 255, 255, 255, 255,ETA_CENTER,"YOU WIN" );
TextXY( Screen->GetBack(), 200, 135, 255, 255, 255, 255,ETA_CENTER,"PRESS START TO PLAY AGAIN" );
}
if( Input->GetKeyState(SDLK_SPACE)||
Input->GetKeyState(CDXKEY_JOYBUTN11)){
bgsound->Stop("road to myself.ogg");
bgsound->Play("road to myself.ogg");
score=0;
countball=0;
x=0;
y=0;
time(&start);
remaining_time=30;
playtime=30;
for( i = 0; i < 10; i++ ){
for( j = 0; j < 8; j++ ){
myball[i][j].Active = true;
}
}
}
TextXY( Screen->GetBack(), 2, 10, 233, 227, 255, 255, ETA_LEFT ,"Score: %d", score );
if (remaining_time > 0 && countball < 80) {
time(&end);
remaining_time = playtime - difftime(end, start);
} else {
remaining_time = 0;
}
TextXY( Screen->GetBack(), 2, 10, 233, 227, 255, 255, ETA_RIGHT ,"Time: %d", int (remaining_time) );
if ( remaining_time <= 0 && countball != 80 ) {
TextXY( Screen->GetBack(), 200, 125, 255, 255, 255, 255,ETA_CENTER,"TIME OUT" );
TextXY( Screen->GetBack(), 200, 135, 255, 255, 255, 255,ETA_CENTER,"PRESS START TO PLAY AGAIN" );
for( i = 0; i < 10; i++ )
for( j = 0; j < 8; j++ )
{
myball[i][j].Active = false;
}
}
if( Input->GetKeyState(SDLK_SPACE)||
Input->GetKeyState(CDXKEY_JOYBUTN11)){
bgsound->Stop("road to myself.ogg");
bgsound->Play("road to myself.ogg");
score=0;
countball=0;
x=0;
y=0;
time(&start);
remaining_time=30;
playtime=30;
for( i = 0; i < 10; i++ ){
for( j = 0; j < 8; j++ ){
myball[i][j].Active = true;
}
}
}
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;
}