Code:
#include <iostream>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <sstream>
const int WINDOWS_Height = 480;
const int WINDOWS_Width = 640;
const int SPRITE_SIZE = 32;
const int BALL_SIZE=15;
const char* WINDOWS_Title = "Ball Collecting Game";
using namespace std;
SDL_Surface *grass, *sprite, *temp, *ball, *scores;
SDL_Rect rcSprite, rcGrass, rcSrc, rcBall, rcScores;
int colorkey, score=0, bc=0;
bool runGame = true;
stringstream pScore;
Mix_Music *mainMusic = NULL;
Mix_Music *ballCollection = NULL;;
bool checkCollision(SDL_Rect Sprite1, SDL_Rect Sprite2);
void musicDone();
void handleEvents(SDL_Event event); // Function to handle Events
int main (int argc, char *argv[]){
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) // Initializing SDL and Creating Window
return false;
SDL_Surface *screen = SDL_SetVideoMode(WINDOWS_Width, WINDOWS_Height, 0, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption(WINDOWS_Title, "Ball Game");
int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int audio_channels = 2;
int audio_buffers = 4096;
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
printf("Unable to open audio!\n");
exit(1);
}
temp = SDL_LoadBMP("./sprite.bmp"); //Loading Sprite Sheet
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
SDL_EnableKeyRepeat(70, 70); //Enabling Key Repeat
colorkey = SDL_MapRGB(screen->format, 255, 0, 255); //Setting up sprite colorkey
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
temp = SDL_LoadBMP("./grass.bmp"); //Loading Grass
grass = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
temp = SDL_LoadBMP("./ball.bmp");
ball = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
colorkey = SDL_MapRGB(screen->format, 192, 192, 192); //Setting up ball color-key
SDL_SetColorKey(ball, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
rcSprite.x = 150; //Setting Position of sprite
rcSprite.y = 150;
rcBall.x = rand() % 600 + 30; //Random Position of Ball
rcBall.y = rand() % 450 + 30;
rcSrc.x = 128; //setting frames for animation
rcSrc.y = 0;
rcSrc.w = SPRITE_SIZE;
rcSrc.h = SPRITE_SIZE;
rcScores.x = rcScores.y = 0;
rcScores.h = 20;
rcScores.w = WINDOWS_Width;
TTF_Init();
TTF_Font *times;
times = TTF_OpenFont("./Lato.ttf", 20);
SDL_Color white = {2, 2, 2};
//Game Loop
while (runGame) {
SDL_Event event;
if (SDL_PollEvent(&event)) { //Here we call function handle Event
handleEvents(event);
if (event.key.keysym.sym == SDLK_q || event.key.keysym.sym == SDLK_ESCAPE) {
return 0;
}
}
if (mainMusic == NULL) {
mainMusic = Mix_LoadMUS("./mainSound.wav");
Mix_PlayMusic(mainMusic, -1);
Mix_HookMusicFinished(musicDone);
}
if (rcSprite.x < 0) //Collision with Corners
rcSprite.x = 0;
else if (rcSprite.x > WINDOWS_Width-SPRITE_SIZE)
rcSprite.x = WINDOWS_Width-SPRITE_SIZE;
if (rcSprite.y < 20)
rcSprite.y = 20;
else if (rcSprite.y > WINDOWS_Height-SPRITE_SIZE)
rcSprite.y = WINDOWS_Height-SPRITE_SIZE;
if (checkCollision(rcBall, rcSprite) == true) {
rcBall.x = rand() % 600 + 30;
rcBall.y = rand() % 400 + 30;
score += 10;
pScore.str(std::string());
pScore <<"Score :" << score;
SDL_Flip(screen);
ballCollection = Mix_LoadMUS("./ballCollected.wav");
Mix_PlayMusic(ballCollection, 0);
}
scores = TTF_RenderText_Blended(times, pScore.str().c_str(), white);
for (int x = 0; x < WINDOWS_Width/SPRITE_SIZE; x++) {
for (int y = 0; y <WINDOWS_Height /SPRITE_SIZE; y++ ) {
rcGrass.x = x * SPRITE_SIZE;
rcGrass.y = y *SPRITE_SIZE ;
SDL_BlitSurface(grass, NULL, screen, &rcGrass); //Drawing Grass on Screen
}
}
SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite); //Drawing Sprite on Screen
SDL_BlitSurface(ball, NULL, screen, &rcBall); //Drawing ball on Screen
SDL_BlitSurface(scores, NULL, screen, &rcScores);
SDL_UpdateRect(screen, 0, 0, 0, 0); //Updating Screen
}
SDL_FreeSurface(screen);
SDL_FreeSurface(ball);
SDL_FreeSurface(grass);
SDL_FreeSurface(sprite);
TTF_CloseFont(times);
TTF_Quit();
musicDone();
SDL_Quit();
return 0;
}
void handleEvents(SDL_Event event) {
bool keysHeld[323] = {false};
if (event.type == SDL_QUIT) {
runGame = false;
return;
}
if (event.type == SDL_KEYDOWN){
keysHeld[event.key.keysym.sym] = true;
}
if (event.type == SDL_KEYUP){
keysHeld[event.key.keysym.sym] = false;
}
if (keysHeld[SDLK_UP] ) {
if(rcSrc.x == 0)
rcSrc.x = 32;
else
rcSrc.x = 0;
rcSprite.y -= 7;
}
if (keysHeld[SDLK_DOWN]) {
if (rcSrc.x == 128)
rcSrc.x = 160;
else
rcSrc.x = 128;
rcSprite.y += 7;
}
if (keysHeld[SDLK_LEFT]) {
if (rcSrc.x == 192)
rcSrc.x = 224;
else
rcSrc.x = 192;
rcSprite.x -= 7;
}
if (keysHeld[SDLK_RIGHT]) {
if (rcSrc.x == 64)
rcSrc.x = 96;
else
rcSrc.x = 64;
rcSprite.x += 7;
}
}
bool checkCollision( SDL_Rect A, SDL_Rect B ) {
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
leftB = B.x; //Calculate the sides of rect B
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If any of the sides from A are outside of B
if( bottomA <= topB ) {
return false;
}
if( topA >= bottomB ) {
return false;
}
if( rightA <= leftB ) {
return false;
}
if( leftA >= rightB ) {
return false;
}
//If none of the sides from A are outside B
return true;
}
void musicDone() {
Mix_HaltMusic();
Mix_FreeMusic(mainMusic);
mainMusic = NULL;
Mix_FreeMusic(ballCollection);
ballCollection = NULL;
}