Hi peeps!
Got a poblem here. Can't make my check collision function to work. Not much to say is there? Oh right, when collision happens, i want to move the square somewhere on the surface with a specific height. Got it? Good here's the code:
Main:
Check collision:Code://The square's coordinates int sX = 400; int sY = 300; //Check collision and move the square check_coll(sX, sY, x, y);
Random number:Code://Check if there is a collision int check_coll( int sX, int sY, int pX, int pY ) { //Square's position int sXl = sX; int sXr = sX + 29; int sYl = sY; int sYr = sY + 29; //Piece's position int pXl = pX; int pXr = pX + 75; int pYl = pY; int pYr = pY + 148; //Check if you hit the square int n = sXl; if (sXl == pXr || sXr == pXl) { //New position for the square n = GetRand(0, 640); SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) ); apply_surface( 0, 0, background, screen ); piece = load_image( "piece.png" ); apply_surface( pXl, pYl, piece, screen ); } //Apply the square apply_surface( n, sYl, square, screen ); }
I think this is all code that is necessary. If you need more code, tell me!Code://Random number int GetRand(int min, int max) { static int Init = 0; int rc; if (Init == 0) { srand(time(NULL)); Init = 1; } rc = (rand() % (max - min + 1) + min); return (rc); }
Problems:
1. When if (sXl == pXr || sXr == pXl) gets true, the square switches position instead of staying. So the random function goes weird. It should only get 1 number but it don't.
2. When if (sXl == pXr || sXr == pXl) has stopped being true, the square goes back to the start position of the square.
How do i solve theese problems?
REGARDS!



LinkBack URL
About LinkBacks




