Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


What is wrong with this code?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 KingofSwing94   Members   -  Reputation: 100

Like
-4Likes
Like

Posted 27 May 2011 - 12:48 PM

Here is the code:

#include<iostream>
#include<SDL/SDL.h>
#include<SDL/SDL_ttf.h>
#include<string>

using namespace std;

int frame=0;
bool cap=true;
Timer fps;
const int SCREEN_WIDTH=640;
Const int SCREEN_HEIGHT=480;
Const int SCREEN_BPP=32;
Const int FRAMES_PER_SECOND=20;
int Player_One_Points,Cpu_Points,Player_Two_Points=0;
bool quit=false;
int game_state=o;
intrandomNumber=rand(); 
intrandomVelocity(randomNumber%6)+1;

class Ball
{
 public:
 
 int x,y;
 int xVel,yVel;
void Ball();
void Ball_Track();
void Show_Ball();
void Ball_Track_2_Player();
void Ball_Wall_Collision();
};
 Ball::Ball() :  randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
  x=310;
  y=230;
  xVel=3;
  yVel=0;
};
 Ball::Ball_Track()
{
  if(x+20 > SCREEN_WIDTH)
   {
 	Player_One_Points+=1;
 	x=310;
 	y=230;
 	SDL_Delay(2000);

 	if(Player_One_Points==1)
   	{
     	SDL_FreeSurface(Message0);
     	apply_surface(30,10,Message1,screen);
   	}
 	else if(Player_One_Points==2)
   	{
     	SDL_FreeSurface(Message1);
     	apply_surface(30,10,Message2,screen);
   	}
 	else if(Player_One_Points==3)
   	{
     	SDL_FreeSurface(Message2);
     	apply_surface(30,10,Message3,screen);
   	}
 	else if(Player_One_Points==4)
   	{ 
     	SDL_FreeSurface(Message3);
     	apply_surface(30,10,Message4,screen);
   	}
 	else if(Player_One_Points==5)
   	{ 
     	SDL_FreeSurface(Message4);
     	apply_surface(30,10,Message5,screen);
   	}
 	else if(Player_One_Points==6)
   	{ 
     	SDL_FreeSurface(Message5);
     	apply_surface(30,10,Message6,screen);
   	}
 	else if(Player_One_Points==7)
   	{ 
     	SDL_FreeSurface(Message6);
     	apply_surface(30,10,Message7,screen);
   	} 
 	else if(Player_One_Points==8)
   	{ 
     	SDL_FreeSurface(Message7);
     	apply_surface(30,10,Message8,screen);
   	}    
 	else if(Player_One_Points==9)
   	{ 
     	SDL_FreeSurface(Message8);
     	apply_surface(30,10,Message9,screen);
   	}
 	else if(Player_One_Points==10)
   	{ 
     	SDL_FreeSurface(Message9);
     	apply_surface(30,10,Message10,screen);
     	apply_surface(310,205,MessageCongratsP1,screen);
     	SDL_Delay(4000);
     	SDL_FreeSurface(Message10);
     	SDL_FreeSurface(MessageCongratsP1);
     	SDL_FreeSurface(Ball);
     	SDL_Freesurface(Player_One_Bat);
     	SDL_FreeSurface(Cpu_Bat);
     	game_state==0;
   	}
    }
   else(x<0)
    {
   	Cpu_Points+=1;
   	x=310;
   	y=230;
   	SDL_Delay(2000);
   	
 	if(Cpu_Points==1)
   	{
     	SDL_FreeSurface(Message0);
     	apply_surface(30,10,Message1,screen);
   	}
 	else if(Cpu_Points==2)
   	{
     	SDL_FreeSurface(Message1);
     	apply_surface(30,10,Message2,screen);
   	}
 	else if(Cpu_Points==3)
   	{
     	SDL_FreeSurface(Message2);
     	apply_surface(30,10,Message3,screen);
   	}
 	else if(Cpu_Points==4)
   	{ 
     	SDL_FreeSurface(Message3);
     	apply_surface(30,10,Message4,screen);
   	}
 	else if(Cpu_Points==5)
   	{ 
     	SDL_FreeSurface(Message4);
     	apply_surface(30,10,Message5,screen);
   	}
 	else if(Cpu_Points==6)
   	{ 
     	SDL_FreeSurface(Message5);
     	apply_surface(30,10,Message6,screen);
   	}
 	else if(Cpu_Points==7)
   	{ 
     	SDL_FreeSurface(Message6);
     	apply_surface(30,10,Message7,screen);
   	} 
 	else if(Cpu_Points==8)
   	{ 
     	SDL_FreeSurface(Message7);
     	apply_surface(30,10,Message8,screen);
   	}    
 	else if(Cpu_Points==9)
   	{ 
     	SDL_FreeSurface(Message8);
     	apply_surface(30,10,Message9,screen);
   	}
 	else if(Cpu_Points==10)
   	{ 
     	SDL_FreeSurface(Message9);
     	apply_surface(30,10,Message10,screen);
     	apply_surface(310,205,MessageCongratsCpu,screen);
     	SDL_Delay(4000);
     	SDL_FreeSurface(Message10);
     	SDL_FreeSurface(MessageCongratsCpu);
     	SDL_FreeSurface(Ball);
     	SDL_Freesurface(Player_One_Bat);
     	SDL_FreeSurface(Cpu_Bat);
     	game_state==0;
   	}
   }
};

 Ball::Show_Ball()
{
  apply_surface(x,y,Ball,screen);
};

 Ball::Ball_Track_2_Player()
{
  if(x+20 > SCREEN_WIDTH)
   {
 	Player_One_Points+=1;
 	x=310;
 	y=230;
 	SDL_Delay(2000);

 	if(Player_One_Points==1)
   	{
     	SDL_FreeSurface(Message0);
     	apply_surface(30,10,Message1,screen);
   	}
 	else if(Player_One_Points==2)
   	{
     	SDL_FreeSurface(Message1);
     	apply_surface(30,10,Message2,screen);
   	}
 	else if(Player_One_Points==3)
   	{
     	SDL_FreeSurface(Message2);
     	apply_surface(30,10,Message3,screen);
   	}
 	else if(Player_One_Points==4)
   	{ 
     	SDL_FreeSurface(Message3);
     	apply_surface(30,10,Message4,screen);
   	}
 	else if(Player_One_Points==5)
   	{ 
     	SDL_FreeSurface(Message4);
     	apply_surface(30,10,Message5,screen);
   	}
 	else if(Player_One_Points==6)
   	{ 
     	SDL_FreeSurface(Message5);
     	apply_surface(30,10,Message6,screen);
   	}
 	else if(Player_One_Points==7)
   	{ 
     	SDL_FreeSurface(Message6);
     	apply_surface(30,10,Message7,screen);
   	} 
 	else if(Player_One_Points==8)
   	{ 
     	SDL_FreeSurface(Message7);
     	apply_surface(30,10,Message8,screen);
   	}    
 	else if(Player_One_Points==9)
   	{ 
     	SDL_FreeSurface(Message8);
     	apply_surface(30,10,Message9,screen);
   	}
 	else if(Player_One_Points==10)
   	{ 
     	SDL_FreeSurface(Message9);
     	apply_surface(30,10,Message10,screen);
     	apply_surface(310,205,MessageCongratsP1,screen);
     	SDL_Delay(4000);
     	SDL_FreeSurface(Message10);
     	SDL_FreeSurface(MessageCongratsP1);
     	SDL_FreeSurface(Ball);
     	SDL_Freesurface(Player_One_Bat);
     	SDL_FreeSurface(Player_Two_Bat);
     	game_state==0;
   	}
    }
   else(x<0)
    {
   	Player_Two_Points+=1;
   	x=310;
   	y=230;
   	SDL_Delay(2000);
   	
 	if(Player_Two_Points==1)
   	{
     	SDL_FreeSurface(Message0);
     	apply_surface(30,10,Message1,screen);
   	}
 	else if(Player_Two_Points==2)
   	{
     	SDL_FreeSurface(Message1);
     	apply_surface(30,10,Message2,screen);
   	}
 	else if(Player_Two_Points==3)
   	{
     	SDL_FreeSurface(Message2);
     	apply_surface(30,10,Message3,screen);
   	}
 	else if(Player_Two_Points==4)
   	{ 
     	SDL_FreeSurface(Message3);
     	apply_surface(30,10,Message4,screen);
   	}
 	else if(Player_Two_Points==5)
   	{ 
     	SDL_FreeSurface(Message4);
     	apply_surface(30,10,Message5,screen);
   	}
 	else if(Player_Two_Points==6)
   	{ 
     	SDL_FreeSurface(Message5);
     	apply_surface(30,10,Message6,screen);
   	}
 	else if(Player_Two_Points==7)
   	{ 
     	SDL_FreeSurface(Message6);
     	apply_surface(30,10,Message7,screen);
   	} 
 	else if(Player_Two_Points==8)
   	{ 
     	SDL_FreeSurface(Message7);
     	apply_surface(30,10,Message8,screen);
   	}    
 	else if(Player_Two_Points==9)
   	{ 
     	SDL_FreeSurface(Message8);
     	apply_surface(30,10,Message9,screen);
   	}
 	else if(Player_Two_Points==10)
   	{ 
     	SDL_FreeSurface(Message9);
     	apply_surface(30,10,Message10,screen);
     	apply_surface(310,205,MessageCongratsP2,screen);
     	SDL_Delay(4000);
     	SDL_FreeSurface(Message10);
     	SDL_FreeSurface(MessageCongratsP2);
     	SDL_FreeSurface(Ball);
     	SDL_Freesurface(Player_One_Bat);
     	SDL_FreeSurface(Player_Two_Bat);
     	game_state==0;
   	}
   }
};
 Ball::Ball_Wall_Collision()
{
  if(y==0)&&(xVel==3)
    {
      yVel+=randomVelocity;
    }
  else if(y==0)&&(xVel<0)
    {
      yVel+=randomVelocity;
    }
  else if(y+=20==SCREEN_HEIGHT)&&(xVel==3)
    {
      yVel-=randomVelocity;
    }
  else(y+=20==SCREEN_HEIGHT)&&(xVel<0)
    {
      yVel-=randomVelocity;
    }
};

