บทความที่ได้รับความนิยม

chubu demo#3

โจทย์





Chubu Chubu Demo#3





CODE


‎#include <cdx_app_wiz.h>
#include <time.h>
// ------------------------------------------------------------------
// CDX Objects Create by Ohmegar 2011
// ------------------------------------------------------------------
CDXScreen *Screen = 0; // The screen object, every program must have one
CDXInput *Input = 0;
FPSmanager *Fps = 0;
CDXSprite *chubu = 0;
CDXSprite *mu = 0;
CDXSnd *g_snd;

int speedup = 0;
int x=200, y;
int live = 5;
int x2,y2;
int oncetime = 0;
int randomchance =0;
int second;
int delaytime;
int alpha = 0;
CDXSprite *hearts = 0;
CDXSprite *splash = 0;
bool getup = false;
bool runonce = false;
bool isTitleWasEnd = false;
time_t start;
time_t end;
int timepersec();

int timepersec() {
time(&end);
second = difftime(end,start);
return(second);
}

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();

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;

// ------------------------------------------------------------------
// 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
time(&start);

g_snd = new CDXSnd(); // เสียง
g_snd->Create();
g_snd->Play("sound/play.mp3");

mu = new CDXSprite( ); // พื้นหลัง
mu->Create( "images/bg.png",480,272,1 );

chubu = new CDXSprite( ); // ตัวละคร
chubu->Create( "images/chubu.png",70,75, 1 );

hearts = new CDXSprite();
hearts->Create("images/uplive.png",30,30,1);

srand(time(NULL)); //สร้าง key ในการ Random

for (u=0; u < $_mon; u++) { //สร้างลูกน้อง
monster[u].x = rand () % 480 ;
monster[u].y = rand () % 200 - 300 ;

monster[u].acceleration = rand () % 2 + 0;//ความเร่งของลูกน้อง random
monster[u].size = 1;
monster[u].active = true;
monster[u].src = new CDXSprite();
if (monster[u].size == 1)
{
int monpic = rand()%3;
if (monpic == 0)
{
monster[u].src->Create("images/item1.png", 30, 30, 1);
}
if(monpic == 1) { monster[u].src->Create("images/item2.png", 30, 30, 1);
}
if (monpic == 2) { monster[u].src->Create("images/item3.png", 30,30, 1);
}
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);
}
splash = new CDXSprite();
splash->Create("images/logo.png",1);

return TRUE;
}

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

SAFEDELETE( g_snd ); // คืนค่า หน่วยความจำ
SAFEDELETE( mu );
SAFEDELETE(chubu);

for (u=0; u < $_mon; u++) {
SAFEDELETE( monster[u].src );
}

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
timepersec();
delaytime = 5-second;
if (delaytime > 0) {
splash->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
}
else {
mu->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
if (live > 0) {
for (u=0; u < $_mon; u++) {
if (monster[u].src->GetPosY() > 272 || !monster[u].active) { //เช็คตำแหน่งลูกน้องว่าเลยจอหรื่อไม่
monster[u].x = rand () % 400;
monster[u].y = rand () % 200 - 300 ;
monster[u].acceleration = rand () % 3 + 5; //ความเร็วในการตก
monster[u].size = 1;
monster[u].src->SetPos(monster[u].x, monster[u].y);
monster[u].active = true;
if (monster[u].active) { score++; }
}
monster[u].animate->play();
monster[u].y = monster[u].src->GetPosY()+monster[u].acceleration;
monster[u].src->SetPosY( monster[u].y );//เซ็ทตำแหน่งลูกน้อง
if (monster[u].active && score < 80) monster[u].src->Draw(Screen->GetBack(), 0, 0, CDXBLT_TRANS);
if (score == 20 ) { monster[u].acceleration = rand()%4+2; mu->SetFrame(1); }
if (score == 40 ) { monster[u].acceleration = rand()%5+3; mu->SetFrame(2); }
if (score == 60 ) { monster[u].acceleration = rand()%6+4; mu->SetFrame(3); }
if (chubu->SpriteHit(monster[u].src)) {
monster[u].active = false;
live--;
if(score>0) { score--; }
}
}
}
if (live <= 0) { chubu->SetFrame(4); mu->SetFrame(4);
TextXY( Screen->GetBack(), 240,124, 255,0, 0, 255,ETA_CENTER, "::YOU LOSE::");
//TextXY( Screen->GetBack(), 240,138, 255, 1, 1, 1,ETA_CENTER, "<Sorry You can Play Again>");
}
if (live > 0) {
if( Input->GetKeyState(SDLK_RIGHT)|| Input->GetKeyState(CDXKEY_JOYBUTN9)) {
x += 8;
chubu->SetFrame(2);
}
if( Input->GetKeyState(SDLK_LEFT) || Input->GetKeyState(CDXKEY_JOYBUTN7)) {
x -= 8;
chubu->SetFrame(0);
}
if( Input->GetKeyState(SDLK_DOWN)|| Input->GetKeyState(CDXKEY_JOYBUTN6)) {
y += 8;
chubu->SetFrame(3);
}
if(x<0){ // ช่วงที่ตัวละคร สามารถ เคลื่อนไหวได้
x=0;
}
if(x>400){
x=400;
}
}
chubu->SetPos(x,190); // ให้ตัวละคร เริ่มจุดนี้ก่อน
chubu->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
chubu->SetFrame(1);

TextXY( Screen->GetBack(),416, 10, 255, 0, 0, 255, "SCORE " "%d",score );//สีข้อความแดง
TextXY( Screen->GetBack(),416, 30, 255, 50, 255, 255, "live " "%d",live ); //สีข้อความชมพู
if(score==80){
score=80;
TextXY( Screen->GetBack(), 240,124, 255,28, 7, 134,ETA_CENTER, "::YOU WINER::");
TextXY( Screen->GetBack(), 240,138, 255, 28, 7, 134,ETA_CENTER, "<Congratulation>");
}

if( Input->GetKeyState(SDLK_SPACE)|| Input->GetKeyState(CDXKEY_JOYBUTN11)){
score = 0;
}//วน loop

randomchance = rand()%500;
if (randomchance == 49 && getup == false) {
getup = true;
runonce = true;
}
if (getup == true) {
if (runonce == true) { x2 = rand()%420; y2 = 0; runonce = false;}
y2 = y2+5;
hearts->SetPos(x2,y2);
hearts->Draw( Screen->GetBack(), 0, 0, CDXBLT_TRANS );
if (y2 > 272) { getup = false; }
if ( chubu->SpriteHit(hearts) ) { live++; getup = false; }
}
//if ( Input->GetKeyState(SDLK_3)) { live=10; } //สูตรโกง
}
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;
}

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

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