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;
};