class One_Player_Button
{
  public:
  
  SDL_Rect box;
  One_Player_Button(intx,int,y,intw,inth);
  void handle_events();
  void show();
};  
 One_Player_Button::One_Player_Button(intx,inty,intw,inth)
{
  box.x=x;
  box.y=y;
  box.w=w;
  box.h=h;
};

 One_Player_Button::handle_events()
{
  intx=320,y=240;
   
   if(event.type==SDL_MOUSEMOTION)
 	{
   	x=event.motion.x;
   	y=event.motion.y;
        
     	if(event.type==SDL_MOUSEBUTTONDOWN)
       	{
         	if(event.button.button==SDL_BUTTON_LEFT)
           	{
             	x=event.button.x;
             	y=event.button.y;
               	
                  if((x>box.x)&&(x<box.x+box.w)&&(y>box.y)&&(y<box.y+box.h))
                    {
                      SDL_FreeSurface(MessagePong);
                      SDL_FreeSurface(MessageOnePlayer);
                      SDL_FreeSurface(MessageTwoPlayer);
                      game_state==1;
                    }
           	}
       	}
      }
};
 One_Player_Button::Show()
{
  apply_surface(box.x,box.y,MessageOnePlayer,screen);
};

class Two_Player_Button
{
  public:
  
  SDL_Rect box;
  Two_Player_Button(intx,int,y,intw,inth);
  void handle_events();
  void show();
}; 
 Two_Player_Button::Two_Player_Button(intx,inty,intw,inth)
{
  box.x=x;
  box.y=y;
  box.w=w;
  box.h=h;
};
 Two_Player_Button::handle_events()
{
  intx=320,y=240;
   
   if(event.type==SDL_MOUSEMOTION)
 	{
   	x=event.motion.x;
   	y=event.motion.y;
        
     	if(event.type==SDL_MOUSEBUTTONDOWN)
       	{
         	if(event.button.button==SDL_BUTTON_LEFT)
           	{
             	x=event.button.x;
             	y=event.button.y;
               	
                  if((x>box.x)&&(x<box.x+box.w)&&(y>box.y)&&(y<box.y+box.h))
                    {
                      SDL_FreeSurface(MessagePong);
                      SDL_FreeSurface(MessageOnePlayer);
                      SDL_FreeSurface(MessageTwoPlayer);
                      game_state==2;
                    }
           	}
       	}
      }
};
 Two_Player_Button::Show()
{
  apply_surface(box.x,box.y,MessageTwoPlayer,screen);
};

