//_--------------------------------------------------------------------------- #include #include #include //-- Globalne promenljive ----- // Parametri ekrana, fps, povrsina za pozadinu i event handler const int S_WIDTH= 640; const int S_HEIGHT= 480; const int S_BPP= 32; const int FRAMES_PER_SECOND= 30; SDL_Surface *screen= NULL; SDL_Event event; //-- Klasa osnovne kocke class KOCKA { private: //Atributi SDL_Rect box; int Vx, Vy; int pokretna; public: KOCKA(); KOCKA(int a, int b, int c, int d); void set_x(int i) { box.x= i; } void set_y(int i) {box.y= i; } int get_x() { return box.x; } int get_y() { return box.y; } int get_w() { return box.w; } int get_h() { return box.h; } void set_xy(int a, int b) { box.x= a; box.y= b; } void set_vel(int a, int b) { Vx= a; Vy= b; } void set_Vx(int a) { Vx= a; } void set_Vy(int a) { Vy= a; } void flip_Vx() { Vx= -Vx; } void flip_Vy() { Vy= -Vy; } void mov_x() { box.x+= Vx; } void mov_y() { box.y+= Vy; } void pomeri_x(); void pomeri_y(); void pokreni() { pokretna= 1; } SDL_Rect get_box() { return box; } void prikazi(); void BsudarX(KOCKA *a); void BsudarY(KOCKA *a); }; //== Klasa Player KOCKA koja je prosirena osnovna KOCKA class PKOCKA: public KOCKA { public: PKOCKA (int a, int b, int c, int d): KOCKA(a,b,c,d) { set_vel(0,0); } int Csudar(KOCKA *a); void handle_input(); void prikazi(); }; class Timer { private: //The clock time when the timer started int startTicks; //The ticks stored when the timer was paused int pausedTicks; //The timer status int paused; int started; public: //Initializes variables Timer(); //The various clock actions void start(); void stop(); void pause(); void unpause(); //Get the number of ticks since the timer started //or gets the number of ticks when the timer was paused int get_ticks(); //Checks the status of the timer int is_started(); int is_paused(); }; // Podrazumevani konstruktor KOCKA::KOCKA(){ box.x= 0; box.y= 0; box.w= 20; box.h= 20; Vx= Vy= 0; pokretna= 0; } // Konstruktor KOCKA::KOCKA(int a, int b, int c, int d) { box.x= a; box.y= b; box.w= c; box.h= d; for (int j= 0; j<10; j++) { int g= rand(); } Vx= (int)(((double)rand())/((double)RAND_MAX + (double)1)*10); Vy= 10 - Vx; pokretna= 0; } // -- Pomeranje kocki po ekranu po x osi void KOCKA::pomeri_x() { if (!pokretna) return; box.x+= Vx; int left= box.x; int right= box.x + box.w; if (left<0) { box.x= 0; Vx= -Vx; } if (right>S_WIDTH) { box.x= S_WIDTH - box.w; Vx= -Vx; } } void KOCKA::pomeri_y() { if (!pokretna) return; box.y+= Vy; int upper= box.y; int down= box.y + box.h; if (upper<0) { box.y= 0; Vy= -Vy; } if (down>S_HEIGHT) { box.y= S_HEIGHT - box.h; Vy= -Vy; } } //-- Iscrtavanje na ekran void KOCKA::prikazi() { if (SDL_FillRect(screen, &box, SDL_MapRGB(screen->format,0,0,0xFF))==-1) { printf("Greska u iscrtavanju kocke : %s\n", SDL_GetError()); exit(1); } } //-- Regulisanje sudara kocke sa drugom kockom void KOCKA::BsudarX(KOCKA *A) { int left= box.x; int right= box.x + box.w; int upper= box.y; int down= box.y + box.h; int leftA= A->get_x(); int rightA= A->get_x() + A->get_w(); int upperA= A->get_y(); int downA= A->get_y() + A->get_h(); if (left>=rightA) return; else if (right<=leftA) return; else if (down<=upperA) return; else if (upper>=downA) return; else { if ((rightA-left)<11) set_x(rightA); if ((right-leftA)<11) set_x(leftA-box.w); flip_Vx(); A->flip_Vx(); } return; } void KOCKA::BsudarY(KOCKA *A) { int left= box.x; int right= box.x + box.w; int upper= box.y; int down= box.y + box.h; int leftA= A->get_x(); int rightA= A->get_x() + A->get_w(); int upperA= A->get_y(); int downA= A->get_y() + A->get_h(); if (left>=rightA) return; else if (right<=leftA) return; else if (down<=upperA) return; else if (upper>=downA) return; else { if ((down-upperA)<11) set_y(upperA-box.h); if ((downA-upper)<11) set_y(downA); flip_Vy(); A->flip_Vy(); } return; } // Sudar player kocke sa CPU kockom int PKOCKA::Csudar(KOCKA *A) { int left= get_x(); int right= get_x() + get_w(); int upper= get_y(); int down= get_y() + get_h(); int leftA= A->get_x(); int rightA= A->get_x() + A->get_w(); int upperA= A->get_y(); int downA= A->get_y() + A->get_h(); if (left>rightA) return 0; else if (rightdownA) return 0; else return 1; } //-- Pomeranje player kocke kursorima na tastaturi void PKOCKA::handle_input() { //If a key was pressed if( event.type == SDL_KEYDOWN ) { //Adjust the velocity switch( event.key.keysym.sym ) { case SDLK_UP: set_Vy(-10); break; case SDLK_DOWN: set_Vy(10); break; case SDLK_LEFT: set_Vx(-10); break; case SDLK_RIGHT: set_Vx(10); break; } } //If a key was released else if( event.type == SDL_KEYUP ) { //Adjust the velocity switch( event.key.keysym.sym ) { case SDLK_UP: set_Vy(0); break; case SDLK_DOWN: set_Vy(0); break; case SDLK_LEFT: set_Vx(0); break; case SDLK_RIGHT: set_Vx(0); break; } } mov_x(); mov_y(); int left= get_x(); int right= get_x() + get_w(); int upper= get_y(); int down= get_y() + get_h(); if (left<0) set_x(0); if (right>S_WIDTH) set_x( S_WIDTH - get_w()); if (upper<0) set_y(0); if (down>S_HEIGHT) set_y( S_HEIGHT - get_h()); } void PKOCKA::prikazi() { if (SDL_FillRect(screen, &(get_box()), SDL_MapRGB(screen->format,0xFF,0,0))==-1) { printf("Greska u iscrtavanju kocke : %s\n", SDL_GetError()); exit(1); } } Timer::Timer() { //Initialize the variables startTicks = 0; pausedTicks = 0; paused = 0; started = 0; } void Timer::start() { //Start the timer started = 1; //Unpause the timer paused = 0; //Get the current clock time startTicks = SDL_GetTicks(); } void Timer::stop() { //Stop the timer started = 0; //Unpause the timer paused = 0; } void Timer::pause() { //If the timer is running and isn't already paused if( (started) && (!paused) ) { //Pause the timer paused = 1; //Calculate the paused ticks pausedTicks = SDL_GetTicks() - startTicks; } } void Timer::unpause() { //If the timer is paused if(paused) { //Unpause the timer paused = 0; //Reset the starting ticks startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks pausedTicks = 0; } } int Timer::get_ticks() { //If the timer is running if(started) { //If the timer is paused if(paused) { //Return the number of ticks when the the timer was paused return pausedTicks; } else { //Return the current time minus the start time return SDL_GetTicks() - startTicks; } } //If the timer isn't running return 0; } int Timer::is_started() { return started; } int Timer::is_paused() { return paused; } //-- GLAVNI PROGRAM --- int main( int argc, char* args[] ) { //Neki flagovi int quit= 0; int run= 0; int end= 0; Timer tim; //Predmeti na ekranu KOCKA* K1= new KOCKA(200,300,50,50); KOCKA* K2= new KOCKA(400,300,50,50); PKOCKA* PK= new PKOCKA(310,0,50,50); PK->set_vel(0,0); K1->set_vel(5,-5); // K2->set_vel(1,-1); K1->pokreni(); K2->pokreni(); //-- Inicijalizacija SDL-a if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { printf("Greska u inicijalz. svega : %s\n", SDL_GetError()); exit(2); } // Inicijalizacija ekrana //screen= SDL_SetVideoMode(S_WIDTH, S_HEIGHT, S_BPP, SDL_HWSURFACE|SDL_DOUBLEBUF); screen= SDL_SetVideoMode(S_WIDTH, S_HEIGHT, S_BPP, SDL_SWSURFACE); if (screen== NULL) { printf("Greska u inicijaliz. ekrana : %s\n", SDL_GetError()); exit(3); } // Naslov na prozoru SDL_WM_SetCaption("--- POS ProjecT --- Press SPACE to start!", NULL); SDL_ShowCursor(SDL_DISABLE); // Radni deo -------- while (!quit) { //start the frame timer tim.start(); while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) quit= 1; if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { quit = 1; SDL_WM_SetCaption("--- POS ProjecT ---", NULL); } if ( event.key.keysym.sym == SDLK_SPACE ) { run = 1; } } } // Handles input and moves squares if ((run)&&(!end)) { PK->handle_input(); K1->pomeri_x(); K1->BsudarX(K2); K1->pomeri_y(); K1->BsudarY(K2); K2->pomeri_x(); K2->BsudarX(K1); K2->pomeri_y(); K2->BsudarY(K1); if ((PK->Csudar(K1))||(PK->Csudar(K2))) { end= 1; SDL_WM_SetCaption("--- POS ProjecT --- GAME OVER (press esc to quit)", NULL); } } // Iscrtavanje po ekranu SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format,0xFF,0xFF,0xFF)); PK->prikazi(); K1->prikazi(); K2->prikazi(); //Flipovanje ekrana if(SDL_Flip(screen) == -1) { printf("Greska u flipovanju ekrana : %s\n", SDL_GetError()); exit(3); } //Cap the frame rate while( tim.get_ticks() < 1000 / FRAMES_PER_SECOND ) { //wait SDL_Delay( 1000/FRAMES_PER_SECOND - tim.get_ticks() ); } } // Ciscenje programa delete K1; delete K2; delete PK; printf("Program se uspesno zavrsio"); SDL_Quit(); return 0; }