#include "SDL/SDL.h" #include #include "math.h" #include "SDL/SDL_ttf.h" #include "SDL/SDL_image.h" #include "iostream" //The attributes of the screen const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 400; const int SCREEN_BPP = 32; using std::cout; //The surfaces that will be used SDL_Surface *message = NULL; SDL_Surface *background = NULL; SDL_Surface *screen = NULL; SDL_Surface *point = NULL; SDL_Surface *text = NULL; TTF_Font *font = NULL; SDL_Color textColor = { 255, 255, 255 }; SDL_Event event; SDL_Surface *load_image( std::string filename ) { SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = SDL_LoadBMP( filename.c_str() ); //If nothing went wrong in loading the image if( loadedImage != NULL ) { //Create an optimized image optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old image SDL_FreeSurface( loadedImage ); } //Return the optimized image return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Make a temporary rectangle to hold the offsets SDL_Rect offset; //Give the offsets to the rectangle offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface( source, NULL, destination, &offset ); } //move int move(int direction,int length, int wormX[], int wormY[], int speed) { } 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 an error in setting up the screen if( screen == NULL ) { return 1; } if( TTF_Init() == -1 ) { return false; } font = TTF_OpenFont( "VeraMono.ttf", 12 ); if( font == NULL ) { cout << "Could not find font..."; return false; } //Set the window caption SDL_WM_SetCaption( "LeSnake", NULL ); //Load the images message = load_image( "square.bmp" ); background = load_image( "background.bmp" ); point = load_image("point.bmp"); text = TTF_RenderText_Solid( font, "Points:", textColor ); //Apply the background to the screen int wormX[100]; int wormY[100]; wormX[0] = 100; wormY[0] = 100; int speed = 50; int jump = 10; int length = 6; int pointx = rand() % 64; int pointy = rand() % 40; int new_direction; int direction = 1; int bounces = 2; int runs; int quit=0; while(!quit) { //If there's an event to handle if( SDL_PollEvent( &event ) ) { if( event.type == SDL_KEYDOWN ) { switch( event.key.keysym.sym ) { case SDLK_ESCAPE:quit=1;break; case SDLK_UP: new_direction=0; break; case SDLK_DOWN: new_direction=1; break; case SDLK_LEFT: new_direction=2; break; case SDLK_RIGHT: new_direction=3; break; } } if(direction == 0 && new_direction == 1) { direction = 0; } else if (direction == 1 && new_direction == 0) { direction = 1; } else if (direction == 2 && new_direction == 3) { direction = 2; } else if (direction == 3 && new_direction == 2) { direction = 3; } else { direction = new_direction; } } else if( event.type == SDL_QUIT ) { quit = 1; } for(int counter=length; counter>0; counter--) { wormX[counter] = wormX[counter-1]; wormY[counter] = wormY[counter-1]; } if(direction == 1) { wormY[0]+=jump; } else if(direction == 0) { wormY[0]-=jump; } else if(direction == 2) { wormX[0]-=jump; } else { wormX[0]+=jump; } if(wormX[0] < 0 || wormX[0] > 640) { quit=1; } if(wormY[0] < 0 || wormY[0] > 400) { quit=1; } apply_surface( 0, 0, background, screen ); //Apply the message to the screen //move(direction,length,&wormX[],&wormY[],speed); for(int counter=0; counter