class Player_One_Bat
{
   public:

int x,y;
const float yVel;
int xVel;
void Player_One_Bat();
void Show();
void Player_One_Bat_Collision();
void Player_One_Bat_Handle_Input();
};

 Player_One_Bat::Player_One_Bat() :  randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
  x=10;
  y=215;
  xVel=0;
  yVel=2.5;
};

 Player_One_Bat::Show()
{
  apply_surface(x,y,Player_One_Bat,screen);
};

 Player_One_Bat::Player_One_Bat_Collision()
{
  if(Ball.x==x+=10)
    {
   	Ball.yVel+=||-= randomVelocity;
        
   	if(Ball.xVel<0)
     	{
       	Bal.xVel=3;
     	}
    }
  else if(Ball.x==x+=10)&&(SDLK_W)
    {
   	Ball.yVel-= randomVelocity;
        
   	if(Ball.xVel<0)
     	{
       	Ball.xVel=3;
     	}
    }
  else(Ball.x==x+=10)&&(SDLK_S)
    {
      Ball.yVel+=randomVelocity;
      
      if(Ball.xVel<0)
        {
          Ball.xVel=3;
        }
    }
};

 Player_One_Bat::Player_One_Bat_Handle_Input()
{
  if(event.type==SDL_KEYDOWN)
    { 
      switch(event.key.keysym.sym)
   	{
     	case SDLK_W:y-=yVel;break;
     	case SDLK_S:y+=yVel;break;
   	}
    }
};

