I can upload the current code if needed but I am attempting to create a game where a ball has to move and avoid the obstacles. So far I have got the moment and collision detection working. The problem is when the ball moves it leaves marks behind it...I am assuming this is due to it not updating even though I have put SDL_Flip ( screen ). Which I thought should update the screen.
Also, when someone loses a life, I would like a picture of a little circle disappear of the screen. After, reading the documentation for SDL, I still have no idea how to remove it.
So any clues would be usefl.
The erase/draw code for the ball would be helpful, at minimum. Also, does the grid as a whole move?
I have everything moving and I have everything reacting to collision detection. But when the ball moves it leaves the mark from where it was previously on screen. I have tried locking and unlocking each time but because of the obstacles been in a text file it just won't work properly.
I do not have an erase code...I have no idea how to create it either.
Code://The headers #include "SDL.h" #include "SDL_image.h" #include <string> #include <fstream> #include <iostream> #include <stdio.h> //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //Number of frames const int FRAMES_PER_SECOND = 20; //The surfaces SDL_Surface *background = NULL; SDL_Surface *screen = NULL; SDL_Surface *ball = NULL; SDL_Surface *cross = NULL; SDL_Surface *complete = NULL; SDL_Surface *PinkL = NULL; SDL_Surface *PinkR = NULL; SDL_Surface *PurpleR = NULL; SDL_Surface *PurpleL = NULL; SDL_Surface *Wall = NULL; SDL_Surface *Life = NULL; SDL_Surface *lose = NULL; //Ball attributes int BALL_RADIUS = 20; const int BALL_WIDTH = 40; const int BALL_HEIGHT = 40; const int OBST_WIDTH = 40; //Mouse co-ordinates int mousex; int mousey; //The X and Y offsets of the ball float ballx, bally; //The velocity of the dot float ballxVel, ballyVel; float diffx; float diffy; float grad; int mouse_click; int line_no = -1; int no_of_obst = 0; int move; int dir; float ballr, balll, ballu, balld; int obstacle [100][3]; int bigobstacle [100][5]; //The event structure that will be used SDL_Event event; SDL_Surface *load_image( std::string filename ) { //The image that's loaded SDL_Surface* loadedImage = NULL; //The optimized image 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 image optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old image SDL_FreeSurface( loadedImage ); } //If the image was optimized if( optimizedImage != NULL ) { //Map the color key Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 255, 255 ); //Set all pixels of color R 255, G 255, B 255 (White) to be transparent SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey ); } //Return the optimized image return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Temporary rectangle to hold the offsets SDL_Rect offset; //Get the offsets offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface( source, NULL, destination, &offset ); } bool init() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } //Set up the screen screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE ); //If there was an error in setting up the screen if( screen == NULL ) { return false; } //Set the window caption SDL_WM_SetCaption( "Garden Escape", NULL ); //If everything initialized fine return true; } bool load_files() { //Load the image background = load_image( "background.png" ); ball = load_image ( "ball.png" ); complete = load_image ( "complete.png" ); cross = load_image ( "cross.png" ); PinkL = load_image ( "pink-left.png" ); PinkR = load_image ( "pink-right.png" ); PurpleL = load_image ( "purple-left.png" ); PurpleR = load_image ( "purple-right.png" ); Life = load_image ( "life.png" ); Wall = load_image ( "Wall.png" ); lose = load_image ( "lose.png"); //If there was an error in loading the image if( background == NULL ) { return false; } if (ball == NULL) { return false; } return true; } void show_life1() { apply_surface ( 540, 20, Life, screen ); } void show_life2() { apply_surface ( 560, 20, Life, screen ); } void show_life3() { apply_surface ( 580, 20, Life, screen ); } void show_life4() { apply_surface ( 550, 40, Life, screen ); } void show_life5() { apply_surface ( 570, 40, Life, screen ); } void show_ball() { //Show the ball apply_surface( ballx, bally, ball, screen ); SDL_Flip ( screen ); SDL_Delay ( 10 ); } void Rect_Ball() { ballr = ballx + BALL_WIDTH; balll = ballx; ballu = bally; balld = bally + BALL_HEIGHT; } void collision_detection() { SDL_Rect Rect_Obst; int obstr, obstl, obstu, obstd; int i = 0; Rect_Ball(); if (( bally <= 0 ) && ( ballx <= 640 )) { if ( dir == 1 ) { move = 3; dir = 3; return; } if ( dir == 2 ) { move = 4; dir = 4; return; } } if (( bally >= 440.0 ) && ( ballx <= 640 )) { if ( dir == 3) { move = 1; dir = 1; return; } if ( dir == 4 ) { move = 2; dir = 2; return; } } while ( i < bigobstacle [i][3] ) { Rect_Obst.x = bigobstacle [i][1]; Rect_Obst.y = bigobstacle [i][2]; Rect_Obst.w = OBST_WIDTH; Rect_Obst.h = bigobstacle [i][4] * OBST_WIDTH; i = i + 1; obstr = Rect_Obst.x + Rect_Obst.w; obstl = Rect_Obst.x; obstu = Rect_Obst.y; obstd = Rect_Obst.y + Rect_Obst.h; if (( ballr == obstl ) && ( obstr > ballr ) && ( obstd > ballu ) && ( obstu < balld )) { if ( dir == 3 ) { move = 4; dir = 4; return; } if ( dir == 1) { move = 2; dir = 2; return; } } if (( balll == obstr ) && ( balll >= obstl ) && ( obstd >= ballu ) && ( obstu <= balld )) { if ( dir == 2 ) { move = 1; dir = 1; return; } if ( dir == 4 ) { move = 3; dir = 3; return; } } if (( balld > obstu )&& ( ballr > obstl ) && ( balll < obstr ) && ( ballu < obstd )) { if ( dir == 3 ) { move = 1; dir = 1; return; } if ( dir == 4 ) { move = 2; dir = 2; return; } } if (( ballu < obstd ) && ( ballr >= obstl ) && ( balll <= obstr ) && ( balld > obstu )) { if ( dir == 1 ) { move = 3; dir = 3; return; } if ( dir == 2 ) { move = 4; dir = 4; return; } } if ( ballx == -40 ) { move = 5; return; } if ( ballx == 640 ) { move = 6; return; } } } void move_ball_up_right() { bally = bally - grad; ballx = ballx + 1; show_ball(); collision_detection(); } void move_ball_down_right() { bally = bally + grad; ballx = ballx + 1; show_ball(); collision_detection(); } void move_ball_up_left() { bally = bally - grad; ballx = ballx - 1; show_ball(); collision_detection(); } void move_ball_down_left() { bally = bally + grad; ballx = ballx - 1; show_ball(); collision_detection(); } void handle_input_ball() { //If a key was pressed if( event.type == SDL_KEYDOWN ) { if ( SDLK_SPACE ) { if ( bally < mousey ) { diffy = mousey - bally; diffx = mousex - ballx; grad = diffy/diffx; move = 3; dir = 3; printf ("%d\n", dir); } else if ( bally > mousey ) { diffy = bally - mousey; diffx = mousex - ballx; grad = diffy / diffx; move = 1; dir = 1; } } mouse_click = 1; } } void show_cross() { apply_surface ( mousex, mousey, cross, screen ); } void handle_input_mouse() { if( SDL_PollEvent( &event ) ) { //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. 7 is middle of cross in x, and 17 is middle of y mousex = event.button.x - 7; mousey = event.button.y - 17; show_cross(); } } SDL_Flip ( screen ); } } void obstacles () { FILE *Level; int obstaclex; int obstacley; int obstype; int i = -1; int h = 0; int j = 0; int k = 0; Level = fopen ( "1.txt", "r" ); while ( !feof ( Level ) ) { line_no = line_no + 1; fscanf ( Level, "%3d %3d %1d", &obstaclex, &obstacley, &obstype); obstacle [line_no][1] = obstaclex; obstacle [line_no][2] = obstacley; obstacle [line_no][3] = obstype; if ( obstype == 1 ) { apply_surface ( obstaclex, obstacley, Wall, screen ); } if ( obstype == 2 ) { apply_surface ( obstaclex, obstacley, PinkL, screen ); } if ( obstype == 3 ) { apply_surface ( obstaclex, obstacley, PurpleL, screen ); } } fclose ( Level ); while ( i < line_no - 1 ) { bigobstacle [0][1] = obstacle[0][1]; bigobstacle [0][2] = obstacle[0][2]; bigobstacle [0][3] = 1; bigobstacle [0][5] = obstacle[0][3]; i = i + 1; j = j + 1; if ( obstacle[i][1]!= obstacle[i+1][1] || obstacle[i][3] != obstacle [i+1][3]) { h = h + 1; bigobstacle[h][1] = obstacle[i+1][1]; bigobstacle[h][2] = obstacle[i+1][2]; bigobstacle[h][3] = h + 1; no_of_obst = j; bigobstacle[h-1][4] = no_of_obst; bigobstacle[h][5] = obstacle[i+1][3]; j = 0; } bigobstacle[h][4] = no_of_obst; } } void Ball() { //Initialize the offsets ballx = 0; bally = (SCREEN_HEIGHT / 2) - BALL_RADIUS; //Initialize the velocity ballxVel = 0; ballyVel = 240; } void clean_up() { //Free the surface SDL_FreeSurface( background ); SDL_FreeSurface ( ball ); SDL_FreeSurface ( cross ); SDL_FreeSurface ( complete ); SDL_FreeSurface ( Wall ); SDL_FreeSurface ( PinkL ); SDL_FreeSurface ( PurpleL ); SDL_FreeSurface ( PinkR ); SDL_FreeSurface ( PurpleR ); SDL_FreeSurface ( Life ); SDL_FreeSurface ( lose ); //Quit SDL SDL_Quit(); } int main( int argc, char* args[] ) { //Make sure the program waits for a quit bool quit = false; mouse_click = 0; Ball(); //Initialize if( init() == false ) { return 1; } //Load the files if( load_files() == false ) { return 1; } apply_surface( 0, 0, background, screen ); obstacles(); show_life1(); show_life2(); show_life3(); show_life4(); show_life5(); show_ball(); if (SDL_MUSTLOCK(screen)) { printf ("Lock"); } else printf ("no lock"); //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //While the user hasn't quit while( quit == false ) { //While there's an event to handle while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = 1; } } if ( mouse_click == 0 ) { handle_input_mouse(); } handle_input_ball(); TryAgain: switch ( move ) { case 1: move_ball_up_right(); goto TryAgain; break; case 2: move_ball_up_left(); goto TryAgain; break; case 3: move_ball_down_right(); goto TryAgain; break; case 4: move_ball_down_left(); goto TryAgain; break; case 5: apply_surface ( 120, 120, lose, screen ); SDL_Flip ( screen ); break; case 6: apply_surface ( 120, 120, complete, screen ); SDL_Flip ( screen ); break; } } //Free the surface and quit SDL clean_up(); return 0; }
I would create a hide_ball method that paints over the ball with the background color, move the ball, then call showball.
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks