game code link....


#include <SDL.h>
#include <iostream>
#include <string>
#include <random>
#include <time.h>
#include <SDL_ttf.h>
using namespace std;

struct sprite
{
SDL_Surface* image;
SDL_Rect position;
SDL_Rect position1;
};

const int TITLE = 0;
const int GAME = 1;
const int SCORE = 2;
const int HELP = 3;

bool checkMouseOver(sprite sp, SDL_Event evt);

/*-------------------------------

-------------------------------*/

int main(int argc, char** argv)
{
/*-------------------------------

-------------------------------*/
SDL_Init(SDL_INIT_EVERYTHING);

/*-------------------------------

-------------------------------*/
SDL_Surface* screen;
bool running;
SDL_Event evt;
int gameState;
int circle;
bool buttonCheck;
SDL_Rect position2;// Start Button
SDL_Rect position1;// Select Button
SDL_Rect position;// Quit Button

SDL_Surface* BackgroundPushTheButton; //Main Menu Background
SDL_Surface* TitleScreenPushTheButton; //Main Menu TitleScreen
SDL_Surface* HelpBackground; // Help Background
SDL_Surface* image;

sprite StartButton;
sprite SelectButton;
sprite QuitButton;
sprite RedCircle;
sprite BlueCircle;
int score;
bool clicked;
Uint32 startTime;
Uint32 currentTime;



/*-------------------------------

-------------------------------*/
screen = SDL_SetVideoMode(640, 480,32,SDL_HWSURFACE);
running = true;
gameState = TITLE;
startTime = 0;
currentTime = 0;
buttonCheck = false;

StartButton.image = SDL_LoadBMP("play.bmp"); //Start Button
StartButton.position.x = 200;
StartButton.position.y =20;
StartButton.position.h = StartButton.image -> h;
StartButton.position.w = StartButton.image -> w;

SelectButton.image = SDL_LoadBMP("how to play.bmp"); //Select Button
SelectButton.position.x = 200;
SelectButton.position.y = 210;
SelectButton.position.h = SelectButton.image -> h;
SelectButton.position.w = SelectButton.image -> w;

QuitButton.image = SDL_LoadBMP("exit.bmp");//Quit Button
QuitButton.position.x = 200;
QuitButton.position.y = 295;
QuitButton.position.h = SelectButton.image -> h;
QuitButton.position.w = SelectButton.image -> w;



RedCircle.image = SDL_LoadBMP("RedCircle.bmp");
SDL_SetColorKey(RedCircle.image, SDL_SRCCOLORKEY, 0xffffff );
RedCircle.position.x = 200;
RedCircle.position.y = 125;
RedCircle.position.h = StartButton.image -> h;
RedCircle.position.w = StartButton.image -> w;
SDL_SetColorKey (RedCircle.image,SDL_SRCCOLORKEY,0xc62bc6);



BackgroundPushTheButton = SDL_LoadBMP("title.bmp");//Main Menu Background
TitleScreenPushTheButton = SDL_LoadBMP("logo.bmp");////Main Menu TitleScreen
HelpBackground = SDL_LoadBMP("how to play page.bmp");//Help Background


/*-------------------------------

-------------------------------*/
while(running == true){

/*-------------------------------

-------------------------------*/
while(SDL_PollEvent(&evt)){
if(evt.type == SDL_QUIT){
running = false;
}

switch(gameState){
case TITLE:
startTime = SDL_GetTicks();
score=0;
if (checkMouseOver ( StartButton, evt) == true)
{
gameState = GAME;
}

if (checkMouseOver ( SelectButton, evt) == true)
{
gameState = HELP;
}

if (checkMouseOver ( QuitButton, evt) == true)
{
gameState = SCORE;
}


if(evt.type == SDL_KEYDOWN){
if(evt.key.keysym.sym == SDLK_h){
gameState = HELP;
}


if(evt.key.keysym.sym == SDLK_RETURN){
gameState = GAME;
}
}


break;

case GAME:

if (checkMouseOver (BlueCircle, evt) == true)
{
cout<<"Button clicked";
RedCircle.position.x = rand()%600;
RedCircle.position.y = rand()%400;
buttonCheck = true;
score =score+1;
}

if (checkMouseOver (RedCircle, evt) == true)
{
cout<<"Button clicked";
BlueCircle.position.x = rand()%600;
BlueCircle.position.y = rand()%400;
buttonCheck = false;
score =score+1;
}



break;

case SCORE:
if(evt.type == SDL_KEYDOWN){
if(evt.key.keysym.sym == SDLK_RETURN){
gameState = TITLE;
}
}
break;

default:
break;

case HELP:
if(evt.type == SDL_KEYDOWN){
if(evt.key.keysym.sym == SDLK_RETURN){
gameState = TITLE;
}
}
break;

}

}

/*-------------------------------

-------------------------------*/

/*-------------------------------

-------------------------------*/
currentTime = (SDL_GetTicks() - startTime)/1000;
//std::cout<<"nTime "<<currentTime;

switch(gameState){
case TITLE:
std::cout<<"\ntitleScreen ";
//SDL_FillRect(screen,NULL,0xFF0000);
SDL_BlitSurface(BackgroundPushTheButton,NULL,screen,NULL);//Main Menu Background
SDL_BlitSurface(TitleScreenPushTheButton,NULL,screen,NULL);////Main Menu TitleScreen
SDL_BlitSurface(StartButton.image,NULL,screen,&StartButton.position);//Start Button
SDL_BlitSurface(SelectButton.image,NULL,screen,&SelectButton.position);//Select Button
SDL_BlitSurface(QuitButton.image,NULL,screen,&QuitButton.position); //Quit Button



break;

case GAME:
std::cout<<"\ngame";
//SDL_FillRect(screen,NULL,0xFFFFFF);
SDL_BlitSurface(BackgroundPushTheButton,NULL,screen,NULL);//Main Menu Background
if (currentTime == 5)
{
gameState = SCORE;
}

if (buttonCheck == false){
SDL_BlitSurface(RedCircle.image,NULL,screen,&BlueCircle.position); //Red Circle

break;
}
else if (buttonCheck == true){
SDL_BlitSurface(BlueCircle.image,NULL,screen,&RedCircle.position); //Blue Circle
break;
}

//if (buttonCheck == true && false);
// cout <<"your score is:..." <<endl;

//if (RedCircle

case SCORE:
std::cout<<"\nyour score is..."<<score;
SDL_BlitSurface(BackgroundPushTheButton,NULL,screen,NULL);//Main Menu Background
break;



case HELP:
std::cout<<"\nhelp";
SDL_BlitSurface(BackgroundPushTheButton,NULL,screen,NULL);//Main Menu Background
SDL_BlitSurface(HelpBackground,NULL,screen,NULL);//Help Background
break;

default:
break;

}

SDL_Flip(screen);

} //end game loop



SDL_QUIT;

return 0;
}

bool checkMouseOver(sprite sp, SDL_Event evt){
if(evt.type == SDL_MOUSEBUTTONUP && evt.button.button == SDL_BUTTON_LEFT){
if(evt.button.x> sp.position.x && evt.button.x < (sp.position.x + sp.image->w)){
if(evt.button.y > sp.position.y && evt.button.y < (sp.position.y + sp.image->h)){
return true;
}
}
}
return false;
}
Last edited on
Topic archived. No new replies allowed.