class Cpu_Bat
{
   public:

int x,y;
const float yVel;
int xVel;
   
void Cpu_Bat();
void Show();
void Cpu_Bat_Collision();
void Cpu_Ai();
};

 Cpu_Bat::Cpu_Bat() :  randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
  x=620;
  y=215;
  xVel=0;
  yVel=2.5;
};

 Cpu_Bat::Show()
{
  apply_surface(x,y,Cpu_Bat,screen);
};

 Cpu_Bat::Cpu_Bat_Collision()
{
  if(Ball.x==x+=10)
    {
   	Ball.yVel+=||-= randomVelocity;
        
   	if(Ball.xVel==3)
     	{
       	Bal.xVel-=6;
     	}
    }
};

 Cpu_Bat::Cpu_Bat_Ai()
{ 
  if(y<Ball.y)
    {
      y+=Ball_Speed;
    }
  else(y>Ball.y)
    {
      y-=Ball_Speed;
    }
};

class Player_Two_Bat
{
   public:

int x,y;
const float yVel;
int xVel;
   
void Player_Two_Bat();
void Show();
void Player_Two_Bat_Collision();
void Player_Two_Bat_Handle_Input();
};

 Player_Two_Bat::Player_Two_Bat() :  randomNumber(rand()), randomVelocity((randomNumber%6)+1)
{
  x=620;
  y=215;
  xVel=0;
  yVel=2.5;
};

 Player_Two_Bat::Show()
{
  apply_surface(x,y,Player_Two_Bat,screen);
};

 Player_Two_Bat::Player_Two_Bat_Collision()
{
  if(Ball.x==x)
    {
   	Ball.yVel+=||-= randomVelocity;
        
   	if(Ball.xVel==3)
     	{
       	Bal.xVel-=6;
     	}
    }
  else if(Ball.x==x)&&(SDLK_UP)
    {
   	Ball.yVel-= randomVelocity;
        
   	if(Ball.xVel==3)
     	{
       	Ball.xVel-=6;
     	}
    }
  else(Ball.x==x)&&(SDLK_DOWN)
    {
      Ball.yVel+=randomVelocity;
      
      if(Ball.xVel==3)
        {
          Ball.xVel-=6;
        }
    }
};
 Player_Two_Bat::Player_Two_Bat_Handle_Input()
{
  if(event.type==SDL_KEYDOWN)
    { 
      switch(event.key.keysym.sym)
   	{
     	case SDLK_UP:y-=yVel;break;
     	case SDLK_DOWN:y+=yVel;break;
   	}
    }
};

apply_surface(intx,inty,SDL_Surface*source,SDL_Surface*destination)
{
  SDL_Rect offset;
  offset.x=x;
  offset.y=y;
  SDL_BlitSurface(source,NULL,destination,&offset);
};

SDL_Surface*load_image(string filename)
{
  SDL_Surface*loadedImage=NULL;
  SDL_Surface*optimizedImage=NULL;
  loadedImage=SDL_LoadBMP(filename.c_str());
   
   if(loadedImage!=NULL)
 	{
   	optimizedImage=SDL_DisplayFormat(loadedImage);
   	SDL_FreeSurface(loadedImage);
 	}
   	return optimizedImage;
};

