Thread: A trail in my game...

  1. #1
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    A trail in my game...

    Here is my pong game or the beginning of it anyway. I know my collision detection stuff doesn't work right and I will fix that later. But when I run this I get a trail behind pongBall. Can someone tell me how to stop this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "SDL.h"
    #include <iostream>
    using namespace std;
    ////////////////////////Variables/////////////////
    SDL_Surface *screen;
    SDL_Surface *ball;
    SDL_Surface *lside;
    SDL_Surface *rside;
    SDL_Surface *uside;
    SDL_Surface *dside;
    
    SDL_Rect ballrect;
    SDL_Rect lsiderect;
    SDL_Rect rsiderect;
    SDL_Rect usiderect;
    SDL_Rect dsiderect;
    
    
    int lsidex=0,lsidey=0;
    int rsidex=499,rsidey=0;
    int usidex=0,usidey=0;
    int dsidex=0,dsidey=499;
    /////////////////////Classes//////////////////////
    class Ball{
        public:
            int xpos;
            int ypos;
            int vely;
            int velx;
            Ball(){xpos=250;ypos=250;velx=-1;vely=1;};
            
    };    
    Ball pongBall;
    //////////////////////Function Declarations////////////////
    int LoadScreens();
    int redrawscreen();
    int loadrects();
    int moveball();
    int collisiond();
    
    
    ////////////////////////////Main////////////////////   
    int main(int argc, char*argv[])
    {
      int done=0;
      
      Ball pongBall;
      LoadScreens();
        
      while(done == 0){
      collisiond();
      moveball();  
      loadrects();
      redrawscreen();
    
        SDL_Event event;
    
        while ( SDL_PollEvent(&event) )
        {
          if ( event.type == SDL_QUIT )  {  done = 1;  }
    
          if ( event.type == SDL_KEYDOWN )
          {
            if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
          }
        }
    }    
    
      return 0;
    }
    
    /////////////////////Functions////////////////////
    
    int moveball(){
        pongBall.xpos=pongBall.velx+pongBall.xpos;
        pongBall.ypos=pongBall.vely+pongBall.ypos;
    };    
    
    int collisiond(){
        if (pongBall.xpos==0 || pongBall.xpos==499){pongBall.velx=pongBall.velx*-1;};
        if (pongBall.ypos==0 || pongBall.ypos==499){pongBall.vely=pongBall.vely*-1;};    
    };    
    
    int LoadScreens(){
        screen= SDL_SetVideoMode(500,500,32,SDL_HWSURFACE||SDL_DOUBLEBUF);
        ball = SDL_LoadBMP("Pic1.bmp");
        lside = SDL_LoadBMP("larside.bmp");
        rside = SDL_LoadBMP("larside.bmp");
        uside = SDL_LoadBMP("UpaDwnside.bmp");
        dside = SDL_LoadBMP("UpaDwnside.bmp");
    };
    
    int redrawscreen(){
        SDL_BlitSurface(ball,NULL,screen,&ballrect);
        SDL_BlitSurface(lside,NULL,screen,&lsiderect);
        SDL_BlitSurface(rside,NULL,screen,&rsiderect);
        SDL_BlitSurface(uside,NULL,screen,&usiderect);
        SDL_BlitSurface(dside,NULL,screen,&dsiderect);
        SDL_Flip(screen);   
    };
    
    int loadrects(){
        ballrect.x = pongBall.xpos;
        ballrect.y = pongBall.ypos;
        lsiderect.x=lsidex;
        lsiderect.y=lsidey;
        rsiderect.x=rsidex;
        rsiderect.y=rsidey;
        usiderect.x=usidex;
        usiderect.y=usidey;
        dsiderect.x=dsidex;
        dsiderect.y=dsidey;
    };
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I don't know if your stuck on SDL or not but maybe you should check out allegro I have used both and I like allegro better.
    http://www.loomsoft.net/resources/al...ltut_index.htm is a the tutorial I learned the basics from its very similar to SDL so you should have no problem switching over. Im not sure what compiler you use but http://www.allegro.cc/ is a good site with details on setting it up with dev or msvc(if you use linux im sure your good enough to figure it out yourself )
    Woop?

  3. #3
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39
    Actually I am stuck on SDL because It is what I have been studying and I hope to move on to 3D some time in the future.

    So does anyone know how to get the trail to stop?
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Sounds like you are not erasing the old image of the ball. Double buffering should take care of the problem.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Indeed. You can erase the whole screen and redraw everything, or you can erase the ball and redraw it elsewhere - but if you do either of these, then you'll get flickering, which is solved by using the technique of Double-Buffering.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM