Skip to content

Commit 993513e

Browse files
committed
Fixed a bug that allowed you to change direction twice within a single game tick
1 parent 53e6f54 commit 993513e

6 files changed

+38
-30
lines changed

Snake.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
SDL_Color Snake::SNAKE_COLOR = {0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE}; // green
66

7-
Snake::Snake(int row, int col) : Snake(UP, row, col) {}
7+
Snake::Snake(int row, int col) : Snake(DIRECTION_UP, row, col) {}
88

99
Snake::Snake(Snake::Direction d, int row, int col) :
1010
head_(new Block(row, col)),
@@ -103,8 +103,8 @@ void Snake::move()
103103

104104
bool Snake::setDirectionIfPossible(Snake::Direction d)
105105
{
106-
if ((direction_ == UP && d == DOWN) || (direction_ == DOWN && d == UP)
107-
|| (direction_ == LEFT && d == RIGHT) || (direction_ == RIGHT && d == LEFT))
106+
if ((direction_ == DIRECTION_UP && d == DIRECTION_DOWN) || (direction_ == DIRECTION_DOWN && d == DIRECTION_UP)
107+
|| (direction_ == DIRECTION_LEFT && d == DIRECTION_RIGHT) || (direction_ == DIRECTION_RIGHT && d == DIRECTION_LEFT))
108108
{
109109
return false;
110110
}
@@ -134,16 +134,16 @@ std::pair<int, int> Snake::getNewRowAndColumn()
134134

135135
switch (direction_)
136136
{
137-
case UP:
137+
case DIRECTION_UP:
138138
r -= 1;
139139
break;
140-
case DOWN:
140+
case DIRECTION_DOWN:
141141
r += 1;
142142
break;
143-
case LEFT:
143+
case DIRECTION_LEFT:
144144
c -= 1;
145145
break;
146-
case RIGHT:
146+
case DIRECTION_RIGHT:
147147
c += 1;
148148
break;
149149
}

Snake.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ class Snake
1212
*/
1313
enum Direction
1414
{
15-
UP,
16-
DOWN,
17-
LEFT,
18-
RIGHT
15+
DIRECTION_NONE = -1,
16+
DIRECTION_UP,
17+
DIRECTION_DOWN,
18+
DIRECTION_LEFT,
19+
DIRECTION_RIGHT
1920
};
2021

2122
Snake(int row, int col);

SnakeBoard.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <iostream>
22
#include <utility>
33

4-
#include "SnakeGame.h"
54
#include "SnakeBoard.h"
65

76
const SDL_Color SnakeBoard::FOOD_COLOR = {0xFF, 0xFF, 0x00, SDL_ALPHA_OPAQUE}; // yellow

SnakeBoard.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#define SNAKE_SNAKE_BOARD_H
33

44
#include <utility>
5+
56
#include <SDL2/SDL.h>
67
#include <SDL2_ttf/SDL_ttf.h>
8+
79
#include "Snake.h"
810

911
class SnakeBoard
@@ -13,8 +15,8 @@ class SnakeBoard
1315
* An enum representing the four directions in which a Snake can travel.
1416
*/
1517

16-
static const int COLUMNS = 10;
17-
static const int ROWS = 10;
18+
static const int COLUMNS = 20;
19+
static const int ROWS = 20;
1820

1921
static const int WINDOW_HEIGHT = 720;
2022
static const int WINDOW_WIDTH = WINDOW_HEIGHT;
@@ -48,14 +50,14 @@ class SnakeBoard
4850
bool snakeAte();
4951

5052
private:
51-
static const SDL_Color GRID_LINE_COLOR;
5253
static const SDL_Color FOOD_COLOR;
5354
static const SDL_Color FONT_COLOR;
5455
static const char FONT_LOCATION[];
5556
static const int GAME_OVER_FONT_SIZE = 1000;
57+
static const SDL_Color GRID_LINE_COLOR;
5658
static const int SCORE_FONT_SIZE = 72;
5759

58-
std::pair<int, int> foodCoords_; // row and column of the food (0 <= c < COLUMNS, 0 <= r < ROWS)
60+
std::pair<int, int> foodCoords_; // row and column of the food (0 <= r < ROWS, 0 <= c < COLUMNS)
5961

6062
TTF_Font *game_over_msg_font_;
6163
SDL_Surface *game_over_msg_surface_;

SnakeGame.cpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,26 @@ SnakeGame::~SnakeGame()
1919

2020
void SnakeGame::loop()
2121
{
22+
Snake::Direction d = Snake::DIRECTION_NONE;
2223
do
2324
{
2425
if (!paused_ && SDL_GetTicks() > time_ + DELAY)
2526
{
27+
if (d != Snake::DIRECTION_NONE)
28+
{
29+
board_->setSnakeDirection(d);
30+
}
2631
update();
2732
if (board_->isGameOver())
2833
{
2934
gameOver();
3035
}
3136
time_ = SDL_GetTicks();
3237
}
33-
} while(checkUserInput());
38+
} while(checkUserInput(d));
3439
}
3540

36-
bool SnakeGame::checkUserInput()
41+
bool SnakeGame::checkUserInput(Snake::Direction &d)
3742
{
3843
SDL_Event e;
3944
if (SDL_PollEvent(&e))
@@ -42,32 +47,33 @@ bool SnakeGame::checkUserInput()
4247
{
4348
case SDL_KEYDOWN:
4449
{
45-
// If the game is paused, ignore all user input except unpause
46-
if (paused_ && e.key.keysym.sym == SDLK_p)
47-
{
48-
paused_ = false;
49-
}
50-
else if (!paused_)
50+
if (!paused_)
5151
{
52+
5253
switch (e.key.keysym.sym)
5354
{
5455
case SDLK_UP:
55-
board_->setSnakeDirection(Snake::UP);
56+
d = Snake::DIRECTION_UP;
5657
break;
5758
case SDLK_DOWN:
58-
board_->setSnakeDirection(Snake::DOWN);
59+
d = Snake::DIRECTION_DOWN;
5960
break;
6061
case SDLK_LEFT:
61-
board_->setSnakeDirection(Snake::LEFT);
62+
d = Snake::DIRECTION_LEFT;
6263
break;
6364
case SDLK_RIGHT:
64-
board_->setSnakeDirection(Snake::RIGHT);
65+
d = Snake::DIRECTION_RIGHT;
6566
break;
6667
case SDLK_p:
6768
paused_ = true;
6869
break;
6970
}
7071
}
72+
// If the game is paused, ignore all user input except unpause
73+
else
74+
{
75+
paused_ = e.key.keysym.sym != SDLK_p;
76+
}
7177
}
7278
break;
7379

SnakeGame.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class SnakeGame
1313
{
1414
public:
15-
static const int DELAY = 250;
15+
static const int DELAY = 100;
1616

1717
SnakeGame();
1818
~SnakeGame();
@@ -24,7 +24,7 @@ class SnakeGame
2424
int score_;
2525
int time_;
2626

27-
bool checkUserInput();
27+
bool checkUserInput(Snake::Direction &d);
2828

2929
void gameOver();
3030

0 commit comments

Comments
 (0)