Tuesday 25 August 2009

Source code

hi guys here is the my main.cpp source code just to let you guys see it. Notice the poor coding style and bad practice but if you see any coding tidbits you like feel free to use them

//The headers
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"
#include "ball.h"
#include "Paddle.h"
#include "Button.h"
#include "timer.h"
#include "computerpaddle.h"
#include
#include
#include



//The screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The dimensions of the dot
const int playerpaddle_WIDTH = 20;
const int playerpaddle_HEIGHT = 80;

const int ball_WIDTH = 20;
const int ball_HEIGHT =20;

const int CLIP_MOUSEOVER = 0;
const int CLIP_MOUSEOUT = 1;
const int CLIP_MOUSEDOWN = 2;
const int CLIP_MOUSEUP = 3;


int gamestate = 0;

bool isWinner = false;


SDL_Color textColor = { 255, 255, 255 };

//The surfaces

SDL_Surface *buttonSheet = NULL;
SDL_Surface *Mainmenu = NULL;
SDL_Surface *score = NULL;
SDL_Surface *score2 = NULL;
SDL_Surface *scorepoints = NULL;
SDL_Surface *scorepoints2 = NULL;
SDL_Surface *playerpaddle1 = NULL;
SDL_Surface *ball1 = NULL;
SDL_Surface *paddle1 = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *victory = NULL;

TTF_Font *font = NULL;
//The event structure
SDL_Event event;
SDL_Rect box;
SDL_Rect paddlebox;
SDL_Rect compbox;




SDL_Rect clips[ 1 ];


SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old surface
SDL_FreeSurface( loadedImage );

//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//Return the optimized surface
return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

bool check_collision( 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;

//Calculate the sides of rect B
leftB = B.x;
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;
}


bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}


screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

background = load_image( "PongBackground copy.png" );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}

if( TTF_Init() == -1 )
{
return false;
}

//Set the window caption
SDL_WM_SetCaption( "Pong v1.0", NULL );

//If everything initialized fine
return true;
}

void set_clips()
{
//Clip the sprite sheet
clips[ CLIP_MOUSEOVER ].x = 0;
clips[ CLIP_MOUSEOVER ].y = 0;
clips[ CLIP_MOUSEOVER ].w = 320;
clips[ CLIP_MOUSEOVER ].h = 240;


}

bool load_files()
{
//Load the dot image
playerpaddle1 = load_image( "paddle.png" );
paddle1 = load_image( "paddle.png" );
ball1 = load_image( "ball.png" );
buttonSheet = load_image( "Start button copy.png" );
Mainmenu = load_image( "Pong main menu copy.png" );


font = TTF_OpenFont( "lazy.ttf", 28 );

//If there was a problem in loading the dot
if( playerpaddle1 == NULL )
{
return false;
}
if( paddle1 == NULL )
{
return false;
}

if( ball1 == NULL )
{
return false;
}

if( font == NULL )
{
return false;
}
if( buttonSheet == NULL )
{
std::cout << x =" 0;" y =" 0;" w =" 20;" h =" 80;" y =" 80;" xvel =" 0;" yvel =" 0;" type ="="" type ="=""> SCREEN_HEIGHT ) || ( check_collision( box, paddlebox ) ) )
{



paddlebox.y -= yVel;
}




// check for collision between ball and paddle. if true add score


}

void playerpaddle::show()
{
//Show the dot
apply_surface( paddlebox.x, paddlebox.y, playerpaddle1, screen );
}





//The timer




ball::ball()
{
//Initialize the offsets
box.x = 400;
box.y = 200;
box.w = 20;
box.h = 20;



playerscore = 0;
playerscore2 = 0;
//Initialize the velocity
xVel = 20;
yVel = 10;
}


void ball::move()
{
//Move the playerpaddle left or right
box.x += xVel;

//If the playerpaddle went too far to the left or right
if( ( box.x <> SCREEN_WIDTH ) || ( check_collision( box, paddlebox ) ) || ( check_collision( box, compbox ) ) )
{
//move back
box.x -= xVel;


xVel *= -1;
}
if( ( box.y <> SCREEN_HEIGHT ) || ( check_collision( box, paddlebox ) ) || ( check_collision( box, compbox ) ) )
{
//move back

box.y -= yVel;



yVel *= -1;

}

if(box.x == 0)
{
playerscore2 += 5;
}
if(box.x == 780)
{
playerscore += 5;
}
//Move the playerpaddle up or down
box.y += yVel;

//If the dot went too far up or down


}

void ball::show()
{
//Show the dot
apply_surface( box.x, box.y, ball1, screen );
}
void ball::get_score()
{

std::stringstream stream;
stream << text =" stream.str();" ptr1 =" text.data();" scorepoints =" TTF_RenderText_Solid(" text2 =" stream2.str();" ptr2 =" text2.data();" scorepoints2 =" TTF_RenderText_Solid(" playerscore ="="" victory =" TTF_RenderText_Solid(" iswinner =" true;" playerscore2 ="="" victory =" TTF_RenderText_Solid(" iswinner =" true;" screen_width =" 640;" screen_height =" 480;" x =" 780;" y =" 200;" w =" 20;" h =" 80;" xvel =" 0;" yvel =" 0;" type ="="" type ="=""> compbox.y)
{
yVel = 9;

}
if(box.y < yvel =" -9;"> SCREEN_HEIGHT ) || ( check_collision( box, paddlebox ) ) )
{



compbox.y -= yVel;
}



}

void paddle::show()
{
//Show the dot
apply_surface( compbox.x, compbox.y, paddle1, screen );
}

Button::Button( int x, int y, int w, int h )
{
//Set the button's attributes
box.x = x;
box.y = y;
box.w = w;
box.h = h;

//Set the default sprite
clip = &clips[ CLIP_MOUSEDOWN ];
}

void Button::handle_events()
{
//The mouse offsets
int x = 0, y = 0;

//If the mouse moved
if( event.type == SDL_MOUSEMOTION )
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;

//If the mouse is over the button
if( ( x > box.x ) && ( x <> box.y ) && ( y < clip =" &clips[" clip =" &clips[" type ="="" button ="="" x =" event.button.x;" y =" event.button.y;" gamestate =" 1;"> box.x ) && ( x <> box.y ) && ( y < clip =" &clips[" type ="="" button ="="" x =" event.button.x;" y =" event.button.y;"> box.x ) && ( x <> box.y ) && ( y < clip =" &clips[" quit =" false;" counter =" 0;" quit ="="" type ="="" quit =" true;" gamestate ="="">clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
apply_surface(0,0, Mainmenu, screen);
button1.show();

}

if( isWinner == true)
{ gamestate = 0;
}
if(gamestate == 1)
{
counter += 1;

score = TTF_RenderText_Solid( font, "score:", textColor ); //If there was an error in rendering the text
if( score == NULL )
{
return 1;
}

score2 = TTF_RenderText_Solid( font, "score:", textColor ); //If there was an error in rendering the text
if( score2 == NULL )
{
return 1;
}



apply_surface(0, 0, background, screen);

apply_surface( 0, 0, score, screen );
apply_surface( 600, 0, score2, screen );



gameball.get_score();
myplayerpaddle.show();
mypaddle.show();
gameball.show();

if(counter >= 30)
{

if(isWinner == false)
{

mypaddle.move();
gameball.move();
myplayerpaddle.move();


}
}


}

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}

//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}

//Clean up
clean_up();

return 0;
}


I wont include the header files as they are basically just the file classes and they are quite literally the same

No comments:

Post a Comment

money maker