The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.
SDL
Simple Directmedia Layer Forums
tic tac toe game
LAURENT*
Joined: 06 Jan 2013
Posts: 32
Posted: Wed Mar 12, 2014 8:19 pm
Hello forum. I've decided to do a simple game of tic tac toe but I came across problems after fixing problems. It gotten me to the point where I'm questioning my way of doing this. I'm going to reveal what my goals were. I wanted to create 9 buttons and set them to a constant value when they were clicked. I would neatly arrange everything including the detection boxes of the button and check certain button combinations to determine a winner.
Anyways my problems are
*the image isn't being clipped correctly.
*multiple buttons appear activated by one.
*No idea how to deal with AI/computer/cpu/ect
I have a gut feeling my code is wonky with my event handling function at the bottom but I can't figure why.
Code:
void OX::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//If the mouse is over the button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
clip = &clips[CLIP_O];
}
}
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;
//Get the offsets
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, clip, destination, &offset );
}
void set_clips()
{
//Clip range for the top left
clips[ CLIP_O ].x = 0;
clips[ CLIP_O ].y = 0;
clips[ CLIP_O ].w = 26;
clips[ CLIP_O ].h = 31;
//Clip range for the top right
clips[ CLIP_X ].x = 27;
clips[ CLIP_X ].y = 0;
clips[ CLIP_X ].w = 50;
clips[ CLIP_X ].h = 31;
//Clip range for white space
clips[ CLIP_BLINK ].x = 54;
clips[ CLIP_BLINK ].y = 0;
clips[ CLIP_BLINK ].w = 79;
clips[ CLIP_BLINK ].h = 31;
}
OX::OX( 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_BLINK ];
}
void OX::handle_events()
{
//The mouse offsets
int x = 0, y = 0;
//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//If the mouse is over the button
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
clip = &clips[CLIP_O];
}
}
}
}