All pastes #439275 Raw Edit

Simple TRON clone

public text v1 · immutable
·published 2007-04-14 03:40 UTC
rendered paste body
#include "sdl_init.h"

/*    Simple TRON clone

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

class TCar {
 private:
  int direction;
  void handle_input();
  void draw();
  int x,y;
  std::vector<SDL_Rect> rects;
 
 public:
  TCar() {
   direction = 1;
   x = 0;
   y = 0;
  }
  void go() {
   handle_input();
   draw();
  }
};
  
void TCar::handle_input() {
 //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 = true; 
  }
 
  //If a key was pressed 
  if( event.type == SDL_KEYDOWN ) {
   switch( event.key.keysym.sym ) {
     case SDLK_UP: direction = 1; break;
     case SDLK_DOWN: direction = 2; break;
     case SDLK_LEFT: direction = 3; break;
     case SDLK_RIGHT: direction = 4; break;
   }
  }
 }
}; 
void TCar::draw() {
 for(int i=0; i<rects.size(); i++) {
  SDL_FillRect(rects[i],screen,255);
 }
}

void TCar::go() {
 hande_input();
 draw(); 
 SDL_Rect lol;

 if(direction == 1) // Up
  y+-30;
 if(direction == 2) // Down
  y+=30;
 if(direction == 3) // Left
  x-=30;
 if(direction == 4) // Right
  x+=30;
 
 lol.x = x;
 lol.y = y;
 lol.w = 30;
 lol.h = 30;
 rects.push_back(lol);
}

class TGame {
 private: 
  TCar *player;
  
 public:
  void run();
  TGame() {
   player = new TCar();
  }
};

void TGame::run() {
 player->go();
}

int main( int argc, char* args[] ) {

 // Initialize all SDL subsystems
 if(SDL_Init(SDL_INIT_EVERYTHING) == -1) { return 1; }

 // Set up the screen 
 screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

 // If there was in error in setting up the screen 
 if( screen == NULL ) { return 1; }

 // Disable key repeating
 SDL_EnableKeyRepeat(0,0);

 // Set the window title
 SDL_WM_SetCaption(" T R O N ",NULL);

 TGame game = new TGame(); 

 while(!quit) { // While we havn't quit
  // blackify the scren 
  SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x0, 0x0, 0x0 ) );

  game->run(); // Go

  if( SDL_Flip( screen ) == -1 ) { return 1; } // redraw screen
 }
}