YourFace
<span style="color:#9C9C9C;"><strong>Runner Up - T
- Reaction score
- 91
Code:
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <vector>
#include <cstdlib>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 20;
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;
SDL_Surface* screen = NULL;
SDL_Surface* dot = NULL;
//direction of snake
const int DUP = 0;
const int DDOWN = 1;
const int DRIGHT = 2;
const int DLEFT = 3;
bool Game_Over = false;
std::vector<SDL_Rect>TurnLocation;
std::vector<int>TurnDirection;
SDL_Event event;
//the thing getting objects to grow its tail
class Snake
{
private:
SDL_Rect box;
SDL_Rect last_turn;
int num_follower;
int direction;
bool dChangeable;
public:
Snake();
void move();
void change_direction();
void show();
int get_direction();
int get_follower();
SDL_Rect get_location();
bool Snake_Death(SDL_Rect fbox);
void increase_follower();
};
class Timer
{
private:
int startTicks;
int pauseTicks;
bool started;
bool paused;
public:
Timer();
int get_ticks();
void start();
void stop();
void pause();
void unpause();
bool is_started();
bool is_paused();
};
class Follower
{
private:
SDL_Rect fbox;
int direction;
bool lastfollower;
int followers;
public:
void make_last_follower();
void set_last_follower(Follower *last_follower);
void show();
void follow();
void set_direction();
SDL_Rect follower_location();
Follower(Snake *mySnake, std::vector<Follower> myFollower);
~Follower();
};
class Food
{
private:
SDL_Rect food_location;
public:
Food();
void change_location();
void food_show();
SDL_Rect food_get_location();
};
SDL_Surface* load_image(std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
if(optimizedImage != NULL)
{
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF,0xFF));
}
}
return optimizedImage;
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
srand(SDL_GetTicks());
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if( screen == NULL)
{
return false;
}
SDL_WM_SetCaption("GARRY ROCKSS!", NULL);
return true;
}
bool load_files()
{
dot = load_image("dot.bmp");
if(dot == NULL)
{
return false;
}
return true;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( dot, clip, screen, &offset);
}
void clean_up(std::vector<Follower> *theFollower)
{
SDL_FreeSurface(dot);
theFollower->clear();
TurnLocation.clear();
TurnDirection.clear();
SDL_Quit();
}
bool check_collision(SDL_Rect A, SDL_Rect B)
{
int ALeft, ARight;
int AUp, ADown;
int BLeft, BRight;
int BUp, BDown;
ALeft = A.x;
ARight = A.x + A.w;
AUp = A.y;
ADown = A.y + A.h;
BLeft = B.x;
BRight = B.x +B.w;
BUp = B.y;
BDown = B.y + B.h;
if ( ALeft> BRight)
{
return false;
}
if ( ARight< BLeft)
{
return false;
}
if( AUp > BDown)
{
return false;
}
if(ADown <BUp)
{
return false;
}
return true;
}
//THIS IS WHERE THE SNAKE CODE BEGINS
//THIS IS WHERE THE SNAKE CODE BEGINS
//THIS IS WHERE THE SNAKE CODE BEGINS
Snake::Snake()
{
last_turn.x = 0;
last_turn.y = 0;
box.x = 0;
box.y = 0;
direction = DDOWN;
num_follower = 0;
dChangeable = false;
}
void Snake::increase_follower()
{
num_follower++;
}
void Snake::move()
{
if((box.x < 0) || (box.y < 0) || (box.x + DOT_WIDTH > SCREEN_WIDTH) ||(box.y + DOT_HEIGHT >SCREEN_HEIGHT))
{
Game_Over = true;
}
else
{
switch(direction)
{
case DUP: box.y -= DOT_HEIGHT; break;
case DDOWN: box.y += DOT_HEIGHT; break;
case DRIGHT: box.x += DOT_WIDTH; break;
case DLEFT: box.x -= DOT_WIDTH; break;
}
last_turn = get_location();
TurnLocation.push_back(get_location());
TurnDirection.push_back(get_direction());
}
}
void Snake::change_direction()
{
dChangeable = false;
if(event.type == SDL_KEYDOWN)
{
SDL_Rect* p2last_turn = &last_turn;
if(p2last_turn != NULL)
{
if(box.x>last_turn.x + DOT_WIDTH)
{
dChangeable = true;
}
if( box.y>last_turn.y + DOT_HEIGHT)
{
dChangeable = true;
}
if(box.x < last_turn.x - DOT_WIDTH)
{
dChangeable = true;
}
if(box.y < last_turn.y - DOT_HEIGHT)
{
dChangeable = true;
}
}
else if(p2last_turn == NULL)
{
dChangeable = true;
}
if (dChangeable == true)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
if(direction == DDOWN)
{
break;
}
else
{
direction = DUP; break;
}
case SDLK_DOWN:
if(direction == DUP)
{
break;
}
else{
direction = DDOWN; break;
}
case SDLK_RIGHT:
if(direction == DLEFT)
{
break;
}
else
{
direction = DRIGHT; break;
}
case SDLK_LEFT:
if(direction == DRIGHT)
{
break;
}
else
{
direction = DLEFT; break;
}
}
}
}
}
void Snake::show()
{
apply_surface(box.x, box.y, dot, screen);
}
int Snake::get_direction()
{
return direction;
}
int Snake::get_follower()
{
return num_follower;
}
bool Snake::Snake_Death(SDL_Rect fbox)
{
if((box.x <= 0) || (box.x >= SCREEN_WIDTH))
{
Game_Over = true;
return true;
}
if((box.y <= 0) ||(box.y >= SCREEN_HEIGHT))
{
Game_Over = true;
return true;
}
if(check_collision(box, fbox) == true)
{
Game_Over = true;
return true;
}
return false;
}
SDL_Rect Snake::get_location()
{
return box;
}
//THIS IS WHERE THE FOLLOWER CODE BEGINS
Follower::Follower(Snake *mySnake, std::vector<Follower> myFollower)
{
if(mySnake->get_follower() == 0)
{
fbox = mySnake->get_location();
switch(mySnake->get_direction())
{
case DUP:
fbox.y +=DOT_HEIGHT; break;
case DDOWN:
fbox.y -=DOT_HEIGHT; break;
case DRIGHT:
fbox.x -=DOT_HEIGHT; break;
case DLEFT:
fbox.y +=DOT_HEIGHT; break;
}
}
else
{
int i;
i = myFollower.size();
i--;
direction = TurnDirection.back();
switch(direction)
{
case DUP:
fbox.x = myFollower[i].fbox.x;
fbox.y = myFollower[i].fbox.y + DOT_HEIGHT;
break;
case DDOWN:
fbox.x = myFollower[i].fbox.x;
fbox.y = myFollower[i].fbox.y-DOT_HEIGHT;
case DRIGHT:
fbox.x = myFollower[i].fbox.x-DOT_WIDTH;
fbox.y = myFollower[i].fbox.y;
case DLEFT:
fbox.x = myFollower[i].fbox.x + DOT_WIDTH;
fbox.y = myFollower[i].fbox.y;
}
}
lastfollower = true;
mySnake->increase_follower();
}
Follower::~Follower()
{
lastfollower = false;
followers = 0 ;
direction = -1;
fbox.x = -1;
fbox.y = -1;
}
void Follower::set_last_follower(Follower *last_follower)
{
last_follower->lastfollower = false;
}
void Follower::show()
{
apply_surface(fbox.x,fbox.y, dot, screen);
}
void Follower::follow()
{
if(direction == DUP)
{
fbox.y -= DOT_HEIGHT;
}
if(direction == DDOWN)
{
fbox.y += DOT_HEIGHT;
}
if(direction == DRIGHT)
{
fbox.x +=DOT_WIDTH;
}
if(direction == DLEFT)
{
fbox.x -=DOT_WIDTH;
}
}
void Follower::set_direction()
{
if(TurnLocation.size() != 0)
{
for(int i = 0; i<TurnLocation.size(); i++)
{
if((fbox.x == TurnLocation[i].x) &&(fbox.y == TurnLocation[i].y))
{
direction = TurnDirection[i];
if(lastfollower == true)
{
TurnLocation.erase(TurnLocation.begin() + i);
TurnDirection.erase(TurnDirection.begin() + i);
}
}
}
}
}
void Follower::make_last_follower()
{
lastfollower = true;
}
SDL_Rect Follower::follower_location()
{
return fbox;
}
Timer::Timer()
{
startTicks = 0;
pauseTicks = 0;
bool started = false;
bool paused = false;
}
void Timer::start()
{
started = true;
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
started = false;
paused = false;
}
void Timer::pause()
{
paused = true;
pauseTicks = SDL_GetTicks() - startTicks;
}
void Timer::unpause()
{
if(paused == true)
{
paused = false;
startTicks = SDL_GetTicks() - pauseTicks;
}
}
int Timer::get_ticks()
{
if((started == true) && (paused == true))
{
return pauseTicks;
}
else if(started == true)
{
return SDL_GetTicks() - startTicks;
}
}
Food::Food()
{
food_location.x = rand() % SCREEN_WIDTH;
food_location.y = rand() % SCREEN_HEIGHT;
}
void Food::change_location()
{
food_location.x = rand() % SCREEN_WIDTH;
food_location.y = rand() % SCREEN_HEIGHT;
}
void Food::food_show()
{
apply_surface(food_location.x, food_location.y, dot, screen);
}
SDL_Rect Food::food_get_location()
{
return food_location;
}
int main(int argc, char* args[])
{
bool quit = false;
if(init() == false)
{
return 1;
}
if(load_files() == false)
{
return 1;
}
Timer fps;
Snake theSnake;
Food theFood;
std::vector<Follower> theFollower;
while((quit == false) &&(Game_Over == false))
{
fps.start();
while(SDL_PollEvent(&event))
{
theSnake.change_direction();
if(event.type == SDL_QUIT)
{
quit = true;
Game_Over = true;
}
}
if(check_collision(theSnake.get_location(), theFood.food_get_location()) == true)
{
Follower newFollower(&theSnake,theFollower);
theFollower.push_back(newFollower);
for(int i = 0; i<theFollower.size(); i++)
{
theFollower[i].set_last_follower(&theFollower[i]);
}
int b = theFollower.size() - 1;
theFollower[b].make_last_follower();
theFood.change_location();
}
theSnake.move();
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF,0xFF,0xFF));
theSnake.show();
theFood.food_show();
if(theFollower.size() != 0)
{
for(int i = 0; i<theFollower.size(); i++)
{
if(theSnake.Snake_Death(theFollower[i].follower_location()) == true)
{
quit = true;
}
theFollower[i].follow();
theFollower[i].show();
}
}
if(fps.get_ticks() < 1000/FRAMES_PER_SECOND)
{
SDL_Delay((1000/FRAMES_PER_SECOND) - fps.get_ticks());
}
}
clean_up(&theFollower);
return 0;
}