int main (int argc, char*args [])
{
  if(SDL_Init(SDL_INIT_EVERYTHING)==-1)
    {
      return 1;
    }
SDL_Surface *screen=NULL;
SDL_Surface *background=NULL;
SDL_Surface *Player_One_Bat=NULL;
SDL_Surface *Cpu_Bat=NULL;
SDL_Surface *Player_Two_Bat=NULL;
SDL_Surface *MessagePong=NULL;
SDL_Surface *MessageOnePlayer=NULL;
SDL_Surface *messageTwoPlayer=NULL;
SDL_Surface *Message0=NULL;
SDL_Surface *Message1=NULL;
SDL_Surface *Message2=NULL;
SDL_Surface *Message3=NULL;
SDL_Surface *Message4=NULL;
SDL_Surface *Message5=NULL;
SDL_Surface *Message6=NULL;
SDL_Surface *Message7=NULL;
SDL_Surface *Message8=NULL;
SDL_Surface *Message9=NULL;
SDL_Surface *Message10=NULL;
SDL_Surface *MessageCongratsP1=NULL;
SDL_Surface *MessageCongratsCpu=NULL;
SDL_Surface *MessageCongratsP2=NULL;
SDL_Surface *Ball=NULL;


TTF_Font*font=NULL:
TTF_Font*font2=NULL;
 


SDL_Color textColor={255,255,255};
screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
   
   if(screen==NULL)
 	{
   	return 1;
 	}

   if(TTF_Init()==-1)
 	{
   	return false;
 	}

SDL_WM_SetCaption("Ping Pong",NULL);

background=load_image("background.bmp");
Player_One_Bat=load_image("paddle.bmp");
Player_Two_Bat=load_image("paddle.bmp");
Cpu_Bat=load_image("paddle.bmp");
Ball=load_image("ball.bmp");

   if(background==NULL)
 	{
   	return false;
 	}
   if(Player_One_Bat==NULL)
 	{
   	return false;
 	}
   if(Player_Two_Bat==NULL)
 	{
   	return false;
 	}
   if(Cpu_Bat==NULL)
 	{
   	return false;
 	}
   if(Ball==NULL)
 	{
   	return false;
 	}
font=TTF_OpenFont("EthnocentricRg-Regular",36);
font2=TTF_OpenFont("EthnocentricRg-Regular",28);
 	
   if(font==NULL)
 	{
   	return false;
 	}
   if(font2==NULL)
 	{
   	return false;
 	}

MessagePong=TTF_RenderText_Solid(font,"Ping Pong",textColor);
MessageOnePlayer=TTF_RenderText_Solid(font2,"One Player",textColor);
MessageTwoPlayer=TTF_RenderText_Solid(font2,"Two Player",textColor);
Message0=TTF_RenderText_Solid(font2,"0",textColor);
Message1=TTF_RenderText_Solid(font2,"1",textColor);
Message2=TTF_RenderText_Solid(font2,"2",textColor);
Message3=TTF_RenderText_Solid(font2,"3",textColor);
Message4=TTF_RenderText_Solid(font2,"4",textColor);
Message5=TTF_RenderText_Solid(font2,"5",textColor);
Message6=TTF_RenderText_Solid(font2,"6",textColor);
Message7=TTF_RenderText_Solid(font2,"7",textColor);
Message8=TTF_RenderText_Solid(font2,"8",textColor);
Message9=TTF_RenderText_Solid(font2,"9",textColor);
Message10=TTF_RenderText_Solid(font2,"10",textColor);
MessageCongratsP1=TTF_RenderText_Solid(font2,"Congratulations Player One",textColor);
MessageCongratsCpu=TTF_RenderText_Solid(font2,"Congratulations Cpu",textColor);
MessageCongratsP2=TTF_RenderText_Solid(font2,"Congratulations Player Two",textColor);
  
   if(MessagePong==NULL)
 	{
   	return 1;
 	}  
   if(MessageOnePlayer==NULL)
 	{
   	return 1;
 	}
   if(MessageTwoPlayer==NULL)
 	{
   	return 1;
 	}
   if(Message0==NULL)
 	{
   	return 1;
 	}
   if(Message1==NULL)
 	{
   	return 1;
 	}
   if(Message2==NULL)
 	{
   	return 1;
 	}
   if(Message3==NULL)
 	{
   	return 1;
 	}
   if(Message4==NULL)
 	{
   	return 1;
 	}
   if(Message5==NULL)
 	{
   	return 1;
 	}
   if(Message6==NULL)
 	{
   	return 1;
 	}
   if(Message7==NULL)
 	{
   	return 1;
 	}
   if(Message8==NULL)
 	{
   	return 1;
 	}
   if(Message9==NULL)
 	{
   	return 1;
 	}
   if(Message10==NULL)
 	{
   	return 1;
 	}
   if(MessageCongratsP1==NULL)
 	{
   	return 1;
 	}
   if(MessageCongratsCpu==NULL)
 	{
   	return 1;
 	}
   if(MessageCongratsP2==NULL)
 	{
   	return 1;
 	}  

SDL_Event event;

 

    while(quit=false)
     	{
       	fps.start();

       	if(game_state==0)
         	{
           	apply_surface(0,0,background,screen);
           	apply_surface(310,10,MessagePong,screen);
           	Player_One_Button.Show();
           	Player_Two_Button.Show();
              }
       	else if(game_state==1)
                  {
                    Ball.Ball_Track();
                    Ball.Ball_Wall_Collision();
                    Cpu_Bat.Cpu_Bat_Ai();
                    Cpu_Bat.Cpu_Bat_Collision();
                    Player_One_Bat.Player_One_Bat_Collision();

                    Ball.Show_Ball();
                    Player_One_Bat.Show();
                    Cpu_Bat.Show();
                    apply-surface(30,10,Message0,screen);
                    apply-surface(600,10,Message0,screen);
                    }
       	else if(game_state==2)
                  {
                    Ball.Ball_Two_Player_Track();
                    Ball.Ball_Wall_Collision();
                    Player_One_Bat.Player_One_Bat_Collision();
                    Player_Two_Bat.Player_Two_Bat_Collision();
         	
                    Ball.Show_Ball();
                    Player_One_Bat.Show();
                    Player_Two_Bat.Show();
                    apply-surface(30,10,Message0,screen);
                    apply-surface(600,10,Message0,screen);
               	}
    while(SDL_PollEvent(&event))
     	{
       	if(game_state==0)
         	{
           	One_Player_Button.handle_events();
           	Two_Player_Button.handle_events();
             	
           	if(event.type==SDL_QUIT)
             	{
               	quit=true;
             	}
         	}
       	else if(game_state==1)
           	{
             	Player_One_Bat.Player_One_Bat_Handle_Input();
 	
             	if(event.type==SDL_QUIT)
             	{
               	quit=true;
             	}
           	}
       	else(game_state==2)
           	{
             	Player_One_Bat.Player_One_Bat_Handle_Input();
             	Player_Two_Bat.Player_Two_Bat_Handle_Input();
 
                if(event.type==SDL_QUIT)
             	{
               	quit=true;
             	}
                }
          }
        if(SDL_Flip(screen)==-1)
          {
            return 1;
          }
       	frame++;

        if((cap==true)&&(fps.get_ticks()<1000/FRAMES_PER_SECOND))
          {
            SDL_Delay((1000/FRAMES_PER_SECOND)-fps.get_ticks());
          }
          
   	}
   SDL_FreeSurface(background);
   
   return 0;
}


