Hello guys,
I've tried making simple snake game with SDL 2, but..The code doesn't work in Visual Studio, I mean when I compile it, there is only black screen, but when I compile it in Code::Blocks, there is other result..Any ideas?
#include <iostream>
#include <SDL.h>
#include <deque>
using namespace std;
static const int WINDOW_WIDTH = 800;
static const int WINDOW_HEIGHT = 600;
static const int GO_UP = 1;
static const int GO_LEFT = 2;
static const int GO_RIGHT = 3;
static const int GO_DOWN = 4;
static const int CELL_SIZE = 20;
static const int CELL_WIDTH = WINDOW_WIDTH / CELL_SIZE;
static const int CELL_HEIGHT = WINDOW_HEIGHT / CELL_SIZE;
static const int START_X = CELL_WIDTH / 2;
static const int START_Y = CELL_HEIGHT / 2;
static const int START_LENGTH = 3;
bool b[4] = { 0, 0, 0, 0 };
class SnakeParts {
public:
int x, y;
SnakeParts(int x, int y)
{
this->x = x;
this->y = y;
}
};
class Food {
public:
int x, y;
Food()
{
newFood();
}
void newFood()
{
x = 1 + rand() % (CELL_WIDTH - 2);
y = 1 + rand() % (CELL_HEIGHT - 2);
}
};
class Snake {
public:
Snake(int headColor, int bodyColor, int appleColor)
{
this->headColor = headColor;
this->bodyColor = bodyColor;
this->appleColor = appleColor;
restart();
}
~Snake()
{
snakeParts.clear();
}
void restart()
{
snakeParts.clear();
for(int i = 0; i < START_LENGTH; i++)
{
addPart(START_X - i, START_Y);
direction = GO_RIGHT;
time = 0;
timeOut = 6;
eaten = false;
}
}
void update()
{
if(eaten) { return; };
updateInputControls();
time++;
if(time < timeOut)
{
return;
}
time = 0;
if(isCollidingWithWall() == true || isCollidingWithSelf() == true) {
eaten = true;
}
if(isCollidingWithApple() == true) {
apple.newFood();
}
else
{
snakeParts.pop_back();
}
moveSnake();
}
void render(SDL_Surface* screen)
{
if(eaten) { return; }
renderRectangle.x = CELL_SIZE;
renderRectangle.y = CELL_SIZE;
renderApple(screen);
renderSnake(screen);
}
inline bool isEaten() { return eaten; }
private:
deque<SnakeParts> snakeParts;
Food apple;
bool eaten;
int direction;
int time, timeOut;
int headColor, bodyColor, appleColor;
SDL_Rect renderRectangle;
void updateInputControls()
{
if(b[0] == true && direction != GO_DOWN)
{
direction = GO_UP;
}
else
{
if(b[1] == true && direction != GO_DOWN)
{
direction = GO_DOWN;
}
}
if(b[2] == true && direction != GO_LEFT)
{
direction = GO_LEFT;
}
else
{
if(b[3] == true && direction != GO_RIGHT)
{
direction = GO_RIGHT;
}
}
}
void renderSnake(SDL_Surface* screen)
{
renderRectangle.x = snakeParts[0].x * CELL_SIZE;
renderRectangle.y = snakeParts[0].y * CELL_SIZE;
SDL_FillRect(screen, &renderRectangle, SDL_MapRGB(screen->format, 33, 246, 39));
for(int i = 0; i < snakeParts.size(); i++)
{
renderRectangle.x = snakeParts[i].x * CELL_SIZE;
renderRectangle.y = snakeParts[i].y * CELL_SIZE;
SDL_FillRect(screen, &renderRectangle, SDL_MapRGB(screen->format, 93, 246, 39));
}
}
void renderApple(SDL_Surface* screen)
{
renderRectangle.x = apple.x * CELL_SIZE;
renderRectangle.y = apple.y * CELL_SIZE;
SDL_FillRect(screen, &renderRectangle, SDL_MapRGB(screen->format, 215, 37, 49));
}
void addPart(int x, int y)
{
SnakeParts body(x, y);
snakeParts.push_back(body);
}
void moveSnake()
{
static const int moveX[] = { 0, 0, -1, 1 };
static const int moveY[] = { -1, 1, 0, 0 };
int x = snakeParts[0].x + moveX[direction];
int y = snakeParts[0].y + moveY[direction];
SnakeParts nextPart(x, y);
snakeParts.push_front(nextPart);
}
bool isCollidingWithWall()
{
int snakeHeadPositionX = snakeParts[0].x;
int snakeHeadPositionY = snakeParts[0].y;
return ((snakeHeadPositionX == 0) || (snakeHeadPositionY == 0) || (snakeHeadPositionX == CELL_WIDTH) || (snakeHeadPositionY == CELL_HEIGHT));
}
bool isCollidingWithSelf()
{
int headPositionX = snakeParts[0].x;
int headPositionY = snakeParts[0].y;
for(int i = 0; i < snakeParts.size(); i++)
{
if(snakeParts[i].x == headPositionX && snakeParts[i].y == headPositionY)
{
return true;
}
}
return false;
}
bool isCollidingWithApple()
{
return (snakeParts[0].x == apple.x && snakeParts[0].y == apple.y);
}
};
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Surface* screen = NULL;
bool appQuit = false;
SDL_Event event;
// bool b[4] = { 0, 0, 0, 0 };
Snake snake(1, 2, 3);
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
cout << "Error, while initializing SDL - " << SDL_GetError() << endl;
}
window = SDL_CreateWindow("Snake Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL)
{
cout << "Error, while oppening the window! " << SDL_GetError() << endl;
}
while(appQuit != true)
{
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
appQuit = true;
}
if(event.type == SDL_KEYUP)
{
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0] = 0;
break;
case SDLK_LEFT:
b[1] = 0;
break;
case SDLK_RIGHT:
b[2] = 0;
break;
case SDLK_DOWN:
b[3] = 0;
break;
}
}
}
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
b[0] = 1;
break;
case SDLK_LEFT:
b[1] = 1;
break;
case SDLK_RIGHT:
b[2] = 1;
break;
case SDLK_DOWN:
b[3] = 1;
break;
}
}
}
screen = SDL_GetWindowSurface(window);
// SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
// SDL_FillRect(screen, &boxRectangle, SDL_MapRGB(screen->format, 0x33, 0x99, 0xFF));
snake.render(screen);
snake.update();
SDL_UpdateWindowSurface(window);
}
SDL_Quit();
return 0;
}