Navigation

Pong Clone

#include <SDL/SDL.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

class CObject
{public:
  CObject(float x,float y,float vx,float vy):m_X(x),m_Y(y),m_VX(vx),m_VY(vy){};
  ~CObject(){};
  float m_X,m_Y;
  float m_VX,m_VY;
};

vector<CObject *> Objects;
int BSize=10;
float Pad1=0,Pad2=0,Y1=480-(BSize*3),Y2=BSize*2;
int Siz1=100,Siz2=100;
bool On=true;
float OpSpeed=0,OpMaxS=10,OpRate=0.01,OpGoal=0;
float BSpeed=0.3;

void Init();
void Input();
void Update();
void Draw();
void Exit();
void DrawRect(int x,int y,int sx,int sy,int r,int g,int b);

void Order();

int main()
{
  Init();
  while(On)
  {
    Input();
    Update();
    Draw();
  };
  Exit();
  return 0;
};

void Init()
{
  int i,j;
  SDL_Init(SDL_INIT_VIDEO);
  SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  atexit(Exit);
  atexit(SDL_Quit);
  //SDL_WM_GrabInput(SDL_GRAB_ON);
  //SDL_ShowCursor(SDL_DISABLE);
  for(i=0;i<4;i++)
    Objects.push_back(new CObject(640/2,480/2,0,0.1));
};
void Input()
{
  SDL_Event e;
  int i;
  while(SDL_PollEvent(&e))
    switch(e.type)
    {
    case SDL_QUIT:
      On=false;
      break;
    };
  SDL_GetMouseState(&i,NULL);
  Pad1=i;
};
void Update()
{
  int i,j,k;
  float l;
  bool fx,fy;
  for(i=0;i<Objects.size();i++)
    {
      fx=fy=false;
      if((Objects[i]->m_X+BSize)-Pad1>0&&Objects[i]->m_X<Pad1+Siz1&&(Objects[i]->m_Y+BSize)-Y1>0&&Objects[i]->m_Y<Y1+BSize)
    if(Objects[i]->m_VY>0)
      {
        fy=true;
        Objects[i]->m_VX+=((rand()%10)/10.0f)-0.5f;
        Objects[i]->m_VY=0.1f+((rand()%10)/10.0f);
      };
      if((Objects[i]->m_X+BSize)-Pad2>0&&Objects[i]->m_X<Pad2+Siz2&&(Objects[i]->m_Y+BSize)-Y2>0&&Objects[i]->m_Y<Y2+BSize)
    if(Objects[i]->m_VY<0)
      {
        fy=true;
        Objects[i]->m_VX+=((rand()%10)/10.0f)-0.5f;
        Objects[i]->m_VY=-(0.1f+((rand()%10)/10.0f));
      };
      if(Objects[i]->m_X<0)
    fx=true;
      if(Objects[i]->m_X>640-BSize)
    fx=true;
      if(Objects[i]->m_Y<0)
    fy=true;
      if(Objects[i]->m_Y>480-BSize)
    fy=true;
      if(fx)
    Objects[i]->m_VX=-Objects[i]->m_VX;
      if(fy)
    Objects[i]->m_VY=-Objects[i]->m_VY;
      l=sqrt(pow(Objects[i]->m_VX,2)+pow(Objects[i]->m_VY,2));
      Objects[i]->m_VX=(Objects[i]->m_VX/l)*BSpeed;
      Objects[i]->m_VY=(Objects[i]->m_VY/l)*BSpeed;
      Objects[i]->m_X+=Objects[i]->m_VX;
      Objects[i]->m_Y+=Objects[i]->m_VY;
      if(Objects[i]->m_Y>480-BSize||Objects[i]->m_Y<0)
    {
      delete(Objects[i]);
      Objects.erase(Objects.begin()+i);
      i--;
      j=(rand()%4==0?10:0);
      if(j==0&&Objects.size()==0)
        j=4;
      if(Objects.size()<10)
        for(k=0;k<j;k++)
          Objects.push_back(new CObject(640/2,480/2,0,-0.1));
    };
    };
  Order();
  OpGoal=(640/2)-(Siz2/2);
  for(i=0;i<Objects.size();i++)
    if(Objects[i]->m_VY<0&&Objects[i]->m_Y>Y2+BSize/5)
      {
    OpGoal=Objects[i]->m_X-(Siz2/2);
    break;
      };
  if(OpGoal<Pad2)
    {
      if(OpSpeed>0)
    OpSpeed=0;
      OpSpeed-=OpRate;
    };
  if(OpGoal>Pad2)
    {
      if(OpSpeed<0)
    OpSpeed=0;
      OpSpeed+=OpRate;
    };
  if(OpGoal==Pad2)
    OpSpeed=0;
  if(abs(OpSpeed)>OpMaxS)
    if(OpSpeed>0)
      OpSpeed=OpMaxS;
    else
      OpSpeed=-OpMaxS;
  Pad2+=OpSpeed;
};
void Draw()
{
  int i,j;
  DrawRect(0,0,640,480,0,0,128);
  for(i=0;i<Objects.size();i++)
    {
      if(i!=0)
    DrawRect(Objects[i]->m_X,Objects[i]->m_Y,BSize,BSize,255,255,255);
      else
    DrawRect(Objects[i]->m_X,Objects[i]->m_Y,BSize,BSize,0,0,255);
    };
  DrawRect(Pad1,Y1,Siz1,BSize,255,255,255);
  DrawRect(Pad2,Y2,Siz2,BSize,255,255,255);
  SDL_Flip(SDL_GetVideoSurface());
};

void Exit()
{
  while(Objects.size()>0)
    {
      delete(Objects[0]);
      Objects.erase(Objects.begin());
    };
  SDL_ShowCursor(SDL_ENABLE);
  SDL_WM_GrabInput(SDL_GRAB_OFF);
};

void DrawRect(int x,int y,int sx,int sy,int r,int g,int b)
{
  SDL_Color c;
  SDL_Rect re;
  re.x = x;
  re.y = y;
  re.w = sx;
  re.h = sy;
  SDL_FillRect(SDL_GetVideoSurface(),&re,SDL_MapRGB(SDL_GetVideoSurface()->format,r,g,b));
};

void Order()
{
  CObject *o;
  bool d;
  int i,c=0;
  d=true;
  while(d)
    {
      d=false;
      for(i=1;i<Objects.size();i++)
    if(Objects[i-1]->m_Y>Objects[i]->m_Y)
      {
        d=true;
        o=Objects[i];
        Objects.erase(Objects.begin()+i);
        Objects.insert(Objects.begin()+(i-1),o);
      };
      c++;
      if(c==1000)
    return;
    };
};