What is the problem, it won't compile?

Sponsor:

#2 JoeCooper   Members   -  Reputation: 338

Like
2Likes
Like

Posted 27 May 2011 - 01:06 PM

Big code! I think there's a code tag or something. Let's have a look. Also I just drank a ton of whiskey before sitting down so if I type something stupid, sorry in advance.

The compiler should tell you why it doesn't compile, please add what the compiler is telling you.

But when I read through it myself, the first thing is you say "Const". C++ is case sensitive. This means that "Const" is different from "const". Trawl through your code and see how many times you make a case mistake. C++ will throw a fit.

Also generally speaking I recommend C# as a starter language but if you're not in the mood to change, don't worry about it; not a big deal.

I'm a little concerned with all the if 1 do this, if 2 do that, if 3 yada yada type code and the Message1 Message2 etc. declarations.

Are you familiar with the Standard Templates?

That means vectors, maps, etc.

Vectors, maps and arrays and so on are "containers" and are absolutely crucial concepts before you can build a bigger program.

See if you can edit that post with the code tag or whatever it's called now (GDnet got all rejigged back in January or something and I'm totally lost) and also tell us what the compiler is spittin' out and what tool chain you're using (what compiler vendor, platform, etc.)

#3 BeerNutts   Crossbones+   -  Reputation: 3594

Like
2Likes
Like

Posted 27 May 2011 - 01:14 PM

Aggggh, my eyes!

Put code bracers around that, and correct your typos! And explain what you're doing, what you're seeing, and any ideas you might have.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#4 Wooh   Members   -  Reputation: 875

Like
0Likes
Like

Posted 27 May 2011 - 01:18 PM

As previously mentioned that code looks terrible, no indentation. Use code tags, or attach your code file, or post your code at ideone.com. No one want's to read your code as it is now.

intrandomNumber=rand();
intrandomVelocity(randomNumber%6)+1;
You need a space (or any whitespace character) between int and the variable name. You need to use = when giving a value to randomVelocity.

#5 GregMichael   Members   -  Reputation: 143

Like
0Likes
Like

Posted 27 May 2011 - 01:25 PM

My eyes bleed as well.

It would help to know what errors you are getting...as mentioned previously using "const" instead of "Const" will be a good starting point.

#6 Prefect   Members   -  Reputation: 373

Like
2Likes
Like

Posted 27 May 2011 - 01:27 PM

Hate to say this, but what's wrong with the code is that it is perfect TheDailyWTF-material.

In particular, use an array instead of 10 variables Message0 to Message9, and use loops to setup the array. Then use array indexing instead of endless if-else-chains to access the messages that are stored there. (You might also consider rendering those strings on the fly, but that's not really the point.)

As far as your original question is concerned: it is rather impolite (and inefficient) to simply dump a huge amount of code somewhere, say "it doesn't compile", and ask "what is the problem?". When your code doesn't compile, your compiler gives you an error message. What is the error message? Which line of code is referenced by it? Do you understand the error message? Why not? Can you isolate the relevant parts of the code to generate a smaller example that exhibits the problem? Then post that smaller sample, since it makes it easier for people who want to help you.
Widelands - laid back, free software strategy

#7 JoeCooper   Members   -  Reputation: 338

Like
0Likes
Like

Posted 27 May 2011 - 01:42 PM

Oh I gotta add about him just dumping it here that he asked me in another thread if I'd take a look at it and I said yessir.

#8 KingofSwing94   Members   -  Reputation: 100

Like
0Likes
Like

Posted 27 May 2011 - 03:56 PM

I didn't know how to put in a code snippet or whatever you said. it does have indentations in notepad, i am using Bloodshed Dev C++ with SDL, the message's are a font and text. sorry about the trash code, do u think its good for someone who is 16 and is quite new to programming?

#9 JoeCooper   Members   -  Reputation: 338

Like
1Likes
Like

Posted 27 May 2011 - 04:09 PM

I didn't know how to put in a code snippet or whatever you said.


[ c o d e ]
code goes here
[ / c o d e ]

(Delete the spaces between the letters in the brackets.)

i am using Bloodshed Dev C++ with SDL


I'm not familiar with that... No comment.

sorry about the trash code, do u think its good for someone who is 16 and is quite new to programming?


Never mind your age; lots of folks start around 8 like I did.

And you accidentally typed 'o' for '0' in at least one spot which is pretty funny.

But other than that you're using things like initialization lists and such and that's good. You're learning C++, which is a monster language!

But basically it's like this, there's lots and lots of little problems and even once you get it to compile, you'll find it doesn't work.

Also, C++ doesn't do multiple assignments. So you have some code up top like:

int a,b,c=0;

Or something. You might think that this will assign 0 to a, b and c, but it will only assign to c. a and b will be left unassigned.

Just a reminder, never assume that an unassigned variable will be 0, NULL or false. The compiler will do whatever it darn well pleases. It may or may not automatically assign to 0 depending on compiler and compiler settings. We call this "undefined behavior".

But back to the programming, have you written simpler programs and are you familiar with the standard templates? Let's work those out first.

I suspect you have a brain for programming (unless this is a whole lot of copy and paste) but you need to learn the basic tools including maps and vectors.

#10 exoity   Members   -  Reputation: 208

Like
0Likes
Like

Posted 27 May 2011 - 04:10 PM

My first suggestion is to not use Dev-C++, please read this article for why http://www.jasonbadams.net/20081218/why-you-shouldnt-use-dev-c/

It would also be helpful to post the error/warning logs so we can see what errors you are getting

#11 JoeCooper   Members   -  Reputation: 338

Like
0Likes
Like

Posted 27 May 2011 - 04:31 PM

I added you (King) on MSN, fire it up if you want help with anything.

By the way, I did the same if 1 then ... if 2 then ... etc. thing once when I was new and using Visual Basic. I didn't realize you could reuse file handles >_<

#12 rip-off   Moderators   -  Reputation: 9788

Like
2Likes
Like

Posted 27 May 2011 - 04:52 PM

Some things to consider:
  • There are lots of problems with the current code. This is because you've done too much without getting stuff working. I recommend you start smaller and build the game up in pieces. Comment out most of the code, and get a basic game loop running. Then get one paddle working, then both. Put the ball in. Make the ball react to the paddles. Add the scoring system.
  • Things like images are generally loaded at startup and freed at shutdown. Freeing them throughout the game is probably a bad idea, it might lead to crashes if it turns out you do need those images later on. At the very least set the pointers to NULL so you can detect that they are unloaded.
  • Don't use SDL_Delay() for long periods of time. Instead use a timer value such as SDL_GetTicks() and transition when the ticks have passed the threshold. This is important because your application should consume events otherwise the OS will think it is hung, plus if the user tries to exit the program while you are Delaying it will not respond.
  • Use named constants or enumerations instead of magic numbers. For example: gameState == SINGLE_PLAYER is much more meaningful than gameState == 1.
  • The two Button classes can actually be merged. They are almost identical, extract the differences into member data, set via the constructor. The two Paddle classes can also be merged, but this is a slightly more complex task because you'll need to separate the "controller" (e.g. the player or the AI) from the Paddle logic. It is a good thing to separate them though.
  • Don't return "false" or "true" inside main(). Remember that this is allowed because bool is technically an integral type. The convention for main() is to return 0 for "success" and a non-zero value for "failure". Also consider adding some form of logging so that if the game fails to start due to missing resources you can see this quickly from the log rather than having to fire up the debugger.
  • With a little bit of effort, you can make all your global variables into locals. Using globals like that is just about manageable in small games, but it simply doesn't scale. Some tricks you can do to help this is to invert the logic. For example, instead of updating the score in the middle of the Ball collision code, have the Ball return a value (e.g. enum CollisionResult { NO_COLLISION, LEFT_WALL, RIGHT_WALL }) and let main() update the variable.


#13 vleugel   Members   -  Reputation: 252

Like
1Likes
Like

Posted 28 May 2011 - 11:08 AM

Pfff there are a lot of things wrong or bad in your code. I don't have time to look through all the lines, but except for the 'o' and the uninitialized variables I also noted your are using rand() without setting a seed first. rand() generates a random number based on a 'seed' using some kind of algorithm. The seed should be set before using rand() and usually something like the time in milliseconds since the start of the program is used. If you don't set a seed you are getting the same number over and over again.

This has nothing to do with not compiling, but is just something I noticed reading your code.


Next time it might be a good idea to compile your game a little more often. That way you can spot errors more easily and it is easier to debug.
Right now I would suggest commenting away lots of code, make whole functions blank by putting /* and */ in front and at the end of the code.
Remove as much code until your code finaly compiles and than slowly start re-adding the code. (edit: seems someone already posted my great idea...frustrating :) )


#14 Trienco   Crossbones+   -  Reputation: 2427

Like
0Likes
Like

Posted 29 May 2011 - 12:47 AM

If you don't set a seed you are getting the same number over and over again.


I take it you meant "numberS" over and over which isn't such a bad thing and can be vital for debugging. I'd even go as far as suggesting to always set a constant seed (or none at all, which according to the srand documention is the same as srand(1) ) for debugging builds.
f@dzhttp://festini.device-zero.de

#15 metorsummoner7   Members   -  Reputation: 124

Like
0Likes
Like

Posted 02 June 2011 - 08:50 AM

You might want to consider breaking your code down into a few more cpp and header files. You might be better off using Visual Studio Express, I think that would be a better IDE for you. Organize your code and build your project often while you code. Work with one thing at a time and make sure it works and complies before moving on to the next. thing.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS